1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @file Transaction.h
*/
#pragma once
#include <sys/socket.h>
#include <cstdint>
#include <list>
#include <memory>
#include "tscpp/api/Request.h"
#include "tscpp/api/ClientRequest.h"
#include "tscpp/api/Response.h"
#include "tscpp/api/HttpStatus.h"
#include <ts/apidefs.h>
namespace atscppapi
{
// forward declarations
class TransactionPlugin;
struct TransactionState;
namespace utils
{
class internal;
}
/**
* @brief Transactions are the object containing all the state related to a HTTP Transaction
*
* @warning Transactions should never be directly created by the user, they will always be automatically
* created and destroyed as they are needed. Transactions should never be saved beyond the
* scope of the function in which they are delivered otherwise undefined behaviour will result.
*/
class Transaction : noncopyable
{
public:
/**
* @brief ContextValues are a mechanism to share data between plugins using the atscppapi.
*
* Any data can be shared so long as it extends ContextValue, a simple example might
* be:
*
* \code
* struct mydata : ContextValue {
* int id_;
* string foo_;
* mydata(int id, string foo) : id_(id), foo_(foo) { }
* }
*
* Transaction.setContextValue("some-key", std::shared_ptr(new mydata(12, "hello")));
*
* // From another plugin you'll have access to this contextual data:
* std::shared_ptr<Transaction.getContextValue("some-key")
*
* \endcode
*
* Because getContextValue() and setContextValue()
* take shared pointers you dont have to worry about the cleanup as that will happen automatically so long
* as you dont have std::shared_ptrs that cannot go out of scope.
*/
class ContextValue
{
public:
virtual ~ContextValue() {}
};
~Transaction();
/**
* Set the @a event for the currently active hook.
*/
void setEvent(TSEvent event);
/**
* Context Values are a way to share data between plugins, the key is always a string
* and the value can be a std::shared_ptr to any type that extends ContextValue.
* @param key the key to search for.
* @return Shared pointer that is correctly initialized if the
* value existed. It should be checked with .get() != nullptr before use.
*/
std::shared_ptr<ContextValue> getContextValue(const std::string &key);
/**
* Context Values are a way to share data between plugins, the key is always a string
* and the value can be a std::shared_ptr to any type that extends ContextValue.
* @param key the key to insert.
* @param value a shared pointer to a class that extends ContextValue.
*/
void setContextValue(const std::string &key, std::shared_ptr<ContextValue> value);
/**
* Causes the Transaction to continue on to other states in the HTTP state machine
* If you do not call resume() on a Transaction it will remain in that state until
* it's advanced out by a call to resume() or error().
*/
void resume();
/**
* Causes the Transaction to advance to the error state in the HTTP state machine.
* @see error(const std::string &)
*/
void error();
/**
* Causes the Transaction to advance to the error state in the HTTP state machine with
* a specific error message displayed. This is functionally equivalent to the following:
*
* \code
* setErrorBody(content);
* error();
* \endcode
*
* @param content the error page body.
*/
void error(const std::string &content);
/**
* Sets the error body page but this method does not advance the state machine to the error state.
* To do that you must explicitly call error().
*
* @param content the error page content.
*/
void setErrorBody(const std::string &content);
/**
* Sets the error body page with mimetype.
* This method does not advance the state machine to the error state.
* To do that you must explicitly call error().
*
* @param content the error page content.
* @param mimetype the error page's content-type.
*/
void setErrorBody(const std::string &content, const std::string &mimetype);
/**
* Sets the status code.
* This is usable before transaction has the response of client like a remap state.
* A remap logic may advance the state machine to the error state depending on status code.
*
* @param code the status code.
*/
void setStatusCode(HttpStatus code);
/**
* Get the clients address
* @return The sockaddr structure representing the client's address
* @see atscppapi::utils::getIpString() in atscppapi/utils.h
* @see atscppapi::utils::getPort() in atscppapi/utils.h
* @see atscppapi::utils::getIpPortString in atscppapi/utils.h
*/
const sockaddr *getClientAddress() const;
/**
* Get the incoming address
* @return The sockaddr structure representing the incoming address
* @see atscppapi::utils::getIpString() in atscppapi/utils.h
* @see atscppapi::utils::getPort() in atscppapi/utils.h
* @see atscppapi::utils::getIpPortString in atscppapi/utils.h
*/
const sockaddr *getIncomingAddress() const;
/**
* Get the server address
* @return The sockaddr structure representing the server's address
* @see atscppapi::utils::getIpString() in atscppapi/utils.h
* @see atscppapi::utils::getPort() in atscppapi/utils.h
* @see atscppapi::utils::getIpPortString in atscppapi/utils.h
*/
const sockaddr *getServerAddress() const;
/**
* Get the next hop address
* @return The sockaddr structure representing the next hop's address
* @see atscppapi::utils::getIpString() in atscppapi/utils.h
* @see atscppapi::utils::getPort() in atscppapi/utils.h
* @see atscppapi::utils::getIpPortString in atscppapi/utils.h
*/
const sockaddr *getNextHopAddress() const;
/**
* Set the incoming port on the Transaction
*
* @param port is the port to set as the incoming port on the transaction
*/
bool setIncomingPort(uint16_t port);
/**
* Sets the server address on the Transaction to a populated sockaddr *
*
* @param sockaddr* the sockaddr structure populated as the server address.
*/
bool setServerAddress(const sockaddr *);
/**
* Returns a boolean value if the request is an internal request.
* A request is an internal request if it originates from within traffic server.
* An example would be using TSFetchUrl (or the atscppapi equivalent of AsyncHttpFetch)
* to make another request along with the original request. The secondary request
* originated within traffic server and is an internal request.
*
* @return boolean value specifying if the request was an internal request.
*/
bool isInternalRequest() const;
/**
* Returns the ClientRequest object for the incoming request from the client.
*
* @return ClientRequest object that can be used to manipulate the incoming request from the client.
*/
ClientRequest &getClientRequest();
/**
* Returns a Request object which is the request from Traffic Server to the origin server.
*
* @return Request object that can be used to manipulate the outgoing request to the origin server.
*/
Request &getServerRequest();
/**
* Returns a Response object which is the response coming from the origin server
*
* @return Response object that can be used to manipulate the incoming response from the origin server.
*/
Response &getServerResponse();
/**
* Returns a Response object which is the response going to the client
*
* @return Response object that can be used to manipulate the outgoing response from the client.
*/
Response &getClientResponse();
/**
* Returns a Request object which is the cached request
*
* @return Request object
*/
Request &getCachedRequest();
/**
* Returns a Response object which is the cached response
*
* @return Response object
*/
Response &getCachedResponse();
/**
* Returns the Effective URL for this transaction taking into account host.
*/
std::string getEffectiveUrl();
/**
* Sets the url used by the ATS cache for a specific transaction.
* @param url is the url to use in the cache.
*/
bool setCacheUrl(const std::string &);
/**
* Ability to skip the remap phase of the State Machine
* This only really makes sense in TS_HTTP_READ_REQUEST_HDR_HOOK
*/
void setSkipRemapping(int);
/**
* The available types of timeouts you can set on a Transaction.
*/
enum TimeoutType {
TIMEOUT_DNS = 0, /**< Timeout on DNS */
TIMEOUT_CONNECT, /**< Timeout on Connect */
TIMEOUT_NO_ACTIVITY, /**< Timeout on No Activity */
TIMEOUT_ACTIVE /**< Timeout with Activity */
};
/**
* Allows you to set various types of timeouts on a Transaction
*
* @param type The type of timeout
* @param time_ms The timeout time in milliseconds
* @see TimeoutType
*/
void setTimeout(TimeoutType type, int time_ms);
/**
* Represents different states of an object served out of the cache
*/
enum CacheStatus {
CACHE_LOOKUP_MISS = 0, /**< The object was not found in the cache */
CACHE_LOOKUP_HIT_STALE, /**< The object was found in cache but stale */
CACHE_LOOKUP_HIT_FRESH, /**< The object was found in cache and was fresh */
CACHE_LOOKUP_SKIPPED, /**< Cache lookup was not performed */
CACHE_LOOKUP_NONE
};
#define CACHE_LOOKUP_SKIPED CACHE_LOOKUP_SKIPPED
CacheStatus getCacheStatus();
/**
* Returns the TSHttpTxn related to the current Transaction
*
* @return a void * which can be cast back to a TSHttpTxn.
*/
void *getAtsHandle() const;
/**
* Adds a TransactionPlugin to the current Transaction. This effectively transfers ownership and the
* Transaction is now responsible for cleaning it up.
*
* @param TransactionPlugin* the TransactionPlugin that will be now bound to the current Transaction.
*/
void addPlugin(TransactionPlugin *);
/*
* Note: The following methods cannot be attached to a Response
* object because that would require the Response object to
* know that it's a server or client response because of the
* TS C api which is TSHttpTxnServerRespBodyBytesGet.
*/
/**
* Get the number of bytes for the response body as returned by the server
*
* @return server response body size */
size_t getServerResponseBodySize();
/**
* Get the number of bytes for the response headers as returned by the server
*
* @return server response header size */
size_t getServerResponseHeaderSize();
/**
* Get the number of bytes for the client response.
* This can differ from the server response size because of transformations.
*
* @return client response body size */
size_t getClientResponseBodySize();
/**
* Get the number of bytes for the response headers.
* This can differ from the server response because headers can be modified.
*
* @return client response header size */
size_t getClientResponseHeaderSize();
/**
* Redirect the transaction a different @a url.
*/
void redirectTo(std::string const &url);
bool configIntSet(TSOverridableConfigKey conf, int value);
bool configIntGet(TSOverridableConfigKey conf, int *value);
bool configFloatSet(TSOverridableConfigKey conf, float value);
bool configFloatGet(TSOverridableConfigKey conf, float *value);
bool configStringSet(TSOverridableConfigKey conf, std::string const &value);
bool configStringGet(TSOverridableConfigKey conf, std::string &value);
bool configFind(std::string const &name, TSOverridableConfigKey *conf, TSRecordDataType *type);
private:
TransactionState *state_; //!< The internal TransactionState object tied to the current Transaction
friend class TransactionPlugin; //!< TransactionPlugin is a friend so it can call addPlugin()
friend class TransformationPlugin; //!< TransformationPlugin is a friend so it can call addPlugin()
/**
* @private
*
* @param raw_txn a void pointer that represents a TSHttpTxn
*/
Transaction(void *);
/**
* Used to initialize the Request object for the Server.
*
* @private
*/
Request &initServerRequest();
/**
* Reset all the transaction handles (for response/requests).
* This is used to clear handles that may have gone stale.
*
* @private
*/
void resetHandles();
/**
* Returns a list of TransactionPlugin pointers bound to the current Transaction
*
* @private
*
* @return a std::list<TransactionPlugin *> which represents all TransactionPlugin bound to the current Transaction.
*/
const std::list<TransactionPlugin *> &getPlugins() const;
friend class utils::internal;
};
} // namespace atscppapi
|