Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include <time.h>
     12 
     13 #include "webrtc/base/httpcommon-inl.h"
     14 
     15 #include "webrtc/base/asyncsocket.h"
     16 #include "webrtc/base/common.h"
     17 #include "webrtc/base/diskcache.h"
     18 #include "webrtc/base/httpclient.h"
     19 #include "webrtc/base/logging.h"
     20 #include "webrtc/base/pathutils.h"
     21 #include "webrtc/base/scoped_ptr.h"
     22 #include "webrtc/base/socketstream.h"
     23 #include "webrtc/base/stringencode.h"
     24 #include "webrtc/base/stringutils.h"
     25 #include "webrtc/base/thread.h"
     26 
     27 namespace rtc {
     28 
     29 //////////////////////////////////////////////////////////////////////
     30 // Helpers
     31 //////////////////////////////////////////////////////////////////////
     32 
     33 namespace {
     34 
     35 const size_t kCacheHeader = 0;
     36 const size_t kCacheBody = 1;
     37 
     38 // Convert decimal string to integer
     39 bool HttpStringToUInt(const std::string& str, size_t* val) {
     40   ASSERT(NULL != val);
     41   char* eos = NULL;
     42   *val = strtoul(str.c_str(), &eos, 10);
     43   return (*eos == '\0');
     44 }
     45 
     46 bool HttpShouldCache(const HttpTransaction& t) {
     47   bool verb_allows_cache = (t.request.verb == HV_GET)
     48                            || (t.request.verb == HV_HEAD);
     49   bool is_range_response = t.response.hasHeader(HH_CONTENT_RANGE, NULL);
     50   bool has_expires = t.response.hasHeader(HH_EXPIRES, NULL);
     51   bool request_allows_cache =
     52     has_expires || (std::string::npos != t.request.path.find('?'));
     53   bool response_allows_cache =
     54     has_expires || HttpCodeIsCacheable(t.response.scode);
     55 
     56   bool may_cache = verb_allows_cache
     57                    && request_allows_cache
     58                    && response_allows_cache
     59                    && !is_range_response;
     60 
     61   std::string value;
     62   if (t.response.hasHeader(HH_CACHE_CONTROL, &value)) {
     63     HttpAttributeList directives;
     64     HttpParseAttributes(value.data(), value.size(), directives);
     65     // Response Directives Summary:
     66     // public - always cacheable
     67     // private - do not cache in a shared cache
     68     // no-cache - may cache, but must revalidate whether fresh or stale
     69     // no-store - sensitive information, do not cache or store in any way
     70     // max-age - supplants Expires for staleness
     71     // s-maxage - use as max-age for shared caches, ignore otherwise
     72     // must-revalidate - may cache, but must revalidate after stale
     73     // proxy-revalidate - shared cache must revalidate
     74     if (HttpHasAttribute(directives, "no-store", NULL)) {
     75       may_cache = false;
     76     } else if (HttpHasAttribute(directives, "public", NULL)) {
     77       may_cache = true;
     78     }
     79   }
     80   return may_cache;
     81 }
     82 
     83 enum HttpCacheState {
     84   HCS_FRESH,  // In cache, may use
     85   HCS_STALE,  // In cache, must revalidate
     86   HCS_NONE    // Not in cache
     87 };
     88 
     89 HttpCacheState HttpGetCacheState(const HttpTransaction& t) {
     90   // Temporaries
     91   std::string s_temp;
     92   time_t u_temp;
     93 
     94   // Current time
     95   time_t now = time(0);
     96 
     97   HttpAttributeList cache_control;
     98   if (t.response.hasHeader(HH_CACHE_CONTROL, &s_temp)) {
     99     HttpParseAttributes(s_temp.data(), s_temp.size(), cache_control);
    100   }
    101 
    102   // Compute age of cache document
    103   time_t date;
    104   if (!t.response.hasHeader(HH_DATE, &s_temp)
    105       || !HttpDateToSeconds(s_temp, &date))
    106     return HCS_NONE;
    107 
    108   // TODO: Timestamp when cache request sent and response received?
    109   time_t request_time = date;
    110   time_t response_time = date;
    111 
    112   time_t apparent_age = 0;
    113   if (response_time > date) {
    114     apparent_age = response_time - date;
    115   }
    116 
    117   time_t corrected_received_age = apparent_age;
    118   size_t i_temp;
    119   if (t.response.hasHeader(HH_AGE, &s_temp)
    120       && HttpStringToUInt(s_temp, (&i_temp))) {
    121     u_temp = static_cast<time_t>(i_temp);
    122     corrected_received_age = stdmax(apparent_age, u_temp);
    123   }
    124 
    125   time_t response_delay = response_time - request_time;
    126   time_t corrected_initial_age = corrected_received_age + response_delay;
    127   time_t resident_time = now - response_time;
    128   time_t current_age = corrected_initial_age + resident_time;
    129 
    130   // Compute lifetime of document
    131   time_t lifetime;
    132   if (HttpHasAttribute(cache_control, "max-age", &s_temp)) {
    133     lifetime = atoi(s_temp.c_str());
    134   } else if (t.response.hasHeader(HH_EXPIRES, &s_temp)
    135              && HttpDateToSeconds(s_temp, &u_temp)) {
    136     lifetime = u_temp - date;
    137   } else if (t.response.hasHeader(HH_LAST_MODIFIED, &s_temp)
    138              && HttpDateToSeconds(s_temp, &u_temp)) {
    139     // TODO: Issue warning 113 if age > 24 hours
    140     lifetime = static_cast<size_t>(now - u_temp) / 10;
    141   } else {
    142     return HCS_STALE;
    143   }
    144 
    145   return (lifetime > current_age) ? HCS_FRESH : HCS_STALE;
    146 }
    147 
    148 enum HttpValidatorStrength {
    149   HVS_NONE,
    150   HVS_WEAK,
    151   HVS_STRONG
    152 };
    153 
    154 HttpValidatorStrength
    155 HttpRequestValidatorLevel(const HttpRequestData& request) {
    156   if (HV_GET != request.verb)
    157     return HVS_STRONG;
    158   return request.hasHeader(HH_RANGE, NULL) ? HVS_STRONG : HVS_WEAK;
    159 }
    160 
    161 HttpValidatorStrength
    162 HttpResponseValidatorLevel(const HttpResponseData& response) {
    163   std::string value;
    164   if (response.hasHeader(HH_ETAG, &value)) {
    165     bool is_weak = (strnicmp(value.c_str(), "W/", 2) == 0);
    166     return is_weak ? HVS_WEAK : HVS_STRONG;
    167   }
    168   if (response.hasHeader(HH_LAST_MODIFIED, &value)) {
    169     time_t last_modified, date;
    170     if (HttpDateToSeconds(value, &last_modified)
    171         && response.hasHeader(HH_DATE, &value)
    172         && HttpDateToSeconds(value, &date)
    173         && (last_modified + 60 < date)) {
    174       return HVS_STRONG;
    175     }
    176     return HVS_WEAK;
    177   }
    178   return HVS_NONE;
    179 }
    180 
    181 std::string GetCacheID(const HttpRequestData& request) {
    182   std::string id, url;
    183   id.append(ToString(request.verb));
    184   id.append("_");
    185   request.getAbsoluteUri(&url);
    186   id.append(url);
    187   return id;
    188 }
    189 
    190 }  // anonymous namespace
    191 
    192 //////////////////////////////////////////////////////////////////////
    193 // Public Helpers
    194 //////////////////////////////////////////////////////////////////////
    195 
    196 bool HttpWriteCacheHeaders(const HttpResponseData* response,
    197                            StreamInterface* output, size_t* size) {
    198   size_t length = 0;
    199   // Write all unknown and end-to-end headers to a cache file
    200   for (HttpData::const_iterator it = response->begin();
    201        it != response->end(); ++it) {
    202     HttpHeader header;
    203     if (FromString(header, it->first) && !HttpHeaderIsEndToEnd(header))
    204       continue;
    205     length += it->first.length() + 2 + it->second.length() + 2;
    206     if (!output)
    207       continue;
    208     std::string formatted_header(it->first);
    209     formatted_header.append(": ");
    210     formatted_header.append(it->second);
    211     formatted_header.append("\r\n");
    212     StreamResult result = output->WriteAll(formatted_header.data(),
    213                                            formatted_header.length(),
    214                                            NULL, NULL);
    215     if (SR_SUCCESS != result) {
    216       return false;
    217     }
    218   }
    219   if (output && (SR_SUCCESS != output->WriteAll("\r\n", 2, NULL, NULL))) {
    220     return false;
    221   }
    222   length += 2;
    223   if (size)
    224     *size = length;
    225   return true;
    226 }
    227 
    228 bool HttpReadCacheHeaders(StreamInterface* input, HttpResponseData* response,
    229                           HttpData::HeaderCombine combine) {
    230   while (true) {
    231     std::string formatted_header;
    232     StreamResult result = input->ReadLine(&formatted_header);
    233     if ((SR_EOS == result) || (1 == formatted_header.size())) {
    234       break;
    235     }
    236     if (SR_SUCCESS != result) {
    237       return false;
    238     }
    239     size_t end_of_name = formatted_header.find(':');
    240     if (std::string::npos == end_of_name) {
    241       LOG_F(LS_WARNING) << "Malformed cache header";
    242       continue;
    243     }
    244     size_t start_of_value = end_of_name + 1;
    245     size_t end_of_value = formatted_header.length();
    246     while ((start_of_value < end_of_value)
    247            && isspace(formatted_header[start_of_value]))
    248       ++start_of_value;
    249     while ((start_of_value < end_of_value)
    250            && isspace(formatted_header[end_of_value-1]))
    251      --end_of_value;
    252     size_t value_length = end_of_value - start_of_value;
    253 
    254     std::string name(formatted_header.substr(0, end_of_name));
    255     std::string value(formatted_header.substr(start_of_value, value_length));
    256     response->changeHeader(name, value, combine);
    257   }
    258   return true;
    259 }
    260 
    261 //////////////////////////////////////////////////////////////////////
    262 // HttpClient
    263 //////////////////////////////////////////////////////////////////////
    264 
    265 const size_t kDefaultRetries = 1;
    266 const size_t kMaxRedirects = 5;
    267 
    268 HttpClient::HttpClient(const std::string& agent, StreamPool* pool,
    269                        HttpTransaction* transaction)
    270     : agent_(agent), pool_(pool),
    271       transaction_(transaction), free_transaction_(false),
    272       retries_(kDefaultRetries), attempt_(0), redirects_(0),
    273       redirect_action_(REDIRECT_DEFAULT),
    274       uri_form_(URI_DEFAULT), cache_(NULL), cache_state_(CS_READY),
    275       resolver_(NULL) {
    276   base_.notify(this);
    277   if (NULL == transaction_) {
    278     free_transaction_ = true;
    279     transaction_ = new HttpTransaction;
    280   }
    281 }
    282 
    283 HttpClient::~HttpClient() {
    284   base_.notify(NULL);
    285   base_.abort(HE_SHUTDOWN);
    286   if (resolver_) {
    287     resolver_->Destroy(false);
    288   }
    289   release();
    290   if (free_transaction_)
    291     delete transaction_;
    292 }
    293 
    294 void HttpClient::reset() {
    295   server_.Clear();
    296   request().clear(true);
    297   response().clear(true);
    298   context_.reset();
    299   redirects_ = 0;
    300   base_.abort(HE_OPERATION_CANCELLED);
    301 }
    302 
    303 void HttpClient::OnResolveResult(AsyncResolverInterface* resolver) {
    304   if (resolver != resolver_) {
    305     return;
    306   }
    307   int error = resolver_->GetError();
    308   server_ = resolver_->address();
    309   resolver_->Destroy(false);
    310   resolver_ = NULL;
    311   if (error != 0) {
    312     LOG(LS_ERROR) << "Error " << error << " resolving name: "
    313                   << server_;
    314     onHttpComplete(HM_CONNECT, HE_CONNECT_FAILED);
    315   } else {
    316     connect();
    317   }
    318 }
    319 
    320 void HttpClient::StartDNSLookup() {
    321   resolver_ = new AsyncResolver();
    322   resolver_->SignalDone.connect(this, &HttpClient::OnResolveResult);
    323   resolver_->Start(server_);
    324 }
    325 
    326 void HttpClient::set_server(const SocketAddress& address) {
    327   server_ = address;
    328   // Setting 'Host' here allows it to be overridden before starting the request,
    329   // if necessary.
    330   request().setHeader(HH_HOST, HttpAddress(server_, false), true);
    331 }
    332 
    333 StreamInterface* HttpClient::GetDocumentStream() {
    334   return base_.GetDocumentStream();
    335 }
    336 
    337 void HttpClient::start() {
    338   if (base_.mode() != HM_NONE) {
    339     // call reset() to abort an in-progress request
    340     ASSERT(false);
    341     return;
    342   }
    343 
    344   ASSERT(!IsCacheActive());
    345 
    346   if (request().hasHeader(HH_TRANSFER_ENCODING, NULL)) {
    347     // Exact size must be known on the client.  Instead of using chunked
    348     // encoding, wrap data with auto-caching file or memory stream.
    349     ASSERT(false);
    350     return;
    351   }
    352 
    353   attempt_ = 0;
    354 
    355   // If no content has been specified, using length of 0.
    356   request().setHeader(HH_CONTENT_LENGTH, "0", false);
    357 
    358   if (!agent_.empty()) {
    359     request().setHeader(HH_USER_AGENT, agent_, false);
    360   }
    361 
    362   UriForm uri_form = uri_form_;
    363   if (PROXY_HTTPS == proxy_.type) {
    364     // Proxies require absolute form
    365     uri_form = URI_ABSOLUTE;
    366     request().version = HVER_1_0;
    367     request().setHeader(HH_PROXY_CONNECTION, "Keep-Alive", false);
    368   } else {
    369     request().setHeader(HH_CONNECTION, "Keep-Alive", false);
    370   }
    371 
    372   if (URI_ABSOLUTE == uri_form) {
    373     // Convert to absolute uri form
    374     std::string url;
    375     if (request().getAbsoluteUri(&url)) {
    376       request().path = url;
    377     } else {
    378       LOG(LS_WARNING) << "Couldn't obtain absolute uri";
    379     }
    380   } else if (URI_RELATIVE == uri_form) {
    381     // Convert to relative uri form
    382     std::string host, path;
    383     if (request().getRelativeUri(&host, &path)) {
    384       request().setHeader(HH_HOST, host);
    385       request().path = path;
    386     } else {
    387       LOG(LS_WARNING) << "Couldn't obtain relative uri";
    388     }
    389   }
    390 
    391   if ((NULL != cache_) && CheckCache()) {
    392     return;
    393   }
    394 
    395   connect();
    396 }
    397 
    398 void HttpClient::connect() {
    399   int stream_err;
    400   if (server_.IsUnresolvedIP()) {
    401     StartDNSLookup();
    402     return;
    403   }
    404   StreamInterface* stream = pool_->RequestConnectedStream(server_, &stream_err);
    405   if (stream == NULL) {
    406     ASSERT(0 != stream_err);
    407     LOG(LS_ERROR) << "RequestConnectedStream error: " << stream_err;
    408     onHttpComplete(HM_CONNECT, HE_CONNECT_FAILED);
    409   } else {
    410     base_.attach(stream);
    411     if (stream->GetState() == SS_OPEN) {
    412       base_.send(&transaction_->request);
    413     }
    414   }
    415 }
    416 
    417 void HttpClient::prepare_get(const std::string& url) {
    418   reset();
    419   Url<char> purl(url);
    420   set_server(SocketAddress(purl.host(), purl.port()));
    421   request().verb = HV_GET;
    422   request().path = purl.full_path();
    423 }
    424 
    425 void HttpClient::prepare_post(const std::string& url,
    426                               const std::string& content_type,
    427                               StreamInterface* request_doc) {
    428   reset();
    429   Url<char> purl(url);
    430   set_server(SocketAddress(purl.host(), purl.port()));
    431   request().verb = HV_POST;
    432   request().path = purl.full_path();
    433   request().setContent(content_type, request_doc);
    434 }
    435 
    436 void HttpClient::release() {
    437   if (StreamInterface* stream = base_.detach()) {
    438     pool_->ReturnConnectedStream(stream);
    439   }
    440 }
    441 
    442 bool HttpClient::ShouldRedirect(std::string* location) const {
    443   // TODO: Unittest redirection.
    444   if ((REDIRECT_NEVER == redirect_action_)
    445       || !HttpCodeIsRedirection(response().scode)
    446       || !response().hasHeader(HH_LOCATION, location)
    447       || (redirects_ >= kMaxRedirects))
    448     return false;
    449   return (REDIRECT_ALWAYS == redirect_action_)
    450          || (HC_SEE_OTHER == response().scode)
    451          || (HV_HEAD == request().verb)
    452          || (HV_GET == request().verb);
    453 }
    454 
    455 bool HttpClient::BeginCacheFile() {
    456   ASSERT(NULL != cache_);
    457   ASSERT(CS_READY == cache_state_);
    458 
    459   std::string id = GetCacheID(request());
    460   CacheLock lock(cache_, id, true);
    461   if (!lock.IsLocked()) {
    462     LOG_F(LS_WARNING) << "Couldn't lock cache";
    463     return false;
    464   }
    465 
    466   if (HE_NONE != WriteCacheHeaders(id)) {
    467     return false;
    468   }
    469 
    470   scoped_ptr<StreamInterface> stream(cache_->WriteResource(id, kCacheBody));
    471   if (!stream) {
    472     LOG_F(LS_ERROR) << "Couldn't open body cache";
    473     return false;
    474   }
    475   lock.Commit();
    476 
    477   // Let's secretly replace the response document with Folgers Crystals,
    478   // er, StreamTap, so that we can mirror the data to our cache.
    479   StreamInterface* output = response().document.release();
    480   if (!output) {
    481     output = new NullStream;
    482   }
    483   StreamTap* tap = new StreamTap(output, stream.release());
    484   response().document.reset(tap);
    485   return true;
    486 }
    487 
    488 HttpError HttpClient::WriteCacheHeaders(const std::string& id) {
    489   scoped_ptr<StreamInterface> stream(cache_->WriteResource(id, kCacheHeader));
    490   if (!stream) {
    491     LOG_F(LS_ERROR) << "Couldn't open header cache";
    492     return HE_CACHE;
    493   }
    494 
    495   if (!HttpWriteCacheHeaders(&transaction_->response, stream.get(), NULL)) {
    496     LOG_F(LS_ERROR) << "Couldn't write header cache";
    497     return HE_CACHE;
    498   }
    499 
    500   return HE_NONE;
    501 }
    502 
    503 void HttpClient::CompleteCacheFile() {
    504   // Restore previous response document
    505   StreamTap* tap = static_cast<StreamTap*>(response().document.release());
    506   response().document.reset(tap->Detach());
    507 
    508   int error;
    509   StreamResult result = tap->GetTapResult(&error);
    510 
    511   // Delete the tap and cache stream (which completes cache unlock)
    512   delete tap;
    513 
    514   if (SR_SUCCESS != result) {
    515     LOG(LS_ERROR) << "Cache file error: " << error;
    516     cache_->DeleteResource(GetCacheID(request()));
    517   }
    518 }
    519 
    520 bool HttpClient::CheckCache() {
    521   ASSERT(NULL != cache_);
    522   ASSERT(CS_READY == cache_state_);
    523 
    524   std::string id = GetCacheID(request());
    525   if (!cache_->HasResource(id)) {
    526     // No cache file available
    527     return false;
    528   }
    529 
    530   HttpError error = ReadCacheHeaders(id, true);
    531 
    532   if (HE_NONE == error) {
    533     switch (HttpGetCacheState(*transaction_)) {
    534     case HCS_FRESH:
    535       // Cache content is good, read from cache
    536       break;
    537     case HCS_STALE:
    538       // Cache content may be acceptable.  Issue a validation request.
    539       if (PrepareValidate()) {
    540         return false;
    541       }
    542       // Couldn't validate, fall through.
    543     case HCS_NONE:
    544       // Cache content is not useable.  Issue a regular request.
    545       response().clear(false);
    546       return false;
    547     }
    548   }
    549 
    550   if (HE_NONE == error) {
    551     error = ReadCacheBody(id);
    552     cache_state_ = CS_READY;
    553   }
    554 
    555   if (HE_CACHE == error) {
    556     LOG_F(LS_WARNING) << "Cache failure, continuing with normal request";
    557     response().clear(false);
    558     return false;
    559   }
    560 
    561   SignalHttpClientComplete(this, error);
    562   return true;
    563 }
    564 
    565 HttpError HttpClient::ReadCacheHeaders(const std::string& id, bool override) {
    566   scoped_ptr<StreamInterface> stream(cache_->ReadResource(id, kCacheHeader));
    567   if (!stream) {
    568     return HE_CACHE;
    569   }
    570 
    571   HttpData::HeaderCombine combine =
    572     override ? HttpData::HC_REPLACE : HttpData::HC_AUTO;
    573 
    574   if (!HttpReadCacheHeaders(stream.get(), &transaction_->response, combine)) {
    575     LOG_F(LS_ERROR) << "Error reading cache headers";
    576     return HE_CACHE;
    577   }
    578 
    579   response().scode = HC_OK;
    580   return HE_NONE;
    581 }
    582 
    583 HttpError HttpClient::ReadCacheBody(const std::string& id) {
    584   cache_state_ = CS_READING;
    585 
    586   HttpError error = HE_NONE;
    587 
    588   size_t data_size;
    589   scoped_ptr<StreamInterface> stream(cache_->ReadResource(id, kCacheBody));
    590   if (!stream || !stream->GetAvailable(&data_size)) {
    591     LOG_F(LS_ERROR) << "Unavailable cache body";
    592     error = HE_CACHE;
    593   } else {
    594     error = OnHeaderAvailable(false, false, data_size);
    595   }
    596 
    597   if ((HE_NONE == error)
    598       && (HV_HEAD != request().verb)
    599       && response().document) {
    600     // Allocate on heap to not explode the stack.
    601     const int array_size = 1024 * 64;
    602     scoped_ptr<char[]> buffer(new char[array_size]);
    603     StreamResult result = Flow(stream.get(), buffer.get(), array_size,
    604                                response().document.get());
    605     if (SR_SUCCESS != result) {
    606       error = HE_STREAM;
    607     }
    608   }
    609 
    610   return error;
    611 }
    612 
    613 bool HttpClient::PrepareValidate() {
    614   ASSERT(CS_READY == cache_state_);
    615   // At this point, request() contains the pending request, and response()
    616   // contains the cached response headers.  Reformat the request to validate
    617   // the cached content.
    618   HttpValidatorStrength vs_required = HttpRequestValidatorLevel(request());
    619   HttpValidatorStrength vs_available = HttpResponseValidatorLevel(response());
    620   if (vs_available < vs_required) {
    621     return false;
    622   }
    623   std::string value;
    624   if (response().hasHeader(HH_ETAG, &value)) {
    625     request().addHeader(HH_IF_NONE_MATCH, value);
    626   }
    627   if (response().hasHeader(HH_LAST_MODIFIED, &value)) {
    628     request().addHeader(HH_IF_MODIFIED_SINCE, value);
    629   }
    630   response().clear(false);
    631   cache_state_ = CS_VALIDATING;
    632   return true;
    633 }
    634 
    635 HttpError HttpClient::CompleteValidate() {
    636   ASSERT(CS_VALIDATING == cache_state_);
    637 
    638   std::string id = GetCacheID(request());
    639 
    640   // Merge cached headers with new headers
    641   HttpError error = ReadCacheHeaders(id, false);
    642   if (HE_NONE != error) {
    643     // Rewrite merged headers to cache
    644     CacheLock lock(cache_, id);
    645     error = WriteCacheHeaders(id);
    646   }
    647   if (HE_NONE != error) {
    648     error = ReadCacheBody(id);
    649   }
    650   return error;
    651 }
    652 
    653 HttpError HttpClient::OnHeaderAvailable(bool ignore_data, bool chunked,
    654                                         size_t data_size) {
    655   // If we are ignoring the data, this is an intermediate header.
    656   // TODO: don't signal intermediate headers.  Instead, do all header-dependent
    657   // processing now, and either set up the next request, or fail outright.
    658   // TODO: by default, only write response documents with a success code.
    659   SignalHeaderAvailable(this, !ignore_data, ignore_data ? 0 : data_size);
    660   if (!ignore_data && !chunked && (data_size != SIZE_UNKNOWN)
    661       && response().document) {
    662     // Attempt to pre-allocate space for the downloaded data.
    663     if (!response().document->ReserveSize(data_size)) {
    664       return HE_OVERFLOW;
    665     }
    666   }
    667   return HE_NONE;
    668 }
    669 
    670 //
    671 // HttpBase Implementation
    672 //
    673 
    674 HttpError HttpClient::onHttpHeaderComplete(bool chunked, size_t& data_size) {
    675   if (CS_VALIDATING == cache_state_) {
    676     if (HC_NOT_MODIFIED == response().scode) {
    677       return CompleteValidate();
    678     }
    679     // Should we remove conditional headers from request?
    680     cache_state_ = CS_READY;
    681     cache_->DeleteResource(GetCacheID(request()));
    682     // Continue processing response as normal
    683   }
    684 
    685   ASSERT(!IsCacheActive());
    686   if ((request().verb == HV_HEAD) || !HttpCodeHasBody(response().scode)) {
    687     // HEAD requests and certain response codes contain no body
    688     data_size = 0;
    689   }
    690   if (ShouldRedirect(NULL)
    691       || ((HC_PROXY_AUTHENTICATION_REQUIRED == response().scode)
    692           && (PROXY_HTTPS == proxy_.type))) {
    693     // We're going to issue another request, so ignore the incoming data.
    694     base_.set_ignore_data(true);
    695   }
    696 
    697   HttpError error = OnHeaderAvailable(base_.ignore_data(), chunked, data_size);
    698   if (HE_NONE != error) {
    699     return error;
    700   }
    701 
    702   if ((NULL != cache_)
    703       && !base_.ignore_data()
    704       && HttpShouldCache(*transaction_)) {
    705     if (BeginCacheFile()) {
    706       cache_state_ = CS_WRITING;
    707     }
    708   }
    709   return HE_NONE;
    710 }
    711 
    712 void HttpClient::onHttpComplete(HttpMode mode, HttpError err) {
    713   if (((HE_DISCONNECTED == err) || (HE_CONNECT_FAILED == err)
    714        || (HE_SOCKET_ERROR == err))
    715       && (HC_INTERNAL_SERVER_ERROR == response().scode)
    716       && (attempt_ < retries_)) {
    717     // If the response code has not changed from the default, then we haven't
    718     // received anything meaningful from the server, so we are eligible for a
    719     // retry.
    720     ++attempt_;
    721     if (request().document && !request().document->Rewind()) {
    722       // Unable to replay the request document.
    723       err = HE_STREAM;
    724     } else {
    725       release();
    726       connect();
    727       return;
    728     }
    729   } else if (err != HE_NONE) {
    730     // fall through
    731   } else if (mode == HM_CONNECT) {
    732     base_.send(&transaction_->request);
    733     return;
    734   } else if ((mode == HM_SEND) || HttpCodeIsInformational(response().scode)) {
    735     // If you're interested in informational headers, catch
    736     // SignalHeaderAvailable.
    737     base_.recv(&transaction_->response);
    738     return;
    739   } else {
    740     if (!HttpShouldKeepAlive(response())) {
    741       LOG(LS_VERBOSE) << "HttpClient: closing socket";
    742       base_.stream()->Close();
    743     }
    744     std::string location;
    745     if (ShouldRedirect(&location)) {
    746       Url<char> purl(location);
    747       set_server(SocketAddress(purl.host(), purl.port()));
    748       request().path = purl.full_path();
    749       if (response().scode == HC_SEE_OTHER) {
    750         request().verb = HV_GET;
    751         request().clearHeader(HH_CONTENT_TYPE);
    752         request().clearHeader(HH_CONTENT_LENGTH);
    753         request().document.reset();
    754       } else if (request().document && !request().document->Rewind()) {
    755         // Unable to replay the request document.
    756         ASSERT(REDIRECT_ALWAYS == redirect_action_);
    757         err = HE_STREAM;
    758       }
    759       if (err == HE_NONE) {
    760         ++redirects_;
    761         context_.reset();
    762         response().clear(false);
    763         release();
    764         start();
    765         return;
    766       }
    767     } else if ((HC_PROXY_AUTHENTICATION_REQUIRED == response().scode)
    768                && (PROXY_HTTPS == proxy_.type)) {
    769       std::string authorization, auth_method;
    770       HttpData::const_iterator begin = response().begin(HH_PROXY_AUTHENTICATE);
    771       HttpData::const_iterator end = response().end(HH_PROXY_AUTHENTICATE);
    772       for (HttpData::const_iterator it = begin; it != end; ++it) {
    773         HttpAuthContext *context = context_.get();
    774         HttpAuthResult res = HttpAuthenticate(
    775           it->second.data(), it->second.size(),
    776           proxy_.address,
    777           ToString(request().verb), request().path,
    778           proxy_.username, proxy_.password,
    779           context, authorization, auth_method);
    780         context_.reset(context);
    781         if (res == HAR_RESPONSE) {
    782           request().setHeader(HH_PROXY_AUTHORIZATION, authorization);
    783           if (request().document && !request().document->Rewind()) {
    784             err = HE_STREAM;
    785           } else {
    786             // Explicitly do not reset the HttpAuthContext
    787             response().clear(false);
    788             // TODO: Reuse socket when authenticating?
    789             release();
    790             start();
    791             return;
    792           }
    793         } else if (res == HAR_IGNORE) {
    794           LOG(INFO) << "Ignoring Proxy-Authenticate: " << auth_method;
    795           continue;
    796         } else {
    797           break;
    798         }
    799       }
    800     }
    801   }
    802   if (CS_WRITING == cache_state_) {
    803     CompleteCacheFile();
    804     cache_state_ = CS_READY;
    805   } else if (CS_READING == cache_state_) {
    806     cache_state_ = CS_READY;
    807   }
    808   release();
    809   SignalHttpClientComplete(this, err);
    810 }
    811 
    812 void HttpClient::onHttpClosed(HttpError err) {
    813   // This shouldn't occur, since we return the stream to the pool upon command
    814   // completion.
    815   ASSERT(false);
    816 }
    817 
    818 //////////////////////////////////////////////////////////////////////
    819 // HttpClientDefault
    820 //////////////////////////////////////////////////////////////////////
    821 
    822 HttpClientDefault::HttpClientDefault(SocketFactory* factory,
    823                                      const std::string& agent,
    824                                      HttpTransaction* transaction)
    825     : ReuseSocketPool(factory ? factory : Thread::Current()->socketserver()),
    826       HttpClient(agent, NULL, transaction) {
    827   set_pool(this);
    828 }
    829 
    830 //////////////////////////////////////////////////////////////////////
    831 
    832 }  // namespace rtc
    833