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