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