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
3 // found in the LICENSE file.
5 #include "net/url_request/url_request_job.h"
8 #include "base/compiler_specific.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/power_monitor/power_monitor.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "net/base/auth.h"
14 #include "net/base/host_port_pair.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/load_states.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/network_delegate.h"
19 #include "net/filter/filter.h"
20 #include "net/http/http_response_headers.h"
21 #include "net/url_request/url_request.h"
25 URLRequestJob::URLRequestJob(URLRequest
* request
,
26 NetworkDelegate
* network_delegate
)
29 prefilter_bytes_read_(0),
30 postfilter_bytes_read_(0),
31 filter_input_byte_count_(0),
32 filter_needs_more_output_space_(false),
33 filtered_read_buffer_len_(0),
34 has_handled_response_(false),
35 expected_content_size_(-1),
36 network_delegate_(network_delegate
),
38 base::PowerMonitor
* power_monitor
= base::PowerMonitor::Get();
40 power_monitor
->AddObserver(this);
43 void URLRequestJob::SetUpload(UploadDataStream
* upload
) {
46 void URLRequestJob::SetExtraRequestHeaders(const HttpRequestHeaders
& headers
) {
49 void URLRequestJob::SetPriority(RequestPriority priority
) {
52 void URLRequestJob::Kill() {
53 weak_factory_
.InvalidateWeakPtrs();
54 // Make sure the request is notified that we are done. We assume that the
55 // request took care of setting its error status before calling Kill.
60 void URLRequestJob::DetachRequest() {
64 // This function calls ReadData to get stream data. If a filter exists, passes
65 // the data to the attached filter. Then returns the output from filter back to
67 bool URLRequestJob::Read(IOBuffer
* buf
, int buf_size
, int *bytes_read
) {
70 DCHECK_LT(buf_size
, 1000000); // Sanity check.
73 DCHECK(filtered_read_buffer_
.get() == NULL
);
74 DCHECK_EQ(0, filtered_read_buffer_len_
);
78 // Skip Filter if not present.
80 rv
= ReadRawDataHelper(buf
, buf_size
, bytes_read
);
82 // Save the caller's buffers while we do IO
83 // in the filter's buffers.
84 filtered_read_buffer_
= buf
;
85 filtered_read_buffer_len_
= buf_size
;
87 if (ReadFilteredData(bytes_read
)) {
88 rv
= true; // We have data to return.
90 // It is fine to call DoneReading even if ReadFilteredData receives 0
91 // bytes from the net, but we avoid making that call if we know for
92 // sure that's the case (ReadRawDataHelper path).
96 rv
= false; // Error, or a new IO is pending.
99 if (rv
&& *bytes_read
== 0)
100 NotifyDone(URLRequestStatus());
104 void URLRequestJob::StopCaching() {
105 // Nothing to do here.
108 bool URLRequestJob::GetFullRequestHeaders(HttpRequestHeaders
* headers
) const {
109 // Most job types don't send request headers.
113 int64
URLRequestJob::GetTotalReceivedBytes() const {
117 LoadState
URLRequestJob::GetLoadState() const {
118 return LOAD_STATE_IDLE
;
121 UploadProgress
URLRequestJob::GetUploadProgress() const {
122 return UploadProgress();
125 bool URLRequestJob::GetCharset(std::string
* charset
) {
129 void URLRequestJob::GetResponseInfo(HttpResponseInfo
* info
) {
132 void URLRequestJob::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
133 // Only certain request types return more than just request start times.
136 bool URLRequestJob::GetResponseCookies(std::vector
<std::string
>* cookies
) {
140 Filter
* URLRequestJob::SetupFilter() const {
144 bool URLRequestJob::IsRedirectResponse(GURL
* location
,
145 int* http_status_code
) {
146 // For non-HTTP jobs, headers will be null.
147 HttpResponseHeaders
* headers
= request_
->response_headers();
152 if (!headers
->IsRedirect(&value
))
155 *location
= request_
->url().Resolve(value
);
156 *http_status_code
= headers
->response_code();
160 bool URLRequestJob::CopyFragmentOnRedirect(const GURL
& location
) const {
164 bool URLRequestJob::IsSafeRedirect(const GURL
& location
) {
168 bool URLRequestJob::NeedsAuth() {
172 void URLRequestJob::GetAuthChallengeInfo(
173 scoped_refptr
<AuthChallengeInfo
>* auth_info
) {
174 // This will only be called if NeedsAuth() returns true, in which
175 // case the derived class should implement this!
179 void URLRequestJob::SetAuth(const AuthCredentials
& credentials
) {
180 // This will only be called if NeedsAuth() returns true, in which
181 // case the derived class should implement this!
185 void URLRequestJob::CancelAuth() {
186 // This will only be called if NeedsAuth() returns true, in which
187 // case the derived class should implement this!
191 void URLRequestJob::ContinueWithCertificate(
192 X509Certificate
* client_cert
) {
193 // The derived class should implement this!
197 void URLRequestJob::ContinueDespiteLastError() {
198 // Implementations should know how to recover from errors they generate.
199 // If this code was reached, we are trying to recover from an error that
200 // we don't know how to recover from.
204 void URLRequestJob::FollowDeferredRedirect() {
205 DCHECK_NE(-1, deferred_redirect_info_
.status_code
);
207 // NOTE: deferred_redirect_info_ may be invalid, and attempting to follow it
208 // will fail inside FollowRedirect. The DCHECK above asserts that we called
209 // OnReceivedRedirect.
211 // It is also possible that FollowRedirect will drop the last reference to
212 // this job, so we need to reset our members before calling it.
214 RedirectInfo redirect_info
= deferred_redirect_info_
;
215 deferred_redirect_info_
= RedirectInfo();
216 FollowRedirect(redirect_info
);
219 void URLRequestJob::ResumeNetworkStart() {
220 // This should only be called for HTTP Jobs, and implemented in the derived
225 bool URLRequestJob::GetMimeType(std::string
* mime_type
) const {
229 int URLRequestJob::GetResponseCode() const {
233 HostPortPair
URLRequestJob::GetSocketAddress() const {
234 return HostPortPair();
237 void URLRequestJob::OnSuspend() {
241 void URLRequestJob::NotifyURLRequestDestroyed() {
244 URLRequestJob::~URLRequestJob() {
245 base::PowerMonitor
* power_monitor
= base::PowerMonitor::Get();
247 power_monitor
->RemoveObserver(this);
250 void URLRequestJob::NotifyCertificateRequested(
251 SSLCertRequestInfo
* cert_request_info
) {
253 return; // The request was destroyed, so there is no more work to do.
255 request_
->NotifyCertificateRequested(cert_request_info
);
258 void URLRequestJob::NotifySSLCertificateError(const SSLInfo
& ssl_info
,
261 return; // The request was destroyed, so there is no more work to do.
263 request_
->NotifySSLCertificateError(ssl_info
, fatal
);
266 bool URLRequestJob::CanGetCookies(const CookieList
& cookie_list
) const {
268 return false; // The request was destroyed, so there is no more work to do.
270 return request_
->CanGetCookies(cookie_list
);
273 bool URLRequestJob::CanSetCookie(const std::string
& cookie_line
,
274 CookieOptions
* options
) const {
276 return false; // The request was destroyed, so there is no more work to do.
278 return request_
->CanSetCookie(cookie_line
, options
);
281 bool URLRequestJob::CanEnablePrivacyMode() const {
283 return false; // The request was destroyed, so there is no more work to do.
285 return request_
->CanEnablePrivacyMode();
288 CookieStore
* URLRequestJob::GetCookieStore() const {
291 return request_
->cookie_store();
294 void URLRequestJob::NotifyBeforeNetworkStart(bool* defer
) {
298 request_
->NotifyBeforeNetworkStart(defer
);
301 void URLRequestJob::NotifyHeadersComplete() {
302 if (!request_
|| !request_
->has_delegate())
303 return; // The request was destroyed, so there is no more work to do.
305 if (has_handled_response_
)
308 DCHECK(!request_
->status().is_io_pending());
310 // Initialize to the current time, and let the subclass optionally override
311 // the time stamps if it has that information. The default request_time is
312 // set by URLRequest before it calls our Start method.
313 request_
->response_info_
.response_time
= base::Time::Now();
314 GetResponseInfo(&request_
->response_info_
);
316 // When notifying the delegate, the delegate can release the request
317 // (and thus release 'this'). After calling to the delgate, we must
318 // check the request pointer to see if it still exists, and return
319 // immediately if it has been destroyed. self_preservation ensures our
320 // survival until we can get out of this method.
321 scoped_refptr
<URLRequestJob
> self_preservation(this);
324 request_
->OnHeadersComplete();
327 int http_status_code
;
328 if (IsRedirectResponse(&new_location
, &http_status_code
)) {
329 // Redirect response bodies are not read. Notify the transaction
330 // so it does not treat being stopped as an error.
331 DoneReadingRedirectResponse();
333 RedirectInfo redirect_info
=
334 ComputeRedirectInfo(new_location
, http_status_code
);
336 bool defer_redirect
= false;
337 request_
->NotifyReceivedRedirect(redirect_info
, &defer_redirect
);
339 // Ensure that the request wasn't detached or destroyed in
340 // NotifyReceivedRedirect
341 if (!request_
|| !request_
->has_delegate())
344 // If we were not cancelled, then maybe follow the redirect.
345 if (request_
->status().is_success()) {
346 if (defer_redirect
) {
347 deferred_redirect_info_
= redirect_info
;
349 FollowRedirect(redirect_info
);
353 } else if (NeedsAuth()) {
354 scoped_refptr
<AuthChallengeInfo
> auth_info
;
355 GetAuthChallengeInfo(&auth_info
);
356 // Need to check for a NULL auth_info because the server may have failed
357 // to send a challenge with the 401 response.
358 if (auth_info
.get()) {
359 request_
->NotifyAuthRequired(auth_info
.get());
360 // Wait for SetAuth or CancelAuth to be called.
365 has_handled_response_
= true;
366 if (request_
->status().is_success())
367 filter_
.reset(SetupFilter());
369 if (!filter_
.get()) {
370 std::string content_length
;
371 request_
->GetResponseHeaderByName("content-length", &content_length
);
372 if (!content_length
.empty())
373 base::StringToInt64(content_length
, &expected_content_size_
);
376 request_
->NotifyResponseStarted();
379 void URLRequestJob::NotifyReadComplete(int bytes_read
) {
380 if (!request_
|| !request_
->has_delegate())
381 return; // The request was destroyed, so there is no more work to do.
383 // TODO(darin): Bug 1004233. Re-enable this test once all of the chrome
384 // unit_tests have been fixed to not trip this.
386 DCHECK(!request_
->status().is_io_pending());
388 // The headers should be complete before reads complete
389 DCHECK(has_handled_response_
);
391 OnRawReadComplete(bytes_read
);
393 // Don't notify if we had an error.
394 if (!request_
->status().is_success())
397 // When notifying the delegate, the delegate can release the request
398 // (and thus release 'this'). After calling to the delegate, we must
399 // check the request pointer to see if it still exists, and return
400 // immediately if it has been destroyed. self_preservation ensures our
401 // survival until we can get out of this method.
402 scoped_refptr
<URLRequestJob
> self_preservation(this);
405 // Tell the filter that it has more data
406 FilteredDataRead(bytes_read
);
409 int filter_bytes_read
= 0;
410 if (ReadFilteredData(&filter_bytes_read
)) {
411 if (!filter_bytes_read
)
413 request_
->NotifyReadCompleted(filter_bytes_read
);
416 request_
->NotifyReadCompleted(bytes_read
);
418 DVLOG(1) << __FUNCTION__
<< "() "
419 << "\"" << (request_
? request_
->url().spec() : "???") << "\""
420 << " pre bytes read = " << bytes_read
421 << " pre total = " << prefilter_bytes_read_
422 << " post total = " << postfilter_bytes_read_
;
425 void URLRequestJob::NotifyStartError(const URLRequestStatus
&status
) {
426 DCHECK(!has_handled_response_
);
427 has_handled_response_
= true;
429 // There may be relevant information in the response info even in the
431 GetResponseInfo(&request_
->response_info_
);
433 request_
->set_status(status
);
434 request_
->NotifyResponseStarted();
435 // We may have been deleted.
439 void URLRequestJob::NotifyDone(const URLRequestStatus
&status
) {
440 DCHECK(!done_
) << "Job sending done notification twice";
445 // Unless there was an error, we should have at least tried to handle
446 // the response before getting here.
447 DCHECK(has_handled_response_
|| !status
.is_success());
449 // As with NotifyReadComplete, we need to take care to notice if we were
450 // destroyed during a delegate callback.
452 request_
->set_is_pending(false);
453 // With async IO, it's quite possible to have a few outstanding
454 // requests. We could receive a request to Cancel, followed shortly
455 // by a successful IO. For tracking the status(), once there is
456 // an error, we do not change the status back to success. To
457 // enforce this, only set the status if the job is so far
459 if (request_
->status().is_success()) {
460 if (status
.status() == URLRequestStatus::FAILED
) {
461 request_
->net_log().AddEventWithNetErrorCode(NetLog::TYPE_FAILED
,
464 request_
->set_status(status
);
468 // Complete this notification later. This prevents us from re-entering the
469 // delegate if we're done because of a synchronous call.
470 base::MessageLoop::current()->PostTask(
472 base::Bind(&URLRequestJob::CompleteNotifyDone
,
473 weak_factory_
.GetWeakPtr()));
476 void URLRequestJob::CompleteNotifyDone() {
477 // Check if we should notify the delegate that we're done because of an error.
479 !request_
->status().is_success() &&
480 request_
->has_delegate()) {
481 // We report the error differently depending on whether we've called
482 // OnResponseStarted yet.
483 if (has_handled_response_
) {
484 // We signal the error by calling OnReadComplete with a bytes_read of -1.
485 request_
->NotifyReadCompleted(-1);
487 has_handled_response_
= true;
488 request_
->NotifyResponseStarted();
493 void URLRequestJob::NotifyCanceled() {
495 NotifyDone(URLRequestStatus(URLRequestStatus::CANCELED
, ERR_ABORTED
));
499 void URLRequestJob::NotifyRestartRequired() {
500 DCHECK(!has_handled_response_
);
501 if (GetStatus().status() != URLRequestStatus::CANCELED
)
505 void URLRequestJob::OnCallToDelegate() {
506 request_
->OnCallToDelegate();
509 void URLRequestJob::OnCallToDelegateComplete() {
510 request_
->OnCallToDelegateComplete();
513 bool URLRequestJob::ReadRawData(IOBuffer
* buf
, int buf_size
,
520 void URLRequestJob::DoneReading() {
524 void URLRequestJob::DoneReadingRedirectResponse() {
527 void URLRequestJob::FilteredDataRead(int bytes_read
) {
529 filter_
->FlushStreamBuffer(bytes_read
);
532 bool URLRequestJob::ReadFilteredData(int* bytes_read
) {
534 DCHECK(filtered_read_buffer_
);
535 DCHECK_GT(filtered_read_buffer_len_
, 0);
536 DCHECK_LT(filtered_read_buffer_len_
, 1000000); // Sanity check.
537 DCHECK(!raw_read_buffer_
);
546 if (!filter_needs_more_output_space_
&& !filter_
->stream_data_len()) {
547 // We don't have any raw data to work with, so read from the transaction.
548 int filtered_data_read
;
549 if (ReadRawDataForFilter(&filtered_data_read
)) {
550 if (filtered_data_read
> 0) {
551 // Give data to filter.
552 filter_
->FlushStreamBuffer(filtered_data_read
);
557 return false; // IO Pending (or error).
561 if ((filter_
->stream_data_len() || filter_needs_more_output_space_
) &&
563 // Get filtered data.
564 int filtered_data_len
= filtered_read_buffer_len_
;
565 int output_buffer_size
= filtered_data_len
;
566 Filter::FilterStatus status
=
567 filter_
->ReadData(filtered_read_buffer_
->data(), &filtered_data_len
);
569 if (filter_needs_more_output_space_
&& !filtered_data_len
) {
570 // filter_needs_more_output_space_ was mistaken... there are no more
571 // bytes and we should have at least tried to fill up the filter's input
572 // buffer. Correct the state, and try again.
573 filter_needs_more_output_space_
= false;
576 filter_needs_more_output_space_
=
577 (filtered_data_len
== output_buffer_size
);
580 case Filter::FILTER_DONE
: {
581 filter_needs_more_output_space_
= false;
582 *bytes_read
= filtered_data_len
;
583 postfilter_bytes_read_
+= filtered_data_len
;
587 case Filter::FILTER_NEED_MORE_DATA
: {
588 // We have finished filtering all data currently in the buffer.
589 // There might be some space left in the output buffer. One can
590 // consider reading more data from the stream to feed the filter
591 // and filling up the output buffer. This leads to more complicated
592 // buffer management and data notification mechanisms.
593 // We can revisit this issue if there is a real perf need.
594 if (filtered_data_len
> 0) {
595 *bytes_read
= filtered_data_len
;
596 postfilter_bytes_read_
+= filtered_data_len
;
599 // Read again since we haven't received enough data yet (e.g., we
600 // may not have a complete gzip header yet).
605 case Filter::FILTER_OK
: {
606 *bytes_read
= filtered_data_len
;
607 postfilter_bytes_read_
+= filtered_data_len
;
611 case Filter::FILTER_ERROR
: {
612 DVLOG(1) << __FUNCTION__
<< "() "
613 << "\"" << (request_
? request_
->url().spec() : "???")
614 << "\"" << " Filter Error";
615 filter_needs_more_output_space_
= false;
616 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
617 ERR_CONTENT_DECODING_FAILED
));
623 filter_needs_more_output_space_
= false;
629 // If logging all bytes is enabled, log the filtered bytes read.
630 if (rv
&& request() && request()->net_log().IsLoggingBytes() &&
631 filtered_data_len
> 0) {
632 request()->net_log().AddByteTransferEvent(
633 NetLog::TYPE_URL_REQUEST_JOB_FILTERED_BYTES_READ
,
634 filtered_data_len
, filtered_read_buffer_
->data());
637 // we are done, or there is no data left.
644 // When we successfully finished a read, we no longer need to save the
645 // caller's buffers. Release our reference.
646 filtered_read_buffer_
= NULL
;
647 filtered_read_buffer_len_
= 0;
652 void URLRequestJob::DestroyFilters() {
656 const URLRequestStatus
URLRequestJob::GetStatus() {
658 return request_
->status();
659 // If the request is gone, we must be cancelled.
660 return URLRequestStatus(URLRequestStatus::CANCELED
,
664 void URLRequestJob::SetStatus(const URLRequestStatus
&status
) {
666 request_
->set_status(status
);
669 void URLRequestJob::SetProxyServer(const HostPortPair
& proxy_server
) {
670 request_
->proxy_server_
= proxy_server
;
673 bool URLRequestJob::ReadRawDataForFilter(int* bytes_read
) {
677 DCHECK(filter_
.get());
681 // Get more pre-filtered data if needed.
682 // TODO(mbelshe): is it possible that the filter needs *MORE* data
683 // when there is some data already in the buffer?
684 if (!filter_
->stream_data_len() && !is_done()) {
685 IOBuffer
* stream_buffer
= filter_
->stream_buffer();
686 int stream_buffer_size
= filter_
->stream_buffer_size();
687 rv
= ReadRawDataHelper(stream_buffer
, stream_buffer_size
, bytes_read
);
692 bool URLRequestJob::ReadRawDataHelper(IOBuffer
* buf
, int buf_size
,
694 DCHECK(!request_
->status().is_io_pending());
695 DCHECK(raw_read_buffer_
.get() == NULL
);
697 // Keep a pointer to the read buffer, so we have access to it in the
698 // OnRawReadComplete() callback in the event that the read completes
700 raw_read_buffer_
= buf
;
701 bool rv
= ReadRawData(buf
, buf_size
, bytes_read
);
703 if (!request_
->status().is_io_pending()) {
704 // If the read completes synchronously, either success or failure,
705 // invoke the OnRawReadComplete callback so we can account for the
707 OnRawReadComplete(*bytes_read
);
712 void URLRequestJob::FollowRedirect(const RedirectInfo
& redirect_info
) {
713 int rv
= request_
->Redirect(redirect_info
);
715 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, rv
));
718 void URLRequestJob::OnRawReadComplete(int bytes_read
) {
719 DCHECK(raw_read_buffer_
.get());
720 // If |filter_| is non-NULL, bytes will be logged after it is applied instead.
721 if (!filter_
.get() && request() && request()->net_log().IsLoggingBytes() &&
723 request()->net_log().AddByteTransferEvent(
724 NetLog::TYPE_URL_REQUEST_JOB_BYTES_READ
,
725 bytes_read
, raw_read_buffer_
->data());
728 if (bytes_read
> 0) {
729 RecordBytesRead(bytes_read
);
731 raw_read_buffer_
= NULL
;
734 void URLRequestJob::RecordBytesRead(int bytes_read
) {
735 filter_input_byte_count_
+= bytes_read
;
736 prefilter_bytes_read_
+= bytes_read
;
738 postfilter_bytes_read_
+= bytes_read
;
739 DVLOG(2) << __FUNCTION__
<< "() "
740 << "\"" << (request_
? request_
->url().spec() : "???") << "\""
741 << " pre bytes read = " << bytes_read
742 << " pre total = " << prefilter_bytes_read_
743 << " post total = " << postfilter_bytes_read_
;
744 UpdatePacketReadTimes(); // Facilitate stats recording if it is active.
745 if (network_delegate_
)
746 network_delegate_
->NotifyRawBytesRead(*request_
, bytes_read
);
749 bool URLRequestJob::FilterHasData() {
750 return filter_
.get() && filter_
->stream_data_len();
753 void URLRequestJob::UpdatePacketReadTimes() {
756 RedirectInfo
URLRequestJob::ComputeRedirectInfo(const GURL
& location
,
757 int http_status_code
) {
758 const GURL
& url
= request_
->url();
760 RedirectInfo redirect_info
;
762 redirect_info
.status_code
= http_status_code
;
764 // The request method may change, depending on the status code.
765 redirect_info
.new_method
= URLRequest::ComputeMethodForRedirect(
766 request_
->method(), http_status_code
);
768 // Move the reference fragment of the old location to the new one if the
769 // new one has none. This duplicates mozilla's behavior.
770 if (url
.is_valid() && url
.has_ref() && !location
.has_ref() &&
771 CopyFragmentOnRedirect(location
)) {
772 GURL::Replacements replacements
;
773 // Reference the |ref| directly out of the original URL to avoid a
775 replacements
.SetRef(url
.spec().data(),
776 url
.parsed_for_possibly_invalid_spec().ref
);
777 redirect_info
.new_url
= location
.ReplaceComponents(replacements
);
779 redirect_info
.new_url
= location
;
782 // Update the first-party URL if appropriate.
783 if (request_
->first_party_url_policy() ==
784 URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT
) {
785 redirect_info
.new_first_party_for_cookies
= redirect_info
.new_url
;
787 redirect_info
.new_first_party_for_cookies
=
788 request_
->first_party_for_cookies();
791 // Suppress the referrer if we're redirecting out of https.
792 if (request_
->referrer_policy() ==
793 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
&&
794 GURL(request_
->referrer()).SchemeIsSecure() &&
795 !redirect_info
.new_url
.SchemeIsSecure()) {
796 redirect_info
.new_referrer
.clear();
798 redirect_info
.new_referrer
= request_
->referrer();
801 return redirect_info
;