OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/singleton.h" | 9 #include "base/singleton.h" |
10 #include "base/stats_counters.h" | 10 #include "base/stats_counters.h" |
11 #include "net/base/load_flags.h" | 11 #include "net/base/load_flags.h" |
12 #include "net/base/load_log.h" | |
13 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/net_log.h" |
14 #include "net/base/ssl_cert_request_info.h" | 14 #include "net/base/ssl_cert_request_info.h" |
15 #include "net/base/upload_data.h" | 15 #include "net/base/upload_data.h" |
16 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
17 #include "net/http/http_util.h" | 17 #include "net/http/http_util.h" |
18 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
19 #include "net/url_request/url_request_job.h" | 19 #include "net/url_request/url_request_job.h" |
20 #include "net/url_request/url_request_job_manager.h" | 20 #include "net/url_request/url_request_job_manager.h" |
21 | 21 |
22 using base::Time; | 22 using base::Time; |
23 using net::UploadData; | 23 using net::UploadData; |
(...skipping 13 matching lines...) Expand all Loading... |
37 URLRequest::URLRequest(const GURL& url, Delegate* delegate) | 37 URLRequest::URLRequest(const GURL& url, Delegate* delegate) |
38 : url_(url), | 38 : url_(url), |
39 original_url_(url), | 39 original_url_(url), |
40 method_("GET"), | 40 method_("GET"), |
41 load_flags_(net::LOAD_NORMAL), | 41 load_flags_(net::LOAD_NORMAL), |
42 delegate_(delegate), | 42 delegate_(delegate), |
43 is_pending_(false), | 43 is_pending_(false), |
44 enable_profiling_(false), | 44 enable_profiling_(false), |
45 redirect_limit_(kMaxRedirects), | 45 redirect_limit_(kMaxRedirects), |
46 final_upload_progress_(0), | 46 final_upload_progress_(0), |
47 priority_(net::LOWEST), | 47 priority_(net::LOWEST) { |
48 ALLOW_THIS_IN_INITIALIZER_LIST(request_tracker_node_(this)) { | |
49 SIMPLE_STATS_COUNTER("URLRequestCount"); | 48 SIMPLE_STATS_COUNTER("URLRequestCount"); |
50 | 49 |
51 // Sanity check out environment. | 50 // Sanity check out environment. |
52 DCHECK(MessageLoop::current()) << | 51 DCHECK(MessageLoop::current()) << |
53 "The current MessageLoop must exist"; | 52 "The current MessageLoop must exist"; |
54 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << | 53 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << |
55 "The current MessageLoop must be TYPE_IO"; | 54 "The current MessageLoop must be TYPE_IO"; |
56 } | 55 } |
57 | 56 |
58 URLRequest::~URLRequest() { | 57 URLRequest::~URLRequest() { |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 void URLRequest::Start() { | 248 void URLRequest::Start() { |
250 StartJob(GetJobManager()->CreateJob(this)); | 249 StartJob(GetJobManager()->CreateJob(this)); |
251 } | 250 } |
252 | 251 |
253 /////////////////////////////////////////////////////////////////////////////// | 252 /////////////////////////////////////////////////////////////////////////////// |
254 | 253 |
255 void URLRequest::StartJob(URLRequestJob* job) { | 254 void URLRequest::StartJob(URLRequestJob* job) { |
256 DCHECK(!is_pending_); | 255 DCHECK(!is_pending_); |
257 DCHECK(!job_); | 256 DCHECK(!job_); |
258 | 257 |
259 net::LoadLog::BeginEvent(load_log_, net::LoadLog::TYPE_URL_REQUEST_START); | 258 net_log_.BeginEvent(net::NetLog::TYPE_URL_REQUEST_START); |
260 | 259 |
261 job_ = job; | 260 job_ = job; |
262 job_->SetExtraRequestHeaders(extra_request_headers_); | 261 job_->SetExtraRequestHeaders(extra_request_headers_); |
263 | 262 |
264 if (upload_.get()) | 263 if (upload_.get()) |
265 job_->SetUpload(upload_.get()); | 264 job_->SetUpload(upload_.get()); |
266 | 265 |
267 is_pending_ = true; | 266 is_pending_ = true; |
268 | 267 |
269 response_info_.request_time = Time::Now(); | 268 response_info_.request_time = Time::Now(); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 URLRequestJob* job = GetJobManager()->MaybeInterceptRedirect(this, location); | 355 URLRequestJob* job = GetJobManager()->MaybeInterceptRedirect(this, location); |
357 if (job) { | 356 if (job) { |
358 RestartWithJob(job); | 357 RestartWithJob(job); |
359 } else if (delegate_) { | 358 } else if (delegate_) { |
360 delegate_->OnReceivedRedirect(this, location, defer_redirect); | 359 delegate_->OnReceivedRedirect(this, location, defer_redirect); |
361 } | 360 } |
362 } | 361 } |
363 | 362 |
364 void URLRequest::ResponseStarted() { | 363 void URLRequest::ResponseStarted() { |
365 if (!status_.is_success()) | 364 if (!status_.is_success()) |
366 net::LoadLog::AddErrorCode(load_log_, status_.os_error()); | 365 net_log_.AddErrorCode(status_.os_error()); |
367 | 366 |
368 net::LoadLog::EndEvent(load_log_, net::LoadLog::TYPE_URL_REQUEST_START); | 367 net_log_.EndEvent(net::NetLog::TYPE_URL_REQUEST_START); |
369 | 368 |
370 URLRequestJob* job = GetJobManager()->MaybeInterceptResponse(this); | 369 URLRequestJob* job = GetJobManager()->MaybeInterceptResponse(this); |
371 if (job) { | 370 if (job) { |
372 RestartWithJob(job); | 371 RestartWithJob(job); |
373 } else if (delegate_) { | 372 } else if (delegate_) { |
374 delegate_->OnResponseStarted(this); | 373 delegate_->OnResponseStarted(this); |
375 } | 374 } |
376 } | 375 } |
377 | 376 |
378 void URLRequest::FollowDeferredRedirect() { | 377 void URLRequest::FollowDeferredRedirect() { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 static const char* const kPostHeaders[] = { | 430 static const char* const kPostHeaders[] = { |
432 "content-type", | 431 "content-type", |
433 "content-length", | 432 "content-length", |
434 "origin" | 433 "origin" |
435 }; | 434 }; |
436 return net::HttpUtil::StripHeaders( | 435 return net::HttpUtil::StripHeaders( |
437 headers, kPostHeaders, arraysize(kPostHeaders)); | 436 headers, kPostHeaders, arraysize(kPostHeaders)); |
438 } | 437 } |
439 | 438 |
440 int URLRequest::Redirect(const GURL& location, int http_status_code) { | 439 int URLRequest::Redirect(const GURL& location, int http_status_code) { |
441 if (net::LoadLog::IsUnbounded(load_log_)) { | 440 if (net_log_.HasListener()) { |
442 net::LoadLog::AddString(load_log_, StringPrintf("Redirected (%d) to %s", | 441 net_log_.AddString(StringPrintf("Redirected (%d) to %s", |
443 http_status_code, location.spec().c_str())); | 442 http_status_code, location.spec().c_str())); |
444 } | 443 } |
445 if (redirect_limit_ <= 0) { | 444 if (redirect_limit_ <= 0) { |
446 DLOG(INFO) << "disallowing redirect: exceeds limit"; | 445 DLOG(INFO) << "disallowing redirect: exceeds limit"; |
447 return net::ERR_TOO_MANY_REDIRECTS; | 446 return net::ERR_TOO_MANY_REDIRECTS; |
448 } | 447 } |
449 | 448 |
450 if (!location.is_valid()) | 449 if (!location.is_valid()) |
451 return net::ERR_INVALID_URL; | 450 return net::ERR_INVALID_URL; |
452 | 451 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 | 496 |
498 URLRequestContext* URLRequest::context() { | 497 URLRequestContext* URLRequest::context() { |
499 return context_.get(); | 498 return context_.get(); |
500 } | 499 } |
501 | 500 |
502 void URLRequest::set_context(URLRequestContext* context) { | 501 void URLRequest::set_context(URLRequestContext* context) { |
503 scoped_refptr<URLRequestContext> prev_context = context_; | 502 scoped_refptr<URLRequestContext> prev_context = context_; |
504 | 503 |
505 context_ = context; | 504 context_ = context; |
506 | 505 |
507 // If the context this request belongs to has changed, update the tracker(s). | 506 // If the context this request belongs to has changed, update the tracker. |
508 if (prev_context != context) { | 507 if (prev_context != context) { |
509 if (prev_context) | 508 net_log_.EndEvent(net::NetLog::TYPE_REQUEST_ALIVE); |
510 prev_context->url_request_tracker()->Remove(this); | 509 net_log_ = net::BoundNetLog(); |
| 510 |
511 if (context) { | 511 if (context) { |
512 if (!load_log_) { | 512 net_log_ = net::BoundNetLog::Make(context->net_log(), |
513 // Create the LoadLog -- we waited until now to create it so we know | 513 net::NetLog::SOURCE_URL_REQUEST); |
514 // what constraints the URLRequestContext is enforcing on log levels. | |
515 load_log_ = context->url_request_tracker()->CreateLoadLog(); | |
516 } | |
517 | 514 |
518 context->url_request_tracker()->Add(this); | 515 net_log_.BeginEventWithString(net::NetLog::TYPE_REQUEST_ALIVE, |
| 516 original_url_.possibly_invalid_spec()); |
519 } | 517 } |
520 } | 518 } |
521 } | 519 } |
522 | 520 |
523 int64 URLRequest::GetExpectedContentSize() const { | 521 int64 URLRequest::GetExpectedContentSize() const { |
524 int64 expected_content_size = -1; | 522 int64 expected_content_size = -1; |
525 if (job_) | 523 if (job_) |
526 expected_content_size = job_->expected_content_size(); | 524 expected_content_size = job_->expected_content_size(); |
527 | 525 |
528 return expected_content_size; | 526 return expected_content_size; |
529 } | 527 } |
530 | 528 |
531 URLRequest::UserData* URLRequest::GetUserData(const void* key) const { | 529 URLRequest::UserData* URLRequest::GetUserData(const void* key) const { |
532 UserDataMap::const_iterator found = user_data_.find(key); | 530 UserDataMap::const_iterator found = user_data_.find(key); |
533 if (found != user_data_.end()) | 531 if (found != user_data_.end()) |
534 return found->second.get(); | 532 return found->second.get(); |
535 return NULL; | 533 return NULL; |
536 } | 534 } |
537 | 535 |
538 void URLRequest::SetUserData(const void* key, UserData* data) { | 536 void URLRequest::SetUserData(const void* key, UserData* data) { |
539 user_data_[key] = linked_ptr<UserData>(data); | 537 user_data_[key] = linked_ptr<UserData>(data); |
540 } | 538 } |
541 | |
542 void URLRequest::GetInfoForTracker( | |
543 RequestTracker<URLRequest>::RecentRequestInfo* info) const { | |
544 DCHECK(info); | |
545 info->original_url = original_url_; | |
546 info->load_log = load_log_; | |
547 } | |
OLD | NEW |