| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/net/url_fetcher.h" | 5 #include "chrome/common/net/url_fetcher.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 void NotifyMalformedContent(); | 94 void NotifyMalformedContent(); |
| 95 | 95 |
| 96 // Deletes the request, removes it from the registry, and removes the | 96 // Deletes the request, removes it from the registry, and removes the |
| 97 // destruction observer. | 97 // destruction observer. |
| 98 void ReleaseRequest(); | 98 void ReleaseRequest(); |
| 99 | 99 |
| 100 // Returns the max value of exponential back-off release time for | 100 // Returns the max value of exponential back-off release time for |
| 101 // |original_url_| and |url_|. | 101 // |original_url_| and |url_|. |
| 102 base::TimeTicks GetBackoffReleaseTime(); | 102 base::TimeTicks GetBackoffReleaseTime(); |
| 103 | 103 |
| 104 void CompleteAddingUploadDataChunk(const std::string& data); |
| 105 |
| 106 // Adds a block of data to be uploaded in a POST body. This can only be called |
| 107 // after Start(). |
| 108 void AppendChunkToUpload(const std::string& data); |
| 109 |
| 104 URLFetcher* fetcher_; // Corresponding fetcher object | 110 URLFetcher* fetcher_; // Corresponding fetcher object |
| 105 GURL original_url_; // The URL we were asked to fetch | 111 GURL original_url_; // The URL we were asked to fetch |
| 106 GURL url_; // The URL we eventually wound up at | 112 GURL url_; // The URL we eventually wound up at |
| 107 RequestType request_type_; // What type of request is this? | 113 RequestType request_type_; // What type of request is this? |
| 108 URLFetcher::Delegate* delegate_; // Object to notify on completion | 114 URLFetcher::Delegate* delegate_; // Object to notify on completion |
| 109 scoped_refptr<base::MessageLoopProxy> delegate_loop_proxy_; | 115 scoped_refptr<base::MessageLoopProxy> delegate_loop_proxy_; |
| 110 // Message loop proxy of the creating | 116 // Message loop proxy of the creating |
| 111 // thread. | 117 // thread. |
| 112 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 118 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 113 // The message loop proxy for the thread | 119 // The message loop proxy for the thread |
| 114 // on which the request IO happens. | 120 // on which the request IO happens. |
| 115 scoped_ptr<net::URLRequest> request_; // The actual request this wraps | 121 scoped_ptr<net::URLRequest> request_; // The actual request this wraps |
| 116 int load_flags_; // Flags for the load operation | 122 int load_flags_; // Flags for the load operation |
| 117 int response_code_; // HTTP status code for the request | 123 int response_code_; // HTTP status code for the request |
| 118 std::string data_; // Results of the request | 124 std::string data_; // Results of the request |
| 119 scoped_refptr<net::IOBuffer> buffer_; | 125 scoped_refptr<net::IOBuffer> buffer_; |
| 120 // Read buffer | 126 // Read buffer |
| 121 scoped_refptr<URLRequestContextGetter> request_context_getter_; | 127 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
| 122 // Cookie/cache info for the request | 128 // Cookie/cache info for the request |
| 123 ResponseCookies cookies_; // Response cookies | 129 ResponseCookies cookies_; // Response cookies |
| 124 net::HttpRequestHeaders extra_request_headers_; | 130 net::HttpRequestHeaders extra_request_headers_; |
| 125 scoped_refptr<net::HttpResponseHeaders> response_headers_; | 131 scoped_refptr<net::HttpResponseHeaders> response_headers_; |
| 126 | 132 |
| 127 std::string upload_content_; // HTTP POST payload | 133 std::string upload_content_; // HTTP POST payload |
| 128 std::string upload_content_type_; // MIME type of POST payload | 134 std::string upload_content_type_; // MIME type of POST payload |
| 129 std::string referrer_; // HTTP Referer header value | 135 std::string referrer_; // HTTP Referer header value |
| 136 bool is_chunked_upload_; // True if using chunked transfer encoding |
| 130 | 137 |
| 131 // Used to determine how long to wait before making a request or doing a | 138 // Used to determine how long to wait before making a request or doing a |
| 132 // retry. | 139 // retry. |
| 133 // Both of them can only be accessed on the IO thread. | 140 // Both of them can only be accessed on the IO thread. |
| 134 // We need not only the throttler entry for |original_URL|, but also the one | 141 // We need not only the throttler entry for |original_URL|, but also the one |
| 135 // for |url|. For example, consider the case that URL A redirects to URL B, | 142 // for |url|. For example, consider the case that URL A redirects to URL B, |
| 136 // for which the server returns a 500 response. In this case, the exponential | 143 // for which the server returns a 500 response. In this case, the exponential |
| 137 // back-off release time of URL A won't increase. If we retry without | 144 // back-off release time of URL A won't increase. If we retry without |
| 138 // considering the back-off constraint of URL B, we may send out too many | 145 // considering the back-off constraint of URL B, we may send out too many |
| 139 // requests for URL A in a short period of time. | 146 // requests for URL A in a short period of time. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 URLFetcher::Delegate* d) | 221 URLFetcher::Delegate* d) |
| 215 : fetcher_(fetcher), | 222 : fetcher_(fetcher), |
| 216 original_url_(original_url), | 223 original_url_(original_url), |
| 217 request_type_(request_type), | 224 request_type_(request_type), |
| 218 delegate_(d), | 225 delegate_(d), |
| 219 delegate_loop_proxy_(base::MessageLoopProxy::CreateForCurrentThread()), | 226 delegate_loop_proxy_(base::MessageLoopProxy::CreateForCurrentThread()), |
| 220 request_(NULL), | 227 request_(NULL), |
| 221 load_flags_(net::LOAD_NORMAL), | 228 load_flags_(net::LOAD_NORMAL), |
| 222 response_code_(-1), | 229 response_code_(-1), |
| 223 buffer_(new net::IOBuffer(kBufferSize)), | 230 buffer_(new net::IOBuffer(kBufferSize)), |
| 231 is_chunked_upload_(false), |
| 224 num_retries_(0), | 232 num_retries_(0), |
| 225 was_cancelled_(false) { | 233 was_cancelled_(false) { |
| 226 } | 234 } |
| 227 | 235 |
| 228 URLFetcher::Core::~Core() { | 236 URLFetcher::Core::~Core() { |
| 229 // |request_| should be NULL. If not, it's unsafe to delete it here since we | 237 // |request_| should be NULL. If not, it's unsafe to delete it here since we |
| 230 // may not be on the IO thread. | 238 // may not be on the IO thread. |
| 231 DCHECK(!request_.get()); | 239 DCHECK(!request_.get()); |
| 232 } | 240 } |
| 233 | 241 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 int bytes_read = 0; | 283 int bytes_read = 0; |
| 276 // Some servers may treat HEAD requests as GET requests. To free up the | 284 // Some servers may treat HEAD requests as GET requests. To free up the |
| 277 // network connection as soon as possible, signal that the request has | 285 // network connection as soon as possible, signal that the request has |
| 278 // completed immediately, without trying to read any data back (all we care | 286 // completed immediately, without trying to read any data back (all we care |
| 279 // about is the response code and headers, which we already have). | 287 // about is the response code and headers, which we already have). |
| 280 if (request_->status().is_success() && (request_type_ != HEAD)) | 288 if (request_->status().is_success() && (request_type_ != HEAD)) |
| 281 request_->Read(buffer_, kBufferSize, &bytes_read); | 289 request_->Read(buffer_, kBufferSize, &bytes_read); |
| 282 OnReadCompleted(request_.get(), bytes_read); | 290 OnReadCompleted(request_.get(), bytes_read); |
| 283 } | 291 } |
| 284 | 292 |
| 293 void URLFetcher::Core::CompleteAddingUploadDataChunk( |
| 294 const std::string& content) { |
| 295 DCHECK(is_chunked_upload_); |
| 296 DCHECK(request_.get()); |
| 297 if (content.length()) { |
| 298 request_->AppendChunkToUpload(content.data(), |
| 299 static_cast<int>(content.length())); |
| 300 } else { |
| 301 request_->MarkEndOfChunks(); |
| 302 } |
| 303 } |
| 304 |
| 305 void URLFetcher::Core::AppendChunkToUpload(const std::string& content) { |
| 306 DCHECK(delegate_loop_proxy_); |
| 307 CHECK(io_message_loop_proxy_.get()); |
| 308 io_message_loop_proxy_->PostTask( |
| 309 FROM_HERE, |
| 310 NewRunnableMethod(this, &Core::CompleteAddingUploadDataChunk, content)); |
| 311 } |
| 312 |
| 285 void URLFetcher::Core::OnReadCompleted(net::URLRequest* request, | 313 void URLFetcher::Core::OnReadCompleted(net::URLRequest* request, |
| 286 int bytes_read) { | 314 int bytes_read) { |
| 287 DCHECK(request == request_); | 315 DCHECK(request == request_); |
| 288 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 316 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 289 | 317 |
| 290 url_ = request->url(); | 318 url_ = request->url(); |
| 291 url_throttler_entry_ = | 319 url_throttler_entry_ = |
| 292 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); | 320 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); |
| 293 | 321 |
| 294 do { | 322 do { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 | 355 |
| 328 CHECK(request_context_getter_); | 356 CHECK(request_context_getter_); |
| 329 DCHECK(!request_.get()); | 357 DCHECK(!request_.get()); |
| 330 | 358 |
| 331 g_registry.Get().AddURLFetcherCore(this); | 359 g_registry.Get().AddURLFetcherCore(this); |
| 332 request_.reset(new net::URLRequest(original_url_, this)); | 360 request_.reset(new net::URLRequest(original_url_, this)); |
| 333 int flags = request_->load_flags() | load_flags_; | 361 int flags = request_->load_flags() | load_flags_; |
| 334 if (!g_interception_enabled) { | 362 if (!g_interception_enabled) { |
| 335 flags = flags | net::LOAD_DISABLE_INTERCEPT; | 363 flags = flags | net::LOAD_DISABLE_INTERCEPT; |
| 336 } | 364 } |
| 365 if (is_chunked_upload_) |
| 366 request_->EnableChunkedUpload(); |
| 337 request_->set_load_flags(flags); | 367 request_->set_load_flags(flags); |
| 338 request_->set_context(request_context_getter_->GetURLRequestContext()); | 368 request_->set_context(request_context_getter_->GetURLRequestContext()); |
| 339 request_->set_referrer(referrer_); | 369 request_->set_referrer(referrer_); |
| 340 | 370 |
| 341 switch (request_type_) { | 371 switch (request_type_) { |
| 342 case GET: | 372 case GET: |
| 343 break; | 373 break; |
| 344 | 374 |
| 345 case POST: | 375 case POST: |
| 346 DCHECK(!upload_content_.empty()); | 376 DCHECK(!upload_content_.empty() || is_chunked_upload_); |
| 347 DCHECK(!upload_content_type_.empty()); | 377 DCHECK(!upload_content_type_.empty()); |
| 348 | 378 |
| 349 request_->set_method("POST"); | 379 request_->set_method("POST"); |
| 350 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kContentType, | 380 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kContentType, |
| 351 upload_content_type_); | 381 upload_content_type_); |
| 352 request_->AppendBytesToUpload(upload_content_.data(), | 382 if (!upload_content_.empty()) { |
| 353 static_cast<int>(upload_content_.size())); | 383 request_->AppendBytesToUpload( |
| 384 upload_content_.data(), static_cast<int>(upload_content_.length())); |
| 385 } |
| 354 break; | 386 break; |
| 355 | 387 |
| 356 case HEAD: | 388 case HEAD: |
| 357 request_->set_method("HEAD"); | 389 request_->set_method("HEAD"); |
| 358 break; | 390 break; |
| 359 | 391 |
| 360 default: | 392 default: |
| 361 NOTREACHED(); | 393 NOTREACHED(); |
| 362 } | 394 } |
| 363 | 395 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 destination_url_backoff = | 499 destination_url_backoff = |
| 468 url_throttler_entry_->GetExponentialBackoffReleaseTime(); | 500 url_throttler_entry_->GetExponentialBackoffReleaseTime(); |
| 469 } | 501 } |
| 470 | 502 |
| 471 return original_url_backoff > destination_url_backoff ? | 503 return original_url_backoff > destination_url_backoff ? |
| 472 original_url_backoff : destination_url_backoff; | 504 original_url_backoff : destination_url_backoff; |
| 473 } | 505 } |
| 474 | 506 |
| 475 void URLFetcher::set_upload_data(const std::string& upload_content_type, | 507 void URLFetcher::set_upload_data(const std::string& upload_content_type, |
| 476 const std::string& upload_content) { | 508 const std::string& upload_content) { |
| 509 DCHECK(!core_->is_chunked_upload_); |
| 477 core_->upload_content_type_ = upload_content_type; | 510 core_->upload_content_type_ = upload_content_type; |
| 478 core_->upload_content_ = upload_content; | 511 core_->upload_content_ = upload_content; |
| 479 } | 512 } |
| 480 | 513 |
| 514 void URLFetcher::set_chunked_upload(const std::string& content_type) { |
| 515 DCHECK(core_->is_chunked_upload_ || |
| 516 (core_->upload_content_type_.empty() && |
| 517 core_->upload_content_.empty())); |
| 518 core_->upload_content_type_ = content_type; |
| 519 core_->upload_content_.clear(); |
| 520 core_->is_chunked_upload_ = true; |
| 521 } |
| 522 |
| 523 void URLFetcher::AppendChunkToUpload(const std::string& data) { |
| 524 DCHECK(data.length()); |
| 525 core_->AppendChunkToUpload(data); |
| 526 } |
| 527 |
| 528 void URLFetcher::MarkEndOfChunks() { |
| 529 core_->AppendChunkToUpload(std::string()); |
| 530 } |
| 531 |
| 481 const std::string& URLFetcher::upload_data() const { | 532 const std::string& URLFetcher::upload_data() const { |
| 482 return core_->upload_content_; | 533 return core_->upload_content_; |
| 483 } | 534 } |
| 484 | 535 |
| 485 void URLFetcher::set_referrer(const std::string& referrer) { | 536 void URLFetcher::set_referrer(const std::string& referrer) { |
| 486 core_->referrer_ = referrer; | 537 core_->referrer_ = referrer; |
| 487 } | 538 } |
| 488 | 539 |
| 489 void URLFetcher::set_load_flags(int load_flags) { | 540 void URLFetcher::set_load_flags(int load_flags) { |
| 490 core_->load_flags_ = load_flags; | 541 core_->load_flags_ = load_flags; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 } | 577 } |
| 527 | 578 |
| 528 // static | 579 // static |
| 529 void URLFetcher::CancelAll() { | 580 void URLFetcher::CancelAll() { |
| 530 Core::CancelAll(); | 581 Core::CancelAll(); |
| 531 } | 582 } |
| 532 | 583 |
| 533 URLFetcher::Delegate* URLFetcher::delegate() const { | 584 URLFetcher::Delegate* URLFetcher::delegate() const { |
| 534 return core_->delegate(); | 585 return core_->delegate(); |
| 535 } | 586 } |
| OLD | NEW |