Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(625)

Side by Side Diff: net/url_request/url_request_job.cc

Issue 173049: Revert r23616 from 172. (Closed) Base URL: svn://chrome-svn.corp.google.com/chrome/branches/172/src/
Patch Set: Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/url_request/url_request_job.h ('k') | net/url_request/url_request_unittest.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 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 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 "net/url_request/url_request_job.h" 5 #include "net/url_request/url_request_job.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "googleurl/src/gurl.h"
9 #include "net/base/auth.h" 10 #include "net/base/auth.h"
10 #include "net/base/io_buffer.h" 11 #include "net/base/io_buffer.h"
11 #include "net/base/load_flags.h" 12 #include "net/base/load_flags.h"
12 #include "net/base/net_errors.h" 13 #include "net/base/net_errors.h"
13 #include "net/url_request/url_request.h" 14 #include "net/url_request/url_request.h"
14 #include "net/url_request/url_request_job_metrics.h" 15 #include "net/url_request/url_request_job_metrics.h"
15 #include "net/url_request/url_request_job_tracker.h" 16 #include "net/url_request/url_request_job_tracker.h"
16 17
17 using base::Time; 18 using base::Time;
18 using base::TimeTicks; 19 using base::TimeTicks;
19 20
20 // Buffer size allocated when de-compressing data. 21 // Buffer size allocated when de-compressing data.
21 // static 22 // static
22 const int URLRequestJob::kFilterBufSize = 32 * 1024; 23 const int URLRequestJob::kFilterBufSize = 32 * 1024;
23 24
24 URLRequestJob::URLRequestJob(URLRequest* request) 25 URLRequestJob::URLRequestJob(URLRequest* request)
25 : request_(request), 26 : request_(request),
26 done_(false), 27 done_(false),
27 filter_needs_more_output_space_(false), 28 filter_needs_more_output_space_(false),
28 read_buffer_(NULL), 29 read_buffer_(NULL),
29 read_buffer_len_(0), 30 read_buffer_len_(0),
30 has_handled_response_(false), 31 has_handled_response_(false),
31 expected_content_size_(-1), 32 expected_content_size_(-1),
32 filter_input_byte_count_(0) , 33 filter_input_byte_count_(0) {
33 deferred_redirect_status_code_(-1) {
34 load_flags_ = request_->load_flags(); 34 load_flags_ = request_->load_flags();
35 is_profiling_ = request->enable_profiling(); 35 is_profiling_ = request->enable_profiling();
36 if (is_profiling()) { 36 if (is_profiling()) {
37 metrics_.reset(new URLRequestJobMetrics()); 37 metrics_.reset(new URLRequestJobMetrics());
38 metrics_->start_time_ = TimeTicks::Now(); 38 metrics_->start_time_ = TimeTicks::Now();
39 } 39 }
40 g_url_request_job_tracker.AddNewJob(this); 40 g_url_request_job_tracker.AddNewJob(this);
41 } 41 }
42 42
43 URLRequestJob::~URLRequestJob() { 43 URLRequestJob::~URLRequestJob() {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 NOTREACHED(); 86 NOTREACHED();
87 } 87 }
88 88
89 void URLRequestJob::ContinueDespiteLastError() { 89 void URLRequestJob::ContinueDespiteLastError() {
90 // Implementations should know how to recover from errors they generate. 90 // Implementations should know how to recover from errors they generate.
91 // If this code was reached, we are trying to recover from an error that 91 // If this code was reached, we are trying to recover from an error that
92 // we don't know how to recover from. 92 // we don't know how to recover from.
93 NOTREACHED(); 93 NOTREACHED();
94 } 94 }
95 95
96 void URLRequestJob::FollowDeferredRedirect() {
97 DCHECK(deferred_redirect_status_code_ != -1);
98 // NOTE: deferred_redirect_url_ may be invalid, and attempting to redirect to
99 // such an URL will fail inside FollowRedirect. The DCHECK above asserts
100 // that we called OnReceivedRedirect.
101
102 FollowRedirect(deferred_redirect_url_, deferred_redirect_status_code_);
103 deferred_redirect_url_ = GURL();
104 deferred_redirect_status_code_ = -1;
105 }
106
107 int64 URLRequestJob::GetByteReadCount() const { 96 int64 URLRequestJob::GetByteReadCount() const {
108 return filter_input_byte_count_ ; 97 return filter_input_byte_count_ ;
109 } 98 }
110 99
111 bool URLRequestJob::GetURL(GURL* gurl) const { 100 bool URLRequestJob::GetURL(GURL* gurl) const {
112 if (!request_) 101 if (!request_)
113 return false; 102 return false;
114 *gurl = request_->url(); 103 *gurl = request_->url();
115 return true; 104 return true;
116 } 105 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 if (!filter_->stream_data_len() && !is_done()) { 164 if (!filter_->stream_data_len() && !is_done()) {
176 net::IOBuffer* stream_buffer = filter_->stream_buffer(); 165 net::IOBuffer* stream_buffer = filter_->stream_buffer();
177 int stream_buffer_size = filter_->stream_buffer_size(); 166 int stream_buffer_size = filter_->stream_buffer_size();
178 rv = ReadRawData(stream_buffer, stream_buffer_size, bytes_read); 167 rv = ReadRawData(stream_buffer, stream_buffer_size, bytes_read);
179 if (rv && *bytes_read > 0) 168 if (rv && *bytes_read > 0)
180 RecordBytesRead(*bytes_read); 169 RecordBytesRead(*bytes_read);
181 } 170 }
182 return rv; 171 return rv;
183 } 172 }
184 173
185 void URLRequestJob::FollowRedirect(const GURL& location, int http_status_code) {
186 g_url_request_job_tracker.OnJobRedirect(this, location, http_status_code);
187
188 int rv = request_->Redirect(location, http_status_code);
189 if (rv != net::OK)
190 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
191 }
192
193 void URLRequestJob::FilteredDataRead(int bytes_read) { 174 void URLRequestJob::FilteredDataRead(int bytes_read) {
194 DCHECK(filter_.get()); // don't add data if there is no filter 175 DCHECK(filter_.get()); // don't add data if there is no filter
195 filter_->FlushStreamBuffer(bytes_read); 176 filter_->FlushStreamBuffer(bytes_read);
196 } 177 }
197 178
198 bool URLRequestJob::ReadFilteredData(int *bytes_read) { 179 bool URLRequestJob::ReadFilteredData(int *bytes_read) {
199 DCHECK(filter_.get()); // don't add data if there is no filter 180 DCHECK(filter_.get()); // don't add data if there is no filter
200 DCHECK(read_buffer_ != NULL); // we need to have a buffer to fill 181 DCHECK(read_buffer_ != NULL); // we need to have a buffer to fill
201 DCHECK(read_buffer_len_ > 0); // sanity check 182 DCHECK(read_buffer_len_ > 0); // sanity check
202 DCHECK(read_buffer_len_ < 1000000); // sanity check 183 DCHECK(read_buffer_len_ < 1000000); // sanity check
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 request_->response_info_.response_time = Time::Now(); 311 request_->response_info_.response_time = Time::Now();
331 GetResponseInfo(&request_->response_info_); 312 GetResponseInfo(&request_->response_info_);
332 313
333 // When notifying the delegate, the delegate can release the request 314 // When notifying the delegate, the delegate can release the request
334 // (and thus release 'this'). After calling to the delgate, we must 315 // (and thus release 'this'). After calling to the delgate, we must
335 // check the request pointer to see if it still exists, and return 316 // check the request pointer to see if it still exists, and return
336 // immediately if it has been destroyed. self_preservation ensures our 317 // immediately if it has been destroyed. self_preservation ensures our
337 // survival until we can get out of this method. 318 // survival until we can get out of this method.
338 scoped_refptr<URLRequestJob> self_preservation = this; 319 scoped_refptr<URLRequestJob> self_preservation = this;
339 320
321 int http_status_code;
340 GURL new_location; 322 GURL new_location;
341 int http_status_code;
342 if (IsRedirectResponse(&new_location, &http_status_code)) { 323 if (IsRedirectResponse(&new_location, &http_status_code)) {
343 const GURL& url = request_->url(); 324 const GURL& url = request_->url();
344 325
345 // Move the reference fragment of the old location to the new one if the 326 // Move the reference fragment of the old location to the new one if the
346 // new one has none. This duplicates mozilla's behavior. 327 // new one has none. This duplicates mozilla's behavior.
347 if (url.is_valid() && url.has_ref() && !new_location.has_ref()) { 328 if (url.is_valid() && url.has_ref() && !new_location.has_ref()) {
348 GURL::Replacements replacements; 329 GURL::Replacements replacements;
349 // Reference the |ref| directly out of the original URL to avoid a 330 // Reference the |ref| directly out of the original URL to avoid a
350 // malloc. 331 // malloc.
351 replacements.SetRef(url.spec().data(), 332 replacements.SetRef(url.spec().data(),
352 url.parsed_for_possibly_invalid_spec().ref); 333 url.parsed_for_possibly_invalid_spec().ref);
353 new_location = new_location.ReplaceComponents(replacements); 334 new_location = new_location.ReplaceComponents(replacements);
354 } 335 }
355 336
356 bool defer_redirect = false; 337 // Toggle this flag to true so the consumer can access response headers.
357 request_->ReceivedRedirect(new_location, &defer_redirect); 338 // Then toggle it back if we choose to follow the redirect.
339 has_handled_response_ = true;
340 request_->delegate()->OnReceivedRedirect(request_, new_location);
358 341
359 // Ensure that the request wasn't destroyed in OnReceivedRedirect 342 // Ensure that the request wasn't destroyed in OnReceivedRedirect
360 if (!request_ || !request_->delegate()) 343 if (!request_ || !request_->delegate())
361 return; 344 return;
362 345
363 // If we were not cancelled, then maybe follow the redirect. 346 // If we were not cancelled, then follow the redirect.
364 if (request_->status().is_success()) { 347 if (request_->status().is_success()) {
365 if (defer_redirect) { 348 has_handled_response_ = false;
366 deferred_redirect_url_ = new_location; 349 FollowRedirect(new_location, http_status_code);
367 deferred_redirect_status_code_ = http_status_code;
368 } else {
369 FollowRedirect(new_location, http_status_code);
370 }
371 return; 350 return;
372 } 351 }
373 } else if (NeedsAuth()) { 352 } else if (NeedsAuth()) {
374 scoped_refptr<net::AuthChallengeInfo> auth_info; 353 scoped_refptr<net::AuthChallengeInfo> auth_info;
375 GetAuthChallengeInfo(&auth_info); 354 GetAuthChallengeInfo(&auth_info);
376 // Need to check for a NULL auth_info because the server may have failed 355 // Need to check for a NULL auth_info because the server may have failed
377 // to send a challenge with the 401 response. 356 // to send a challenge with the 401 response.
378 if (auth_info) { 357 if (auth_info) {
379 request_->delegate()->OnAuthRequired(request_, auth_info); 358 request_->delegate()->OnAuthRequired(request_, auth_info);
380 // Wait for SetAuth or CancelAuth to be called. 359 // Wait for SetAuth or CancelAuth to be called.
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 if (!done_) { 491 if (!done_) {
513 NotifyDone(URLRequestStatus(URLRequestStatus::CANCELED, 492 NotifyDone(URLRequestStatus(URLRequestStatus::CANCELED,
514 net::ERR_ABORTED)); 493 net::ERR_ABORTED));
515 } 494 }
516 } 495 }
517 496
518 bool URLRequestJob::FilterHasData() { 497 bool URLRequestJob::FilterHasData() {
519 return filter_.get() && filter_->stream_data_len(); 498 return filter_.get() && filter_->stream_data_len();
520 } 499 }
521 500
501 void URLRequestJob::FollowRedirect(const GURL& location,
502 int http_status_code) {
503 g_url_request_job_tracker.OnJobRedirect(this, location, http_status_code);
504 Kill();
505 // Kill could have notified the Delegate and destroyed the request.
506 if (!request_)
507 return;
508
509 int rv = request_->Redirect(location, http_status_code);
510 if (rv != net::OK)
511 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, rv));
512 }
513
522 void URLRequestJob::RecordBytesRead(int bytes_read) { 514 void URLRequestJob::RecordBytesRead(int bytes_read) {
523 if (is_profiling()) { 515 if (is_profiling()) {
524 ++(metrics_->number_of_read_IO_); 516 ++(metrics_->number_of_read_IO_);
525 metrics_->total_bytes_read_ += bytes_read; 517 metrics_->total_bytes_read_ += bytes_read;
526 } 518 }
527 filter_input_byte_count_ += bytes_read; 519 filter_input_byte_count_ += bytes_read;
528 g_url_request_job_tracker.OnBytesRead(this, bytes_read); 520 g_url_request_job_tracker.OnBytesRead(this, bytes_read);
529 } 521 }
530 522
531 const URLRequestStatus URLRequestJob::GetStatus() { 523 const URLRequestStatus URLRequestJob::GetStatus() {
532 if (request_) 524 if (request_)
533 return request_->status(); 525 return request_->status();
534 // If the request is gone, we must be cancelled. 526 // If the request is gone, we must be cancelled.
535 return URLRequestStatus(URLRequestStatus::CANCELED, 527 return URLRequestStatus(URLRequestStatus::CANCELED,
536 net::ERR_ABORTED); 528 net::ERR_ABORTED);
537 } 529 }
538 530
539 void URLRequestJob::SetStatus(const URLRequestStatus &status) { 531 void URLRequestJob::SetStatus(const URLRequestStatus &status) {
540 if (request_) 532 if (request_)
541 request_->set_status(status); 533 request_->set_status(status);
542 } 534 }
OLDNEW
« no previous file with comments | « net/url_request/url_request_job.h ('k') | net/url_request/url_request_unittest.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698