Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.h" | 5 #include "net/url_request/url_request.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/debug/stack_trace.h" | 11 #include "base/debug/stack_trace.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/metrics/stats_counters.h" | 15 #include "base/metrics/stats_counters.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "net/base/auth.h" | 18 #include "net/base/auth.h" |
| 19 #include "net/base/host_port_pair.h" | 19 #include "net/base/host_port_pair.h" |
| 20 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
| 21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 22 #include "net/base/net_log.h" | 22 #include "net/base/net_log.h" |
| 23 #include "net/base/network_change_notifier.h" | 23 #include "net/base/network_change_notifier.h" |
| 24 #include "net/base/network_delegate.h" | 24 #include "net/base/network_delegate.h" |
| 25 #include "net/base/ssl_cert_request_info.h" | 25 #include "net/base/ssl_cert_request_info.h" |
| 26 #include "net/base/upload_data.h" | 26 #include "net/base/upload_data.h" |
| 27 #include "net/base/upload_data_stream.h" | |
| 27 #include "net/http/http_response_headers.h" | 28 #include "net/http/http_response_headers.h" |
| 28 #include "net/http/http_util.h" | 29 #include "net/http/http_util.h" |
| 29 #include "net/url_request/url_request_context.h" | 30 #include "net/url_request/url_request_context.h" |
| 30 #include "net/url_request/url_request_error_job.h" | 31 #include "net/url_request/url_request_error_job.h" |
| 31 #include "net/url_request/url_request_job.h" | 32 #include "net/url_request/url_request_job.h" |
| 32 #include "net/url_request/url_request_job_manager.h" | 33 #include "net/url_request/url_request_job_manager.h" |
| 33 #include "net/url_request/url_request_netlog_params.h" | 34 #include "net/url_request/url_request_netlog_params.h" |
| 34 #include "net/url_request/url_request_redirect_job.h" | 35 #include "net/url_request/url_request_redirect_job.h" |
| 35 | 36 |
| 36 using base::Time; | 37 using base::Time; |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 void URLRequest::RegisterRequestInterceptor(Interceptor* interceptor) { | 247 void URLRequest::RegisterRequestInterceptor(Interceptor* interceptor) { |
| 247 URLRequestJobManager::GetInstance()->RegisterRequestInterceptor(interceptor); | 248 URLRequestJobManager::GetInstance()->RegisterRequestInterceptor(interceptor); |
| 248 } | 249 } |
| 249 | 250 |
| 250 // static | 251 // static |
| 251 void URLRequest::UnregisterRequestInterceptor(Interceptor* interceptor) { | 252 void URLRequest::UnregisterRequestInterceptor(Interceptor* interceptor) { |
| 252 URLRequestJobManager::GetInstance()->UnregisterRequestInterceptor( | 253 URLRequestJobManager::GetInstance()->UnregisterRequestInterceptor( |
| 253 interceptor); | 254 interceptor); |
| 254 } | 255 } |
| 255 | 256 |
| 256 void URLRequest::AppendBytesToUpload(const char* bytes, int bytes_len) { | |
| 257 DCHECK(bytes_len > 0 && bytes); | |
| 258 if (!upload_) | |
| 259 upload_ = new UploadData(); | |
| 260 upload_->AppendBytes(bytes, bytes_len); | |
| 261 } | |
| 262 | |
| 263 void URLRequest::EnableChunkedUpload() { | 257 void URLRequest::EnableChunkedUpload() { |
| 264 DCHECK(!upload_ || upload_->is_chunked()); | 258 DCHECK(!upload_ || upload_->is_chunked()); |
| 265 if (!upload_) { | 259 if (!upload_) { |
| 266 upload_ = new UploadData(); | 260 upload_ = new UploadData(); |
| 267 upload_->set_is_chunked(true); | 261 upload_->set_is_chunked(true); |
| 262 upload_data_stream_.reset(new UploadDataStream(upload_)); | |
| 268 } | 263 } |
| 269 } | 264 } |
| 270 | 265 |
| 271 void URLRequest::AppendChunkToUpload(const char* bytes, | 266 void URLRequest::AppendChunkToUpload(const char* bytes, |
| 272 int bytes_len, | 267 int bytes_len, |
| 273 bool is_last_chunk) { | 268 bool is_last_chunk) { |
| 274 DCHECK(upload_); | 269 DCHECK(upload_); |
| 275 DCHECK(upload_->is_chunked()); | 270 DCHECK(upload_->is_chunked()); |
| 276 DCHECK_GT(bytes_len, 0); | 271 DCHECK_GT(bytes_len, 0); |
| 277 upload_->AppendChunk(bytes, bytes_len, is_last_chunk); | 272 upload_->AppendChunk(bytes, bytes_len, is_last_chunk); |
| 278 } | 273 } |
| 279 | 274 |
| 280 void URLRequest::set_upload(UploadData* upload) { | 275 void URLRequest::set_upload(UploadData* upload) { |
| 281 upload_ = upload; | 276 upload_ = upload; |
| 277 upload_data_stream_.reset(new UploadDataStream(upload_)); | |
| 282 } | 278 } |
| 283 | 279 |
| 284 // Get the upload data directly. | 280 const UploadDataStream* URLRequest::get_upload() const { |
| 285 const UploadData* URLRequest::get_upload() const { | 281 return upload_data_stream_.get(); |
| 286 return upload_.get(); | |
| 287 } | |
| 288 | |
| 289 UploadData* URLRequest::get_upload_mutable() { | |
| 290 return upload_.get(); | |
| 291 } | 282 } |
| 292 | 283 |
| 293 bool URLRequest::has_upload() const { | 284 bool URLRequest::has_upload() const { |
| 294 return upload_ != NULL; | 285 return upload_data_stream_.get() != NULL; |
| 295 } | 286 } |
| 296 | 287 |
| 297 void URLRequest::SetExtraRequestHeaderById(int id, const string& value, | 288 void URLRequest::SetExtraRequestHeaderById(int id, const string& value, |
| 298 bool overwrite) { | 289 bool overwrite) { |
| 299 DCHECK(!is_pending_ || is_redirecting_); | 290 DCHECK(!is_pending_ || is_redirecting_); |
| 300 NOTREACHED() << "implement me!"; | 291 NOTREACHED() << "implement me!"; |
| 301 } | 292 } |
| 302 | 293 |
| 303 void URLRequest::SetExtraRequestHeaderByName(const string& name, | 294 void URLRequest::SetExtraRequestHeaderByName(const string& name, |
| 304 const string& value, | 295 const string& value, |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 523 | 514 |
| 524 net_log_.BeginEvent( | 515 net_log_.BeginEvent( |
| 525 NetLog::TYPE_URL_REQUEST_START_JOB, | 516 NetLog::TYPE_URL_REQUEST_START_JOB, |
| 526 base::Bind(&NetLogURLRequestStartCallback, | 517 base::Bind(&NetLogURLRequestStartCallback, |
| 527 &url(), &method_, load_flags_, priority_, | 518 &url(), &method_, load_flags_, priority_, |
| 528 upload_.get() ? upload_->identifier() : -1)); | 519 upload_.get() ? upload_->identifier() : -1)); |
| 529 | 520 |
| 530 job_ = job; | 521 job_ = job; |
| 531 job_->SetExtraRequestHeaders(extra_request_headers_); | 522 job_->SetExtraRequestHeaders(extra_request_headers_); |
| 532 | 523 |
| 533 if (upload_.get()) | 524 if (upload_data_stream_.get()) |
| 534 job_->SetUpload(upload_.get()); | 525 job_->SetUpload(upload_data_stream_.get()); |
| 535 | 526 |
| 536 is_pending_ = true; | 527 is_pending_ = true; |
| 537 is_redirecting_ = false; | 528 is_redirecting_ = false; |
| 538 | 529 |
| 539 response_info_.was_cached = false; | 530 response_info_.was_cached = false; |
| 540 | 531 |
| 541 // Don't allow errors to be sent from within Start(). | 532 // Don't allow errors to be sent from within Start(). |
| 542 // TODO(brettw) this may cause NotifyDone to be sent synchronously, | 533 // TODO(brettw) this may cause NotifyDone to be sent synchronously, |
| 543 // we probably don't want this: they should be sent asynchronously so | 534 // we probably don't want this: they should be sent asynchronously so |
| 544 // the caller does not get reentered. | 535 // the caller does not get reentered. |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 775 | 766 |
| 776 // For 303 redirects, all request methods except HEAD are converted to GET, | 767 // For 303 redirects, all request methods except HEAD are converted to GET, |
| 777 // as per the latest httpbis draft. The draft also allows POST requests to | 768 // as per the latest httpbis draft. The draft also allows POST requests to |
| 778 // be converted to GETs when following 301/302 redirects, for historical | 769 // be converted to GETs when following 301/302 redirects, for historical |
| 779 // reasons. Most major browsers do this and so shall we. Both RFC 2616 and | 770 // reasons. Most major browsers do this and so shall we. Both RFC 2616 and |
| 780 // the httpbis draft say to prompt the user to confirm the generation of new | 771 // the httpbis draft say to prompt the user to confirm the generation of new |
| 781 // requests, other than GET and HEAD requests, but IE omits these prompts and | 772 // requests, other than GET and HEAD requests, but IE omits these prompts and |
| 782 // so shall we. | 773 // so shall we. |
| 783 // See: https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#sectio n-7.3 | 774 // See: https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#sectio n-7.3 |
| 784 bool was_post = method_ == "POST"; | 775 bool was_post = method_ == "POST"; |
| 776 bool discard_upload = false; | |
| 785 if ((http_status_code == 303 && method_ != "HEAD") || | 777 if ((http_status_code == 303 && method_ != "HEAD") || |
| 786 ((http_status_code == 301 || http_status_code == 302) && was_post)) { | 778 ((http_status_code == 301 || http_status_code == 302) && was_post)) { |
| 787 method_ = "GET"; | 779 method_ = "GET"; |
| 788 upload_ = NULL; | 780 discard_upload = true; |
| 789 if (was_post) { | 781 if (was_post) { |
| 790 // If being switched from POST to GET, must remove headers that were | 782 // If being switched from POST to GET, must remove headers that were |
| 791 // specific to the POST and don't have meaning in GET. For example | 783 // specific to the POST and don't have meaning in GET. For example |
| 792 // the inclusion of a multipart Content-Type header in GET can cause | 784 // the inclusion of a multipart Content-Type header in GET can cause |
| 793 // problems with some servers: | 785 // problems with some servers: |
| 794 // http://code.google.com/p/chromium/issues/detail?id=843 | 786 // http://code.google.com/p/chromium/issues/detail?id=843 |
| 795 StripPostSpecificHeaders(&extra_request_headers_); | 787 StripPostSpecificHeaders(&extra_request_headers_); |
| 796 } | 788 } |
| 797 } | 789 } |
| 798 | 790 |
| 799 // Suppress the referrer if we're redirecting out of https. | 791 // Suppress the referrer if we're redirecting out of https. |
| 800 if (referrer_policy_ == | 792 if (referrer_policy_ == |
| 801 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE && | 793 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE && |
| 802 GURL(referrer_).SchemeIsSecure() && !location.SchemeIsSecure()) { | 794 GURL(referrer_).SchemeIsSecure() && !location.SchemeIsSecure()) { |
| 803 referrer_.clear(); | 795 referrer_.clear(); |
| 804 } | 796 } |
| 805 | 797 |
| 806 url_chain_.push_back(location); | 798 url_chain_.push_back(location); |
| 807 --redirect_limit_; | 799 --redirect_limit_; |
| 808 | 800 |
| 809 if (!final_upload_progress_.position()) | 801 if (!final_upload_progress_.position()) |
| 810 final_upload_progress_ = job_->GetUploadProgress(); | 802 final_upload_progress_ = job_->GetUploadProgress(); |
|
mmenke
2012/11/20 19:57:03
Can't we just move this up to before the block of
hashimoto
2012/11/21 03:41:02
Moved GetUploadProgress() and PrepareToRestart() t
| |
| 811 | 803 |
| 812 PrepareToRestart(); | 804 PrepareToRestart(); |
| 805 if (discard_upload) | |
| 806 upload_data_stream_.reset(); | |
| 813 Start(); | 807 Start(); |
| 814 return OK; | 808 return OK; |
| 815 } | 809 } |
| 816 | 810 |
| 817 const URLRequestContext* URLRequest::context() const { | 811 const URLRequestContext* URLRequest::context() const { |
| 818 return context_; | 812 return context_; |
| 819 } | 813 } |
| 820 | 814 |
| 821 int64 URLRequest::GetExpectedContentSize() const { | 815 int64 URLRequest::GetExpectedContentSize() const { |
| 822 int64 expected_content_size = -1; | 816 int64 expected_content_size = -1; |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 987 new base::debug::StackTrace(NULL, 0); | 981 new base::debug::StackTrace(NULL, 0); |
| 988 *stack_trace_copy = stack_trace; | 982 *stack_trace_copy = stack_trace; |
| 989 stack_trace_.reset(stack_trace_copy); | 983 stack_trace_.reset(stack_trace_copy); |
| 990 } | 984 } |
| 991 | 985 |
| 992 const base::debug::StackTrace* URLRequest::stack_trace() const { | 986 const base::debug::StackTrace* URLRequest::stack_trace() const { |
| 993 return stack_trace_.get(); | 987 return stack_trace_.get(); |
| 994 } | 988 } |
| 995 | 989 |
| 996 } // namespace net | 990 } // namespace net |
| OLD | NEW |