| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/url_request_http_job.h" | 5 #include "net/url_request/url_request_http_job.h" |
| 6 | 6 |
| 7 #include "base/base_switches.h" | 7 #include "base/base_switches.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/file_version_info.h" | 11 #include "base/file_version_info.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "net/base/cookie_monster.h" | 14 #include "net/base/cookie_monster.h" |
| 15 #include "net/base/filter.h" | 15 #include "net/base/filter.h" |
| 16 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/base/net_util.h" | 18 #include "net/base/net_util.h" |
| 19 #include "net/base/sdch_manager.h" | 19 #include "net/base/sdch_manager.h" |
| 20 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
| 21 #include "net/http/http_response_info.h" | 21 #include "net/http/http_response_info.h" |
| 22 #include "net/http/http_transaction.h" | 22 #include "net/http/http_transaction.h" |
| 23 #include "net/http/http_transaction_factory.h" | 23 #include "net/http/http_transaction_factory.h" |
| 24 #include "net/http/http_util.h" |
| 24 #include "net/url_request/url_request.h" | 25 #include "net/url_request/url_request.h" |
| 25 #include "net/url_request/url_request_context.h" | 26 #include "net/url_request/url_request_context.h" |
| 26 #include "net/url_request/url_request_error_job.h" | 27 #include "net/url_request/url_request_error_job.h" |
| 27 | 28 |
| 28 // TODO(darin): make sure the port blocking code is not lost | 29 // TODO(darin): make sure the port blocking code is not lost |
| 29 | 30 |
| 30 // static | 31 // static |
| 31 URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request, | 32 URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request, |
| 32 const std::string& scheme) { | 33 const std::string& scheme) { |
| 33 DCHECK(scheme == "http" || scheme == "https"); | 34 DCHECK(scheme == "http" || scheme == "https"); |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 DCHECK(transaction_.get()); | 292 DCHECK(transaction_.get()); |
| 292 | 293 |
| 293 // Proxy gets set first, then WWW. | 294 // Proxy gets set first, then WWW. |
| 294 if (proxy_auth_state_ == net::AUTH_STATE_NEED_AUTH) { | 295 if (proxy_auth_state_ == net::AUTH_STATE_NEED_AUTH) { |
| 295 proxy_auth_state_ = net::AUTH_STATE_HAVE_AUTH; | 296 proxy_auth_state_ = net::AUTH_STATE_HAVE_AUTH; |
| 296 } else { | 297 } else { |
| 297 DCHECK(server_auth_state_ == net::AUTH_STATE_NEED_AUTH); | 298 DCHECK(server_auth_state_ == net::AUTH_STATE_NEED_AUTH); |
| 298 server_auth_state_ = net::AUTH_STATE_HAVE_AUTH; | 299 server_auth_state_ = net::AUTH_STATE_HAVE_AUTH; |
| 299 } | 300 } |
| 300 | 301 |
| 302 RestartTransactionWithAuth(username, password); |
| 303 } |
| 304 |
| 305 void URLRequestHttpJob::RestartTransactionWithAuth( |
| 306 const std::wstring& username, |
| 307 const std::wstring& password) { |
| 308 |
| 301 // These will be reset in OnStartCompleted. | 309 // These will be reset in OnStartCompleted. |
| 302 response_info_ = NULL; | 310 response_info_ = NULL; |
| 303 response_cookies_.clear(); | 311 response_cookies_.clear(); |
| 304 | 312 |
| 313 // Update the cookies, since the cookie store may have been updated from the |
| 314 // headers in the 401/407. Since cookies were already appended to |
| 315 // extra_headers by AddExtraHeaders(), we need to strip them out. |
| 316 static const char* const cookie_name[] = { "cookie" }; |
| 317 request_info_.extra_headers = net::HttpUtil::StripHeaders( |
| 318 request_info_.extra_headers, cookie_name, arraysize(cookie_name)); |
| 319 // TODO(eroman): this ordering is inconsistent with non-restarted request, |
| 320 // where cookies header appears second from the bottom. |
| 321 request_info_.extra_headers += AssembleRequestCookies(); |
| 322 |
| 305 // No matter what, we want to report our status as IO pending since we will | 323 // No matter what, we want to report our status as IO pending since we will |
| 306 // be notifying our consumer asynchronously via OnStartCompleted. | 324 // be notifying our consumer asynchronously via OnStartCompleted. |
| 307 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 325 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 308 | 326 |
| 309 int rv = transaction_->RestartWithAuth(username, password, | 327 int rv = transaction_->RestartWithAuth(username, password, |
| 310 &start_callback_); | 328 &start_callback_); |
| 311 if (rv == net::ERR_IO_PENDING) | 329 if (rv == net::ERR_IO_PENDING) |
| 312 return; | 330 return; |
| 313 | 331 |
| 314 // The transaction started synchronously, but we need to notify the | 332 // The transaction started synchronously, but we need to notify the |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 // before we even download it (so that we don't waste memory or bandwidth). | 484 // before we even download it (so that we don't waste memory or bandwidth). |
| 467 if (response_info_->headers->EnumerateHeader(&iter, name, &url_text)) { | 485 if (response_info_->headers->EnumerateHeader(&iter, name, &url_text)) { |
| 468 // request_->url() won't be valid in the destructor, so we use an | 486 // request_->url() won't be valid in the destructor, so we use an |
| 469 // alternate copy. | 487 // alternate copy. |
| 470 DCHECK(request_->url() == request_info_.url); | 488 DCHECK(request_->url() == request_info_.url); |
| 471 // Resolve suggested URL relative to request url. | 489 // Resolve suggested URL relative to request url. |
| 472 sdch_dictionary_url_ = request_info_.url.Resolve(url_text); | 490 sdch_dictionary_url_ = request_info_.url.Resolve(url_text); |
| 473 } | 491 } |
| 474 } | 492 } |
| 475 | 493 |
| 494 // The HTTP transaction may be restarted several times for the purposes |
| 495 // of sending authorization information. Each time it restarts, we get |
| 496 // notified of the headers completion so that we can update the cookie store. |
| 497 if (transaction_->IsReadyToRestartForAuth()) { |
| 498 DCHECK(!response_info_->auth_challenge.get()); |
| 499 RestartTransactionWithAuth(std::wstring(), std::wstring()); |
| 500 return; |
| 501 } |
| 502 |
| 476 URLRequestJob::NotifyHeadersComplete(); | 503 URLRequestJob::NotifyHeadersComplete(); |
| 477 } | 504 } |
| 478 | 505 |
| 479 void URLRequestHttpJob::DestroyTransaction() { | 506 void URLRequestHttpJob::DestroyTransaction() { |
| 480 DCHECK(transaction_.get()); | 507 DCHECK(transaction_.get()); |
| 481 | 508 |
| 482 transaction_.reset(); | 509 transaction_.reset(); |
| 483 response_info_ = NULL; | 510 response_info_ = NULL; |
| 484 } | 511 } |
| 485 | 512 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 &avail_dictionaries); | 563 &avail_dictionaries); |
| 537 if (!avail_dictionaries.empty()) { | 564 if (!avail_dictionaries.empty()) { |
| 538 request_info_.extra_headers += "Avail-Dictionary: " | 565 request_info_.extra_headers += "Avail-Dictionary: " |
| 539 + avail_dictionaries + "\r\n"; | 566 + avail_dictionaries + "\r\n"; |
| 540 request_info_.load_flags |= net::LOAD_SDCH_DICTIONARY_ADVERTISED; | 567 request_info_.load_flags |= net::LOAD_SDCH_DICTIONARY_ADVERTISED; |
| 541 } | 568 } |
| 542 } | 569 } |
| 543 | 570 |
| 544 URLRequestContext* context = request_->context(); | 571 URLRequestContext* context = request_->context(); |
| 545 if (context) { | 572 if (context) { |
| 546 // Add in the cookie header. TODO might we need more than one header? | 573 request_info_.extra_headers += AssembleRequestCookies(); |
| 547 if (context->cookie_store() && | |
| 548 context->cookie_policy()->CanGetCookies(request_->url(), | |
| 549 request_->policy_url())) { | |
| 550 net::CookieMonster::CookieOptions options; | |
| 551 options.set_include_httponly(); | |
| 552 std::string cookies = request_->context()->cookie_store()-> | |
| 553 GetCookiesWithOptions(request_->url(), options); | |
| 554 if (!cookies.empty()) | |
| 555 request_info_.extra_headers += "Cookie: " + cookies + "\r\n"; | |
| 556 } | |
| 557 if (!context->accept_language().empty()) | 574 if (!context->accept_language().empty()) |
| 558 request_info_.extra_headers += "Accept-Language: " + | 575 request_info_.extra_headers += "Accept-Language: " + |
| 559 context->accept_language() + "\r\n"; | 576 context->accept_language() + "\r\n"; |
| 560 if (!context->accept_charset().empty()) | 577 if (!context->accept_charset().empty()) |
| 561 request_info_.extra_headers += "Accept-Charset: " + | 578 request_info_.extra_headers += "Accept-Charset: " + |
| 562 context->accept_charset() + "\r\n"; | 579 context->accept_charset() + "\r\n"; |
| 563 } | 580 } |
| 564 } | 581 } |
| 565 | 582 |
| 583 std::string URLRequestHttpJob::AssembleRequestCookies() { |
| 584 URLRequestContext* context = request_->context(); |
| 585 if (context) { |
| 586 // Add in the cookie header. TODO might we need more than one header? |
| 587 if (context->cookie_store() && |
| 588 context->cookie_policy()->CanGetCookies(request_->url(), |
| 589 request_->policy_url())) { |
| 590 net::CookieMonster::CookieOptions options; |
| 591 options.set_include_httponly(); |
| 592 std::string cookies = request_->context()->cookie_store()-> |
| 593 GetCookiesWithOptions(request_->url(), options); |
| 594 if (!cookies.empty()) |
| 595 return "Cookie: " + cookies + "\r\n"; |
| 596 } |
| 597 } |
| 598 return std::string(); |
| 599 } |
| 600 |
| 566 void URLRequestHttpJob::FetchResponseCookies() { | 601 void URLRequestHttpJob::FetchResponseCookies() { |
| 567 DCHECK(response_info_); | 602 DCHECK(response_info_); |
| 568 DCHECK(response_cookies_.empty()); | 603 DCHECK(response_cookies_.empty()); |
| 569 | 604 |
| 570 std::string name = "Set-Cookie"; | 605 std::string name = "Set-Cookie"; |
| 571 std::string value; | 606 std::string value; |
| 572 | 607 |
| 573 void* iter = NULL; | 608 void* iter = NULL; |
| 574 while (response_info_->headers->EnumerateHeader(&iter, name, &value)) | 609 while (response_info_->headers->EnumerateHeader(&iter, name, &value)) |
| 575 response_cookies_.push_back(value); | 610 response_cookies_.push_back(value); |
| 576 } | 611 } |
| OLD | NEW |