[email protected] | cb370a063 | 2010-01-30 08:24:12 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 5 | #include "net/url_request/url_request_http_job.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6 | |
[email protected] | 4ed2755f | 2008-12-15 09:01:33 | [diff] [blame] | 7 | #include "base/base_switches.h" |
| 8 | #include "base/command_line.h" |
[email protected] | 39ce5c0 | 2008-08-22 04:03:44 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 10 | #include "base/file_util.h" |
| 11 | #include "base/file_version_info.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 12 | #include "base/message_loop.h" |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 13 | #include "base/rand_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 14 | #include "base/string_util.h" |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 15 | #include "net/base/cert_status_flags.h" |
[email protected] | cb370a063 | 2010-01-30 08:24:12 | [diff] [blame] | 16 | #include "net/base/cookie_policy.h" |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 17 | #include "net/base/cookie_store.h" |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 18 | #include "net/base/filter.h" |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 19 | #include "net/base/transport_security_state.h" |
[email protected] | b843072 | 2008-09-17 20:05:44 | [diff] [blame] | 20 | #include "net/base/load_flags.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | #include "net/base/net_errors.h" |
| 22 | #include "net/base/net_util.h" |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 23 | #include "net/base/sdch_manager.h" |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 24 | #include "net/base/ssl_cert_request_info.h" |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 25 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 26 | #include "net/http/http_response_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 27 | #include "net/http/http_response_info.h" |
| 28 | #include "net/http/http_transaction.h" |
| 29 | #include "net/http/http_transaction_factory.h" |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 30 | #include "net/http/http_util.h" |
[email protected] | be4d55fe | 2010-06-01 13:40:02 | [diff] [blame] | 31 | #include "net/url_request/https_prober.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 32 | #include "net/url_request/url_request.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 33 | #include "net/url_request/url_request_context.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 34 | #include "net/url_request/url_request_error_job.h" |
[email protected] | 06965e0 | 2009-09-04 21:36:42 | [diff] [blame] | 35 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 36 | #include "net/url_request/url_request_throttler_header_adapter.h" |
| 37 | #include "net/url_request/url_request_throttler_manager.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 38 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 39 | static const char kAvailDictionaryHeader[] = "Avail-Dictionary"; |
| 40 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 41 | namespace net { |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | class HTTPSProberDelegateImpl : public HTTPSProberDelegate { |
| 46 | public: |
| 47 | HTTPSProberDelegateImpl(const std::string& host, int max_age, |
| 48 | bool include_subdomains, |
| 49 | TransportSecurityState* sts) |
| 50 | : host_(host), |
| 51 | max_age_(max_age), |
| 52 | include_subdomains_(include_subdomains), |
| 53 | sts_(sts) { } |
| 54 | |
| 55 | virtual void ProbeComplete(bool result) { |
| 56 | if (result) { |
| 57 | base::Time current_time(base::Time::Now()); |
| 58 | base::TimeDelta max_age_delta = base::TimeDelta::FromSeconds(max_age_); |
| 59 | |
| 60 | TransportSecurityState::DomainState domain_state; |
| 61 | domain_state.expiry = current_time + max_age_delta; |
| 62 | domain_state.mode = |
| 63 | TransportSecurityState::DomainState::MODE_OPPORTUNISTIC; |
| 64 | domain_state.include_subdomains = include_subdomains_; |
| 65 | |
| 66 | sts_->EnableHost(host_, domain_state); |
| 67 | } |
| 68 | |
| 69 | delete this; |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | const std::string host_; |
| 74 | const int max_age_; |
| 75 | const bool include_subdomains_; |
| 76 | scoped_refptr<TransportSecurityState> sts_; |
| 77 | }; |
| 78 | |
| 79 | } // namespace |
| 80 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 81 | // TODO(darin): make sure the port blocking code is not lost |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 82 | // static |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 83 | URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request, |
| 84 | const std::string& scheme) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 85 | DCHECK(scheme == "http" || scheme == "https"); |
| 86 | |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 87 | int port = request->url().IntPort(); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 88 | if (!IsPortAllowedByDefault(port) && !IsPortAllowedByOverride(port)) |
| 89 | return new URLRequestErrorJob(request, ERR_UNSAFE_PORT); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 90 | |
| 91 | if (!request->context() || |
| 92 | !request->context()->http_transaction_factory()) { |
| 93 | NOTREACHED() << "requires a valid context"; |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 94 | return new URLRequestErrorJob(request, ERR_INVALID_ARGUMENT); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 97 | TransportSecurityState::DomainState domain_state; |
[email protected] | 90b15301 | 2009-09-10 18:35:16 | [diff] [blame] | 98 | if (scheme == "http" && |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 99 | (request->url().port().empty() || port == 80) && |
| 100 | request->context()->transport_security_state() && |
| 101 | request->context()->transport_security_state()->IsEnabledForHost( |
| 102 | &domain_state, request->url().host())) { |
| 103 | if (domain_state.mode == |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 104 | TransportSecurityState::DomainState::MODE_STRICT) { |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 105 | DCHECK_EQ(request->url().scheme(), "http"); |
| 106 | url_canon::Replacements<char> replacements; |
| 107 | static const char kNewScheme[] = "https"; |
| 108 | replacements.SetScheme(kNewScheme, |
| 109 | url_parse::Component(0, strlen(kNewScheme))); |
| 110 | GURL new_location = request->url().ReplaceComponents(replacements); |
| 111 | return new URLRequestRedirectJob(request, new_location); |
| 112 | } else { |
| 113 | // TODO(agl): implement opportunistic HTTPS upgrade. |
| 114 | } |
[email protected] | 06965e0 | 2009-09-04 21:36:42 | [diff] [blame] | 115 | } |
[email protected] | 4ed2755f | 2008-12-15 09:01:33 | [diff] [blame] | 116 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 117 | return new URLRequestHttpJob(request); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 120 | URLRequestHttpJob::URLRequestHttpJob(URLRequest* request) |
| 121 | : URLRequestJob(request), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 122 | response_info_(NULL), |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 123 | response_cookies_save_index_(0), |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 124 | proxy_auth_state_(AUTH_STATE_DONT_NEED_AUTH), |
| 125 | server_auth_state_(AUTH_STATE_DONT_NEED_AUTH), |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 126 | ALLOW_THIS_IN_INITIALIZER_LIST(can_get_cookies_callback_( |
| 127 | this, &URLRequestHttpJob::OnCanGetCookiesCompleted)), |
| 128 | ALLOW_THIS_IN_INITIALIZER_LIST(can_set_cookie_callback_( |
| 129 | this, &URLRequestHttpJob::OnCanSetCookieCompleted)), |
| 130 | ALLOW_THIS_IN_INITIALIZER_LIST(start_callback_( |
| 131 | this, &URLRequestHttpJob::OnStartCompleted)), |
| 132 | ALLOW_THIS_IN_INITIALIZER_LIST(read_callback_( |
| 133 | this, &URLRequestHttpJob::OnReadCompleted)), |
[email protected] | 3589e55 | 2008-08-20 23:11:34 | [diff] [blame] | 134 | read_in_progress_(false), |
[email protected] | 2aecf738 | 2009-06-17 04:14:27 | [diff] [blame] | 135 | transaction_(NULL), |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 136 | throttling_entry_(URLRequestThrottlerManager::GetInstance()-> |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 137 | RegisterRequestUrl(request->url())), |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 138 | sdch_dictionary_advertised_(false), |
| 139 | sdch_test_activated_(false), |
[email protected] | d8fd513 | 2009-05-15 01:06:53 | [diff] [blame] | 140 | sdch_test_control_(false), |
[email protected] | 00e48bf | 2010-12-03 06:15:42 | [diff] [blame] | 141 | is_cached_content_(false), |
| 142 | ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 145 | URLRequestHttpJob::~URLRequestHttpJob() { |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 146 | DCHECK(!sdch_test_control_ || !sdch_test_activated_); |
[email protected] | d8fd513 | 2009-05-15 01:06:53 | [diff] [blame] | 147 | if (!IsCachedContent()) { |
| 148 | if (sdch_test_control_) |
| 149 | RecordPacketStats(SDCH_EXPERIMENT_HOLDBACK); |
| 150 | if (sdch_test_activated_) |
| 151 | RecordPacketStats(SDCH_EXPERIMENT_DECODE); |
| 152 | } |
[email protected] | 284c373d4 | 2009-05-19 23:39:03 | [diff] [blame] | 153 | // Make sure SDCH filters are told to emit histogram data while this class |
| 154 | // can still service the IsCachedContent() call. |
| 155 | DestroyFilters(); |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 156 | |
[email protected] | 7234e6c | 2009-02-11 21:37:04 | [diff] [blame] | 157 | if (sdch_dictionary_url_.is_valid()) { |
[email protected] | d55ad15d | 2009-02-17 19:40:50 | [diff] [blame] | 158 | // Prior to reaching the destructor, request_ has been set to a NULL |
| 159 | // pointer, so request_->url() is no longer valid in the destructor, and we |
| 160 | // use an alternate copy |request_info_.url|. |
[email protected] | a41fae8 | 2009-02-21 06:11:45 | [diff] [blame] | 161 | SdchManager* manager = SdchManager::Global(); |
| 162 | // To be extra safe, since this is a "different time" from when we decided |
| 163 | // to get the dictionary, we'll validate that an SdchManager is available. |
| 164 | // At shutdown time, care is taken to be sure that we don't delete this |
| 165 | // globally useful instance "too soon," so this check is just defensive |
| 166 | // coding to assure that IF the system is shutting down, we don't have any |
| 167 | // problem if the manager was deleted ahead of time. |
| 168 | if (manager) // Defensive programming. |
| 169 | manager->FetchDictionary(request_info_.url, sdch_dictionary_url_); |
[email protected] | 7234e6c | 2009-02-11 21:37:04 | [diff] [blame] | 170 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 173 | void URLRequestHttpJob::SetUpload(UploadData* upload) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 174 | DCHECK(!transaction_.get()) << "cannot change once started"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 175 | request_info_.upload_data = upload; |
| 176 | } |
| 177 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 178 | void URLRequestHttpJob::SetExtraRequestHeaders( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 179 | const HttpRequestHeaders& headers) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 180 | DCHECK(!transaction_.get()) << "cannot change once started"; |
[email protected] | ee1a29b0 | 2010-05-06 20:42:12 | [diff] [blame] | 181 | request_info_.extra_headers.CopyFrom(headers); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 184 | void URLRequestHttpJob::Start() { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 185 | DCHECK(!transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 186 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 187 | // Ensure that we do not send username and password fields in the referrer. |
[email protected] | e600c82 | 2009-08-31 16:57:08 | [diff] [blame] | 188 | GURL referrer(request_->GetSanitizedReferrer()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 189 | |
| 190 | request_info_.url = request_->url(); |
| 191 | request_info_.referrer = referrer; |
| 192 | request_info_.method = request_->method(); |
| 193 | request_info_.load_flags = request_->load_flags(); |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 194 | request_info_.priority = request_->priority(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 195 | |
[email protected] | 6f681a4 | 2009-01-27 22:28:54 | [diff] [blame] | 196 | if (request_->context()) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 197 | request_info_.extra_headers.SetHeader( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 198 | HttpRequestHeaders::kUserAgent, |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 199 | request_->context()->GetUserAgent(request_->url())); |
[email protected] | 6f681a4 | 2009-01-27 22:28:54 | [diff] [blame] | 200 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 201 | |
| 202 | AddExtraHeaders(); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 203 | AddCookieHeaderAndStart(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 206 | void URLRequestHttpJob::Kill() { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 207 | if (!transaction_.get()) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 208 | return; |
| 209 | |
| 210 | DestroyTransaction(); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 211 | URLRequestJob::Kill(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 212 | } |
| 213 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 214 | LoadState URLRequestHttpJob::GetLoadState() const { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 215 | return transaction_.get() ? |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 216 | transaction_->GetLoadState() : LOAD_STATE_IDLE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 217 | } |
| 218 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 219 | uint64 URLRequestHttpJob::GetUploadProgress() const { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 220 | return transaction_.get() ? transaction_->GetUploadProgress() : 0; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 221 | } |
| 222 | |
[email protected] | 60c413c9 | 2009-03-09 16:53:31 | [diff] [blame] | 223 | bool URLRequestHttpJob::GetMimeType(std::string* mime_type) const { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 224 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 225 | |
| 226 | if (!response_info_) |
| 227 | return false; |
| 228 | |
| 229 | return response_info_->headers->GetMimeType(mime_type); |
| 230 | } |
| 231 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 232 | bool URLRequestHttpJob::GetCharset(std::string* charset) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 233 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 234 | |
| 235 | if (!response_info_) |
| 236 | return false; |
| 237 | |
| 238 | return response_info_->headers->GetCharset(charset); |
| 239 | } |
| 240 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 241 | void URLRequestHttpJob::GetResponseInfo(HttpResponseInfo* info) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 242 | DCHECK(request_); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 243 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 244 | |
| 245 | if (response_info_) |
| 246 | *info = *response_info_; |
| 247 | } |
| 248 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 249 | bool URLRequestHttpJob::GetResponseCookies( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 250 | std::vector<std::string>* cookies) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 251 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 252 | |
| 253 | if (!response_info_) |
| 254 | return false; |
| 255 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 256 | // TODO(darin): Why are we extracting response cookies again? Perhaps we |
| 257 | // should just leverage response_cookies_. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 258 | |
| 259 | cookies->clear(); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 260 | FetchResponseCookies(response_info_, cookies); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 261 | return true; |
| 262 | } |
| 263 | |
[email protected] | 84973ad | 2009-03-30 18:05:43 | [diff] [blame] | 264 | int URLRequestHttpJob::GetResponseCode() const { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 265 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 266 | |
| 267 | if (!response_info_) |
| 268 | return -1; |
| 269 | |
| 270 | return response_info_->headers->response_code(); |
| 271 | } |
| 272 | |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 273 | bool URLRequestHttpJob::GetContentEncodings( |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 274 | std::vector<Filter::FilterType>* encoding_types) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 275 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 276 | if (!response_info_) |
| 277 | return false; |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 278 | DCHECK(encoding_types->empty()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 279 | |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 280 | std::string encoding_type; |
| 281 | void* iter = NULL; |
| 282 | while (response_info_->headers->EnumerateHeader(&iter, "Content-Encoding", |
| 283 | &encoding_type)) { |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 284 | encoding_types->push_back(Filter::ConvertEncodingToType(encoding_type)); |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 285 | } |
[email protected] | c631b6aa | 2008-10-15 21:21:37 | [diff] [blame] | 286 | |
[email protected] | 77e9fcf | 2009-03-28 01:45:58 | [diff] [blame] | 287 | // Even if encoding types are empty, there is a chance that we need to add |
| 288 | // some decoding, as some proxies strip encoding completely. In such cases, |
| 289 | // we may need to add (for example) SDCH filtering (when the context suggests |
| 290 | // it is appropriate). |
| 291 | Filter::FixupEncodingTypes(*this, encoding_types); |
| 292 | |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 293 | return !encoding_types->empty(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 294 | } |
| 295 | |
[email protected] | c631b6aa | 2008-10-15 21:21:37 | [diff] [blame] | 296 | bool URLRequestHttpJob::IsSdchResponse() const { |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 297 | return sdch_dictionary_advertised_; |
[email protected] | c631b6aa | 2008-10-15 21:21:37 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 300 | bool URLRequestHttpJob::IsSafeRedirect(const GURL& location) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 301 | // We only allow redirects to certain "safe" protocols. This does not |
| 302 | // restrict redirects to externally handled protocols. Our consumer would |
| 303 | // need to take care of those. |
| 304 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 305 | if (!URLRequest::IsHandledURL(location)) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 306 | return true; |
| 307 | |
| 308 | static const char* kSafeSchemes[] = { |
| 309 | "http", |
| 310 | "https", |
| 311 | "ftp" |
| 312 | }; |
| 313 | |
| 314 | for (size_t i = 0; i < arraysize(kSafeSchemes); ++i) { |
| 315 | if (location.SchemeIs(kSafeSchemes[i])) |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | return false; |
| 320 | } |
| 321 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 322 | bool URLRequestHttpJob::NeedsAuth() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 323 | int code = GetResponseCode(); |
| 324 | if (code == -1) |
| 325 | return false; |
| 326 | |
| 327 | // Check if we need either Proxy or WWW Authentication. This could happen |
| 328 | // because we either provided no auth info, or provided incorrect info. |
| 329 | switch (code) { |
| 330 | case 407: |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 331 | if (proxy_auth_state_ == AUTH_STATE_CANCELED) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 332 | return false; |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 333 | proxy_auth_state_ = AUTH_STATE_NEED_AUTH; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 334 | return true; |
| 335 | case 401: |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 336 | if (server_auth_state_ == AUTH_STATE_CANCELED) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 337 | return false; |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 338 | server_auth_state_ = AUTH_STATE_NEED_AUTH; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 339 | return true; |
| 340 | } |
| 341 | return false; |
| 342 | } |
| 343 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 344 | void URLRequestHttpJob::GetAuthChallengeInfo( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 345 | scoped_refptr<AuthChallengeInfo>* result) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 346 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 347 | DCHECK(response_info_); |
| 348 | |
| 349 | // sanity checks: |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 350 | DCHECK(proxy_auth_state_ == AUTH_STATE_NEED_AUTH || |
| 351 | server_auth_state_ == AUTH_STATE_NEED_AUTH); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 352 | DCHECK(response_info_->headers->response_code() == 401 || |
| 353 | response_info_->headers->response_code() == 407); |
| 354 | |
| 355 | *result = response_info_->auth_challenge; |
| 356 | } |
| 357 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 358 | void URLRequestHttpJob::SetAuth(const string16& username, |
| 359 | const string16& password) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 360 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 361 | |
| 362 | // Proxy gets set first, then WWW. |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 363 | if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) { |
| 364 | proxy_auth_state_ = AUTH_STATE_HAVE_AUTH; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 365 | } else { |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 366 | DCHECK(server_auth_state_ == AUTH_STATE_NEED_AUTH); |
| 367 | server_auth_state_ = AUTH_STATE_HAVE_AUTH; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 370 | RestartTransactionWithAuth(username, password); |
| 371 | } |
| 372 | |
| 373 | void URLRequestHttpJob::RestartTransactionWithAuth( |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 374 | const string16& username, |
| 375 | const string16& password) { |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 376 | username_ = username; |
| 377 | password_ = password; |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 378 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 379 | // These will be reset in OnStartCompleted. |
| 380 | response_info_ = NULL; |
| 381 | response_cookies_.clear(); |
| 382 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 383 | // Update the cookies, since the cookie store may have been updated from the |
| 384 | // headers in the 401/407. Since cookies were already appended to |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 385 | // extra_headers, we need to strip them out before adding them again. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 386 | request_info_.extra_headers.RemoveHeader( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 387 | HttpRequestHeaders::kCookie); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 388 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 389 | AddCookieHeaderAndStart(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 390 | } |
| 391 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 392 | void URLRequestHttpJob::CancelAuth() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 393 | // Proxy gets set first, then WWW. |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 394 | if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) { |
| 395 | proxy_auth_state_ = AUTH_STATE_CANCELED; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 396 | } else { |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 397 | DCHECK(server_auth_state_ == AUTH_STATE_NEED_AUTH); |
| 398 | server_auth_state_ = AUTH_STATE_CANCELED; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // These will be reset in OnStartCompleted. |
| 402 | response_info_ = NULL; |
| 403 | response_cookies_.clear(); |
| 404 | |
| 405 | // OK, let the consumer read the error page... |
| 406 | // |
| 407 | // Because we set the AUTH_STATE_CANCELED flag, NeedsAuth will return false, |
| 408 | // which will cause the consumer to receive OnResponseStarted instead of |
| 409 | // OnAuthRequired. |
| 410 | // |
| 411 | // We have to do this via InvokeLater to avoid "recursing" the consumer. |
| 412 | // |
[email protected] | 00e48bf | 2010-12-03 06:15:42 | [diff] [blame] | 413 | MessageLoop::current()->PostTask( |
| 414 | FROM_HERE, |
| 415 | method_factory_.NewRunnableMethod( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 416 | &URLRequestHttpJob::OnStartCompleted, OK)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 417 | } |
| 418 | |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 419 | void URLRequestHttpJob::ContinueWithCertificate( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 420 | X509Certificate* client_cert) { |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 421 | DCHECK(transaction_.get()); |
| 422 | |
| 423 | DCHECK(!response_info_) << "should not have a response yet"; |
| 424 | |
| 425 | // No matter what, we want to report our status as IO pending since we will |
| 426 | // be notifying our consumer asynchronously via OnStartCompleted. |
| 427 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 428 | |
| 429 | int rv = transaction_->RestartWithCertificate(client_cert, &start_callback_); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 430 | if (rv == ERR_IO_PENDING) |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 431 | return; |
| 432 | |
| 433 | // The transaction started synchronously, but we need to notify the |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 434 | // URLRequest delegate via the message loop. |
[email protected] | 00e48bf | 2010-12-03 06:15:42 | [diff] [blame] | 435 | MessageLoop::current()->PostTask( |
| 436 | FROM_HERE, |
| 437 | method_factory_.NewRunnableMethod( |
| 438 | &URLRequestHttpJob::OnStartCompleted, rv)); |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 439 | } |
| 440 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 441 | void URLRequestHttpJob::ContinueDespiteLastError() { |
[email protected] | 9ec4875 | 2009-02-06 23:33:58 | [diff] [blame] | 442 | // If the transaction was destroyed, then the job was cancelled. |
| 443 | if (!transaction_.get()) |
| 444 | return; |
| 445 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 446 | DCHECK(!response_info_) << "should not have a response yet"; |
| 447 | |
| 448 | // No matter what, we want to report our status as IO pending since we will |
| 449 | // be notifying our consumer asynchronously via OnStartCompleted. |
| 450 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 451 | |
| 452 | int rv = transaction_->RestartIgnoringLastError(&start_callback_); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 453 | if (rv == ERR_IO_PENDING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 454 | return; |
| 455 | |
| 456 | // The transaction started synchronously, but we need to notify the |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 457 | // URLRequest delegate via the message loop. |
[email protected] | 00e48bf | 2010-12-03 06:15:42 | [diff] [blame] | 458 | MessageLoop::current()->PostTask( |
| 459 | FROM_HERE, |
| 460 | method_factory_.NewRunnableMethod( |
| 461 | &URLRequestHttpJob::OnStartCompleted, rv)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 462 | } |
| 463 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 464 | bool URLRequestHttpJob::ReadRawData(IOBuffer* buf, int buf_size, |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 465 | int *bytes_read) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 466 | DCHECK_NE(buf_size, 0); |
| 467 | DCHECK(bytes_read); |
| 468 | DCHECK(!read_in_progress_); |
| 469 | |
| 470 | int rv = transaction_->Read(buf, buf_size, &read_callback_); |
| 471 | if (rv >= 0) { |
| 472 | *bytes_read = rv; |
| 473 | return true; |
| 474 | } |
| 475 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 476 | if (rv == ERR_IO_PENDING) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 477 | read_in_progress_ = true; |
| 478 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 479 | } else { |
| 480 | NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |
| 481 | } |
| 482 | |
| 483 | return false; |
| 484 | } |
| 485 | |
[email protected] | 9dd90e5 | 2010-02-23 19:15:01 | [diff] [blame] | 486 | void URLRequestHttpJob::StopCaching() { |
| 487 | if (transaction_.get()) |
| 488 | transaction_->StopCaching(); |
| 489 | } |
| 490 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 491 | void URLRequestHttpJob::OnCanGetCookiesCompleted(int policy) { |
| 492 | // If the request was destroyed, then there is no more work to do. |
| 493 | if (request_ && request_->delegate()) { |
[email protected] | 52fa0750 | 2010-12-07 08:54:42 | [diff] [blame] | 494 | if (request_->context()->cookie_store()) { |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 495 | if (policy == ERR_ACCESS_DENIED) { |
[email protected] | 52fa0750 | 2010-12-07 08:54:42 | [diff] [blame] | 496 | request_->delegate()->OnGetCookies(request_, true); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 497 | } else if (policy == OK) { |
[email protected] | 52fa0750 | 2010-12-07 08:54:42 | [diff] [blame] | 498 | request_->delegate()->OnGetCookies(request_, false); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 499 | CookieOptions options; |
[email protected] | 52fa0750 | 2010-12-07 08:54:42 | [diff] [blame] | 500 | options.set_include_httponly(); |
| 501 | std::string cookies = |
| 502 | request_->context()->cookie_store()->GetCookiesWithOptions( |
| 503 | request_->url(), options); |
| 504 | if (!cookies.empty()) { |
| 505 | request_info_.extra_headers.SetHeader( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 506 | HttpRequestHeaders::kCookie, cookies); |
[email protected] | 52fa0750 | 2010-12-07 08:54:42 | [diff] [blame] | 507 | } |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 508 | } |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 509 | } |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 510 | // We may have been canceled within OnGetCookies. |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 511 | if (GetStatus().is_success()) { |
| 512 | StartTransaction(); |
| 513 | } else { |
| 514 | NotifyCanceled(); |
| 515 | } |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 516 | } |
| 517 | Release(); // Balance AddRef taken in AddCookieHeaderAndStart |
| 518 | } |
| 519 | |
| 520 | void URLRequestHttpJob::OnCanSetCookieCompleted(int policy) { |
| 521 | // If the request was destroyed, then there is no more work to do. |
| 522 | if (request_ && request_->delegate()) { |
[email protected] | 52fa0750 | 2010-12-07 08:54:42 | [diff] [blame] | 523 | if (request_->context()->cookie_store()) { |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 524 | if (policy == ERR_ACCESS_DENIED) { |
[email protected] | 52fa0750 | 2010-12-07 08:54:42 | [diff] [blame] | 525 | request_->delegate()->OnSetCookie( |
| 526 | request_, |
| 527 | response_cookies_[response_cookies_save_index_], |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 528 | CookieOptions(), |
[email protected] | 52fa0750 | 2010-12-07 08:54:42 | [diff] [blame] | 529 | true); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 530 | } else if (policy == OK || policy == OK_FOR_SESSION_ONLY) { |
[email protected] | 52fa0750 | 2010-12-07 08:54:42 | [diff] [blame] | 531 | // OK to save the current response cookie now. |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 532 | CookieOptions options; |
[email protected] | 52fa0750 | 2010-12-07 08:54:42 | [diff] [blame] | 533 | options.set_include_httponly(); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 534 | if (policy == OK_FOR_SESSION_ONLY) |
[email protected] | 52fa0750 | 2010-12-07 08:54:42 | [diff] [blame] | 535 | options.set_force_session(); |
| 536 | request_->context()->cookie_store()->SetCookieWithOptions( |
| 537 | request_->url(), response_cookies_[response_cookies_save_index_], |
| 538 | options); |
| 539 | request_->delegate()->OnSetCookie( |
| 540 | request_, |
| 541 | response_cookies_[response_cookies_save_index_], |
| 542 | options, |
| 543 | false); |
| 544 | } |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 545 | } |
| 546 | response_cookies_save_index_++; |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 547 | // We may have been canceled within OnSetCookie. |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 548 | if (GetStatus().is_success()) { |
| 549 | SaveNextCookie(); |
| 550 | } else { |
| 551 | NotifyCanceled(); |
| 552 | } |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 553 | } |
| 554 | Release(); // Balance AddRef taken in SaveNextCookie |
| 555 | } |
| 556 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 557 | void URLRequestHttpJob::OnStartCompleted(int result) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 558 | // If the request was destroyed, then there is no more work to do. |
| 559 | if (!request_ || !request_->delegate()) |
| 560 | return; |
| 561 | |
| 562 | // If the transaction was destroyed, then the job was cancelled, and |
| 563 | // we can just ignore this notification. |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 564 | if (!transaction_.get()) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 565 | return; |
| 566 | |
| 567 | // Clear the IO_PENDING status |
| 568 | SetStatus(URLRequestStatus()); |
| 569 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 570 | if (result == OK) { |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 571 | SaveCookiesAndNotifyHeadersComplete(); |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 572 | } else if (ShouldTreatAsCertificateError(result)) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 573 | // We encountered an SSL certificate error. Ask our delegate to decide |
| 574 | // what we should do. |
| 575 | // TODO(wtc): also pass ssl_info.cert_status, or just pass the whole |
| 576 | // ssl_info. |
| 577 | request_->delegate()->OnSSLCertificateError( |
| 578 | request_, result, transaction_->GetResponseInfo()->ssl_info.cert); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 579 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 580 | request_->delegate()->OnCertificateRequested( |
| 581 | request_, transaction_->GetResponseInfo()->cert_request_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 582 | } else { |
| 583 | NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 584 | } |
| 585 | } |
| 586 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 587 | void URLRequestHttpJob::OnReadCompleted(int result) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 588 | read_in_progress_ = false; |
| 589 | |
| 590 | if (result == 0) { |
| 591 | NotifyDone(URLRequestStatus()); |
| 592 | } else if (result < 0) { |
| 593 | NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 594 | } else { |
| 595 | // Clear the IO_PENDING status |
| 596 | SetStatus(URLRequestStatus()); |
| 597 | } |
| 598 | |
| 599 | NotifyReadComplete(result); |
| 600 | } |
| 601 | |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 602 | bool URLRequestHttpJob::ShouldTreatAsCertificateError(int result) { |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 603 | if (!IsCertificateError(result)) |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 604 | return false; |
| 605 | |
[email protected] | 77f6fb43 | 2009-09-05 14:21:09 | [diff] [blame] | 606 | // Check whether our context is using Strict-Transport-Security. |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 607 | if (!context_->transport_security_state()) |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 608 | return true; |
| 609 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 610 | TransportSecurityState::DomainState domain_state; |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 611 | // TODO(agl): don't ignore opportunistic mode. |
| 612 | const bool r = context_->transport_security_state()->IsEnabledForHost( |
| 613 | &domain_state, request_info_.url.host()); |
| 614 | |
| 615 | return !r || domain_state.mode == |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 616 | TransportSecurityState::DomainState::MODE_OPPORTUNISTIC; |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 617 | } |
| 618 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 619 | void URLRequestHttpJob::NotifyHeadersComplete() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 620 | DCHECK(!response_info_); |
| 621 | |
| 622 | response_info_ = transaction_->GetResponseInfo(); |
| 623 | |
[email protected] | d8fd513 | 2009-05-15 01:06:53 | [diff] [blame] | 624 | // Save boolean, as we'll need this info at destruction time, and filters may |
| 625 | // also need this info. |
| 626 | is_cached_content_ = response_info_->was_cached; |
| 627 | |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 628 | if (!is_cached_content_) { |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 629 | URLRequestThrottlerHeaderAdapter response_adapter( |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 630 | response_info_->headers); |
| 631 | throttling_entry_->UpdateWithResponse(&response_adapter); |
| 632 | } |
| 633 | |
[email protected] | 77f6fb43 | 2009-09-05 14:21:09 | [diff] [blame] | 634 | ProcessStrictTransportSecurityHeader(); |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 635 | |
[email protected] | fe21987 | 2008-09-23 02:17:00 | [diff] [blame] | 636 | if (SdchManager::Global() && |
| 637 | SdchManager::Global()->IsInSupportedDomain(request_->url())) { |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 638 | static const std::string name = "Get-Dictionary"; |
| 639 | std::string url_text; |
| 640 | void* iter = NULL; |
| 641 | // TODO(jar): We need to not fetch dictionaries the first time they are |
| 642 | // seen, but rather wait until we can justify their usefulness. |
| 643 | // For now, we will only fetch the first dictionary, which will at least |
| 644 | // require multiple suggestions before we get additional ones for this site. |
| 645 | // Eventually we should wait until a dictionary is requested several times |
| 646 | // before we even download it (so that we don't waste memory or bandwidth). |
| 647 | if (response_info_->headers->EnumerateHeader(&iter, name, &url_text)) { |
[email protected] | d55ad15d | 2009-02-17 19:40:50 | [diff] [blame] | 648 | // request_->url() won't be valid in the destructor, so we use an |
| 649 | // alternate copy. |
| 650 | DCHECK(request_->url() == request_info_.url); |
| 651 | // Resolve suggested URL relative to request url. |
| 652 | sdch_dictionary_url_ = request_info_.url.Resolve(url_text); |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 656 | // The HTTP transaction may be restarted several times for the purposes |
| 657 | // of sending authorization information. Each time it restarts, we get |
| 658 | // notified of the headers completion so that we can update the cookie store. |
| 659 | if (transaction_->IsReadyToRestartForAuth()) { |
| 660 | DCHECK(!response_info_->auth_challenge.get()); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 661 | RestartTransactionWithAuth(string16(), string16()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 662 | return; |
| 663 | } |
| 664 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 665 | URLRequestJob::NotifyHeadersComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 666 | } |
| 667 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 668 | void URLRequestHttpJob::DestroyTransaction() { |
[email protected] | c6a4eb9 | 2010-03-03 23:51:19 | [diff] [blame] | 669 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 670 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 671 | transaction_.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 672 | response_info_ = NULL; |
[email protected] | fa4332d | 2010-11-23 09:59:09 | [diff] [blame] | 673 | context_ = NULL; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 674 | } |
| 675 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 676 | void URLRequestHttpJob::StartTransaction() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 677 | // NOTE: This method assumes that request_info_ is already setup properly. |
| 678 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 679 | // If we already have a transaction, then we should restart the transaction |
| 680 | // with auth provided by username_ and password_. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 681 | |
[email protected] | 99c0790 | 2010-08-17 18:59:52 | [diff] [blame] | 682 | int rv; |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 683 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 684 | if (transaction_.get()) { |
[email protected] | 99c0790 | 2010-08-17 18:59:52 | [diff] [blame] | 685 | rv = transaction_->RestartWithAuth(username_, password_, &start_callback_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 686 | username_.clear(); |
| 687 | password_.clear(); |
| 688 | } else { |
| 689 | DCHECK(request_->context()); |
| 690 | DCHECK(request_->context()->http_transaction_factory()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 691 | |
[email protected] | 99c0790 | 2010-08-17 18:59:52 | [diff] [blame] | 692 | rv = request_->context()->http_transaction_factory()->CreateTransaction( |
| 693 | &transaction_); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 694 | if (rv == OK) { |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 695 | if (!throttling_entry_->IsDuringExponentialBackoff()) { |
| 696 | rv = transaction_->Start( |
| 697 | &request_info_, &start_callback_, request_->net_log()); |
| 698 | } else { |
| 699 | // Special error code for the exponential back-off module. |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 700 | rv = ERR_TEMPORARILY_THROTTLED; |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 701 | } |
[email protected] | fa4332d | 2010-11-23 09:59:09 | [diff] [blame] | 702 | // Make sure the context is alive for the duration of the |
| 703 | // transaction. |
| 704 | context_ = request_->context(); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 705 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 706 | } |
| 707 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 708 | if (rv == ERR_IO_PENDING) |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 709 | return; |
| 710 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 711 | // The transaction started synchronously, but we need to notify the |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 712 | // URLRequest delegate via the message loop. |
[email protected] | 00e48bf | 2010-12-03 06:15:42 | [diff] [blame] | 713 | MessageLoop::current()->PostTask( |
| 714 | FROM_HERE, |
| 715 | method_factory_.NewRunnableMethod( |
| 716 | &URLRequestHttpJob::OnStartCompleted, rv)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 717 | } |
| 718 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 719 | void URLRequestHttpJob::AddExtraHeaders() { |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 720 | // TODO(jar): Consider optimizing away SDCH advertising bytes when the URL is |
| 721 | // probably an img or such (and SDCH encoding is not likely). |
| 722 | bool advertise_sdch = SdchManager::Global() && |
| 723 | SdchManager::Global()->IsInSupportedDomain(request_->url()); |
| 724 | std::string avail_dictionaries; |
| 725 | if (advertise_sdch) { |
| 726 | SdchManager::Global()->GetAvailDictionaryList(request_->url(), |
| 727 | &avail_dictionaries); |
| 728 | |
| 729 | // The AllowLatencyExperiment() is only true if we've successfully done a |
| 730 | // full SDCH compression recently in this browser session for this host. |
| 731 | // Note that for this path, there might be no applicable dictionaries, and |
| 732 | // hence we can't participate in the experiment. |
| 733 | if (!avail_dictionaries.empty() && |
| 734 | SdchManager::Global()->AllowLatencyExperiment(request_->url())) { |
| 735 | // We are participating in the test (or control), and hence we'll |
| 736 | // eventually record statistics via either SDCH_EXPERIMENT_DECODE or |
| 737 | // SDCH_EXPERIMENT_HOLDBACK, and we'll need some packet timing data. |
| 738 | EnablePacketCounting(kSdchPacketHistogramCount); |
[email protected] | a88af523 | 2009-06-05 01:34:53 | [diff] [blame] | 739 | if (base::RandDouble() < .01) { |
| 740 | sdch_test_control_ = true; // 1% probability. |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 741 | advertise_sdch = false; |
| 742 | } else { |
| 743 | sdch_test_activated_ = true; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 748 | // Supply Accept-Encoding headers first so that it is more likely that they |
| 749 | // will be in the first transmitted packet. This can sometimes make it easier |
| 750 | // to filter and analyze the streams to assure that a proxy has not damaged |
| 751 | // these headers. Some proxies deliberately corrupt Accept-Encoding headers. |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 752 | if (!advertise_sdch) { |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 753 | // Tell the server what compression formats we support (other than SDCH). |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 754 | request_info_.extra_headers.SetHeader( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 755 | HttpRequestHeaders::kAcceptEncoding, "gzip,deflate"); |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 756 | } else { |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 757 | // Include SDCH in acceptable list. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 758 | request_info_.extra_headers.SetHeader( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 759 | HttpRequestHeaders::kAcceptEncoding, "gzip,deflate,sdch"); |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 760 | if (!avail_dictionaries.empty()) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 761 | request_info_.extra_headers.SetHeader( |
| 762 | kAvailDictionaryHeader, |
| 763 | avail_dictionaries); |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 764 | sdch_dictionary_advertised_ = true; |
| 765 | // Since we're tagging this transaction as advertising a dictionary, we'll |
| 766 | // definately employ an SDCH filter (or tentative sdch filter) when we get |
| 767 | // a response. When done, we'll record histograms via SDCH_DECODE or |
| 768 | // SDCH_PASSTHROUGH. Hence we need to record packet arrival times. |
| 769 | EnablePacketCounting(kSdchPacketHistogramCount); |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 770 | } |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 771 | } |
| 772 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 773 | URLRequestContext* context = request_->context(); |
| 774 | if (context) { |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 775 | // Only add default Accept-Language and Accept-Charset if the request |
| 776 | // didn't have them specified. |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 777 | if (!request_info_.extra_headers.HasHeader( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 778 | HttpRequestHeaders::kAcceptLanguage)) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 779 | request_info_.extra_headers.SetHeader( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 780 | HttpRequestHeaders::kAcceptLanguage, |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 781 | context->accept_language()); |
| 782 | } |
| 783 | if (!request_info_.extra_headers.HasHeader( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 784 | HttpRequestHeaders::kAcceptCharset)) { |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 785 | request_info_.extra_headers.SetHeader( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 786 | HttpRequestHeaders::kAcceptCharset, |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 787 | context->accept_charset()); |
| 788 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 789 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 790 | } |
| 791 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 792 | void URLRequestHttpJob::AddCookieHeaderAndStart() { |
| 793 | // No matter what, we want to report our status as IO pending since we will |
| 794 | // be notifying our consumer asynchronously via OnStartCompleted. |
| 795 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 796 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 797 | AddRef(); // Balanced in OnCanGetCookiesCompleted |
| 798 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 799 | int policy = OK; |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 800 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 801 | if (request_info_.load_flags & LOAD_DO_NOT_SEND_COOKIES) { |
| 802 | policy = ERR_FAILED; |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 803 | } else if (request_->context()->cookie_policy()) { |
| 804 | policy = request_->context()->cookie_policy()->CanGetCookies( |
| 805 | request_->url(), |
| 806 | request_->first_party_for_cookies(), |
| 807 | &can_get_cookies_callback_); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 808 | if (policy == ERR_IO_PENDING) |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 809 | return; // Wait for completion callback |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 810 | } |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 811 | |
| 812 | OnCanGetCookiesCompleted(policy); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 813 | } |
| 814 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 815 | void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete() { |
| 816 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 817 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 818 | const HttpResponseInfo* response_info = transaction_->GetResponseInfo(); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 819 | DCHECK(response_info); |
| 820 | |
| 821 | response_cookies_.clear(); |
| 822 | response_cookies_save_index_ = 0; |
| 823 | |
| 824 | FetchResponseCookies(response_info, &response_cookies_); |
| 825 | |
| 826 | // Now, loop over the response cookies, and attempt to persist each. |
| 827 | SaveNextCookie(); |
| 828 | } |
| 829 | |
| 830 | void URLRequestHttpJob::SaveNextCookie() { |
| 831 | if (response_cookies_save_index_ == response_cookies_.size()) { |
| 832 | response_cookies_.clear(); |
| 833 | response_cookies_save_index_ = 0; |
| 834 | SetStatus(URLRequestStatus()); // Clear the IO_PENDING status |
| 835 | NotifyHeadersComplete(); |
| 836 | return; |
| 837 | } |
| 838 | |
| 839 | // No matter what, we want to report our status as IO pending since we will |
| 840 | // be notifying our consumer asynchronously via OnStartCompleted. |
| 841 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 842 | |
| 843 | AddRef(); // Balanced in OnCanSetCookieCompleted |
| 844 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 845 | int policy = OK; |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 846 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 847 | if (request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) { |
| 848 | policy = ERR_FAILED; |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 849 | } else if (request_->context()->cookie_policy()) { |
| 850 | policy = request_->context()->cookie_policy()->CanSetCookie( |
| 851 | request_->url(), |
| 852 | request_->first_party_for_cookies(), |
| 853 | response_cookies_[response_cookies_save_index_], |
| 854 | &can_set_cookie_callback_); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 855 | if (policy == ERR_IO_PENDING) |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 856 | return; // Wait for completion callback |
| 857 | } |
| 858 | |
| 859 | OnCanSetCookieCompleted(policy); |
| 860 | } |
| 861 | |
| 862 | void URLRequestHttpJob::FetchResponseCookies( |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 863 | const HttpResponseInfo* response_info, |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 864 | std::vector<std::string>* cookies) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 865 | std::string name = "Set-Cookie"; |
| 866 | std::string value; |
| 867 | |
| 868 | void* iter = NULL; |
[email protected] | 2adf288 | 2010-09-27 08:30:37 | [diff] [blame] | 869 | while (response_info->headers->EnumerateHeader(&iter, name, &value)) { |
| 870 | if (!value.empty()) |
| 871 | cookies->push_back(value); |
| 872 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 873 | } |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 874 | |
[email protected] | 77f6fb43 | 2009-09-05 14:21:09 | [diff] [blame] | 875 | void URLRequestHttpJob::ProcessStrictTransportSecurityHeader() { |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 876 | DCHECK(response_info_); |
| 877 | |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 878 | URLRequestContext* ctx = request_->context(); |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 879 | if (!ctx || !ctx->transport_security_state()) |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 880 | return; |
| 881 | |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 882 | const bool https = response_info_->ssl_info.is_valid(); |
| 883 | const bool valid_https = |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 884 | https && !IsCertStatusError(response_info_->ssl_info.cert_status); |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 885 | |
[email protected] | 77f6fb43 | 2009-09-05 14:21:09 | [diff] [blame] | 886 | std::string name = "Strict-Transport-Security"; |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 887 | std::string value; |
| 888 | |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 889 | int max_age; |
| 890 | bool include_subdomains; |
| 891 | |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 892 | void* iter = NULL; |
[email protected] | 77f6fb43 | 2009-09-05 14:21:09 | [diff] [blame] | 893 | while (response_info_->headers->EnumerateHeader(&iter, name, &value)) { |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 894 | const bool ok = TransportSecurityState::ParseHeader( |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 895 | value, &max_age, &include_subdomains); |
| 896 | if (!ok) |
| 897 | continue; |
| 898 | // We will only accept strict mode if we saw the header from an HTTPS |
| 899 | // connection with no certificate problems. |
| 900 | if (!valid_https) |
| 901 | continue; |
| 902 | base::Time current_time(base::Time::Now()); |
| 903 | base::TimeDelta max_age_delta = base::TimeDelta::FromSeconds(max_age); |
| 904 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 905 | TransportSecurityState::DomainState domain_state; |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 906 | domain_state.expiry = current_time + max_age_delta; |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 907 | domain_state.mode = TransportSecurityState::DomainState::MODE_STRICT; |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 908 | domain_state.include_subdomains = include_subdomains; |
| 909 | |
| 910 | ctx->transport_security_state()->EnableHost(request_info_.url.host(), |
| 911 | domain_state); |
| 912 | } |
| 913 | |
| 914 | // TODO(agl): change this over when we have fixed things at the server end. |
| 915 | // The string should be "Opportunistic-Transport-Security"; |
| 916 | name = "X-Bodge-Transport-Security"; |
| 917 | |
| 918 | while (response_info_->headers->EnumerateHeader(&iter, name, &value)) { |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 919 | const bool ok = TransportSecurityState::ParseHeader( |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 920 | value, &max_age, &include_subdomains); |
| 921 | if (!ok) |
| 922 | continue; |
| 923 | // If we saw an opportunistic request over HTTPS, then clearly we can make |
| 924 | // HTTPS connections to the host so we should remember this. |
| 925 | if (https) { |
| 926 | base::Time current_time(base::Time::Now()); |
| 927 | base::TimeDelta max_age_delta = base::TimeDelta::FromSeconds(max_age); |
| 928 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 929 | TransportSecurityState::DomainState domain_state; |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 930 | domain_state.expiry = current_time + max_age_delta; |
| 931 | domain_state.mode = |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 932 | TransportSecurityState::DomainState::MODE_SPDY_ONLY; |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 933 | domain_state.include_subdomains = include_subdomains; |
| 934 | |
| 935 | ctx->transport_security_state()->EnableHost(request_info_.url.host(), |
| 936 | domain_state); |
| 937 | continue; |
| 938 | } |
| 939 | |
| 940 | if (!request()) |
| 941 | break; |
| 942 | |
| 943 | // At this point, we have a request for opportunistic encryption over HTTP. |
| 944 | // In this case we need to probe to check that we can make HTTPS |
| 945 | // connections to that host. |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 946 | HTTPSProber* const prober = HTTPSProber::GetInstance(); |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 947 | if (prober->HaveProbed(request_info_.url.host()) || |
| 948 | prober->InFlight(request_info_.url.host())) { |
| 949 | continue; |
| 950 | } |
| 951 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 952 | HTTPSProberDelegateImpl* delegate = |
| 953 | new HTTPSProberDelegateImpl(request_info_.url.host(), max_age, |
| 954 | include_subdomains, |
| 955 | ctx->transport_security_state()); |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 956 | if (!prober->ProbeHost(request_info_.url.host(), request()->context(), |
| 957 | delegate)) { |
| 958 | delete delegate; |
| 959 | } |
[email protected] | 77f6fb43 | 2009-09-05 14:21:09 | [diff] [blame] | 960 | } |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 961 | } |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame^] | 962 | |
| 963 | } // namespace net |