OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "headless/public/util/generic_url_request_job.h" |
| 6 |
| 7 #include <string.h> |
| 8 #include <algorithm> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "headless/public/util/url_request_dispatcher.h" |
| 12 #include "net/base/io_buffer.h" |
| 13 #include "net/base/net_errors.h" |
| 14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 15 #include "net/cookies/cookie_store.h" |
| 16 #include "net/http/http_response_headers.h" |
| 17 #include "net/url_request/url_request_context.h" |
| 18 |
| 19 namespace headless { |
| 20 namespace { |
| 21 |
| 22 // True if the request method is "safe" (per section 4.2.1 of RFC 7231). |
| 23 bool IsMethodSafe(const std::string& method) { |
| 24 return method == "GET" || method == "HEAD" || method == "OPTIONS" || |
| 25 method == "TRACE"; |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
| 30 GenericURLRequestJob::GenericURLRequestJob( |
| 31 net::URLRequest* request, |
| 32 net::NetworkDelegate* network_delegate, |
| 33 URLRequestDispatcher* url_request_dispatcher, |
| 34 std::unique_ptr<URLFetcher> url_fetcher, |
| 35 Delegate* delegate) |
| 36 : ManagedDispatchURLRequestJob(request, |
| 37 network_delegate, |
| 38 url_request_dispatcher), |
| 39 url_fetcher_(std::move(url_fetcher)), |
| 40 delegate_(delegate), |
| 41 weak_factory_(this) {} |
| 42 |
| 43 GenericURLRequestJob::~GenericURLRequestJob() = default; |
| 44 |
| 45 void GenericURLRequestJob::SetExtraRequestHeaders( |
| 46 const net::HttpRequestHeaders& headers) { |
| 47 extra_request_headers_ = headers; |
| 48 } |
| 49 |
| 50 void GenericURLRequestJob::Start() { |
| 51 auto callback = [this](RewriteResult result, const GURL& url) { |
| 52 switch (result) { |
| 53 case RewriteResult::kAllow: |
| 54 // Note that we use the rewritten url for selecting cookies. |
| 55 // Also, rewriting does not affect the request initiator. |
| 56 PrepareCookies(url, url::Origin(url)); |
| 57 break; |
| 58 case RewriteResult::kDeny: |
| 59 DispatchStartError(net::ERR_FILE_NOT_FOUND); |
| 60 break; |
| 61 case RewriteResult::kFailure: |
| 62 DispatchStartError(net::ERR_UNEXPECTED); |
| 63 break; |
| 64 default: |
| 65 DCHECK(false); |
| 66 } |
| 67 }; |
| 68 |
| 69 if (!delegate_->BlockOrRewriteRequest(request_->url(), request_->referrer(), |
| 70 callback)) { |
| 71 PrepareCookies(request()->url(), |
| 72 url::Origin(request_->first_party_for_cookies())); |
| 73 } |
| 74 } |
| 75 |
| 76 void GenericURLRequestJob::PrepareCookies(const GURL& rewritten_url, |
| 77 const url::Origin& site_for_cookies) { |
| 78 net::CookieStore* cookie_store = request_->context()->cookie_store(); |
| 79 net::CookieOptions options; |
| 80 options.set_include_httponly(); |
| 81 |
| 82 // See net::URLRequestHttpJob::AddCookieHeaderAndStart(). |
| 83 url::Origin requested_origin(rewritten_url); |
| 84 if (net::registry_controlled_domains::SameDomainOrHost( |
| 85 requested_origin, site_for_cookies, |
| 86 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { |
| 87 if (net::registry_controlled_domains::SameDomainOrHost( |
| 88 requested_origin, request_->initiator(), |
| 89 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { |
| 90 options.set_same_site_cookie_mode( |
| 91 net::CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); |
| 92 } else if (IsMethodSafe(request_->method())) { |
| 93 options.set_same_site_cookie_mode( |
| 94 net::CookieOptions::SameSiteCookieMode::INCLUDE_LAX); |
| 95 } |
| 96 } |
| 97 |
| 98 cookie_store->GetCookieListWithOptionsAsync( |
| 99 rewritten_url, options, |
| 100 base::Bind(&GenericURLRequestJob::OnCookiesAvailable, |
| 101 weak_factory_.GetWeakPtr(), rewritten_url)); |
| 102 } |
| 103 |
| 104 void GenericURLRequestJob::OnCookiesAvailable( |
| 105 const GURL& rewritten_url, |
| 106 const net::CookieList& cookie_list) { |
| 107 // TODO(alexclarke): Set user agent. |
| 108 // Pass cookies, the referrer and any extra headers into the fetch request. |
| 109 extra_request_headers_.SetHeader( |
| 110 net::HttpRequestHeaders::kCookie, |
| 111 net::CookieStore::BuildCookieLine(cookie_list)); |
| 112 |
| 113 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kReferer, |
| 114 request_->referrer()); |
| 115 |
| 116 // The resource may have been supplied in the request. |
| 117 const HttpResponse* matched_resource = |
| 118 delegate_->MaybeMatchResource(rewritten_url, extra_request_headers_); |
| 119 |
| 120 if (matched_resource) { |
| 121 OnFetchCompleteExtractHeaders( |
| 122 matched_resource->final_url, matched_resource->http_response_code, |
| 123 matched_resource->response_data, matched_resource->response_data_size); |
| 124 } else { |
| 125 url_fetcher_->StartFetch(rewritten_url, extra_request_headers_, this); |
| 126 } |
| 127 } |
| 128 |
| 129 void GenericURLRequestJob::OnStartError(net::Error error) { |
| 130 DispatchStartError(error); |
| 131 } |
| 132 |
| 133 void GenericURLRequestJob::OnFetchComplete( |
| 134 const GURL& final_url, |
| 135 int http_response_code, |
| 136 scoped_refptr<net::HttpResponseHeaders> response_headers, |
| 137 const char* body, |
| 138 size_t body_size) { |
| 139 http_response_code_ = http_response_code; |
| 140 response_headers_ = response_headers; |
| 141 body_ = body; |
| 142 body_size_ = body_size; |
| 143 |
| 144 DispatchHeadersComplete(); |
| 145 |
| 146 std::string mime_type; |
| 147 GetMimeType(&mime_type); |
| 148 |
| 149 delegate_->OnResourceLoadComplete(final_url, mime_type, http_response_code); |
| 150 } |
| 151 |
| 152 int GenericURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) { |
| 153 // TODO(skyostil): Implement ranged fetches. |
| 154 // TODO(alexclarke): Add coverage for all the cases below. |
| 155 size_t bytes_available = body_size_ - read_offset_; |
| 156 size_t bytes_to_copy = |
| 157 std::min(static_cast<size_t>(buf_size), bytes_available); |
| 158 if (bytes_to_copy) { |
| 159 std::memcpy(buf->data(), &body_[read_offset_], bytes_to_copy); |
| 160 read_offset_ += bytes_to_copy; |
| 161 } |
| 162 return bytes_to_copy; |
| 163 } |
| 164 |
| 165 int GenericURLRequestJob::GetResponseCode() const { |
| 166 return http_response_code_; |
| 167 } |
| 168 |
| 169 void GenericURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 170 info->headers = response_headers_; |
| 171 } |
| 172 |
| 173 bool GenericURLRequestJob::GetMimeType(std::string* mime_type) const { |
| 174 if (!response_headers_) |
| 175 return false; |
| 176 return response_headers_->GetMimeType(mime_type); |
| 177 } |
| 178 |
| 179 bool GenericURLRequestJob::GetCharset(std::string* charset) { |
| 180 if (!response_headers_) |
| 181 return false; |
| 182 return response_headers_->GetCharset(charset); |
| 183 } |
| 184 |
| 185 } // namespace headless |
OLD | NEW |