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 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 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 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 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 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 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 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 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 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 resolver_(NULL) { 292 base_.notify(this); 293 if (NULL == transaction_) { 294 free_transaction_ = true; 295 transaction_ = new HttpTransaction; 296 } 297 } 298 299 HttpClient::~HttpClient() { 300 base_.notify(NULL); 301 base_.abort(HE_SHUTDOWN); 302 if (resolver_) { 303 resolver_->Destroy(false); 304 } 305 release(); 306 if (free_transaction_) 307 delete transaction_; 308 } 309 310 void HttpClient::reset() { 311 server_.Clear(); 312 request().clear(true); 313 response().clear(true); 314 context_.reset(); 315 redirects_ = 0; 316 base_.abort(HE_OPERATION_CANCELLED); 317 } 318 319 void HttpClient::OnResolveResult(SignalThread* thread) { 320 if (thread != resolver_) { 321 return; 322 } 323 int error = resolver_->error(); 324 server_ = resolver_->address(); 325 resolver_->Destroy(false); 326 resolver_ = NULL; 327 if (error != 0) { 328 LOG(LS_ERROR) << "Error " << error << " resolving name: " 329 << server_; 330 onHttpComplete(HM_CONNECT, HE_CONNECT_FAILED); 331 } else { 332 connect(); 333 } 334 } 335 336 void HttpClient::StartDNSLookup() { 337 resolver_ = new AsyncResolver(); 338 resolver_->set_address(server_); 339 resolver_->SignalWorkDone.connect(this, &HttpClient::OnResolveResult); 340 resolver_->Start(); 341 } 342 343 void HttpClient::set_server(const SocketAddress& address) { 344 server_ = address; 345 // Setting 'Host' here allows it to be overridden before starting the request, 346 // if necessary. 347 request().setHeader(HH_HOST, HttpAddress(server_, false), true); 348 } 349 350 StreamInterface* HttpClient::GetDocumentStream() { 351 return base_.GetDocumentStream(); 352 } 353 354 void HttpClient::start() { 355 if (base_.mode() != HM_NONE) { 356 // call reset() to abort an in-progress request 357 ASSERT(false); 358 return; 359 } 360 361 ASSERT(!IsCacheActive()); 362 363 if (request().hasHeader(HH_TRANSFER_ENCODING, NULL)) { 364 // Exact size must be known on the client. Instead of using chunked 365 // encoding, wrap data with auto-caching file or memory stream. 366 ASSERT(false); 367 return; 368 } 369 370 attempt_ = 0; 371 372 // If no content has been specified, using length of 0. 373 request().setHeader(HH_CONTENT_LENGTH, "0", false); 374 375 if (!agent_.empty()) { 376 request().setHeader(HH_USER_AGENT, agent_, false); 377 } 378 379 UriForm uri_form = uri_form_; 380 if (PROXY_HTTPS == proxy_.type) { 381 // Proxies require absolute form 382 uri_form = URI_ABSOLUTE; 383 request().version = HVER_1_0; 384 request().setHeader(HH_PROXY_CONNECTION, "Keep-Alive", false); 385 } else { 386 request().setHeader(HH_CONNECTION, "Keep-Alive", false); 387 } 388 389 if (URI_ABSOLUTE == uri_form) { 390 // Convert to absolute uri form 391 std::string url; 392 if (request().getAbsoluteUri(&url)) { 393 request().path = url; 394 } else { 395 LOG(LS_WARNING) << "Couldn't obtain absolute uri"; 396 } 397 } else if (URI_RELATIVE == uri_form) { 398 // Convert to relative uri form 399 std::string host, path; 400 if (request().getRelativeUri(&host, &path)) { 401 request().setHeader(HH_HOST, host); 402 request().path = path; 403 } else { 404 LOG(LS_WARNING) << "Couldn't obtain relative uri"; 405 } 406 } 407 408 if ((NULL != cache_) && CheckCache()) { 409 return; 410 } 411 412 connect(); 413 } 414 415 void HttpClient::connect() { 416 int stream_err; 417 if (server_.IsUnresolvedIP()) { 418 StartDNSLookup(); 419 return; 420 } 421 StreamInterface* stream = pool_->RequestConnectedStream(server_, &stream_err); 422 if (stream == NULL) { 423 ASSERT(0 != stream_err); 424 LOG(LS_ERROR) << "RequestConnectedStream error: " << stream_err; 425 onHttpComplete(HM_CONNECT, HE_CONNECT_FAILED); 426 } else { 427 base_.attach(stream); 428 if (stream->GetState() == SS_OPEN) { 429 base_.send(&transaction_->request); 430 } 431 } 432 } 433 434 void HttpClient::prepare_get(const std::string& url) { 435 reset(); 436 Url<char> purl(url); 437 set_server(SocketAddress(purl.host(), purl.port())); 438 request().verb = HV_GET; 439 request().path = purl.full_path(); 440 } 441 442 void HttpClient::prepare_post(const std::string& url, 443 const std::string& content_type, 444 StreamInterface* request_doc) { 445 reset(); 446 Url<char> purl(url); 447 set_server(SocketAddress(purl.host(), purl.port())); 448 request().verb = HV_POST; 449 request().path = purl.full_path(); 450 request().setContent(content_type, request_doc); 451 } 452 453 void HttpClient::release() { 454 if (StreamInterface* stream = base_.detach()) { 455 pool_->ReturnConnectedStream(stream); 456 } 457 } 458 459 bool HttpClient::ShouldRedirect(std::string* location) const { 460 // TODO: Unittest redirection. 461 if ((REDIRECT_NEVER == redirect_action_) 462 || !HttpCodeIsRedirection(response().scode) 463 || !response().hasHeader(HH_LOCATION, location) 464 || (redirects_ >= kMaxRedirects)) 465 return false; 466 return (REDIRECT_ALWAYS == redirect_action_) 467 || (HC_SEE_OTHER == response().scode) 468 || (HV_HEAD == request().verb) 469 || (HV_GET == request().verb); 470 } 471 472 bool HttpClient::BeginCacheFile() { 473 ASSERT(NULL != cache_); 474 ASSERT(CS_READY == cache_state_); 475 476 std::string id = GetCacheID(request()); 477 CacheLock lock(cache_, id, true); 478 if (!lock.IsLocked()) { 479 LOG_F(LS_WARNING) << "Couldn't lock cache"; 480 return false; 481 } 482 483 if (HE_NONE != WriteCacheHeaders(id)) { 484 return false; 485 } 486 487 scoped_ptr<StreamInterface> stream(cache_->WriteResource(id, kCacheBody)); 488 if (!stream) { 489 LOG_F(LS_ERROR) << "Couldn't open body cache"; 490 return false; 491 } 492 lock.Commit(); 493 494 // Let's secretly replace the response document with Folgers Crystals, 495 // er, StreamTap, so that we can mirror the data to our cache. 496 StreamInterface* output = response().document.release(); 497 if (!output) { 498 output = new NullStream; 499 } 500 StreamTap* tap = new StreamTap(output, stream.release()); 501 response().document.reset(tap); 502 return true; 503 } 504 505 HttpError HttpClient::WriteCacheHeaders(const std::string& id) { 506 scoped_ptr<StreamInterface> stream(cache_->WriteResource(id, kCacheHeader)); 507 if (!stream) { 508 LOG_F(LS_ERROR) << "Couldn't open header cache"; 509 return HE_CACHE; 510 } 511 512 if (!HttpWriteCacheHeaders(&transaction_->response, stream.get(), NULL)) { 513 LOG_F(LS_ERROR) << "Couldn't write header cache"; 514 return HE_CACHE; 515 } 516 517 return HE_NONE; 518 } 519 520 void HttpClient::CompleteCacheFile() { 521 // Restore previous response document 522 StreamTap* tap = static_cast<StreamTap*>(response().document.release()); 523 response().document.reset(tap->Detach()); 524 525 int error; 526 StreamResult result = tap->GetTapResult(&error); 527 528 // Delete the tap and cache stream (which completes cache unlock) 529 delete tap; 530 531 if (SR_SUCCESS != result) { 532 LOG(LS_ERROR) << "Cache file error: " << error; 533 cache_->DeleteResource(GetCacheID(request())); 534 } 535 } 536 537 bool HttpClient::CheckCache() { 538 ASSERT(NULL != cache_); 539 ASSERT(CS_READY == cache_state_); 540 541 std::string id = GetCacheID(request()); 542 if (!cache_->HasResource(id)) { 543 // No cache file available 544 return false; 545 } 546 547 HttpError error = ReadCacheHeaders(id, true); 548 549 if (HE_NONE == error) { 550 switch (HttpGetCacheState(*transaction_)) { 551 case HCS_FRESH: 552 // Cache content is good, read from cache 553 break; 554 case HCS_STALE: 555 // Cache content may be acceptable. Issue a validation request. 556 if (PrepareValidate()) { 557 return false; 558 } 559 // Couldn't validate, fall through. 560 case HCS_NONE: 561 // Cache content is not useable. Issue a regular request. 562 response().clear(false); 563 return false; 564 } 565 } 566 567 if (HE_NONE == error) { 568 error = ReadCacheBody(id); 569 cache_state_ = CS_READY; 570 } 571 572 if (HE_CACHE == error) { 573 LOG_F(LS_WARNING) << "Cache failure, continuing with normal request"; 574 response().clear(false); 575 return false; 576 } 577 578 SignalHttpClientComplete(this, error); 579 return true; 580 } 581 582 HttpError HttpClient::ReadCacheHeaders(const std::string& id, bool override) { 583 scoped_ptr<StreamInterface> stream(cache_->ReadResource(id, kCacheHeader)); 584 if (!stream) { 585 return HE_CACHE; 586 } 587 588 HttpData::HeaderCombine combine = 589 override ? HttpData::HC_REPLACE : HttpData::HC_AUTO; 590 591 if (!HttpReadCacheHeaders(stream.get(), &transaction_->response, combine)) { 592 LOG_F(LS_ERROR) << "Error reading cache headers"; 593 return HE_CACHE; 594 } 595 596 response().scode = HC_OK; 597 return HE_NONE; 598 } 599 600 HttpError HttpClient::ReadCacheBody(const std::string& id) { 601 cache_state_ = CS_READING; 602 603 HttpError error = HE_NONE; 604 605 size_t data_size; 606 scoped_ptr<StreamInterface> stream(cache_->ReadResource(id, kCacheBody)); 607 if (!stream || !stream->GetAvailable(&data_size)) { 608 LOG_F(LS_ERROR) << "Unavailable cache body"; 609 error = HE_CACHE; 610 } else { 611 error = OnHeaderAvailable(false, false, data_size); 612 } 613 614 if ((HE_NONE == error) 615 && (HV_HEAD != request().verb) 616 && response().document) { 617 char buffer[1024 * 64]; 618 StreamResult result = Flow(stream.get(), buffer, ARRAY_SIZE(buffer), 619 response().document.get()); 620 if (SR_SUCCESS != result) { 621 error = HE_STREAM; 622 } 623 } 624 625 return error; 626 } 627 628 bool HttpClient::PrepareValidate() { 629 ASSERT(CS_READY == cache_state_); 630 // At this point, request() contains the pending request, and response() 631 // contains the cached response headers. Reformat the request to validate 632 // the cached content. 633 HttpValidatorStrength vs_required = HttpRequestValidatorLevel(request()); 634 HttpValidatorStrength vs_available = HttpResponseValidatorLevel(response()); 635 if (vs_available < vs_required) { 636 return false; 637 } 638 std::string value; 639 if (response().hasHeader(HH_ETAG, &value)) { 640 request().addHeader(HH_IF_NONE_MATCH, value); 641 } 642 if (response().hasHeader(HH_LAST_MODIFIED, &value)) { 643 request().addHeader(HH_IF_MODIFIED_SINCE, value); 644 } 645 response().clear(false); 646 cache_state_ = CS_VALIDATING; 647 return true; 648 } 649 650 HttpError HttpClient::CompleteValidate() { 651 ASSERT(CS_VALIDATING == cache_state_); 652 653 std::string id = GetCacheID(request()); 654 655 // Merge cached headers with new headers 656 HttpError error = ReadCacheHeaders(id, false); 657 if (HE_NONE != error) { 658 // Rewrite merged headers to cache 659 CacheLock lock(cache_, id); 660 error = WriteCacheHeaders(id); 661 } 662 if (HE_NONE != error) { 663 error = ReadCacheBody(id); 664 } 665 return error; 666 } 667 668 HttpError HttpClient::OnHeaderAvailable(bool ignore_data, bool chunked, 669 size_t data_size) { 670 // If we are ignoring the data, this is an intermediate header. 671 // TODO: don't signal intermediate headers. Instead, do all header-dependent 672 // processing now, and either set up the next request, or fail outright. 673 // TODO: by default, only write response documents with a success code. 674 SignalHeaderAvailable(this, !ignore_data, ignore_data ? 0 : data_size); 675 if (!ignore_data && !chunked && (data_size != SIZE_UNKNOWN) 676 && response().document) { 677 // Attempt to pre-allocate space for the downloaded data. 678 if (!response().document->ReserveSize(data_size)) { 679 return HE_OVERFLOW; 680 } 681 } 682 return HE_NONE; 683 } 684 685 // 686 // HttpBase Implementation 687 // 688 689 HttpError HttpClient::onHttpHeaderComplete(bool chunked, size_t& data_size) { 690 if (CS_VALIDATING == cache_state_) { 691 if (HC_NOT_MODIFIED == response().scode) { 692 return CompleteValidate(); 693 } 694 // Should we remove conditional headers from request? 695 cache_state_ = CS_READY; 696 cache_->DeleteResource(GetCacheID(request())); 697 // Continue processing response as normal 698 } 699 700 ASSERT(!IsCacheActive()); 701 if ((request().verb == HV_HEAD) || !HttpCodeHasBody(response().scode)) { 702 // HEAD requests and certain response codes contain no body 703 data_size = 0; 704 } 705 if (ShouldRedirect(NULL) 706 || ((HC_PROXY_AUTHENTICATION_REQUIRED == response().scode) 707 && (PROXY_HTTPS == proxy_.type))) { 708 // We're going to issue another request, so ignore the incoming data. 709 base_.set_ignore_data(true); 710 } 711 712 HttpError error = OnHeaderAvailable(base_.ignore_data(), chunked, data_size); 713 if (HE_NONE != error) { 714 return error; 715 } 716 717 if ((NULL != cache_) 718 && !base_.ignore_data() 719 && HttpShouldCache(*transaction_)) { 720 if (BeginCacheFile()) { 721 cache_state_ = CS_WRITING; 722 } 723 } 724 return HE_NONE; 725 } 726 727 void HttpClient::onHttpComplete(HttpMode mode, HttpError err) { 728 if (((HE_DISCONNECTED == err) || (HE_CONNECT_FAILED == err) 729 || (HE_SOCKET_ERROR == err)) 730 && (HC_INTERNAL_SERVER_ERROR == response().scode) 731 && (attempt_ < retries_)) { 732 // If the response code has not changed from the default, then we haven't 733 // received anything meaningful from the server, so we are eligible for a 734 // retry. 735 ++attempt_; 736 if (request().document && !request().document->Rewind()) { 737 // Unable to replay the request document. 738 err = HE_STREAM; 739 } else { 740 release(); 741 connect(); 742 return; 743 } 744 } else if (err != HE_NONE) { 745 // fall through 746 } else if (mode == HM_CONNECT) { 747 base_.send(&transaction_->request); 748 return; 749 } else if ((mode == HM_SEND) || HttpCodeIsInformational(response().scode)) { 750 // If you're interested in informational headers, catch 751 // SignalHeaderAvailable. 752 base_.recv(&transaction_->response); 753 return; 754 } else { 755 if (!HttpShouldKeepAlive(response())) { 756 LOG(LS_VERBOSE) << "HttpClient: closing socket"; 757 base_.stream()->Close(); 758 } 759 std::string location; 760 if (ShouldRedirect(&location)) { 761 Url<char> purl(location); 762 set_server(SocketAddress(purl.host(), purl.port())); 763 request().path = purl.full_path(); 764 if (response().scode == HC_SEE_OTHER) { 765 request().verb = HV_GET; 766 request().clearHeader(HH_CONTENT_TYPE); 767 request().clearHeader(HH_CONTENT_LENGTH); 768 request().document.reset(); 769 } else if (request().document && !request().document->Rewind()) { 770 // Unable to replay the request document. 771 ASSERT(REDIRECT_ALWAYS == redirect_action_); 772 err = HE_STREAM; 773 } 774 if (err == HE_NONE) { 775 ++redirects_; 776 context_.reset(); 777 response().clear(false); 778 release(); 779 start(); 780 return; 781 } 782 } else if ((HC_PROXY_AUTHENTICATION_REQUIRED == response().scode) 783 && (PROXY_HTTPS == proxy_.type)) { 784 std::string authorization, auth_method; 785 HttpData::const_iterator begin = response().begin(HH_PROXY_AUTHENTICATE); 786 HttpData::const_iterator end = response().end(HH_PROXY_AUTHENTICATE); 787 for (HttpData::const_iterator it = begin; it != end; ++it) { 788 HttpAuthContext *context = context_.get(); 789 HttpAuthResult res = HttpAuthenticate( 790 it->second.data(), it->second.size(), 791 proxy_.address, 792 ToString(request().verb), request().path, 793 proxy_.username, proxy_.password, 794 context, authorization, auth_method); 795 context_.reset(context); 796 if (res == HAR_RESPONSE) { 797 request().setHeader(HH_PROXY_AUTHORIZATION, authorization); 798 if (request().document && !request().document->Rewind()) { 799 err = HE_STREAM; 800 } else { 801 // Explicitly do not reset the HttpAuthContext 802 response().clear(false); 803 // TODO: Reuse socket when authenticating? 804 release(); 805 start(); 806 return; 807 } 808 } else if (res == HAR_IGNORE) { 809 LOG(INFO) << "Ignoring Proxy-Authenticate: " << auth_method; 810 continue; 811 } else { 812 break; 813 } 814 } 815 } 816 } 817 if (CS_WRITING == cache_state_) { 818 CompleteCacheFile(); 819 cache_state_ = CS_READY; 820 } else if (CS_READING == cache_state_) { 821 cache_state_ = CS_READY; 822 } 823 release(); 824 SignalHttpClientComplete(this, err); 825 } 826 827 void HttpClient::onHttpClosed(HttpError err) { 828 // This shouldn't occur, since we return the stream to the pool upon command 829 // completion. 830 ASSERT(false); 831 } 832 833 ////////////////////////////////////////////////////////////////////// 834 // HttpClientDefault 835 ////////////////////////////////////////////////////////////////////// 836 837 HttpClientDefault::HttpClientDefault(SocketFactory* factory, 838 const std::string& agent, 839 HttpTransaction* transaction) 840 : ReuseSocketPool(factory ? factory : Thread::Current()->socketserver()), 841 HttpClient(agent, NULL, transaction) { 842 set_pool(this); 843 } 844 845 ////////////////////////////////////////////////////////////////////// 846 847 } // namespace talk_base 848