license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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 |
| 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" |
| 13 | #include "base/string_util.h" |
| 14 | #include "net/base/cookie_monster.h" |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 15 | #include "net/base/filter.h" |
[email protected] | b843072 | 2008-09-17 20:05:44 | [diff] [blame] | 16 | #include "net/base/load_flags.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 17 | #include "net/base/net_errors.h" |
| 18 | #include "net/base/net_util.h" |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 19 | #include "net/base/sdch_manager.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 20 | #include "net/http/http_response_info.h" |
| 21 | #include "net/http/http_transaction.h" |
| 22 | #include "net/http/http_transaction_factory.h" |
| 23 | #include "net/url_request/url_request.h" |
| 24 | #include "net/url_request/url_request_error_job.h" |
| 25 | |
| 26 | // TODO(darin): make sure the port blocking code is not lost |
| 27 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 28 | // static |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 29 | URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request, |
| 30 | const std::string& scheme) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 31 | DCHECK(scheme == "http" || scheme == "https"); |
| 32 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 33 | if (!net::IsPortAllowedByDefault(request->url().IntPort())) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 34 | return new URLRequestErrorJob(request, net::ERR_UNSAFE_PORT); |
| 35 | |
| 36 | if (!request->context() || |
| 37 | !request->context()->http_transaction_factory()) { |
| 38 | NOTREACHED() << "requires a valid context"; |
| 39 | return new URLRequestErrorJob(request, net::ERR_INVALID_ARGUMENT); |
| 40 | } |
| 41 | |
[email protected] | 4ed2755f | 2008-12-15 09:01:33 | [diff] [blame] | 42 | // We cache the value of the switch because this code path is hit on every |
| 43 | // network request. |
| 44 | static const bool kForceHTTPS = |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 45 | CommandLine::ForCurrentProcess()->HasSwitch(switches::kForceHTTPS); |
[email protected] | 4ed2755f | 2008-12-15 09:01:33 | [diff] [blame] | 46 | if (kForceHTTPS && scheme != "https") |
| 47 | return new URLRequestErrorJob(request, net::ERR_DISALLOWED_URL_SCHEME); |
| 48 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 49 | return new URLRequestHttpJob(request); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 52 | URLRequestHttpJob::URLRequestHttpJob(URLRequest* request) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 53 | : URLRequestJob(request), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 54 | transaction_(NULL), |
| 55 | response_info_(NULL), |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 56 | proxy_auth_state_(net::AUTH_STATE_DONT_NEED_AUTH), |
| 57 | server_auth_state_(net::AUTH_STATE_DONT_NEED_AUTH), |
[email protected] | 39ce5c0 | 2008-08-22 04:03:44 | [diff] [blame] | 58 | ALLOW_THIS_IN_INITIALIZER_LIST( |
| 59 | start_callback_(this, &URLRequestHttpJob::OnStartCompleted)), |
| 60 | ALLOW_THIS_IN_INITIALIZER_LIST( |
| 61 | read_callback_(this, &URLRequestHttpJob::OnReadCompleted)), |
[email protected] | 3589e55 | 2008-08-20 23:11:34 | [diff] [blame] | 62 | read_in_progress_(false), |
| 63 | context_(request->context()) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 66 | URLRequestHttpJob::~URLRequestHttpJob() { |
[email protected] | 7234e6c | 2009-02-11 21:37:04 | [diff] [blame^] | 67 | if (sdch_dictionary_url_.is_valid()) { |
| 68 | SdchManager::Global()->FetchDictionary(sdch_dictionary_url_); |
| 69 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 72 | void URLRequestHttpJob::SetUpload(net::UploadData* upload) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 73 | DCHECK(!transaction_.get()) << "cannot change once started"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 74 | request_info_.upload_data = upload; |
| 75 | } |
| 76 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 77 | void URLRequestHttpJob::SetExtraRequestHeaders( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 78 | const std::string& headers) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 79 | DCHECK(!transaction_.get()) << "cannot change once started"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 80 | request_info_.extra_headers = headers; |
| 81 | } |
| 82 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 83 | void URLRequestHttpJob::Start() { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 84 | DCHECK(!transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 85 | |
| 86 | // TODO(darin): URLRequest::referrer() should return a GURL |
| 87 | GURL referrer(request_->referrer()); |
| 88 | |
| 89 | // Ensure that we do not send username and password fields in the referrer. |
| 90 | if (referrer.has_username() || referrer.has_password()) { |
| 91 | GURL::Replacements referrer_mods; |
| 92 | referrer_mods.ClearUsername(); |
| 93 | referrer_mods.ClearPassword(); |
| 94 | referrer = referrer.ReplaceComponents(referrer_mods); |
| 95 | } |
| 96 | |
| 97 | request_info_.url = request_->url(); |
| 98 | request_info_.referrer = referrer; |
| 99 | request_info_.method = request_->method(); |
| 100 | request_info_.load_flags = request_->load_flags(); |
| 101 | |
[email protected] | 6f681a4 | 2009-01-27 22:28:54 | [diff] [blame] | 102 | if (request_->context()) { |
| 103 | request_info_.user_agent = |
| 104 | request_->context()->GetUserAgent(request_->url()); |
| 105 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 106 | |
| 107 | AddExtraHeaders(); |
| 108 | |
| 109 | StartTransaction(); |
| 110 | } |
| 111 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 112 | void URLRequestHttpJob::Kill() { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 113 | if (!transaction_.get()) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 114 | return; |
| 115 | |
| 116 | DestroyTransaction(); |
| 117 | URLRequestJob::Kill(); |
| 118 | } |
| 119 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 120 | net::LoadState URLRequestHttpJob::GetLoadState() const { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 121 | return transaction_.get() ? |
| 122 | transaction_->GetLoadState() : net::LOAD_STATE_IDLE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 123 | } |
| 124 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 125 | uint64 URLRequestHttpJob::GetUploadProgress() const { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 126 | return transaction_.get() ? transaction_->GetUploadProgress() : 0; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 129 | bool URLRequestHttpJob::GetMimeType(std::string* mime_type) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 130 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 131 | |
| 132 | if (!response_info_) |
| 133 | return false; |
| 134 | |
| 135 | return response_info_->headers->GetMimeType(mime_type); |
| 136 | } |
| 137 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 138 | bool URLRequestHttpJob::GetCharset(std::string* charset) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 139 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 140 | |
| 141 | if (!response_info_) |
| 142 | return false; |
| 143 | |
| 144 | return response_info_->headers->GetCharset(charset); |
| 145 | } |
| 146 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 147 | void URLRequestHttpJob::GetResponseInfo(net::HttpResponseInfo* info) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 148 | DCHECK(request_); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 149 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 150 | |
| 151 | if (response_info_) |
| 152 | *info = *response_info_; |
| 153 | } |
| 154 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 155 | bool URLRequestHttpJob::GetResponseCookies( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 156 | std::vector<std::string>* cookies) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 157 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 158 | |
| 159 | if (!response_info_) |
| 160 | return false; |
| 161 | |
| 162 | if (response_cookies_.empty()) |
| 163 | FetchResponseCookies(); |
| 164 | |
| 165 | cookies->clear(); |
| 166 | cookies->swap(response_cookies_); |
| 167 | return true; |
| 168 | } |
| 169 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 170 | int URLRequestHttpJob::GetResponseCode() { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 171 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 172 | |
| 173 | if (!response_info_) |
| 174 | return -1; |
| 175 | |
| 176 | return response_info_->headers->response_code(); |
| 177 | } |
| 178 | |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 179 | bool URLRequestHttpJob::GetContentEncodings( |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 180 | std::vector<Filter::FilterType>* encoding_types) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 181 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 182 | if (!response_info_) |
| 183 | return false; |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 184 | DCHECK(encoding_types->empty()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 185 | |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 186 | std::string encoding_type; |
| 187 | void* iter = NULL; |
| 188 | while (response_info_->headers->EnumerateHeader(&iter, "Content-Encoding", |
| 189 | &encoding_type)) { |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 190 | encoding_types->push_back(Filter::ConvertEncodingToType(encoding_type)); |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 191 | } |
[email protected] | c631b6aa | 2008-10-15 21:21:37 | [diff] [blame] | 192 | |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 193 | if (!encoding_types->empty()) { |
| 194 | std::string mime_type; |
| 195 | GetMimeType(&mime_type); |
| 196 | Filter::FixupEncodingTypes(IsSdchResponse(), mime_type, encoding_types); |
[email protected] | c631b6aa | 2008-10-15 21:21:37 | [diff] [blame] | 197 | } |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 198 | return !encoding_types->empty(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | c631b6aa | 2008-10-15 21:21:37 | [diff] [blame] | 201 | bool URLRequestHttpJob::IsSdchResponse() const { |
| 202 | return response_info_ && |
| 203 | (request_info_.load_flags & net::LOAD_SDCH_DICTIONARY_ADVERTISED); |
| 204 | } |
| 205 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 206 | bool URLRequestHttpJob::IsRedirectResponse(GURL* location, |
| 207 | int* http_status_code) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 208 | if (!response_info_) |
| 209 | return false; |
| 210 | |
| 211 | std::string value; |
| 212 | if (!response_info_->headers->IsRedirect(&value)) |
| 213 | return false; |
| 214 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 215 | // For HTTPS, if we didn't receive a server certificate, the response was |
| 216 | // from the proxy server (a response to the CONNECT request) rather than |
| 217 | // the server. |
| 218 | if (request_->url().SchemeIsSecure() && !response_info_->ssl_info.cert) |
| 219 | return false; |
| 220 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 221 | *location = request_->url().Resolve(value); |
| 222 | *http_status_code = response_info_->headers->response_code(); |
| 223 | return true; |
| 224 | } |
| 225 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 226 | bool URLRequestHttpJob::IsSafeRedirect(const GURL& location) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 227 | // We only allow redirects to certain "safe" protocols. This does not |
| 228 | // restrict redirects to externally handled protocols. Our consumer would |
| 229 | // need to take care of those. |
| 230 | |
| 231 | if (!URLRequest::IsHandledURL(location)) |
| 232 | return true; |
| 233 | |
| 234 | static const char* kSafeSchemes[] = { |
| 235 | "http", |
| 236 | "https", |
| 237 | "ftp" |
| 238 | }; |
| 239 | |
| 240 | for (size_t i = 0; i < arraysize(kSafeSchemes); ++i) { |
| 241 | if (location.SchemeIs(kSafeSchemes[i])) |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | return false; |
| 246 | } |
| 247 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 248 | bool URLRequestHttpJob::NeedsAuth() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 249 | int code = GetResponseCode(); |
| 250 | if (code == -1) |
| 251 | return false; |
| 252 | |
| 253 | // Check if we need either Proxy or WWW Authentication. This could happen |
| 254 | // because we either provided no auth info, or provided incorrect info. |
| 255 | switch (code) { |
| 256 | case 407: |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 257 | if (proxy_auth_state_ == net::AUTH_STATE_CANCELED) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 258 | return false; |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 259 | proxy_auth_state_ = net::AUTH_STATE_NEED_AUTH; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 260 | return true; |
| 261 | case 401: |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 262 | if (server_auth_state_ == net::AUTH_STATE_CANCELED) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 263 | return false; |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 264 | server_auth_state_ = net::AUTH_STATE_NEED_AUTH; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 265 | return true; |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 270 | void URLRequestHttpJob::GetAuthChallengeInfo( |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 271 | scoped_refptr<net::AuthChallengeInfo>* result) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 272 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 273 | DCHECK(response_info_); |
| 274 | |
| 275 | // sanity checks: |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 276 | DCHECK(proxy_auth_state_ == net::AUTH_STATE_NEED_AUTH || |
| 277 | server_auth_state_ == net::AUTH_STATE_NEED_AUTH); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 278 | DCHECK(response_info_->headers->response_code() == 401 || |
| 279 | response_info_->headers->response_code() == 407); |
| 280 | |
| 281 | *result = response_info_->auth_challenge; |
| 282 | } |
| 283 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 284 | void URLRequestHttpJob::SetAuth(const std::wstring& username, |
| 285 | const std::wstring& password) { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 286 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 287 | |
| 288 | // Proxy gets set first, then WWW. |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 289 | if (proxy_auth_state_ == net::AUTH_STATE_NEED_AUTH) { |
| 290 | proxy_auth_state_ = net::AUTH_STATE_HAVE_AUTH; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 291 | } else { |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 292 | DCHECK(server_auth_state_ == net::AUTH_STATE_NEED_AUTH); |
| 293 | server_auth_state_ = net::AUTH_STATE_HAVE_AUTH; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // These will be reset in OnStartCompleted. |
| 297 | response_info_ = NULL; |
| 298 | response_cookies_.clear(); |
| 299 | |
| 300 | // No matter what, we want to report our status as IO pending since we will |
| 301 | // be notifying our consumer asynchronously via OnStartCompleted. |
| 302 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 303 | |
| 304 | int rv = transaction_->RestartWithAuth(username, password, |
| 305 | &start_callback_); |
| 306 | if (rv == net::ERR_IO_PENDING) |
| 307 | return; |
| 308 | |
| 309 | // The transaction started synchronously, but we need to notify the |
| 310 | // URLRequest delegate via the message loop. |
| 311 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 312 | this, &URLRequestHttpJob::OnStartCompleted, rv)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 315 | void URLRequestHttpJob::CancelAuth() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 316 | // Proxy gets set first, then WWW. |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 317 | if (proxy_auth_state_ == net::AUTH_STATE_NEED_AUTH) { |
| 318 | proxy_auth_state_ = net::AUTH_STATE_CANCELED; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 319 | } else { |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 320 | DCHECK(server_auth_state_ == net::AUTH_STATE_NEED_AUTH); |
| 321 | server_auth_state_ = net::AUTH_STATE_CANCELED; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | // These will be reset in OnStartCompleted. |
| 325 | response_info_ = NULL; |
| 326 | response_cookies_.clear(); |
| 327 | |
| 328 | // OK, let the consumer read the error page... |
| 329 | // |
| 330 | // Because we set the AUTH_STATE_CANCELED flag, NeedsAuth will return false, |
| 331 | // which will cause the consumer to receive OnResponseStarted instead of |
| 332 | // OnAuthRequired. |
| 333 | // |
| 334 | // We have to do this via InvokeLater to avoid "recursing" the consumer. |
| 335 | // |
| 336 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 337 | this, &URLRequestHttpJob::OnStartCompleted, net::OK)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 338 | } |
| 339 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 340 | void URLRequestHttpJob::ContinueDespiteLastError() { |
[email protected] | 9ec4875 | 2009-02-06 23:33:58 | [diff] [blame] | 341 | // If the transaction was destroyed, then the job was cancelled. |
| 342 | if (!transaction_.get()) |
| 343 | return; |
| 344 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 345 | DCHECK(!response_info_) << "should not have a response yet"; |
| 346 | |
| 347 | // No matter what, we want to report our status as IO pending since we will |
| 348 | // be notifying our consumer asynchronously via OnStartCompleted. |
| 349 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 350 | |
| 351 | int rv = transaction_->RestartIgnoringLastError(&start_callback_); |
| 352 | if (rv == net::ERR_IO_PENDING) |
| 353 | return; |
| 354 | |
| 355 | // The transaction started synchronously, but we need to notify the |
| 356 | // URLRequest delegate via the message loop. |
| 357 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 358 | this, &URLRequestHttpJob::OnStartCompleted, rv)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 359 | } |
| 360 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 361 | bool URLRequestHttpJob::GetMoreData() { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 362 | return transaction_.get() && !read_in_progress_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 363 | } |
| 364 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 365 | bool URLRequestHttpJob::ReadRawData(net::IOBuffer* buf, int buf_size, |
| 366 | int *bytes_read) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 367 | DCHECK_NE(buf_size, 0); |
| 368 | DCHECK(bytes_read); |
| 369 | DCHECK(!read_in_progress_); |
| 370 | |
| 371 | int rv = transaction_->Read(buf, buf_size, &read_callback_); |
| 372 | if (rv >= 0) { |
| 373 | *bytes_read = rv; |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | if (rv == net::ERR_IO_PENDING) { |
| 378 | read_in_progress_ = true; |
| 379 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 380 | } else { |
| 381 | NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |
| 382 | } |
| 383 | |
| 384 | return false; |
| 385 | } |
| 386 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 387 | void URLRequestHttpJob::OnStartCompleted(int result) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 388 | // If the request was destroyed, then there is no more work to do. |
| 389 | if (!request_ || !request_->delegate()) |
| 390 | return; |
| 391 | |
| 392 | // If the transaction was destroyed, then the job was cancelled, and |
| 393 | // we can just ignore this notification. |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 394 | if (!transaction_.get()) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 395 | return; |
| 396 | |
| 397 | // Clear the IO_PENDING status |
| 398 | SetStatus(URLRequestStatus()); |
| 399 | |
| 400 | if (result == net::OK) { |
| 401 | NotifyHeadersComplete(); |
[email protected] | 4ed2755f | 2008-12-15 09:01:33 | [diff] [blame] | 402 | } else if (net::IsCertificateError(result) && |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 403 | !CommandLine::ForCurrentProcess()->HasSwitch( |
| 404 | switches::kForceHTTPS)) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 405 | // We encountered an SSL certificate error. Ask our delegate to decide |
| 406 | // what we should do. |
| 407 | // TODO(wtc): also pass ssl_info.cert_status, or just pass the whole |
| 408 | // ssl_info. |
| 409 | request_->delegate()->OnSSLCertificateError( |
| 410 | request_, result, transaction_->GetResponseInfo()->ssl_info.cert); |
| 411 | } else { |
| 412 | NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 413 | } |
| 414 | } |
| 415 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 416 | void URLRequestHttpJob::OnReadCompleted(int result) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 417 | read_in_progress_ = false; |
| 418 | |
| 419 | if (result == 0) { |
| 420 | NotifyDone(URLRequestStatus()); |
| 421 | } else if (result < 0) { |
| 422 | NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 423 | } else { |
| 424 | // Clear the IO_PENDING status |
| 425 | SetStatus(URLRequestStatus()); |
| 426 | } |
| 427 | |
| 428 | NotifyReadComplete(result); |
| 429 | } |
| 430 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 431 | void URLRequestHttpJob::NotifyHeadersComplete() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 432 | DCHECK(!response_info_); |
| 433 | |
| 434 | response_info_ = transaction_->GetResponseInfo(); |
| 435 | |
| 436 | // Get the Set-Cookie values, and send them to our cookie database. |
[email protected] | b843072 | 2008-09-17 20:05:44 | [diff] [blame] | 437 | if (!(request_info_.load_flags & net::LOAD_DO_NOT_SAVE_COOKIES)) { |
| 438 | URLRequestContext* ctx = request_->context(); |
| 439 | if (ctx && ctx->cookie_store() && |
| 440 | ctx->cookie_policy()->CanSetCookie(request_->url(), |
| 441 | request_->policy_url())) { |
| 442 | FetchResponseCookies(); |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 443 | net::CookieMonster::CookieOptions options; |
| 444 | options.set_include_httponly(); |
| 445 | ctx->cookie_store()->SetCookiesWithOptions(request_->url(), |
| 446 | response_cookies_, |
| 447 | options); |
[email protected] | b843072 | 2008-09-17 20:05:44 | [diff] [blame] | 448 | } |
| 449 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 450 | |
[email protected] | fe21987 | 2008-09-23 02:17:00 | [diff] [blame] | 451 | if (SdchManager::Global() && |
| 452 | SdchManager::Global()->IsInSupportedDomain(request_->url())) { |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 453 | static const std::string name = "Get-Dictionary"; |
| 454 | std::string url_text; |
| 455 | void* iter = NULL; |
| 456 | // TODO(jar): We need to not fetch dictionaries the first time they are |
| 457 | // seen, but rather wait until we can justify their usefulness. |
| 458 | // For now, we will only fetch the first dictionary, which will at least |
| 459 | // require multiple suggestions before we get additional ones for this site. |
| 460 | // Eventually we should wait until a dictionary is requested several times |
| 461 | // before we even download it (so that we don't waste memory or bandwidth). |
| 462 | if (response_info_->headers->EnumerateHeader(&iter, name, &url_text)) { |
| 463 | GURL dictionary_url = request_->url().Resolve(url_text); |
[email protected] | 7234e6c | 2009-02-11 21:37:04 | [diff] [blame^] | 464 | if (SdchManager::Global()->CanFetchDictionary(request_->url(), |
| 465 | dictionary_url)) |
| 466 | sdch_dictionary_url_ = dictionary_url; |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 470 | URLRequestJob::NotifyHeadersComplete(); |
| 471 | } |
| 472 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 473 | void URLRequestHttpJob::DestroyTransaction() { |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 474 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 475 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 476 | transaction_.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 477 | response_info_ = NULL; |
| 478 | } |
| 479 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 480 | void URLRequestHttpJob::StartTransaction() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 481 | // NOTE: This method assumes that request_info_ is already setup properly. |
| 482 | |
| 483 | // Create a transaction. |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 484 | DCHECK(!transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 485 | |
| 486 | DCHECK(request_->context()); |
| 487 | DCHECK(request_->context()->http_transaction_factory()); |
| 488 | |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 489 | transaction_.reset( |
| 490 | request_->context()->http_transaction_factory()->CreateTransaction()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 491 | |
| 492 | // No matter what, we want to report our status as IO pending since we will |
| 493 | // be notifying our consumer asynchronously via OnStartCompleted. |
| 494 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 495 | |
| 496 | int rv; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 497 | if (transaction_.get()) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 498 | rv = transaction_->Start(&request_info_, &start_callback_); |
| 499 | if (rv == net::ERR_IO_PENDING) |
| 500 | return; |
| 501 | } else { |
| 502 | rv = net::ERR_FAILED; |
| 503 | } |
| 504 | |
| 505 | // The transaction started synchronously, but we need to notify the |
| 506 | // URLRequest delegate via the message loop. |
| 507 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 508 | this, &URLRequestHttpJob::OnStartCompleted, rv)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 509 | } |
| 510 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 511 | void URLRequestHttpJob::AddExtraHeaders() { |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 512 | // Supply Accept-Encoding headers first so that it is more likely that they |
| 513 | // will be in the first transmitted packet. This can sometimes make it easier |
| 514 | // to filter and analyze the streams to assure that a proxy has not damaged |
| 515 | // these headers. Some proxies deliberately corrupt Accept-Encoding headers. |
| 516 | if (!SdchManager::Global() || |
| 517 | !SdchManager::Global()->IsInSupportedDomain(request_->url())) { |
| 518 | // Tell the server what compression formats we support (other than SDCH). |
| 519 | request_info_.extra_headers += "Accept-Encoding: gzip,deflate,bzip2\r\n"; |
| 520 | } else { |
| 521 | // Supply SDCH related headers, as well as accepting that encoding. |
| 522 | // Tell the server what compression formats we support. |
| 523 | request_info_.extra_headers += "Accept-Encoding: " |
| 524 | "gzip,deflate,bzip2,sdch\r\n"; |
| 525 | |
| 526 | // TODO(jar): See if it is worth optimizing away these bytes when the URL is |
| 527 | // probably an img or such. (and SDCH encoding is not likely). |
| 528 | std::string avail_dictionaries; |
| 529 | SdchManager::Global()->GetAvailDictionaryList(request_->url(), |
| 530 | &avail_dictionaries); |
| 531 | if (!avail_dictionaries.empty()) { |
| 532 | request_info_.extra_headers += "Avail-Dictionary: " |
| 533 | + avail_dictionaries + "\r\n"; |
| 534 | request_info_.load_flags |= net::LOAD_SDCH_DICTIONARY_ADVERTISED; |
| 535 | } |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 536 | } |
| 537 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 538 | URLRequestContext* context = request_->context(); |
| 539 | if (context) { |
| 540 | // Add in the cookie header. TODO might we need more than one header? |
| 541 | if (context->cookie_store() && |
| 542 | context->cookie_policy()->CanGetCookies(request_->url(), |
| 543 | request_->policy_url())) { |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 544 | net::CookieMonster::CookieOptions options; |
| 545 | options.set_include_httponly(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 546 | std::string cookies = request_->context()->cookie_store()-> |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 547 | GetCookiesWithOptions(request_->url(), options); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 548 | if (!cookies.empty()) |
| 549 | request_info_.extra_headers += "Cookie: " + cookies + "\r\n"; |
| 550 | } |
| 551 | if (!context->accept_language().empty()) |
| 552 | request_info_.extra_headers += "Accept-Language: " + |
| 553 | context->accept_language() + "\r\n"; |
| 554 | if (!context->accept_charset().empty()) |
| 555 | request_info_.extra_headers += "Accept-Charset: " + |
| 556 | context->accept_charset() + "\r\n"; |
| 557 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 558 | } |
| 559 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 560 | void URLRequestHttpJob::FetchResponseCookies() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 561 | DCHECK(response_info_); |
| 562 | DCHECK(response_cookies_.empty()); |
| 563 | |
| 564 | std::string name = "Set-Cookie"; |
| 565 | std::string value; |
| 566 | |
| 567 | void* iter = NULL; |
| 568 | while (response_info_->headers->EnumerateHeader(&iter, name, &value)) |
| 569 | response_cookies_.push_back(value); |
| 570 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 571 | |