00001 /** 00002 Licensed to the Apache Software Foundation (ASF) under one 00003 or more contributor license agreements. See the NOTICE file 00004 distributed with this work for additional information 00005 regarding copyright ownership. The ASF licenses this file 00006 to you under the Apache License, Version 2.0 (the 00007 "License"); you may not use this file except in compliance 00008 with the License. You may obtain a copy of the License at 00009 00010 http://www.apache.org/licenses/LICENSE-2.0 00011 00012 Unless required by applicable law or agreed to in writing, software 00013 distributed under the License is distributed on an "AS IS" BASIS, 00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 See the License for the specific language governing permissions and 00016 limitations under the License. 00017 */ 00018 /** 00019 * @file Transaction.h 00020 */ 00021 00022 #pragma once 00023 #ifndef ATSCPPAPI_TRANSACTION_H_ 00024 #define ATSCPPAPI_TRANSACTION_H_ 00025 00026 #include <sys/socket.h> 00027 #include <stdint.h> 00028 #include <list> 00029 #include "atscppapi/Request.h" 00030 #include "atscppapi/shared_ptr.h" 00031 #include "atscppapi/ClientRequest.h" 00032 #include "atscppapi/Response.h" 00033 00034 namespace atscppapi { 00035 00036 // forward declarations 00037 class TransactionPlugin; 00038 struct TransactionState; 00039 namespace utils { class internal; } 00040 00041 /** 00042 * @brief Transactions are the object containing all the state related to a HTTP Transaction 00043 * 00044 * @warning Transactions should never be directly created by the user, they will always be automatically 00045 * created and destroyed as they are needed. Transactions should never be saved beyond the 00046 * scope of the function in which they are delivered otherwise undefined behaviour will result. 00047 */ 00048 class Transaction: noncopyable { 00049 public: 00050 /** 00051 * @brief ContextValues are a mechanism to share data between plugins using the atscppapi. 00052 * 00053 * Any data can be shared so long as it extends ContextValue, a simple example might 00054 * be: 00055 * 00056 * \code 00057 * struct mydata : ContextValue { 00058 * int id_; 00059 * string foo_; 00060 * mydata(int id, string foo) : id_(id), foo_(foo) { } 00061 * } 00062 * 00063 * Transaction.setContextValue("some-key", shared_ptr(new mydata(12, "hello"))); 00064 * 00065 * // From another plugin you'll have access to this contextual data: 00066 * shared_ptr<Transaction.getContextValue("some-key") 00067 * 00068 * \endcode 00069 * 00070 * Because getContextValue() and setContextValue() 00071 * take shared pointers you dont have to worry about the cleanup as that will happen automatically so long 00072 * as you dont have shared_ptrs that cannot go out of scope. 00073 */ 00074 class ContextValue { 00075 public: 00076 virtual ~ContextValue() { } 00077 }; 00078 00079 ~Transaction(); 00080 00081 /** 00082 * Context Values are a way to share data between plugins, the key is always a string 00083 * and the value can be a shared_ptr to any type that extends ContextValue. 00084 * @param key the key to search for. 00085 * @return Shared pointer that is correctly initialized if the 00086 * value existed. It should be checked with .get() != NULL before use. 00087 */ 00088 shared_ptr<ContextValue> getContextValue(const std::string &key); 00089 00090 /** 00091 * Context Values are a way to share data between plugins, the key is always a string 00092 * and the value can be a shared_ptr to any type that extends ContextValue. 00093 * @param key the key to insert. 00094 * @param value a shared pointer to a class that extends ContextValue. 00095 */ 00096 void setContextValue(const std::string &key, shared_ptr<ContextValue> value); 00097 00098 /** 00099 * Causes the Transaction to continue on to other states in the HTTP state machine 00100 * If you do not call resume() on a Transaction it will remain in that state until 00101 * it's advanced out by a call to resume() or error(). 00102 */ 00103 void resume(); 00104 00105 /** 00106 * Causes the Transaction to advance to the error state in the HTTP state machine. 00107 * @see error(const std::string &) 00108 */ 00109 void error(); 00110 00111 /** 00112 * Causes the Transaction to advance to the error state in the HTTP state machine with 00113 * a specific error message displayed. This is functionally equivalent to the following: 00114 * 00115 * \code 00116 * setErrorBody(content); 00117 * error(); 00118 * \endcode 00119 * 00120 * @param content the error page body. 00121 */ 00122 void error(const std::string &content); 00123 00124 /** 00125 * Sets the error body page but this method does not advance the state machine to the error state. 00126 * To do that you must explicitally call error(). 00127 * 00128 * @param content the error page content. 00129 */ 00130 void setErrorBody(const std::string &content); 00131 00132 /** 00133 * Get the clients address 00134 * @return The sockaddr structure representing the client's address 00135 * @see atscppapi::utils::getIpString() in atscppapi/utils.h 00136 * @see atscppapi::utils::getPort() in atscppapi/utils.h 00137 * @see atscppapi::utils::getIpPortString in atscppapi/utils.h 00138 */ 00139 const sockaddr *getClientAddress() const; 00140 00141 /** 00142 * Get the incoming address 00143 * @return The sockaddr structure representing the incoming address 00144 * @see atscppapi::utils::getIpString() in atscppapi/utils.h 00145 * @see atscppapi::utils::getPort() in atscppapi/utils.h 00146 * @see atscppapi::utils::getIpPortString in atscppapi/utils.h 00147 */ 00148 const sockaddr *getIncomingAddress() const; 00149 00150 /** 00151 * Get the server address 00152 * @return The sockaddr structure representing the server's address 00153 * @see atscppapi::utils::getIpString() in atscppapi/utils.h 00154 * @see atscppapi::utils::getPort() in atscppapi/utils.h 00155 * @see atscppapi::utils::getIpPortString in atscppapi/utils.h 00156 */ 00157 const sockaddr *getServerAddress() const; 00158 00159 /** 00160 * Get the next hop address 00161 * @return The sockaddr structure representing the next hop's address 00162 * @see atscppapi::utils::getIpString() in atscppapi/utils.h 00163 * @see atscppapi::utils::getPort() in atscppapi/utils.h 00164 * @see atscppapi::utils::getIpPortString in atscppapi/utils.h 00165 */ 00166 const sockaddr *getNextHopAddress() const; 00167 00168 00169 /** 00170 * Set the incoming port on the Transaction 00171 * 00172 * @param port is the port to set as the incoming port on the transaction 00173 */ 00174 bool setIncomingPort(uint16_t port); 00175 00176 /** 00177 * Sets the server address on the Transaction to a populated sockaddr * 00178 * 00179 * @param sockaddr* the sockaddr structure populated as the server address. 00180 */ 00181 bool setServerAddress(const sockaddr *); 00182 00183 /** 00184 * Returns a boolean value if the request is an internal request. 00185 * A request is an internal request if it originates from within traffic server. 00186 * An example would be using TSFetchUrl (or the atscppapi equivalent of AsyncHttpFetch) 00187 * to make another request along with the original request. The secondary request 00188 * originated within traffic server and is an internal request. 00189 * 00190 * @return boolean value specifying if the request was an internal request. 00191 */ 00192 bool isInternalRequest() const; 00193 00194 /** 00195 * Returns the ClientRequest object for the incoming request from the client. 00196 * 00197 * @return ClientRequest object that can be used to manipulate the incoming request from the client. 00198 */ 00199 ClientRequest &getClientRequest(); 00200 00201 /** 00202 * Returns a Request object which is the request from Traffic Server to the origin server. 00203 * 00204 * @return Request object that can be used to manipulate the outgoing request to the origin server. 00205 */ 00206 Request &getServerRequest(); 00207 00208 /** 00209 * Returns a Response object which is the response coming from the origin server 00210 * 00211 * @return Response object that can be used to manipulate the incoming response from the origin server. 00212 */ 00213 Response &getServerResponse(); 00214 00215 /** 00216 * Returns a Response object which is the response going to the client 00217 * 00218 * @return Response object that can be used to manipulate the outgoing response from the client. 00219 */ 00220 Response &getClientResponse(); 00221 00222 /** 00223 * Returns the Effective URL for this transaction taking into account host. 00224 */ 00225 std::string getEffectiveUrl(); 00226 00227 /** 00228 * Sets the url used by the ATS cache for a specific transaction. 00229 * @param url is the url to use in the cache. 00230 */ 00231 bool setCacheUrl(const std::string &); 00232 00233 /** 00234 * The available types of timeouts you can set on a Transaction. 00235 */ 00236 enum TimeoutType { 00237 TIMEOUT_DNS = 0, /**< Timeout on DNS */ 00238 TIMEOUT_CONNECT, /**< Timeout on Connect */ 00239 TIMEOUT_NO_ACTIVITY, /**< Timeout on No Activity */ 00240 TIMEOUT_ACTIVE /**< Timeout with Activity */ 00241 }; 00242 00243 /** 00244 * Allows you to set various types of timeouts on a Transaction 00245 * 00246 * @param type The type of timeout 00247 * @param time_ms The timeout time in milliseconds 00248 * @see TimeoutType 00249 */ 00250 void setTimeout(TimeoutType type, int time_ms); 00251 00252 /** 00253 * Returns the TSHttpTxn related to the current Transaction 00254 * 00255 * @return a void * which can be cast back to a TSHttpTxn. 00256 */ 00257 void *getAtsHandle() const; 00258 00259 /** 00260 * Adds a TransactionPlugin to the current Transaction. This effectively transfers ownership and the 00261 * Transaction is now responsible for cleaning it up. 00262 * 00263 * @param TransactionPlugin* the TransactionPlugin that will be now bound to the current Transaction. 00264 */ 00265 void addPlugin(TransactionPlugin *); 00266 00267 00268 /* 00269 * Note: The following methods cannot be attached to a Response 00270 * object because that would require the Response object to 00271 * know that it's a server or client response because of the 00272 * TS C api which is TSHttpTxnServerRespBodyBytesGet. 00273 */ 00274 00275 /** 00276 * Get the number of bytes for the response body as returned by the server 00277 * 00278 * @return server response body size */ 00279 size_t getServerResponseBodySize(); 00280 00281 /** 00282 * Get the nubmber of bytes for the response headers as returned by the server 00283 * 00284 * @return server response header size */ 00285 size_t getServerResponseHeaderSize(); 00286 00287 /** 00288 * Get the number of bytes for the client response. 00289 * This can differ from the server response size because of transformations. 00290 * 00291 * @return client response body size */ 00292 size_t getClientResponseBodySize(); 00293 00294 /** 00295 * Get the number of bytes for the response headers. 00296 * This can differ from the server response because headers can be modified. 00297 * 00298 * @return client response header size */ 00299 size_t getClientResponseHeaderSize(); 00300 00301 private: 00302 TransactionState *state_; //!< The internal TransactionState object tied to the current Transaction 00303 friend class TransactionPlugin; //!< TransactionPlugin is a friend so it can call addPlugin() 00304 friend class TransformationPlugin; //!< TransformationPlugin is a friend so it can call addPlugin() 00305 00306 /** 00307 * @private 00308 * 00309 * @param raw_txn a void pointer that represents a TSHttpTxn 00310 */ 00311 Transaction(void *); 00312 00313 /** 00314 * Used to initialize the Request object for the Server. 00315 * 00316 * @private 00317 */ 00318 void initServerRequest(); 00319 00320 /** 00321 * Used to initialize the Response object for the Server. 00322 * 00323 * @private 00324 */ 00325 void initServerResponse(); 00326 00327 /** 00328 * Used to initialize the Response object for the Client. 00329 * 00330 * @private 00331 */ 00332 void initClientResponse(); 00333 00334 /** 00335 * Returns a list of TransactionPlugin pointers bound to the current Transaction 00336 * 00337 * @private 00338 * 00339 * @return a std::list<TransactionPlugin *> which represents all TransactionPlugin bound to the current Transaction. 00340 */ 00341 const std::list<TransactionPlugin *> &getPlugins() const; 00342 00343 friend class utils::internal; 00344 }; 00345 00346 } /* atscppapi */ 00347 00348 #endif /* ATSCPPAPI_TRANSACTION_H_ */