Google APIs Client Library for C++
http_response.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_RESPONSE_H_
00022 #define APISERVING_CLIENTS_CPP_TRANSPORT_HTTP_RESPONSE_H_
00023 
00024 #include <string>
00025 using std::string;
00026 #include "googleapis/client/transport/http_types.h"
00027 #include "googleapis/base/integral_types.h"
00028 #include "googleapis/base/macros.h"
00029 #include "googleapis/base/mutex.h"
00030 #include "googleapis/base/scoped_ptr.h"
00031 #include "googleapis/base/thread_annotations.h"
00032 #include "googleapis/strings/stringpiece.h"
00033 #include "googleapis/util/status.h"
00034 namespace googleapis {
00035 
00036 namespace client {
00037 class DataReader;
00038 class DataWriter;
00039 
00040 /*
00041  * Captures the response from HttpRequest invocations.
00042  * @ingroup TransportLayerCore
00043  *
00044  * HttpResponse has thread-safe state except the message body is not
00045  * thread-safe. It is assumed that you will have only one body reader
00046  * since DataReader is not thread-safe either and can only be reliably read
00047  * one time.
00048  *
00049  * Responses are typically created and owned by HttpRequest objects rather
00050  * than directly by consumer code.
00051  *
00052  * @see HttpRequest
00053  * @see HttpRequestState
00054  * @see body_reader
00055  * @see set_body_writer
00056  */
00057 class HttpResponse {
00058  public:
00059   /*
00060    * Standard constructor.
00061    */
00062   HttpResponse();
00063 
00064   /*
00065    * Standard destructor.
00066    */
00067   virtual ~HttpResponse();
00068 
00069   /*
00070    * Clear the state and headers from the response
00071    */
00072   virtual void Clear();
00073 
00074   /*
00075    * Returns the current request state.
00076    */
00077   const HttpRequestState& request_state() const {
00078     return *request_state_.get();
00079   }
00080 
00081   /*
00082    * Returns the state code indicating where in the processing lifecycle the
00083    * request currently is.
00084    */
00085   HttpRequestState::StateCode request_state_code() const {
00086     return request_state_->state_code();
00087   }
00088 
00089   /*
00090    * Get modifiable request state.
00091    *
00092    * this is not normally used when using requests but may be needed if you
00093    * are using responses in some other non-standard way.
00094    */
00095   HttpRequestState* mutable_request_state() { return request_state_.get(); }
00096 
00097   /*
00098    * Sets the reader for the message body in the HTTP response.
00099    * Normally this is called by the HttpRequest::DoExecute() method.
00100    *
00101    * @param[in] reader  Takes owenership.
00102    */
00103   void set_body_reader(DataReader* reader);
00104 
00105   /*
00106    * Sets the writer for the message body in the HTTP response.
00107    *
00108    * This must be set before you call HttpRequest::Execute(). The response
00109    * will be constructed with a writer such as NewStringDataWriter(). However,
00110    * if you are expecting a large response and wish to stream it directly to
00111    * a file (or some other type of writer) then you this is how you make that
00112    * happen.
00113    *
00114    * @param[in] writer Takes ownership.
00115    */
00116   void set_body_writer(DataWriter* writer);
00117 
00118   /*
00119    * Returns the current body writer
00120    */
00121   DataWriter* body_writer() { return body_writer_.get(); }
00122 
00123   /*
00124    * Returns the reader for the HTTP message body.
00125    *
00126    * This method is not thread safe until the request is done.
00127    *
00128    * Reading the response from the reader will update the position. The
00129    * reader is not required to support Reset() so you may only have one
00130    * chance to look at the response.
00131    */
00132   DataReader* body_reader() const { return body_reader_.get(); }
00133 
00134   /*
00135    * Reads the entire response HTTP message body as a string.
00136    *
00137    * If the body_reader was already accessed, including calling this method
00138    * before, then this method might not work if the reader was not resetable.
00139    * It will attempt to return the whole body as a string even if the
00140    * body_reader() already read some portion of it.
00141    *
00142    * @param[out] body The entire message body text.
00143    * @return status indicating whether the entire body could be read or not.
00144    *
00145    * This method will block until the body_reader() is done reading.
00146    */
00147   util::Status GetBodyString(string* body);
00148 
00149   /*
00150    * Returns the transport status.
00151    *
00152    * @see HttpRequestState::transport_status()
00153    */
00154   util::Status transport_status() const {
00155     return request_state_->transport_status();
00156   }
00157 
00158   /*
00159    * Returns the overall request status
00160    *
00161    * @see HttpRequestState::transport_status()
00162    */
00163   util::Status status() const { return request_state_->status(); }
00164 
00165   /*
00166    * Sets the HTTP status code for the response.
00167    * @param[in] code  0 indicates no code is available.
00168    */
00169   void set_http_code(int code)  { request_state_->set_http_code(code); }
00170 
00171   /*
00172    * Returns the HTTP status code returned with the HTTP response.
00173    *
00174    * @return 0 if no response has been received yet.
00175    */
00176   int http_code() const  { return request_state_->http_code(); }
00177 
00178   /*
00179    * Returns true if the request is done.
00180    *
00181    * @see HttpRequestState::done()
00182    */
00183   bool done() const { return request_state_->done(); }
00184 
00185   /*
00186    * Returns true if the request is ok.
00187    *
00188    * @see HttpRequestState::ok()
00189    */
00190   bool ok() const   { return request_state_->ok(); }
00191 
00192   /*
00193    * Returns the HTTP response headers
00194    *
00195    * @return individual headers might have multiple values.
00196    */
00197   const HttpHeaderMultiMap& headers() const  { return headers_; }
00198 
00199   /*
00200    * Adds a response header seen in the HTTP response message.
00201    *
00202    * @param[in] name The header name is not necessarily unique.
00203    * @param[in] value The value for the header.
00204    */
00205   void AddHeader(const StringPiece& name, const StringPiece& value) {
00206     headers_.insert(make_pair(name.as_string(), value.as_string()));
00207   }
00208 
00209   /*
00210    * Removes all the response headers from this instance.
00211    */
00212   void ClearHeaders() { headers_.clear(); }
00213 
00214   /*
00215    * Get the value of the named header.
00216    *
00217    * @return NULL if the named header is not present, otherwise returns a
00218    *         pointer to the header value.
00219    *
00220    * A non-NULL result will only be valid until a header is added or removed
00221    * (or the object is destroyed).
00222    */
00223   const string* FindHeaderValue(const StringPiece& name) const;
00224 
00225   /*
00226    * Blocks the callers thread until this response is done() or
00227    * the specified timeout expires.
00228    *
00229    * Note that if the underlying request was DestroyWhenDone then this
00230    * response instance may no longer exist when this method returns. Also
00231    * note that if the request was asynchronous, and the method returns true,
00232    * then the callback (if any) has already finished running as well.
00233    *
00234    * @param[in] timeout_ms  The timeout in millis.
00235    * @return true if the call's timeout expired,
00236    *         false if the response is done().
00237    */
00238   bool WaitUntilDone(int64 timeout_ms = kint64max) {
00239     return request_state_->WaitUntilDone(timeout_ms);
00240   }
00241 
00242  private:
00243   scoped_ptr<HttpRequestState> request_state_;
00244   scoped_ptr<DataReader> body_reader_;
00245   scoped_ptr<DataWriter> body_writer_;
00246   HttpHeaderMultiMap headers_;
00247 
00248   DISALLOW_COPY_AND_ASSIGN(HttpResponse);
00249 };
00250 
00251 }  // namespace client
00252 
00253 } // namespace googleapis
00254 #endif  // APISERVING_CLIENTS_CPP_TRANSPORT_HTTP_RESPONSE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines