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); | |
vandebo (ex-Chrome)
2011/01/14 05:53:44
Should this be private?
Satish
2011/01/14 18:09:29
All these functions are in the private block, but
| |
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. | |
109 void AppendChunkToUpload(const std::string& data); | |
110 void MarkEndOfChunks(); | |
111 | |
105 URLFetcher* fetcher_; // Corresponding fetcher object | 112 URLFetcher* fetcher_; // Corresponding fetcher object |
106 GURL original_url_; // The URL we were asked to fetch | 113 GURL original_url_; // The URL we were asked to fetch |
107 GURL url_; // The URL we eventually wound up at | 114 GURL url_; // The URL we eventually wound up at |
108 RequestType request_type_; // What type of request is this? | 115 RequestType request_type_; // What type of request is this? |
109 URLFetcher::Delegate* delegate_; // Object to notify on completion | 116 URLFetcher::Delegate* delegate_; // Object to notify on completion |
110 scoped_refptr<base::MessageLoopProxy> delegate_loop_proxy_; | 117 scoped_refptr<base::MessageLoopProxy> delegate_loop_proxy_; |
111 // Message loop proxy of the creating | 118 // Message loop proxy of the creating |
112 // thread. | 119 // thread. |
113 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 120 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
114 // The message loop proxy for the thread | 121 // The message loop proxy for the thread |
115 // on which the request IO happens. | 122 // on which the request IO happens. |
116 scoped_ptr<net::URLRequest> request_; // The actual request this wraps | 123 scoped_ptr<net::URLRequest> request_; // The actual request this wraps |
117 int load_flags_; // Flags for the load operation | 124 int load_flags_; // Flags for the load operation |
118 int response_code_; // HTTP status code for the request | 125 int response_code_; // HTTP status code for the request |
119 std::string data_; // Results of the request | 126 std::string data_; // Results of the request |
120 scoped_refptr<net::IOBuffer> buffer_; | 127 scoped_refptr<net::IOBuffer> buffer_; |
121 // Read buffer | 128 // Read buffer |
122 scoped_refptr<URLRequestContextGetter> request_context_getter_; | 129 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
123 // Cookie/cache info for the request | 130 // Cookie/cache info for the request |
124 ResponseCookies cookies_; // Response cookies | 131 ResponseCookies cookies_; // Response cookies |
125 net::HttpRequestHeaders extra_request_headers_; | 132 net::HttpRequestHeaders extra_request_headers_; |
126 scoped_refptr<net::HttpResponseHeaders> response_headers_; | 133 scoped_refptr<net::HttpResponseHeaders> response_headers_; |
127 | 134 |
128 std::string upload_content_; // HTTP POST payload | 135 std::string upload_content_; // HTTP POST payload |
129 std::string upload_content_type_; // MIME type of POST payload | 136 std::string upload_content_type_; // MIME type of POST payload |
137 bool is_chunked_upload_; // True if using chunked transfer encoding | |
wtc
2011/01/14 03:09:31
Please document this issue:
If is_chunked_upload
Satish
2011/01/14 18:09:29
No, only one of them can be valid and the setters
| |
130 | 138 |
131 // Used to determine how long to wait before making a request or doing a | 139 // Used to determine how long to wait before making a request or doing a |
132 // retry. | 140 // retry. |
133 // Both of them can only be accessed on the IO thread. | 141 // 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 | 142 // 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, | 143 // 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 | 144 // 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 | 145 // 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 | 146 // 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. | 147 // 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), | 223 : fetcher_(fetcher), |
216 original_url_(original_url), | 224 original_url_(original_url), |
217 request_type_(request_type), | 225 request_type_(request_type), |
218 delegate_(d), | 226 delegate_(d), |
219 delegate_loop_proxy_(base::MessageLoopProxy::CreateForCurrentThread()), | 227 delegate_loop_proxy_(base::MessageLoopProxy::CreateForCurrentThread()), |
220 request_(NULL), | 228 request_(NULL), |
221 load_flags_(net::LOAD_NORMAL), | 229 load_flags_(net::LOAD_NORMAL), |
222 response_code_(-1), | 230 response_code_(-1), |
223 buffer_(new net::IOBuffer(kBufferSize)), | 231 buffer_(new net::IOBuffer(kBufferSize)), |
224 num_retries_(0), | 232 num_retries_(0), |
233 is_chunked_upload_(false), | |
225 was_cancelled_(false) { | 234 was_cancelled_(false) { |
226 } | 235 } |
227 | 236 |
228 URLFetcher::Core::~Core() { | 237 URLFetcher::Core::~Core() { |
229 // |request_| should be NULL. If not, it's unsafe to delete it here since we | 238 // |request_| should be NULL. If not, it's unsafe to delete it here since we |
230 // may not be on the IO thread. | 239 // may not be on the IO thread. |
231 DCHECK(!request_.get()); | 240 DCHECK(!request_.get()); |
232 } | 241 } |
233 | 242 |
234 void URLFetcher::Core::Start() { | 243 void URLFetcher::Core::Start() { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
275 int bytes_read = 0; | 284 int bytes_read = 0; |
276 // Some servers may treat HEAD requests as GET requests. To free up the | 285 // 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 | 286 // network connection as soon as possible, signal that the request has |
278 // completed immediately, without trying to read any data back (all we care | 287 // completed immediately, without trying to read any data back (all we care |
279 // about is the response code and headers, which we already have). | 288 // about is the response code and headers, which we already have). |
280 if (request_->status().is_success() && (request_type_ != HEAD)) | 289 if (request_->status().is_success() && (request_type_ != HEAD)) |
281 request_->Read(buffer_, kBufferSize, &bytes_read); | 290 request_->Read(buffer_, kBufferSize, &bytes_read); |
282 OnReadCompleted(request_.get(), bytes_read); | 291 OnReadCompleted(request_.get(), bytes_read); |
283 } | 292 } |
284 | 293 |
294 void URLFetcher::Core::AddUploadDataChunkInThread(const std::string& content) { | |
295 DCHECK(is_chunked_upload_); | |
296 DCHECK(request_.get()); | |
297 if (content.length() > 0) { | |
298 request_->AppendChunkToUpload(content.data(), | |
299 static_cast<int>(content.size())); | |
300 } else { | |
301 request_->MarkEndOfChunks(); | |
302 } | |
303 } | |
304 | |
305 void URLFetcher::Core::AppendChunkToUpload(const std::string& content) { | |
306 DCHECK(content.length() > 0); | |
307 DCHECK(delegate_loop_proxy_); | |
308 CHECK(io_message_loop_proxy_.get()); | |
309 io_message_loop_proxy_->PostTask( | |
310 FROM_HERE, | |
311 NewRunnableMethod(this, &Core::AddUploadDataChunkInThread, content)); | |
312 } | |
313 | |
314 void URLFetcher::Core::MarkEndOfChunks() { | |
vandebo (ex-Chrome)
2011/01/14 05:53:44
Seems you don't need both of these in the ::core c
Satish
2011/01/14 18:09:29
Done.
| |
315 DCHECK(delegate_loop_proxy_); | |
316 CHECK(io_message_loop_proxy_.get()); | |
317 std::string empty; | |
318 io_message_loop_proxy_->PostTask( | |
319 FROM_HERE, | |
320 NewRunnableMethod(this, &Core::AddUploadDataChunkInThread, empty)); | |
321 } | |
322 | |
285 void URLFetcher::Core::OnReadCompleted(net::URLRequest* request, | 323 void URLFetcher::Core::OnReadCompleted(net::URLRequest* request, |
286 int bytes_read) { | 324 int bytes_read) { |
287 DCHECK(request == request_); | 325 DCHECK(request == request_); |
288 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 326 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
289 | 327 |
290 url_ = request->url(); | 328 url_ = request->url(); |
291 url_throttler_entry_ = | 329 url_throttler_entry_ = |
292 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); | 330 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); |
293 | 331 |
294 do { | 332 do { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
327 | 365 |
328 CHECK(request_context_getter_); | 366 CHECK(request_context_getter_); |
329 DCHECK(!request_.get()); | 367 DCHECK(!request_.get()); |
330 | 368 |
331 g_registry.Get().AddURLFetcherCore(this); | 369 g_registry.Get().AddURLFetcherCore(this); |
332 request_.reset(new net::URLRequest(original_url_, this)); | 370 request_.reset(new net::URLRequest(original_url_, this)); |
333 int flags = request_->load_flags() | load_flags_; | 371 int flags = request_->load_flags() | load_flags_; |
334 if (!g_interception_enabled) { | 372 if (!g_interception_enabled) { |
335 flags = flags | net::LOAD_DISABLE_INTERCEPT; | 373 flags = flags | net::LOAD_DISABLE_INTERCEPT; |
336 } | 374 } |
375 if (is_chunked_upload_) | |
376 request_->set_chunked_upload(); | |
337 request_->set_load_flags(flags); | 377 request_->set_load_flags(flags); |
338 request_->set_context(request_context_getter_->GetURLRequestContext()); | 378 request_->set_context(request_context_getter_->GetURLRequestContext()); |
339 | 379 |
340 switch (request_type_) { | 380 switch (request_type_) { |
341 case GET: | 381 case GET: |
342 break; | 382 break; |
343 | 383 |
344 case POST: | 384 case POST: |
345 DCHECK(!upload_content_.empty()); | 385 DCHECK(!upload_content_.empty() || is_chunked_upload_); |
346 DCHECK(!upload_content_type_.empty()); | 386 DCHECK(!upload_content_type_.empty()); |
347 | 387 |
348 request_->set_method("POST"); | 388 request_->set_method("POST"); |
349 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kContentType, | 389 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kContentType, |
350 upload_content_type_); | 390 upload_content_type_); |
351 request_->AppendBytesToUpload(upload_content_.data(), | 391 if (!upload_content_.empty()) { |
352 static_cast<int>(upload_content_.size())); | 392 request_->AppendBytesToUpload(upload_content_.data(), |
393 static_cast<int>(upload_content_.size())); | |
394 } | |
353 break; | 395 break; |
354 | 396 |
355 case HEAD: | 397 case HEAD: |
356 request_->set_method("HEAD"); | 398 request_->set_method("HEAD"); |
357 break; | 399 break; |
358 | 400 |
359 default: | 401 default: |
360 NOTREACHED(); | 402 NOTREACHED(); |
361 } | 403 } |
362 | 404 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
468 } | 510 } |
469 | 511 |
470 return original_url_backoff > destination_url_backoff ? | 512 return original_url_backoff > destination_url_backoff ? |
471 original_url_backoff : destination_url_backoff; | 513 original_url_backoff : destination_url_backoff; |
472 } | 514 } |
473 | 515 |
474 void URLFetcher::set_upload_data(const std::string& upload_content_type, | 516 void URLFetcher::set_upload_data(const std::string& upload_content_type, |
475 const std::string& upload_content) { | 517 const std::string& upload_content) { |
476 core_->upload_content_type_ = upload_content_type; | 518 core_->upload_content_type_ = upload_content_type; |
477 core_->upload_content_ = upload_content; | 519 core_->upload_content_ = upload_content; |
520 core_->is_chunked_upload_ = false; | |
vandebo (ex-Chrome)
2011/01/14 05:53:44
Seems that this should just be a DCHECK?
Satish
2011/01/14 18:09:29
Done.
| |
521 } | |
522 | |
523 void URLFetcher::set_chunked_upload(const std::string& content_type) { | |
524 core_->upload_content_type_ = content_type; | |
525 core_->is_chunked_upload_ = true; | |
526 } | |
527 | |
528 void URLFetcher::AppendChunkToUpload(const std::string& data) { | |
529 core_->AppendChunkToUpload(data); | |
530 } | |
531 | |
532 void URLFetcher::MarkEndOfChunks() { | |
533 core_->MarkEndOfChunks(); | |
478 } | 534 } |
479 | 535 |
480 const std::string& URLFetcher::upload_data() const { | 536 const std::string& URLFetcher::upload_data() const { |
481 return core_->upload_content_; | 537 return core_->upload_content_; |
482 } | 538 } |
483 | 539 |
484 void URLFetcher::set_load_flags(int load_flags) { | 540 void URLFetcher::set_load_flags(int load_flags) { |
485 core_->load_flags_ = load_flags; | 541 core_->load_flags_ = load_flags; |
486 } | 542 } |
487 | 543 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
521 } | 577 } |
522 | 578 |
523 // static | 579 // static |
524 void URLFetcher::CancelAll() { | 580 void URLFetcher::CancelAll() { |
525 Core::CancelAll(); | 581 Core::CancelAll(); |
526 } | 582 } |
527 | 583 |
528 URLFetcher::Delegate* URLFetcher::delegate() const { | 584 URLFetcher::Delegate* URLFetcher::delegate() const { |
529 return core_->delegate(); | 585 return core_->delegate(); |
530 } | 586 } |
OLD | NEW |