atscppapi  1.0.9
C++ wrapper for Apache Traffic Server API
 All Classes Files Functions Enumerations Enumerator Macros
Transaction.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 LinkedIn Corp. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of the license at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  *
7  * Unless required by applicable law or agreed to in writing, software distributed under the
8  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
9  * either express or implied.
10  *
11  */
12 
13 /**
14  * @file Transaction.h
15  * @author Brian Geffon
16  * @author Manjesh Nilange
17  */
18 
19 #pragma once
20 #ifndef ATSCPPAPI_TRANSACTION_H_
21 #define ATSCPPAPI_TRANSACTION_H_
22 
23 #include <sys/socket.h>
24 #include <stdint.h>
25 #include <list>
26 #include "atscppapi/Request.h"
27 #include "atscppapi/shared_ptr.h"
29 #include "atscppapi/Response.h"
30 
31 namespace atscppapi {
32 
33 // forward declarations
34 class TransactionPlugin;
35 class TransactionState;
36 namespace utils { class internal; }
37 
38 /**
39  * @brief Transactions are the object containing all the state related to a HTTP Transaction
40  *
41  * @warning Transactions should never be directly created by the user, they will always be automatically
42  * created and destroyed as they are needed. Transactions should never be saved beyond the
43  * scope of the function in which they are delivered otherwise undefined behaviour will result.
44  */
45 class Transaction: noncopyable {
46 public:
47  /**
48  * @brief ContextValues are a mechanism to share data between plugins using the atscppapi.
49  *
50  * Any data can be shared so long as it extends ContextValue, a simple example might
51  * be:
52  *
53  * \code
54  * struct mydata : ContextValue {
55  * int id_;
56  * string foo_;
57  * mydata(int id, string foo) : id_(id), foo_(foo) { }
58  * }
59  *
60  * Transaction.setContextValue("some-key", shared_ptr(new mydata(12, "hello")));
61  *
62  * // From another plugin you'll have access to this contextual data:
63  * shared_ptr<Transaction.getContextValue("some-key")
64  *
65  * \endcode
66  *
67  * Because getContextValue() and setContextValue()
68  * take shared pointers you dont have to worry about the cleanup as that will happen automatically so long
69  * as you dont have shared_ptrs that cannot go out of scope.
70  */
71  class ContextValue {
72  public:
73  virtual ~ContextValue() { }
74  };
75 
76  ~Transaction();
77 
78  /**
79  * Context Values are a way to share data between plugins, the key is always a string
80  * and the value can be a shared_ptr to any type that extends ContextValue.
81  * @param key the key to search for.
82  * @return Shared pointer that is correctly initialized if the
83  * value existed. It should be checked with .get() != NULL before use.
84  */
85  shared_ptr<ContextValue> getContextValue(const std::string &key);
86 
87  /**
88  * Context Values are a way to share data between plugins, the key is always a string
89  * and the value can be a shared_ptr to any type that extends ContextValue.
90  * @param key the key to insert.
91  * @param value a shared pointer to a class that extends ContextValue.
92  */
93  void setContextValue(const std::string &key, shared_ptr<ContextValue> value);
94 
95  /**
96  * Causes the Transaction to continue on to other states in the HTTP state machine
97  * If you do not call resume() on a Transaction it will remain in that state until
98  * it's advanced out by a call to resume() or error().
99  */
100  void resume();
101 
102  /**
103  * Causes the Transaction to advance to the error state in the HTTP state machine.
104  * @see error(const std::string &)
105  */
106  void error();
107 
108  /**
109  * Causes the Transaction to advance to the error state in the HTTP state machine with
110  * a specific error message displayed. This is functionally equivalent to the following:
111  *
112  * \code
113  * setErrorBody(content);
114  * error();
115  * \endcode
116  *
117  * @param content the error page body.
118  */
119  void error(const std::string &content);
120 
121  /**
122  * Sets the error body page but this method does not advance the state machine to the error state.
123  * To do that you must explicitally call error().
124  *
125  * @param content the error page content.
126  */
127  void setErrorBody(const std::string &content);
128 
129  /**
130  * Get the clients address
131  * @return The sockaddr structure representing the client's address
132  * @see atscppapi::utils::getIpString() in atscppapi/utils.h
133  * @see atscppapi::utils::getPort() in atscppapi/utils.h
134  * @see atscppapi::utils::getIpPortString in atscppapi/utils.h
135  */
136  const sockaddr *getClientAddress() const;
137 
138  /**
139  * Get the incoming address
140  * @return The sockaddr structure representing the incoming address
141  * @see atscppapi::utils::getIpString() in atscppapi/utils.h
142  * @see atscppapi::utils::getPort() in atscppapi/utils.h
143  * @see atscppapi::utils::getIpPortString in atscppapi/utils.h
144  */
145  const sockaddr *getIncomingAddress() const;
146 
147  /**
148  * Get the server address
149  * @return The sockaddr structure representing the server's address
150  * @see atscppapi::utils::getIpString() in atscppapi/utils.h
151  * @see atscppapi::utils::getPort() in atscppapi/utils.h
152  * @see atscppapi::utils::getIpPortString in atscppapi/utils.h
153  */
154  const sockaddr *getServerAddress() const;
155 
156  /**
157  * Get the next hop address
158  * @return The sockaddr structure representing the next hop's address
159  * @see atscppapi::utils::getIpString() in atscppapi/utils.h
160  * @see atscppapi::utils::getPort() in atscppapi/utils.h
161  * @see atscppapi::utils::getIpPortString in atscppapi/utils.h
162  */
163  const sockaddr *getNextHopAddress() const;
164 
165 
166  /**
167  * Set the incoming port on the Transaction
168  *
169  * @param port is the port to set as the incoming port on the transaction
170  */
171  bool setIncomingPort(uint16_t port);
172 
173  /**
174  * Sets the server address on the Transaction to a populated sockaddr *
175  *
176  * @param sockaddr* the sockaddr structure populated as the server address.
177  */
178  bool setServerAddress(const sockaddr *);
179 
180  /**
181  * Returns a boolean value if the request is an internal request.
182  * A request is an internal request if it originates from within traffic server.
183  * An example would be using TSFetchUrl (or the atscppapi equivalent of AsyncHttpFetch)
184  * to make another request along with the original request. The secondary request
185  * originated within traffic server and is an internal request.
186  *
187  * @return boolean value specifying if the request was an internal request.
188  */
189  bool isInternalRequest() const;
190 
191  /**
192  * Returns the ClientRequest object for the incoming request from the client.
193  *
194  * @return ClientRequest object that can be used to manipulate the incoming request from the client.
195  */
197 
198  /**
199  * Returns a Request object which is the request from Traffic Server to the origin server.
200  *
201  * @return Request object that can be used to manipulate the outgoing request to the origin server.
202  */
204 
205  /**
206  * Returns a Response object which is the response coming from the origin server
207  *
208  * @return Response object that can be used to manipulate the incoming response from the origin server.
209  */
211 
212  /**
213  * Returns a Response object which is the response going to the client
214  *
215  * @return Response object that can be used to manipulate the outgoing response from the client.
216  */
218 
219  /**
220  * Returns the Effective URL for this transaction taking into account host.
221  */
222  std::string getEffectiveUrl();
223 
224  /**
225  * Sets the url used by the ATS cache for a specific transaction.
226  * @param url is the url to use in the cache.
227  */
228  bool setCacheUrl(const std::string &);
229 
230  /**
231  * The available types of timeouts you can set on a Transaction.
232  */
233  enum TimeoutType {
234  TIMEOUT_DNS = 0, /**< Timeout on DNS */
235  TIMEOUT_CONNECT, /**< Timeout on Connect */
236  TIMEOUT_NO_ACTIVITY, /**< Timeout on No Activity */
237  TIMEOUT_ACTIVE /**< Timeout with Activity */
238  };
239 
240  /**
241  * Allows you to set various types of timeouts on a Transaction
242  *
243  * @param type The type of timeout
244  * @param time_ms The timeout time in milliseconds
245  * @see TimeoutType
246  */
247  void setTimeout(TimeoutType type, int time_ms);
248 
249  /**
250  * Returns the TSHttpTxn related to the current Transaction
251  *
252  * @return a void * which can be cast back to a TSHttpTxn.
253  */
254  void *getAtsHandle() const;
255 
256  /**
257  * Adds a TransactionPlugin to the current Transaction. This effectively transfers ownership and the
258  * Transaction is now responsible for cleaning it up.
259  *
260  * @param TransactionPlugin* the TransactionPlugin that will be now bound to the current Transaction.
261  */
263 
264 private:
265  TransactionState *state_; //!< The internal TransactionState object tied to the current Transaction
266  friend class TransactionPlugin; //!< TransactionPlugin is a friend so it can call addPlugin()
267  friend class TransformationPlugin; //!< TransformationPlugin is a friend so it can call addPlugin()
268 
269  /**
270  * @private
271  *
272  * @param raw_txn a void pointer that represents a TSHttpTxn
273  */
274  Transaction(void *);
275 
276  /**
277  * Used to initialize the Request object for the Server.
278  *
279  * @private
280  */
281  void initServerRequest();
282 
283  /**
284  * Used to initialize the Response object for the Server.
285  *
286  * @private
287  */
288  void initServerResponse();
289 
290  /**
291  * Used to initialize the Response object for the Client.
292  *
293  * @private
294  */
295  void initClientResponse();
296 
297  /**
298  * Returns a list of TransactionPlugin pointers bound to the current Transaction
299  *
300  * @private
301  *
302  * @return a std::list<TransactionPlugin *> which represents all TransactionPlugin bound to the current Transaction.
303  */
304  const std::list<TransactionPlugin *> &getPlugins() const;
305 
306  friend class utils::internal;
307 };
308 
309 } /* atscppapi */
310 
311 #endif /* ATSCPPAPI_TRANSACTION_H_ */