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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 void NotifyMalformedContent(); | 95 void NotifyMalformedContent(); |
96 | 96 |
97 // Deletes the request, removes it from the registry, and removes the | 97 // Deletes the request, removes it from the registry, and removes the |
98 // destruction observer. | 98 // destruction observer. |
99 void ReleaseRequest(); | 99 void ReleaseRequest(); |
100 | 100 |
101 // Returns the max value of exponential back-off release time for | 101 // Returns the max value of exponential back-off release time for |
102 // |original_url_| and |url_|. | 102 // |original_url_| and |url_|. |
103 base::TimeTicks GetBackoffReleaseTime(); | 103 base::TimeTicks GetBackoffReleaseTime(); |
104 | 104 |
105 void AddUploadDataChunkInThread(const std::string& data); | |
wtc
2011/01/20 00:29:47
Nit: we seem to use the naming convention Complete
Satish
2011/01/20 18:02:36
Any examples? Most functions I see are of the form
wtc
2011/01/21 19:35:54
I found only four examples:
- src/chrome/browser/s
| |
106 | |
107 // Adds a block of data to be uploaded in a POST body. This can be called | |
108 // before or after Start() is called. | |
vandebo (ex-Chrome)
2011/01/18 21:51:17
The header says this can be called only after Star
| |
109 void AppendChunkToUpload(const std::string& data); | |
110 | |
105 URLFetcher* fetcher_; // Corresponding fetcher object | 111 URLFetcher* fetcher_; // Corresponding fetcher object |
106 GURL original_url_; // The URL we were asked to fetch | 112 GURL original_url_; // The URL we were asked to fetch |
107 GURL url_; // The URL we eventually wound up at | 113 GURL url_; // The URL we eventually wound up at |
108 RequestType request_type_; // What type of request is this? | 114 RequestType request_type_; // What type of request is this? |
109 URLFetcher::Delegate* delegate_; // Object to notify on completion | 115 URLFetcher::Delegate* delegate_; // Object to notify on completion |
110 scoped_refptr<base::MessageLoopProxy> delegate_loop_proxy_; | 116 scoped_refptr<base::MessageLoopProxy> delegate_loop_proxy_; |
111 // Message loop proxy of the creating | 117 // Message loop proxy of the creating |
112 // thread. | 118 // thread. |
113 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 119 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
114 // The message loop proxy for the thread | 120 // The message loop proxy for the thread |
115 // on which the request IO happens. | 121 // on which the request IO happens. |
116 scoped_ptr<net::URLRequest> request_; // The actual request this wraps | 122 scoped_ptr<net::URLRequest> request_; // The actual request this wraps |
117 int load_flags_; // Flags for the load operation | 123 int load_flags_; // Flags for the load operation |
118 int response_code_; // HTTP status code for the request | 124 int response_code_; // HTTP status code for the request |
119 std::string data_; // Results of the request | 125 std::string data_; // Results of the request |
120 scoped_refptr<net::IOBuffer> buffer_; | 126 scoped_refptr<net::IOBuffer> buffer_; |
121 // Read buffer | 127 // Read buffer |
122 scoped_refptr<URLRequestContextGetter> request_context_getter_; | 128 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
123 // Cookie/cache info for the request | 129 // Cookie/cache info for the request |
124 ResponseCookies cookies_; // Response cookies | 130 ResponseCookies cookies_; // Response cookies |
125 net::HttpRequestHeaders extra_request_headers_; | 131 net::HttpRequestHeaders extra_request_headers_; |
126 scoped_refptr<net::HttpResponseHeaders> response_headers_; | 132 scoped_refptr<net::HttpResponseHeaders> response_headers_; |
127 | 133 |
128 std::string upload_content_; // HTTP POST payload | 134 std::string upload_content_; // HTTP POST payload |
129 std::string upload_content_type_; // MIME type of POST payload | 135 std::string upload_content_type_; // MIME type of POST payload |
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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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)), |
224 num_retries_(0), | 231 num_retries_(0), |
232 is_chunked_upload_(false), | |
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 |
234 void URLFetcher::Core::Start() { | 242 void URLFetcher::Core::Start() { |
(...skipping 40 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()) { | |
wtc
2011/01/20 00:29:47
Nit: use content.length() or content.size() consis
| |
297 request_->AppendChunkToUpload(content.data(), | |
298 static_cast<int>(content.size())); | |
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 | 368 |
340 switch (request_type_) { | 369 switch (request_type_) { |
341 case GET: | 370 case GET: |
342 break; | 371 break; |
343 | 372 |
344 case POST: | 373 case POST: |
345 DCHECK(!upload_content_.empty()); | 374 DCHECK(!upload_content_.empty() || is_chunked_upload_); |
346 DCHECK(!upload_content_type_.empty()); | 375 DCHECK(!upload_content_type_.empty()); |
347 | 376 |
348 request_->set_method("POST"); | 377 request_->set_method("POST"); |
349 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kContentType, | 378 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kContentType, |
350 upload_content_type_); | 379 upload_content_type_); |
351 request_->AppendBytesToUpload(upload_content_.data(), | 380 if (!upload_content_.empty()) { |
352 static_cast<int>(upload_content_.size())); | 381 request_->AppendBytesToUpload(upload_content_.data(), |
382 static_cast<int>(upload_content_.size())); | |
383 } | |
353 break; | 384 break; |
354 | 385 |
355 case HEAD: | 386 case HEAD: |
356 request_->set_method("HEAD"); | 387 request_->set_method("HEAD"); |
357 break; | 388 break; |
358 | 389 |
359 default: | 390 default: |
360 NOTREACHED(); | 391 NOTREACHED(); |
361 } | 392 } |
362 | 393 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
466 destination_url_backoff = | 497 destination_url_backoff = |
467 url_throttler_entry_->GetExponentialBackoffReleaseTime(); | 498 url_throttler_entry_->GetExponentialBackoffReleaseTime(); |
468 } | 499 } |
469 | 500 |
470 return original_url_backoff > destination_url_backoff ? | 501 return original_url_backoff > destination_url_backoff ? |
471 original_url_backoff : destination_url_backoff; | 502 original_url_backoff : destination_url_backoff; |
472 } | 503 } |
473 | 504 |
474 void URLFetcher::set_upload_data(const std::string& upload_content_type, | 505 void URLFetcher::set_upload_data(const std::string& upload_content_type, |
475 const std::string& upload_content) { | 506 const std::string& upload_content) { |
507 DCHECK(!core_->is_chunked_upload_); | |
476 core_->upload_content_type_ = upload_content_type; | 508 core_->upload_content_type_ = upload_content_type; |
477 core_->upload_content_ = upload_content; | 509 core_->upload_content_ = upload_content; |
478 } | 510 } |
479 | 511 |
512 void URLFetcher::set_chunked_upload(const std::string& content_type) { | |
513 DCHECK(core_->is_chunked_upload_ || | |
514 (core_->upload_content_type_.empty() && | |
515 core_->upload_content_.empty())); | |
516 core_->upload_content_type_ = content_type; | |
517 core_->upload_content_.clear(); | |
518 core_->is_chunked_upload_ = true; | |
519 } | |
520 | |
521 void URLFetcher::AppendChunkToUpload(const std::string& data) { | |
522 DCHECK(data.length()); | |
523 core_->AppendChunkToUpload(data); | |
524 } | |
525 | |
526 void URLFetcher::MarkEndOfChunks() { | |
527 core_->AppendChunkToUpload(""); | |
wtc
2011/01/20 00:29:47
Nit: "" => std::string()
| |
528 } | |
529 | |
480 const std::string& URLFetcher::upload_data() const { | 530 const std::string& URLFetcher::upload_data() const { |
481 return core_->upload_content_; | 531 return core_->upload_content_; |
482 } | 532 } |
483 | 533 |
484 void URLFetcher::set_load_flags(int load_flags) { | 534 void URLFetcher::set_load_flags(int load_flags) { |
485 core_->load_flags_ = load_flags; | 535 core_->load_flags_ = load_flags; |
486 } | 536 } |
487 | 537 |
488 int URLFetcher::load_flags() const { | 538 int URLFetcher::load_flags() const { |
489 return core_->load_flags_; | 539 return core_->load_flags_; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
521 } | 571 } |
522 | 572 |
523 // static | 573 // static |
524 void URLFetcher::CancelAll() { | 574 void URLFetcher::CancelAll() { |
525 Core::CancelAll(); | 575 Core::CancelAll(); |
526 } | 576 } |
527 | 577 |
528 URLFetcher::Delegate* URLFetcher::delegate() const { | 578 URLFetcher::Delegate* URLFetcher::delegate() const { |
529 return core_->delegate(); | 579 return core_->delegate(); |
530 } | 580 } |
OLD | NEW |