| 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 AddUploadDataChunkInThread(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::AddUploadDataChunkInThread(const std::string& content) { |
| 294 DCHECK(is_chunked_upload_); |
| 295 DCHECK(request_.get()); |
| 296 if (content.length()) { |
| 297 request_->AppendChunkToUpload(content.data(), |
| 298 static_cast<int>(content.length())); |
| 299 } else { |
| 300 request_->MarkEndOfChunks(); |
| 301 } |
| 302 } |
| 303 |
| 304 void URLFetcher::Core::AppendChunkToUpload(const std::string& content) { |
| 305 DCHECK(delegate_loop_proxy_); |
| 306 CHECK(io_message_loop_proxy_.get()); |
| 307 io_message_loop_proxy_->PostTask( |
| 308 FROM_HERE, |
| 309 NewRunnableMethod(this, &Core::AddUploadDataChunkInThread, content)); |
| 310 } |
| 311 |
| 285 void URLFetcher::Core::OnReadCompleted(net::URLRequest* request, | 312 void URLFetcher::Core::OnReadCompleted(net::URLRequest* request, |
| 286 int bytes_read) { | 313 int bytes_read) { |
| 287 DCHECK(request == request_); | 314 DCHECK(request == request_); |
| 288 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 315 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 289 | 316 |
| 290 url_ = request->url(); | 317 url_ = request->url(); |
| 291 url_throttler_entry_ = | 318 url_throttler_entry_ = |
| 292 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); | 319 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); |
| 293 | 320 |
| 294 do { | 321 do { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 | 354 |
| 328 CHECK(request_context_getter_); | 355 CHECK(request_context_getter_); |
| 329 DCHECK(!request_.get()); | 356 DCHECK(!request_.get()); |
| 330 | 357 |
| 331 g_registry.Get().AddURLFetcherCore(this); | 358 g_registry.Get().AddURLFetcherCore(this); |
| 332 request_.reset(new net::URLRequest(original_url_, this)); | 359 request_.reset(new net::URLRequest(original_url_, this)); |
| 333 int flags = request_->load_flags() | load_flags_; | 360 int flags = request_->load_flags() | load_flags_; |
| 334 if (!g_interception_enabled) { | 361 if (!g_interception_enabled) { |
| 335 flags = flags | net::LOAD_DISABLE_INTERCEPT; | 362 flags = flags | net::LOAD_DISABLE_INTERCEPT; |
| 336 } | 363 } |
| 364 if (is_chunked_upload_) |
| 365 request_->EnableChunkedUpload(); |
| 337 request_->set_load_flags(flags); | 366 request_->set_load_flags(flags); |
| 338 request_->set_context(request_context_getter_->GetURLRequestContext()); | 367 request_->set_context(request_context_getter_->GetURLRequestContext()); |
| 339 request_->set_referrer(referrer_); | 368 request_->set_referrer(referrer_); |
| 340 | 369 |
| 341 switch (request_type_) { | 370 switch (request_type_) { |
| 342 case GET: | 371 case GET: |
| 343 break; | 372 break; |
| 344 | 373 |
| 345 case POST: | 374 case POST: |
| 346 DCHECK(!upload_content_.empty()); | 375 DCHECK(!upload_content_.empty() || is_chunked_upload_); |
| 347 DCHECK(!upload_content_type_.empty()); | 376 DCHECK(!upload_content_type_.empty()); |
| 348 | 377 |
| 349 request_->set_method("POST"); | 378 request_->set_method("POST"); |
| 350 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kContentType, | 379 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kContentType, |
| 351 upload_content_type_); | 380 upload_content_type_); |
| 352 request_->AppendBytesToUpload(upload_content_.data(), | 381 if (!upload_content_.empty()) { |
| 353 static_cast<int>(upload_content_.size())); | 382 request_->AppendBytesToUpload( |
| 383 upload_content_.data(), static_cast<int>(upload_content_.length())); |
| 384 } |
| 354 break; | 385 break; |
| 355 | 386 |
| 356 case HEAD: | 387 case HEAD: |
| 357 request_->set_method("HEAD"); | 388 request_->set_method("HEAD"); |
| 358 break; | 389 break; |
| 359 | 390 |
| 360 default: | 391 default: |
| 361 NOTREACHED(); | 392 NOTREACHED(); |
| 362 } | 393 } |
| 363 | 394 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 destination_url_backoff = | 498 destination_url_backoff = |
| 468 url_throttler_entry_->GetExponentialBackoffReleaseTime(); | 499 url_throttler_entry_->GetExponentialBackoffReleaseTime(); |
| 469 } | 500 } |
| 470 | 501 |
| 471 return original_url_backoff > destination_url_backoff ? | 502 return original_url_backoff > destination_url_backoff ? |
| 472 original_url_backoff : destination_url_backoff; | 503 original_url_backoff : destination_url_backoff; |
| 473 } | 504 } |
| 474 | 505 |
| 475 void URLFetcher::set_upload_data(const std::string& upload_content_type, | 506 void URLFetcher::set_upload_data(const std::string& upload_content_type, |
| 476 const std::string& upload_content) { | 507 const std::string& upload_content) { |
| 508 DCHECK(!core_->is_chunked_upload_); |
| 477 core_->upload_content_type_ = upload_content_type; | 509 core_->upload_content_type_ = upload_content_type; |
| 478 core_->upload_content_ = upload_content; | 510 core_->upload_content_ = upload_content; |
| 479 } | 511 } |
| 480 | 512 |
| 513 void URLFetcher::set_chunked_upload(const std::string& content_type) { |
| 514 DCHECK(core_->is_chunked_upload_ || |
| 515 (core_->upload_content_type_.empty() && |
| 516 core_->upload_content_.empty())); |
| 517 core_->upload_content_type_ = content_type; |
| 518 core_->upload_content_.clear(); |
| 519 core_->is_chunked_upload_ = true; |
| 520 } |
| 521 |
| 522 void URLFetcher::AppendChunkToUpload(const std::string& data) { |
| 523 DCHECK(data.length()); |
| 524 core_->AppendChunkToUpload(data); |
| 525 } |
| 526 |
| 527 void URLFetcher::MarkEndOfChunks() { |
| 528 core_->AppendChunkToUpload(std::string()); |
| 529 } |
| 530 |
| 481 const std::string& URLFetcher::upload_data() const { | 531 const std::string& URLFetcher::upload_data() const { |
| 482 return core_->upload_content_; | 532 return core_->upload_content_; |
| 483 } | 533 } |
| 484 | 534 |
| 485 void URLFetcher::set_referrer(const std::string& referrer) { | 535 void URLFetcher::set_referrer(const std::string& referrer) { |
| 486 core_->referrer_ = referrer; | 536 core_->referrer_ = referrer; |
| 487 } | 537 } |
| 488 | 538 |
| 489 void URLFetcher::set_load_flags(int load_flags) { | 539 void URLFetcher::set_load_flags(int load_flags) { |
| 490 core_->load_flags_ = load_flags; | 540 core_->load_flags_ = load_flags; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 } | 576 } |
| 527 | 577 |
| 528 // static | 578 // static |
| 529 void URLFetcher::CancelAll() { | 579 void URLFetcher::CancelAll() { |
| 530 Core::CancelAll(); | 580 Core::CancelAll(); |
| 531 } | 581 } |
| 532 | 582 |
| 533 URLFetcher::Delegate* URLFetcher::delegate() const { | 583 URLFetcher::Delegate* URLFetcher::delegate() const { |
| 534 return core_->delegate(); | 584 return core_->delegate(); |
| 535 } | 585 } |
| OLD | NEW |