url_request_redirect_job.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/url_request/url_request_redirect_job.h"
  5. #include <string>
  6. #include "base/bind.h"
  7. #include "base/check.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/location.h"
  10. #include "base/strings/stringprintf.h"
  11. #include "base/task/single_thread_task_runner.h"
  12. #include "base/threading/thread_task_runner_handle.h"
  13. #include "base/values.h"
  14. #include "net/base/load_timing_info.h"
  15. #include "net/base/net_errors.h"
  16. #include "net/http/http_log_util.h"
  17. #include "net/http/http_raw_request_headers.h"
  18. #include "net/http/http_response_headers.h"
  19. #include "net/log/net_log.h"
  20. #include "net/log/net_log_event_type.h"
  21. #include "net/log/net_log_with_source.h"
  22. #include "net/url_request/redirect_util.h"
  23. #include "net/url_request/url_request.h"
  24. #include "net/url_request/url_request_job.h"
  25. namespace net {
  26. URLRequestRedirectJob::URLRequestRedirectJob(
  27. URLRequest* request,
  28. const GURL& redirect_destination,
  29. RedirectUtil::ResponseCode response_code,
  30. const std::string& redirect_reason)
  31. : URLRequestJob(request),
  32. redirect_destination_(redirect_destination),
  33. response_code_(response_code),
  34. redirect_reason_(redirect_reason) {
  35. DCHECK(!redirect_reason_.empty());
  36. }
  37. URLRequestRedirectJob::~URLRequestRedirectJob() = default;
  38. void URLRequestRedirectJob::GetResponseInfo(HttpResponseInfo* info) {
  39. // Should only be called after the URLRequest has been notified there's header
  40. // information.
  41. DCHECK(fake_headers_.get());
  42. // This assumes |info| is a freshly constructed HttpResponseInfo.
  43. info->headers = fake_headers_;
  44. info->request_time = response_time_;
  45. info->response_time = response_time_;
  46. }
  47. void URLRequestRedirectJob::GetLoadTimingInfo(
  48. LoadTimingInfo* load_timing_info) const {
  49. // Set send_start, send_end, and receive_headers_start to
  50. // receive_headers_end_ to be consistent with network cache behavior.
  51. load_timing_info->send_start = receive_headers_end_;
  52. load_timing_info->send_end = receive_headers_end_;
  53. load_timing_info->receive_headers_start = receive_headers_end_;
  54. load_timing_info->receive_headers_end = receive_headers_end_;
  55. }
  56. void URLRequestRedirectJob::Start() {
  57. request()->net_log().AddEventWithStringParams(
  58. NetLogEventType::URL_REQUEST_REDIRECT_JOB, "reason", redirect_reason_);
  59. base::ThreadTaskRunnerHandle::Get()->PostTask(
  60. FROM_HERE, base::BindOnce(&URLRequestRedirectJob::StartAsync,
  61. weak_factory_.GetWeakPtr()));
  62. }
  63. void URLRequestRedirectJob::Kill() {
  64. weak_factory_.InvalidateWeakPtrs();
  65. URLRequestJob::Kill();
  66. }
  67. bool URLRequestRedirectJob::CopyFragmentOnRedirect(const GURL& location) const {
  68. // The instantiators have full control over the desired redirection target,
  69. // including the reference fragment part of the URL.
  70. return false;
  71. }
  72. void URLRequestRedirectJob::StartAsync() {
  73. DCHECK(request_);
  74. receive_headers_end_ = base::TimeTicks::Now();
  75. response_time_ = base::Time::Now();
  76. const HttpRequestHeaders& request_headers = request_->extra_request_headers();
  77. fake_headers_ = RedirectUtil::SynthesizeRedirectHeaders(
  78. redirect_destination_, response_code_, redirect_reason_, request_headers);
  79. NetLogResponseHeaders(
  80. request()->net_log(),
  81. NetLogEventType::URL_REQUEST_FAKE_RESPONSE_HEADERS_CREATED,
  82. fake_headers_.get());
  83. // Send request headers along if there's a callback
  84. if (request_headers_callback_) {
  85. HttpRawRequestHeaders raw_request_headers;
  86. for (const auto& header : request_headers.GetHeaderVector()) {
  87. raw_request_headers.Add(header.key, header.value);
  88. }
  89. // Just to make extra sure everyone knows this is an internal header
  90. raw_request_headers.set_request_line(
  91. base::StringPrintf("%s %s HTTP/1.1\r\n", request_->method().c_str(),
  92. request_->url().PathForRequest().c_str()));
  93. request_headers_callback_.Run(std::move(raw_request_headers));
  94. }
  95. // TODO(mmenke): Consider calling the NetworkDelegate with the headers here.
  96. // There's some weirdness about how to handle the case in which the delegate
  97. // tries to modify the redirect location, in terms of how IsSafeRedirect
  98. // should behave, and whether the fragment should be copied.
  99. URLRequestJob::NotifyHeadersComplete();
  100. }
  101. void URLRequestRedirectJob::SetRequestHeadersCallback(
  102. RequestHeadersCallback callback) {
  103. request_headers_callback_ = std::move(callback);
  104. }
  105. } // namespace net