| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "headless/public/util/generic_url_request_job.h" | 5 #include "headless/public/util/generic_url_request_job.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 weak_factory_(this) {} | 41 weak_factory_(this) {} |
| 42 | 42 |
| 43 GenericURLRequestJob::~GenericURLRequestJob() = default; | 43 GenericURLRequestJob::~GenericURLRequestJob() = default; |
| 44 | 44 |
| 45 void GenericURLRequestJob::SetExtraRequestHeaders( | 45 void GenericURLRequestJob::SetExtraRequestHeaders( |
| 46 const net::HttpRequestHeaders& headers) { | 46 const net::HttpRequestHeaders& headers) { |
| 47 extra_request_headers_ = headers; | 47 extra_request_headers_ = headers; |
| 48 } | 48 } |
| 49 | 49 |
| 50 void GenericURLRequestJob::Start() { | 50 void GenericURLRequestJob::Start() { |
| 51 auto callback = [this](RewriteResult result, const GURL& url) { | 51 auto callback = [this](RewriteResult result, const GURL& url, |
| 52 const std::string& method) { |
| 52 switch (result) { | 53 switch (result) { |
| 53 case RewriteResult::kAllow: | 54 case RewriteResult::kAllow: |
| 54 // Note that we use the rewritten url for selecting cookies. | 55 // Note that we use the rewritten url for selecting cookies. |
| 55 // Also, rewriting does not affect the request initiator. | 56 // Also, rewriting does not affect the request initiator. |
| 56 PrepareCookies(url, url::Origin(url)); | 57 PrepareCookies(url, method, url::Origin(url)); |
| 57 break; | 58 break; |
| 58 case RewriteResult::kDeny: | 59 case RewriteResult::kDeny: |
| 59 DispatchStartError(net::ERR_FILE_NOT_FOUND); | 60 DispatchStartError(net::ERR_FILE_NOT_FOUND); |
| 60 break; | 61 break; |
| 61 case RewriteResult::kFailure: | 62 case RewriteResult::kFailure: |
| 62 DispatchStartError(net::ERR_UNEXPECTED); | 63 DispatchStartError(net::ERR_UNEXPECTED); |
| 63 break; | 64 break; |
| 64 default: | 65 default: |
| 65 DCHECK(false); | 66 DCHECK(false); |
| 66 } | 67 } |
| 67 }; | 68 }; |
| 68 | 69 |
| 69 if (!delegate_->BlockOrRewriteRequest(request_->url(), request_->referrer(), | 70 if (!delegate_->BlockOrRewriteRequest(request_->url(), request_->method(), |
| 70 callback)) { | 71 request_->referrer(), callback)) { |
| 71 PrepareCookies(request()->url(), | 72 PrepareCookies(request_->url(), request_->method(), |
| 72 url::Origin(request_->first_party_for_cookies())); | 73 url::Origin(request_->first_party_for_cookies())); |
| 73 } | 74 } |
| 74 } | 75 } |
| 75 | 76 |
| 76 void GenericURLRequestJob::PrepareCookies(const GURL& rewritten_url, | 77 void GenericURLRequestJob::PrepareCookies(const GURL& rewritten_url, |
| 78 const std::string& method, |
| 77 const url::Origin& site_for_cookies) { | 79 const url::Origin& site_for_cookies) { |
| 78 net::CookieStore* cookie_store = request_->context()->cookie_store(); | 80 net::CookieStore* cookie_store = request_->context()->cookie_store(); |
| 79 net::CookieOptions options; | 81 net::CookieOptions options; |
| 80 options.set_include_httponly(); | 82 options.set_include_httponly(); |
| 81 | 83 |
| 82 // See net::URLRequestHttpJob::AddCookieHeaderAndStart(). | 84 // See net::URLRequestHttpJob::AddCookieHeaderAndStart(). |
| 83 url::Origin requested_origin(rewritten_url); | 85 url::Origin requested_origin(rewritten_url); |
| 84 if (net::registry_controlled_domains::SameDomainOrHost( | 86 if (net::registry_controlled_domains::SameDomainOrHost( |
| 85 requested_origin, site_for_cookies, | 87 requested_origin, site_for_cookies, |
| 86 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { | 88 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { |
| 87 if (net::registry_controlled_domains::SameDomainOrHost( | 89 if (net::registry_controlled_domains::SameDomainOrHost( |
| 88 requested_origin, request_->initiator(), | 90 requested_origin, request_->initiator(), |
| 89 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { | 91 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { |
| 90 options.set_same_site_cookie_mode( | 92 options.set_same_site_cookie_mode( |
| 91 net::CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); | 93 net::CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); |
| 92 } else if (IsMethodSafe(request_->method())) { | 94 } else if (IsMethodSafe(request_->method())) { |
| 93 options.set_same_site_cookie_mode( | 95 options.set_same_site_cookie_mode( |
| 94 net::CookieOptions::SameSiteCookieMode::INCLUDE_LAX); | 96 net::CookieOptions::SameSiteCookieMode::INCLUDE_LAX); |
| 95 } | 97 } |
| 96 } | 98 } |
| 97 | 99 |
| 98 cookie_store->GetCookieListWithOptionsAsync( | 100 cookie_store->GetCookieListWithOptionsAsync( |
| 99 rewritten_url, options, | 101 rewritten_url, options, |
| 100 base::Bind(&GenericURLRequestJob::OnCookiesAvailable, | 102 base::Bind(&GenericURLRequestJob::OnCookiesAvailable, |
| 101 weak_factory_.GetWeakPtr(), rewritten_url)); | 103 weak_factory_.GetWeakPtr(), rewritten_url, method)); |
| 102 } | 104 } |
| 103 | 105 |
| 104 void GenericURLRequestJob::OnCookiesAvailable( | 106 void GenericURLRequestJob::OnCookiesAvailable( |
| 105 const GURL& rewritten_url, | 107 const GURL& rewritten_url, |
| 108 const std::string& method, |
| 106 const net::CookieList& cookie_list) { | 109 const net::CookieList& cookie_list) { |
| 107 // TODO(alexclarke): Set user agent. | 110 // TODO(alexclarke): Set user agent. |
| 108 // Pass cookies, the referrer and any extra headers into the fetch request. | 111 // Pass cookies, the referrer and any extra headers into the fetch request. |
| 109 extra_request_headers_.SetHeader( | 112 extra_request_headers_.SetHeader( |
| 110 net::HttpRequestHeaders::kCookie, | 113 net::HttpRequestHeaders::kCookie, |
| 111 net::CookieStore::BuildCookieLine(cookie_list)); | 114 net::CookieStore::BuildCookieLine(cookie_list)); |
| 112 | 115 |
| 113 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kReferer, | 116 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kReferer, |
| 114 request_->referrer()); | 117 request_->referrer()); |
| 115 | 118 |
| 116 // The resource may have been supplied in the request. | 119 // The resource may have been supplied in the request. |
| 117 const HttpResponse* matched_resource = | 120 const HttpResponse* matched_resource = delegate_->MaybeMatchResource( |
| 118 delegate_->MaybeMatchResource(rewritten_url, extra_request_headers_); | 121 rewritten_url, method, extra_request_headers_); |
| 119 | 122 |
| 120 if (matched_resource) { | 123 if (matched_resource) { |
| 121 OnFetchCompleteExtractHeaders( | 124 OnFetchCompleteExtractHeaders( |
| 122 matched_resource->final_url, matched_resource->http_response_code, | 125 matched_resource->final_url, matched_resource->http_response_code, |
| 123 matched_resource->response_data, matched_resource->response_data_size); | 126 matched_resource->response_data, matched_resource->response_data_size); |
| 124 } else { | 127 } else { |
| 125 url_fetcher_->StartFetch(rewritten_url, extra_request_headers_, this); | 128 url_fetcher_->StartFetch(rewritten_url, method, extra_request_headers_, |
| 129 this); |
| 126 } | 130 } |
| 127 } | 131 } |
| 128 | 132 |
| 129 void GenericURLRequestJob::OnFetchStartError(net::Error error) { | 133 void GenericURLRequestJob::OnFetchStartError(net::Error error) { |
| 130 DispatchStartError(error); | 134 DispatchStartError(error); |
| 131 } | 135 } |
| 132 | 136 |
| 133 void GenericURLRequestJob::OnFetchComplete( | 137 void GenericURLRequestJob::OnFetchComplete( |
| 134 const GURL& final_url, | 138 const GURL& final_url, |
| 135 int http_response_code, | 139 int http_response_code, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 return response_headers_->GetMimeType(mime_type); | 180 return response_headers_->GetMimeType(mime_type); |
| 177 } | 181 } |
| 178 | 182 |
| 179 bool GenericURLRequestJob::GetCharset(std::string* charset) { | 183 bool GenericURLRequestJob::GetCharset(std::string* charset) { |
| 180 if (!response_headers_) | 184 if (!response_headers_) |
| 181 return false; | 185 return false; |
| 182 return response_headers_->GetCharset(charset); | 186 return response_headers_->GetCharset(charset); |
| 183 } | 187 } |
| 184 | 188 |
| 185 } // namespace headless | 189 } // namespace headless |
| OLD | NEW |