Google APIs Client Library for C++
http_request.h
Go to the documentation of this file.
00001 /*
00002  * \copyright Copyright 2013 Google Inc. All Rights Reserved.
00003  * \license @{
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  *
00017  * @}
00018  */
00019 
00020 
00021 #ifndef APISERVING_CLIENTS_CPP_TRANSPORT_HTTP_REQUEST_H_
00022 #define APISERVING_CLIENTS_CPP_TRANSPORT_HTTP_REQUEST_H_
00023 
00024 #include <string>
00025 using std::string;
00026 #include "googleapis/client/transport/http_response.h"
00027 #include "googleapis/client/transport/http_types.h"
00028 #include "googleapis/base/macros.h"
00029 #include "googleapis/base/scoped_ptr.h"
00030 #include "googleapis/strings/stringpiece.h"
00031 #include "googleapis/util/status.h"
00032 namespace googleapis {
00033 
00034 namespace client {
00035 class DataReader;
00036 class HttpRequest;
00037 class HttpResponse;
00038 class HttpTransport;
00039 class AuthorizationCredential;
00040 
00041 /*
00042  * Denotes an HTTP request to be sent to an HTTP server.
00043  * @ingroup TransportLayerCore
00044  *
00045  * Requests are used to send messages to HTTP servers. They are not
00046  * created directly by consumer code, rather they are created by HttpTransport
00047  * instances on behalf of requests from consumer code. In practice, this class
00048  * will be subclassed by transport implementations as needed to store
00049  * additional private state that the tranaport implementation may need.
00050  * Consumer code shoudl not subclass requests, however transport
00051  * implementations are required to.
00052  *
00053  * This class is based on a Command Pattern for issueing HTTP requests
00054  * consistent with <a href='http://www.ietf.org/rfc/rfc2616.txt'>RFC 2616,
00055  * HTTP/1.1</a>. The request instance can be given the url to invoke,
00056  * along with the payload and any desired headers. The physical message
00057  * exchanges with the server happen when Execute() is called.
00058  *
00059  * This class is not strictly thread-safe in itself, however it is designed
00060  * such that it can be safely consumed in a multi-threaded environment.
00061  * The critical state that is not naturally thread-safe is managed in the
00062  * thread-safe HttpRequestState class.
00063  *
00064  * Although this class is abstract, it is recommended that consumer code
00065  * use only this class interface so as not to depend on any specific transport
00066  * implementation. Especially if the code will be compiled into libraries used
00067  * by multiple applications or will run on multiple platforms.
00068  *
00069  * @see HttpResponse
00070  * @see HttpTransport
00071  * @see HttpTransport::NewHttpRequest()
00072  */
00073 class HttpRequest {
00074  public:
00075   /*
00076    * Methods are just a free-form StringPiece typedefed to clarify the API.
00077    *
00078    * Some HTTP servers may use extensions or define non-standard methods.
00079    * This type is a free-form StringPiece to accomodate those values.
00080    * It is suggested, but not required, you use the standard value constants
00081    * if you can.
00082    */
00083   typedef StringPiece HttpMethod;
00084 
00085   static const HttpMethod DELETE;  // RFC 2616 DELETE
00086   static const HttpMethod GET;     // RFC 2616 GET
00087   static const HttpMethod HEAD;    // RFC 2616 HEAD
00088   static const HttpMethod POST;    // RFC 2616 POST
00089   static const HttpMethod PUT;     // RFC 2616 PUT
00090   static const HttpMethod PATCH;   // RFC 2068 PATCH
00091 
00092   // Any content-type is valid. These are just symbolic names for ones
00093   // explicitly used within the client libraries.
00094 
00095   static const StringPiece ContentType_HTML;               // text/html
00096   static const StringPiece ContentType_JSON;               // application/json
00097   static const StringPiece ContentType_TEXT;               // text/plain
00098   static const StringPiece ContentType_MULTIPART_MIXED;    // multipart/mixed
00099   static const StringPiece ContentType_MULTIPART_RELATED;  // multipart/related
00100 
00101   // application/x-www-form-urlencoded
00102   static const StringPiece ContentType_FORM_URL_ENCODED;
00103 
00104   // Any header name is valid, these are just common ones used within
00105   // the api itself.
00106   static const StringPiece HttpHeader_AUTHORIZATION;   // Authorization
00107   static const StringPiece HttpHeader_CONTENT_LENGTH;  // Content-Length
00108   static const StringPiece HttpHeader_CONTENT_TYPE;    // Content-Type
00109   static const StringPiece HttpHeader_HOST;            // Host
00110   static const StringPiece HttpHeader_LOCATION;        // Location
00111   static const StringPiece HttpHeader_TRANSFER_ENCODING;  // Transfer-Encoding
00112   static const StringPiece HttpHeader_USER_AGENT;      // User-Agent
00113 
00114   /*
00115    * Standard destructor.
00116    *
00117    * The destructor is public for synchonous requests.
00118    * However, asynchronous requests should instead use DestroyWhenDone()
00119    * to avoid internal race conditions.
00120    */
00121   virtual ~HttpRequest();
00122 
00123   /*
00124    * A safer destructor for asynchronous requests.
00125    *
00126    * Destroys the request once it is done() and after notifications has been
00127    * called. if the requst has already finished, the the instance will be
00128    * destroyed immediately. Otherwise it will self-desruct once it is safe
00129    * to do so.
00130    */
00131   void DestroyWhenDone();
00132 
00133   /*
00134    * Clears the request data, but not the options.
00135    */
00136   virtual void Clear();
00137 
00138   /*
00139    * Gets a mutable options instance to configure instance-specific options.
00140    *
00141    * Options should not be changed once Execute() is called or they will
00142    * not take effect and can potentially confuse response processing.
00143    */
00144   HttpRequestOptions* mutable_options()     { return &options_; }
00145 
00146   /*
00147    * Gets request options.
00148    */
00149   const HttpRequestOptions& options() const { return options_; }
00150 
00151   /*
00152    * Gets request state instance containing additional attribute values.
00153    *
00154    * The request state contains the dynamic attributes requiring thread-safety.
00155    * These include things like status, http response code, and where in the
00156    * execution lifecycle the request currently is.
00157    */
00158   const HttpRequestState& state() const { return response_->request_state(); }
00159 
00160   /*
00161    * Returns the object managing the request's response.
00162    *
00163    * Although this method is on a const request, the response returned is not
00164    * const. That is because reading the body affects the stream pointer, which
00165    * changes its state.
00166    */
00167   HttpResponse* response() const   { return response_.get(); }
00168 
00169   /*
00170    * Returns the URL that this request will invoke when executed.
00171    */
00172   const string& url() const        { return url_; }
00173 
00174   /*
00175    * Sets the URL to invoke when the request is executed.
00176    *
00177    * @param[in] url the desired URL.
00178    */
00179   void set_url(const string& url)  { url_ = url; }
00180 
00181   /*
00182    * Adds a 'Content-Type' header with the given value.
00183    *
00184    * This replaces any existing 'Content-Type' header.
00185    * @param[in] type The new content type.
00186    *
00187    * @see RemoveHeader()
00188    */
00189   void set_content_type(const StringPiece& type) {
00190     AddHeader(HttpHeader_CONTENT_TYPE, type);
00191   }
00192 
00193   /*
00194    * Returns the content_reader specifying this requests's message body.
00195    *
00196    * @note When you read from the resulting data reader, you will modify
00197    * the position in the stream. If the content was already read then it
00198    * may already be at the end (or in the middle) of the stream. If you are
00199    * unsure, check the position and attempt to reset it if it is not at 0.
00200    *
00201    * @return NULL if there is no message body.
00202    *
00203    * @see DataReader::offset()
00204    */
00205   DataReader* content_reader() const { return content_reader_.get(); }
00206 
00207   /*
00208    * Specifies the requests message body using a DataReader.
00209    *
00210    * @param[in] reader Ownership is passed to the request instance.
00211    */
00212   void set_content_reader(DataReader* reader);
00213 
00214   /*
00215    * Removes the named header, if it exists.
00216    *
00217    * @param[in] name The name of the header to remove is not case-sensitive.
00218    */
00219   void RemoveHeader(const StringPiece& name);
00220 
00221   /*
00222    * Adds a header, or replaces its value if it already exists.
00223    *
00224    * @param[in] name Header names are not case sensitive.
00225    * @param[in] value The value to assign the header.
00226    *
00227    * The underlying strings will be copied into this object instance.
00228    *
00229    * @todo(ewiseblatt): 20130430
00230    * http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
00231    * says certain types of request headers can be repeated however here
00232    * we are requiring request headers to be unique. We do permit
00233    * repeatable response headers.
00234    */
00235   void AddHeader(const StringPiece& name, const StringPiece& value);
00236 
00237   /*
00238    * Get the value of the named header.
00239    *
00240    * @return NULL if the named header is not present, otherwise returns a
00241    *         pointer to the header value.
00242    *
00243    * A non-NULL result will only be valid until a header is added or removed
00244    * (or the object is destroyed).
00245    */
00246   const string* FindHeaderValue(const StringPiece& name) const;
00247 
00248   /*
00249    * Get all the headers explicitly added to the request.
00250    *
00251    * @return the set of all explicitly added headers (keyed by name)
00252    */
00253   const HttpHeaderMap& headers() const { return header_map_; }
00254 
00255   /*
00256    * Get the HTTP method for the request.
00257    *
00258    * @return The method bound in the constructor.
00259    */
00260   HttpMethod http_method() const  { return http_method_; }
00261 
00262   /*
00263    * Indicate that the method will never Execute.
00264    *
00265    * This method is intended for higher level uses where a component
00266    * may own an HttpRequest but choose not to call it for some reason such
00267    * as a precondition failure. This method allows the status to be pushed
00268    * into the request and any asynchronous callback to be notified since
00269    * the request is now considered a transport-level failrue.
00270    *
00271    * @param[in] status The transport_status to give the request
00272    * @param[in] callback If non-NULL then
00273    *            run the callback after setting the status.
00274    */
00275   void WillNotExecute(
00276       util::Status status, HttpRequestCallback* callback = NULL);
00277 
00278   /*
00279    * Synchronously send the request to the designated URL and wait for the
00280    * response.
00281    *
00282    * This method blocks the calling thread until the response has been received
00283    * and processed. The request will be done() when this call returns. If
00284    * DestroyWhenDone was called (or option set) then this instance will no
00285    * longer exist when the call returns. Note that since the instance owns
00286    * the reponse, the response will no longer be available once this instance
00287    * is destroyed.
00288    *
00289    * @return the response()->status().
00290    *
00291    * @see ExecuteAsync
00292    * @see response()
00293    * @see state()
00294    */
00295   util::Status Execute();
00296 
00297   /*
00298    * Asynchronously send the request to the designated URL then continue
00299    * this thread while the server is processing the request.
00300    *
00301    * param[in] callback If non-NULL the callback will be run once the
00302    * request is done() for whatever reason. The caller retains ownership
00303    * however typically single-use callbacks are used so the callback will
00304    * self-destruct once run.
00305    *
00306    * Note that the callback may or may not have been executed at the time
00307    * that this call returns. If using a self-destructing instance, you
00308    * should only look at the state and response in the callback.
00309    *
00310    * @see HttpRequestState::WaitUntilDone
00311    * @see NewCallback()
00312    * @see Execute
00313    * @see state()
00314    * @see response()
00315    */
00316   void ExecuteAsync(HttpRequestCallback* callback);
00317 
00318   /*
00319    * Sets the authorization credential.
00320    *
00321    * @param[in] cred Can be NULL. The caller retains ownership.
00322    */
00323   void set_credential(AuthorizationCredential* cred) { credential_ = cred; }
00324 
00325   /*
00326    * Returns the authorization credential.
00327    *
00328    * @return NULL if no authorization credential has been set.
00329    */
00330   AuthorizationCredential* credential() { return credential_; }
00331 
00332   /*
00333    * Returns mutable state.
00334    *
00335    * Ideally this is protected, but it is needed by MediaUploader
00336    * and perhaps other situations with higher level APIs that encounter
00337    * errors before it can send the request.
00338    */
00339   HttpRequestState* mutable_state() {
00340     return response_->mutable_request_state();
00341   }
00342 
00343   /*
00344    * Prepares the request instance so that it can be reused again.
00345    *
00346    * This method strips sensitive request headers and resets the request state.
00347    * It also clears the response state. It will leave the other data including
00348    * url, body, callback, and non-sensitive headers.
00349    *
00350    * @return ok if the request could not be prepared. The most common expected
00351    * reason for failure would be that the content_body could not be reset.
00352    */
00353   util::Status PrepareToReuse();
00354 
00355   /*
00356    * Prepare the request to follow a redirect link.
00357    *
00358    * The target for the redirect is obtained from a response header as
00359    * defined by the HTTP protocol.
00360    *
00361    * @param[in] num_redirects_so_far Number of redirects we've followed so
00362    * far while trying to execute the request.
00363    *
00364    * @return ok if the request could be prepared and failure if not.
00365    * Reasons for failure include exceeding the option limiting the number
00366    * of redirects that can be followed.
00367    *
00368    * @note This method is only intended to implement error handling on
00369    *       redirects. It allows the request object to change internal state to
00370    * redirect itself. This should be used with caution. It is biased
00371    * towards the default error handler and may not be generally applicable.
00372    */
00373   util::Status PrepareRedirect(int num_redirects_so_far);
00374 
00375  protected:
00376   /*
00377    * Constructs method instance.
00378    *
00379    * @param[in] method When choosing a particular HTTP method keep in mind that
00380    *            the server processing the URL may only support a particular
00381    *            supset for the given URL.
00382    * @param[in] transport The transport to bind to the request. Usually
00383    *            requests are created by transports, so this is normally the
00384    *            transport that created the request. Conceptually this is the
00385    *            transport to use when invoking the request.
00386    *
00387    * The constructor is only protected since this class is abstract.
00388    */
00389   HttpRequest(HttpMethod method, HttpTransport* transport);
00390 
00391   /*
00392    * Returns the transport instance bound to the request.
00393    */
00394   HttpTransport* transport() const { return transport_; }
00395 
00396   /*
00397    * Initiate the actually messaging for this message with the HTTP server.
00398    *
00399    * Concrete classes should override this to work with the transport to
00400    * actually send the request and receive the response. The method does
00401    * not return until the request is done().
00402    *
00403    * The caller should leave responsibility to the base class for
00404    * transitioning state and running the callback. The method should set
00405    * the http_code if a response is received or transport_status if the request
00406    * could not be sent. It is also responsible for writing the response data
00407    * into the response body_reader.
00408    *
00409    * This method should not finish with both a transport_status.ok()
00410    * and http_code() == 0.
00411    *
00412    * @see HttpResponse::body_writer
00413    * @see Httpresponse::set_body_reader()
00414    * @see response()
00415    */
00416   virtual void DoExecute(HttpResponse* response) = 0;
00417 
00418  private:
00419   HttpMethod http_method_;
00420   HttpRequestOptions options_;
00421   HttpTransport* transport_;               // Not owned, cannot be NULL.
00422   AuthorizationCredential* credential_;    // Not owned, might be NULL.
00423   scoped_ptr<DataReader> content_reader_;  // payload can be NULL
00424   HttpHeaderMap header_map_;               // headers for request
00425   scoped_ptr<HttpResponse> response_;      // response and request state
00426   string url_;                             // url to send request to
00427   bool busy_;                              // dont destroy it yet.
00428 
00429   /*
00430    * Adds the builtin headers implied by the framework.
00431    *
00432    * These are things like user-agent and content-length.
00433    * See the implementation for the full list.
00434    */
00435   void AddBuiltinHeaders();
00436 
00437   DISALLOW_COPY_AND_ASSIGN(HttpRequest);
00438 };
00439 
00440 }  // namespace client
00441 
00442 } // namespace googleapis
00443 #endif  // APISERVING_CLIENTS_CPP_HTTP_REQUEST_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines