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/profiler/scoped_tracker.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "base/values.h"
15 #include "net/base/auth.h"
16 #include "net/base/host_port_pair.h"
17 #include "net/base/io_buffer.h"
18 #include "net/base/load_states.h"
19 #include "net/base/net_errors.h"
20 #include "net/base/network_delegate.h"
21 #include "net/filter/filter.h"
22 #include "net/http/http_response_headers.h"
28 // Callback for TYPE_URL_REQUEST_FILTERS_SET net-internals event.
29 base::Value
* FiltersSetCallback(Filter
* filter
,
30 NetLog::LogLevel
/* log_level */) {
31 base::DictionaryValue
* event_params
= new base::DictionaryValue();
32 event_params
->SetString("filters", filter
->OrderedFilterList());
36 std::string
ComputeMethodForRedirect(const std::string
& method
,
37 int http_status_code
) {
38 // For 303 redirects, all request methods except HEAD are converted to GET,
39 // as per the latest httpbis draft. The draft also allows POST requests to
40 // be converted to GETs when following 301/302 redirects, for historical
41 // reasons. Most major browsers do this and so shall we. Both RFC 2616 and
42 // the httpbis draft say to prompt the user to confirm the generation of new
43 // requests, other than GET and HEAD requests, but IE omits these prompts and
46 // https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#section-7.3
47 if ((http_status_code
== 303 && method
!= "HEAD") ||
48 ((http_status_code
== 301 || http_status_code
== 302) &&
57 URLRequestJob::URLRequestJob(URLRequest
* request
,
58 NetworkDelegate
* network_delegate
)
61 prefilter_bytes_read_(0),
62 postfilter_bytes_read_(0),
63 filter_input_byte_count_(0),
64 filter_needs_more_output_space_(false),
65 filtered_read_buffer_len_(0),
66 has_handled_response_(false),
67 expected_content_size_(-1),
68 network_delegate_(network_delegate
),
70 base::PowerMonitor
* power_monitor
= base::PowerMonitor::Get();
72 power_monitor
->AddObserver(this);
75 void URLRequestJob::SetUpload(UploadDataStream
* upload
) {
78 void URLRequestJob::SetExtraRequestHeaders(const HttpRequestHeaders
& headers
) {
81 void URLRequestJob::SetPriority(RequestPriority priority
) {
84 void URLRequestJob::Kill() {
85 weak_factory_
.InvalidateWeakPtrs();
86 // Make sure the request is notified that we are done. We assume that the
87 // request took care of setting its error status before calling Kill.
92 void URLRequestJob::DetachRequest() {
96 // This function calls ReadData to get stream data. If a filter exists, passes
97 // the data to the attached filter. Then returns the output from filter back to
99 bool URLRequestJob::Read(IOBuffer
* buf
, int buf_size
, int *bytes_read
) {
102 DCHECK_LT(buf_size
, 1000000); // Sanity check.
105 DCHECK(filtered_read_buffer_
.get() == NULL
);
106 DCHECK_EQ(0, filtered_read_buffer_len_
);
110 // Skip Filter if not present.
111 if (!filter_
.get()) {
112 rv
= ReadRawDataHelper(buf
, buf_size
, bytes_read
);
114 // Save the caller's buffers while we do IO
115 // in the filter's buffers.
116 filtered_read_buffer_
= buf
;
117 filtered_read_buffer_len_
= buf_size
;
119 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
120 tracked_objects::ScopedTracker
tracking_profile2(
121 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequestJob::Read2"));
123 if (ReadFilteredData(bytes_read
)) {
124 rv
= true; // We have data to return.
126 // It is fine to call DoneReading even if ReadFilteredData receives 0
127 // bytes from the net, but we avoid making that call if we know for
128 // sure that's the case (ReadRawDataHelper path).
129 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
131 tracked_objects::ScopedTracker
tracking_profile3(
132 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequestJob::Read3"));
134 if (*bytes_read
== 0)
137 rv
= false; // Error, or a new IO is pending.
141 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
142 tracked_objects::ScopedTracker
tracking_profile4(
143 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequestJob::Read4"));
145 if (rv
&& *bytes_read
== 0)
146 NotifyDone(URLRequestStatus());
150 void URLRequestJob::StopCaching() {
151 // Nothing to do here.
154 bool URLRequestJob::GetFullRequestHeaders(HttpRequestHeaders
* headers
) const {
155 // Most job types don't send request headers.
159 int64
URLRequestJob::GetTotalReceivedBytes() const {
163 LoadState
URLRequestJob::GetLoadState() const {
164 return LOAD_STATE_IDLE
;
167 UploadProgress
URLRequestJob::GetUploadProgress() const {
168 return UploadProgress();
171 bool URLRequestJob::GetCharset(std::string
* charset
) {
175 void URLRequestJob::GetResponseInfo(HttpResponseInfo
* info
) {
178 void URLRequestJob::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
179 // Only certain request types return more than just request start times.
182 bool URLRequestJob::GetResponseCookies(std::vector
<std::string
>* cookies
) {
186 Filter
* URLRequestJob::SetupFilter() const {
190 bool URLRequestJob::IsRedirectResponse(GURL
* location
,
191 int* http_status_code
) {
192 // For non-HTTP jobs, headers will be null.
193 HttpResponseHeaders
* headers
= request_
->response_headers();
198 if (!headers
->IsRedirect(&value
))
201 *location
= request_
->url().Resolve(value
);
202 *http_status_code
= headers
->response_code();
206 bool URLRequestJob::CopyFragmentOnRedirect(const GURL
& location
) const {
210 bool URLRequestJob::IsSafeRedirect(const GURL
& location
) {
214 bool URLRequestJob::NeedsAuth() {
218 void URLRequestJob::GetAuthChallengeInfo(
219 scoped_refptr
<AuthChallengeInfo
>* auth_info
) {
220 // This will only be called if NeedsAuth() returns true, in which
221 // case the derived class should implement this!
225 void URLRequestJob::SetAuth(const AuthCredentials
& credentials
) {
226 // This will only be called if NeedsAuth() returns true, in which
227 // case the derived class should implement this!
231 void URLRequestJob::CancelAuth() {
232 // This will only be called if NeedsAuth() returns true, in which
233 // case the derived class should implement this!
237 void URLRequestJob::ContinueWithCertificate(
238 X509Certificate
* client_cert
) {
239 // The derived class should implement this!
243 void URLRequestJob::ContinueDespiteLastError() {
244 // Implementations should know how to recover from errors they generate.
245 // If this code was reached, we are trying to recover from an error that
246 // we don't know how to recover from.
250 void URLRequestJob::FollowDeferredRedirect() {
251 DCHECK_NE(-1, deferred_redirect_info_
.status_code
);
253 // NOTE: deferred_redirect_info_ may be invalid, and attempting to follow it
254 // will fail inside FollowRedirect. The DCHECK above asserts that we called
255 // OnReceivedRedirect.
257 // It is also possible that FollowRedirect will drop the last reference to
258 // this job, so we need to reset our members before calling it.
260 RedirectInfo redirect_info
= deferred_redirect_info_
;
261 deferred_redirect_info_
= RedirectInfo();
262 FollowRedirect(redirect_info
);
265 void URLRequestJob::ResumeNetworkStart() {
266 // This should only be called for HTTP Jobs, and implemented in the derived
271 bool URLRequestJob::GetMimeType(std::string
* mime_type
) const {
275 int URLRequestJob::GetResponseCode() const {
279 HostPortPair
URLRequestJob::GetSocketAddress() const {
280 return HostPortPair();
283 void URLRequestJob::OnSuspend() {
287 void URLRequestJob::NotifyURLRequestDestroyed() {
291 GURL
URLRequestJob::ComputeReferrerForRedirect(
292 URLRequest::ReferrerPolicy policy
,
293 const std::string
& referrer
,
294 const GURL
& redirect_destination
) {
295 GURL
original_referrer(referrer
);
296 bool secure_referrer_but_insecure_destination
=
297 original_referrer
.SchemeIsSecure() &&
298 !redirect_destination
.SchemeIsSecure();
300 original_referrer
.GetOrigin() == redirect_destination
.GetOrigin();
302 case URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
:
303 return secure_referrer_but_insecure_destination
? GURL()
306 case URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN
:
308 return original_referrer
;
309 } else if (secure_referrer_but_insecure_destination
) {
312 return original_referrer
.GetOrigin();
315 case URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN
:
316 return same_origin
? original_referrer
: original_referrer
.GetOrigin();
318 case URLRequest::NEVER_CLEAR_REFERRER
:
319 return original_referrer
;
326 URLRequestJob::~URLRequestJob() {
327 base::PowerMonitor
* power_monitor
= base::PowerMonitor::Get();
329 power_monitor
->RemoveObserver(this);
332 void URLRequestJob::NotifyCertificateRequested(
333 SSLCertRequestInfo
* cert_request_info
) {
335 return; // The request was destroyed, so there is no more work to do.
337 request_
->NotifyCertificateRequested(cert_request_info
);
340 void URLRequestJob::NotifySSLCertificateError(const SSLInfo
& ssl_info
,
343 return; // The request was destroyed, so there is no more work to do.
345 request_
->NotifySSLCertificateError(ssl_info
, fatal
);
348 bool URLRequestJob::CanGetCookies(const CookieList
& cookie_list
) const {
350 return false; // The request was destroyed, so there is no more work to do.
352 return request_
->CanGetCookies(cookie_list
);
355 bool URLRequestJob::CanSetCookie(const std::string
& cookie_line
,
356 CookieOptions
* options
) const {
358 return false; // The request was destroyed, so there is no more work to do.
360 return request_
->CanSetCookie(cookie_line
, options
);
363 bool URLRequestJob::CanEnablePrivacyMode() const {
365 return false; // The request was destroyed, so there is no more work to do.
367 return request_
->CanEnablePrivacyMode();
370 void URLRequestJob::NotifyBeforeNetworkStart(bool* defer
) {
374 request_
->NotifyBeforeNetworkStart(defer
);
377 void URLRequestJob::NotifyHeadersComplete() {
378 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
379 tracked_objects::ScopedTracker
tracking_profile(
380 FROM_HERE_WITH_EXPLICIT_FUNCTION(
381 "423948 URLRequestJob::NotifyHeadersComplete"));
383 if (!request_
|| !request_
->has_delegate())
384 return; // The request was destroyed, so there is no more work to do.
386 if (has_handled_response_
)
389 DCHECK(!request_
->status().is_io_pending());
391 // Initialize to the current time, and let the subclass optionally override
392 // the time stamps if it has that information. The default request_time is
393 // set by URLRequest before it calls our Start method.
394 request_
->response_info_
.response_time
= base::Time::Now();
395 GetResponseInfo(&request_
->response_info_
);
397 // When notifying the delegate, the delegate can release the request
398 // (and thus release 'this'). After calling to the delgate, 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 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
406 tracked_objects::ScopedTracker
tracking_profile1(
407 FROM_HERE_WITH_EXPLICIT_FUNCTION(
408 "423948 URLRequestJob::NotifyHeadersComplete 1"));
410 request_
->OnHeadersComplete();
413 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
414 tracked_objects::ScopedTracker
tracking_profile2(
415 FROM_HERE_WITH_EXPLICIT_FUNCTION(
416 "423948 URLRequestJob::NotifyHeadersComplete 2"));
419 int http_status_code
;
420 if (IsRedirectResponse(&new_location
, &http_status_code
)) {
421 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
422 tracked_objects::ScopedTracker
tracking_profile3(
423 FROM_HERE_WITH_EXPLICIT_FUNCTION(
424 "423948 URLRequestJob::NotifyHeadersComplete 3"));
426 // Redirect response bodies are not read. Notify the transaction
427 // so it does not treat being stopped as an error.
428 DoneReadingRedirectResponse();
430 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
431 tracked_objects::ScopedTracker
tracking_profile4(
432 FROM_HERE_WITH_EXPLICIT_FUNCTION(
433 "423948 URLRequestJob::NotifyHeadersComplete 4"));
435 RedirectInfo redirect_info
=
436 ComputeRedirectInfo(new_location
, http_status_code
);
438 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
439 tracked_objects::ScopedTracker
tracking_profile5(
440 FROM_HERE_WITH_EXPLICIT_FUNCTION(
441 "423948 URLRequestJob::NotifyHeadersComplete 5"));
443 bool defer_redirect
= false;
444 request_
->NotifyReceivedRedirect(redirect_info
, &defer_redirect
);
446 // Ensure that the request wasn't detached or destroyed in
447 // NotifyReceivedRedirect
448 if (!request_
|| !request_
->has_delegate())
451 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
452 tracked_objects::ScopedTracker
tracking_profile6(
453 FROM_HERE_WITH_EXPLICIT_FUNCTION(
454 "423948 URLRequestJob::NotifyHeadersComplete 6"));
456 // If we were not cancelled, then maybe follow the redirect.
457 if (request_
->status().is_success()) {
458 if (defer_redirect
) {
459 deferred_redirect_info_
= redirect_info
;
461 FollowRedirect(redirect_info
);
465 } else if (NeedsAuth()) {
466 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
467 tracked_objects::ScopedTracker
tracking_profile7(
468 FROM_HERE_WITH_EXPLICIT_FUNCTION(
469 "423948 URLRequestJob::NotifyHeadersComplete 7"));
471 scoped_refptr
<AuthChallengeInfo
> auth_info
;
472 GetAuthChallengeInfo(&auth_info
);
474 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
475 tracked_objects::ScopedTracker
tracking_profile8(
476 FROM_HERE_WITH_EXPLICIT_FUNCTION(
477 "423948 URLRequestJob::NotifyHeadersComplete 8"));
479 // Need to check for a NULL auth_info because the server may have failed
480 // to send a challenge with the 401 response.
481 if (auth_info
.get()) {
482 request_
->NotifyAuthRequired(auth_info
.get());
483 // Wait for SetAuth or CancelAuth to be called.
488 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
489 tracked_objects::ScopedTracker
tracking_profile9(
490 FROM_HERE_WITH_EXPLICIT_FUNCTION(
491 "423948 URLRequestJob::NotifyHeadersComplete 9"));
493 has_handled_response_
= true;
494 if (request_
->status().is_success())
495 filter_
.reset(SetupFilter());
497 if (!filter_
.get()) {
498 std::string content_length
;
499 request_
->GetResponseHeaderByName("content-length", &content_length
);
500 if (!content_length
.empty())
501 base::StringToInt64(content_length
, &expected_content_size_
);
503 request_
->net_log().AddEvent(
504 NetLog::TYPE_URL_REQUEST_FILTERS_SET
,
505 base::Bind(&FiltersSetCallback
, base::Unretained(filter_
.get())));
508 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
509 tracked_objects::ScopedTracker
tracking_profile10(
510 FROM_HERE_WITH_EXPLICIT_FUNCTION(
511 "423948 URLRequestJob::NotifyHeadersComplete 10"));
513 request_
->NotifyResponseStarted();
516 void URLRequestJob::NotifyReadComplete(int bytes_read
) {
517 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
518 tracked_objects::ScopedTracker
tracking_profile(
519 FROM_HERE_WITH_EXPLICIT_FUNCTION(
520 "423948 URLRequestJob::NotifyReadComplete"));
522 if (!request_
|| !request_
->has_delegate())
523 return; // The request was destroyed, so there is no more work to do.
525 // TODO(darin): Bug 1004233. Re-enable this test once all of the chrome
526 // unit_tests have been fixed to not trip this.
528 DCHECK(!request_
->status().is_io_pending());
530 // The headers should be complete before reads complete
531 DCHECK(has_handled_response_
);
533 OnRawReadComplete(bytes_read
);
535 // Don't notify if we had an error.
536 if (!request_
->status().is_success())
539 // When notifying the delegate, the delegate can release the request
540 // (and thus release 'this'). After calling to the delegate, we must
541 // check the request pointer to see if it still exists, and return
542 // immediately if it has been destroyed. self_preservation ensures our
543 // survival until we can get out of this method.
544 scoped_refptr
<URLRequestJob
> self_preservation(this);
547 // Tell the filter that it has more data
548 FilteredDataRead(bytes_read
);
551 int filter_bytes_read
= 0;
552 if (ReadFilteredData(&filter_bytes_read
)) {
553 if (!filter_bytes_read
)
555 request_
->NotifyReadCompleted(filter_bytes_read
);
558 request_
->NotifyReadCompleted(bytes_read
);
560 DVLOG(1) << __FUNCTION__
<< "() "
561 << "\"" << (request_
? request_
->url().spec() : "???") << "\""
562 << " pre bytes read = " << bytes_read
563 << " pre total = " << prefilter_bytes_read_
564 << " post total = " << postfilter_bytes_read_
;
567 void URLRequestJob::NotifyStartError(const URLRequestStatus
&status
) {
568 DCHECK(!has_handled_response_
);
569 has_handled_response_
= true;
571 // There may be relevant information in the response info even in the
573 GetResponseInfo(&request_
->response_info_
);
575 request_
->set_status(status
);
576 request_
->NotifyResponseStarted();
577 // We may have been deleted.
581 void URLRequestJob::NotifyDone(const URLRequestStatus
&status
) {
582 DCHECK(!done_
) << "Job sending done notification twice";
587 // Unless there was an error, we should have at least tried to handle
588 // the response before getting here.
589 DCHECK(has_handled_response_
|| !status
.is_success());
591 // As with NotifyReadComplete, we need to take care to notice if we were
592 // destroyed during a delegate callback.
594 request_
->set_is_pending(false);
595 // With async IO, it's quite possible to have a few outstanding
596 // requests. We could receive a request to Cancel, followed shortly
597 // by a successful IO. For tracking the status(), once there is
598 // an error, we do not change the status back to success. To
599 // enforce this, only set the status if the job is so far
601 if (request_
->status().is_success()) {
602 if (status
.status() == URLRequestStatus::FAILED
) {
603 request_
->net_log().AddEventWithNetErrorCode(NetLog::TYPE_FAILED
,
606 request_
->set_status(status
);
610 // Complete this notification later. This prevents us from re-entering the
611 // delegate if we're done because of a synchronous call.
612 base::MessageLoop::current()->PostTask(
614 base::Bind(&URLRequestJob::CompleteNotifyDone
,
615 weak_factory_
.GetWeakPtr()));
618 void URLRequestJob::CompleteNotifyDone() {
619 // Check if we should notify the delegate that we're done because of an error.
621 !request_
->status().is_success() &&
622 request_
->has_delegate()) {
623 // We report the error differently depending on whether we've called
624 // OnResponseStarted yet.
625 if (has_handled_response_
) {
626 // We signal the error by calling OnReadComplete with a bytes_read of -1.
627 request_
->NotifyReadCompleted(-1);
629 has_handled_response_
= true;
630 request_
->NotifyResponseStarted();
635 void URLRequestJob::NotifyCanceled() {
637 NotifyDone(URLRequestStatus(URLRequestStatus::CANCELED
, ERR_ABORTED
));
641 void URLRequestJob::NotifyRestartRequired() {
642 DCHECK(!has_handled_response_
);
643 if (GetStatus().status() != URLRequestStatus::CANCELED
)
647 void URLRequestJob::OnCallToDelegate() {
648 request_
->OnCallToDelegate();
651 void URLRequestJob::OnCallToDelegateComplete() {
652 request_
->OnCallToDelegateComplete();
655 bool URLRequestJob::ReadRawData(IOBuffer
* buf
, int buf_size
,
662 void URLRequestJob::DoneReading() {
666 void URLRequestJob::DoneReadingRedirectResponse() {
669 void URLRequestJob::FilteredDataRead(int bytes_read
) {
671 filter_
->FlushStreamBuffer(bytes_read
);
674 bool URLRequestJob::ReadFilteredData(int* bytes_read
) {
676 DCHECK(filtered_read_buffer_
.get());
677 DCHECK_GT(filtered_read_buffer_len_
, 0);
678 DCHECK_LT(filtered_read_buffer_len_
, 1000000); // Sanity check.
679 DCHECK(!raw_read_buffer_
.get());
688 if (!filter_needs_more_output_space_
&& !filter_
->stream_data_len()) {
689 // We don't have any raw data to work with, so read from the transaction.
690 int filtered_data_read
;
691 if (ReadRawDataForFilter(&filtered_data_read
)) {
692 if (filtered_data_read
> 0) {
693 // Give data to filter.
694 filter_
->FlushStreamBuffer(filtered_data_read
);
699 return false; // IO Pending (or error).
703 if ((filter_
->stream_data_len() || filter_needs_more_output_space_
) &&
705 // Get filtered data.
706 int filtered_data_len
= filtered_read_buffer_len_
;
707 int output_buffer_size
= filtered_data_len
;
708 Filter::FilterStatus status
=
709 filter_
->ReadData(filtered_read_buffer_
->data(), &filtered_data_len
);
711 if (filter_needs_more_output_space_
&& !filtered_data_len
) {
712 // filter_needs_more_output_space_ was mistaken... there are no more
713 // bytes and we should have at least tried to fill up the filter's input
714 // buffer. Correct the state, and try again.
715 filter_needs_more_output_space_
= false;
718 filter_needs_more_output_space_
=
719 (filtered_data_len
== output_buffer_size
);
722 case Filter::FILTER_DONE
: {
723 filter_needs_more_output_space_
= false;
724 *bytes_read
= filtered_data_len
;
725 postfilter_bytes_read_
+= filtered_data_len
;
729 case Filter::FILTER_NEED_MORE_DATA
: {
730 // We have finished filtering all data currently in the buffer.
731 // There might be some space left in the output buffer. One can
732 // consider reading more data from the stream to feed the filter
733 // and filling up the output buffer. This leads to more complicated
734 // buffer management and data notification mechanisms.
735 // We can revisit this issue if there is a real perf need.
736 if (filtered_data_len
> 0) {
737 *bytes_read
= filtered_data_len
;
738 postfilter_bytes_read_
+= filtered_data_len
;
741 // Read again since we haven't received enough data yet (e.g., we
742 // may not have a complete gzip header yet).
747 case Filter::FILTER_OK
: {
748 *bytes_read
= filtered_data_len
;
749 postfilter_bytes_read_
+= filtered_data_len
;
753 case Filter::FILTER_ERROR
: {
754 DVLOG(1) << __FUNCTION__
<< "() "
755 << "\"" << (request_
? request_
->url().spec() : "???")
756 << "\"" << " Filter Error";
757 filter_needs_more_output_space_
= false;
758 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
759 ERR_CONTENT_DECODING_FAILED
));
765 filter_needs_more_output_space_
= false;
771 // If logging all bytes is enabled, log the filtered bytes read.
772 if (rv
&& request() && request()->net_log().IsLoggingBytes() &&
773 filtered_data_len
> 0) {
774 request()->net_log().AddByteTransferEvent(
775 NetLog::TYPE_URL_REQUEST_JOB_FILTERED_BYTES_READ
,
776 filtered_data_len
, filtered_read_buffer_
->data());
779 // we are done, or there is no data left.
786 // When we successfully finished a read, we no longer need to save the
787 // caller's buffers. Release our reference.
788 filtered_read_buffer_
= NULL
;
789 filtered_read_buffer_len_
= 0;
794 void URLRequestJob::DestroyFilters() {
798 const URLRequestStatus
URLRequestJob::GetStatus() {
800 return request_
->status();
801 // If the request is gone, we must be cancelled.
802 return URLRequestStatus(URLRequestStatus::CANCELED
,
806 void URLRequestJob::SetStatus(const URLRequestStatus
&status
) {
808 request_
->set_status(status
);
811 void URLRequestJob::SetProxyServer(const HostPortPair
& proxy_server
) {
812 request_
->proxy_server_
= proxy_server
;
815 bool URLRequestJob::ReadRawDataForFilter(int* bytes_read
) {
819 DCHECK(filter_
.get());
823 // Get more pre-filtered data if needed.
824 // TODO(mbelshe): is it possible that the filter needs *MORE* data
825 // when there is some data already in the buffer?
826 if (!filter_
->stream_data_len() && !is_done()) {
827 IOBuffer
* stream_buffer
= filter_
->stream_buffer();
828 int stream_buffer_size
= filter_
->stream_buffer_size();
829 rv
= ReadRawDataHelper(stream_buffer
, stream_buffer_size
, bytes_read
);
834 bool URLRequestJob::ReadRawDataHelper(IOBuffer
* buf
, int buf_size
,
836 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
837 tracked_objects::ScopedTracker
tracking_profile(
838 FROM_HERE_WITH_EXPLICIT_FUNCTION(
839 "423948 URLRequestJob::ReadRawDataHelper"));
841 DCHECK(!request_
->status().is_io_pending());
842 DCHECK(raw_read_buffer_
.get() == NULL
);
844 // Keep a pointer to the read buffer, so we have access to it in the
845 // OnRawReadComplete() callback in the event that the read completes
847 raw_read_buffer_
= buf
;
848 bool rv
= ReadRawData(buf
, buf_size
, bytes_read
);
850 if (!request_
->status().is_io_pending()) {
851 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
852 tracked_objects::ScopedTracker
tracking_profile1(
853 FROM_HERE_WITH_EXPLICIT_FUNCTION(
854 "423948 URLRequestJob::ReadRawDataHelper1"));
856 // If the read completes synchronously, either success or failure,
857 // invoke the OnRawReadComplete callback so we can account for the
859 OnRawReadComplete(*bytes_read
);
864 void URLRequestJob::FollowRedirect(const RedirectInfo
& redirect_info
) {
865 int rv
= request_
->Redirect(redirect_info
);
867 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, rv
));
870 void URLRequestJob::OnRawReadComplete(int bytes_read
) {
871 DCHECK(raw_read_buffer_
.get());
872 // If |filter_| is non-NULL, bytes will be logged after it is applied instead.
873 if (!filter_
.get() && request() && request()->net_log().IsLoggingBytes() &&
875 request()->net_log().AddByteTransferEvent(
876 NetLog::TYPE_URL_REQUEST_JOB_BYTES_READ
,
877 bytes_read
, raw_read_buffer_
->data());
880 if (bytes_read
> 0) {
881 RecordBytesRead(bytes_read
);
883 raw_read_buffer_
= NULL
;
886 void URLRequestJob::RecordBytesRead(int bytes_read
) {
887 filter_input_byte_count_
+= bytes_read
;
888 prefilter_bytes_read_
+= bytes_read
;
890 postfilter_bytes_read_
+= bytes_read
;
891 DVLOG(2) << __FUNCTION__
<< "() "
892 << "\"" << (request_
? request_
->url().spec() : "???") << "\""
893 << " pre bytes read = " << bytes_read
894 << " pre total = " << prefilter_bytes_read_
895 << " post total = " << postfilter_bytes_read_
;
896 UpdatePacketReadTimes(); // Facilitate stats recording if it is active.
897 if (network_delegate_
) {
898 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
899 tracked_objects::ScopedTracker
tracking_profile(
900 FROM_HERE_WITH_EXPLICIT_FUNCTION(
901 "423948 URLRequestJob::RecordBytesRead NotifyRawBytesRead"));
903 network_delegate_
->NotifyRawBytesRead(*request_
, bytes_read
);
907 bool URLRequestJob::FilterHasData() {
908 return filter_
.get() && filter_
->stream_data_len();
911 void URLRequestJob::UpdatePacketReadTimes() {
914 RedirectInfo
URLRequestJob::ComputeRedirectInfo(const GURL
& location
,
915 int http_status_code
) {
916 const GURL
& url
= request_
->url();
918 RedirectInfo redirect_info
;
920 redirect_info
.status_code
= http_status_code
;
922 // The request method may change, depending on the status code.
923 redirect_info
.new_method
=
924 ComputeMethodForRedirect(request_
->method(), http_status_code
);
926 // Move the reference fragment of the old location to the new one if the
927 // new one has none. This duplicates mozilla's behavior.
928 if (url
.is_valid() && url
.has_ref() && !location
.has_ref() &&
929 CopyFragmentOnRedirect(location
)) {
930 GURL::Replacements replacements
;
931 // Reference the |ref| directly out of the original URL to avoid a
933 replacements
.SetRef(url
.spec().data(),
934 url
.parsed_for_possibly_invalid_spec().ref
);
935 redirect_info
.new_url
= location
.ReplaceComponents(replacements
);
937 redirect_info
.new_url
= location
;
940 // Update the first-party URL if appropriate.
941 if (request_
->first_party_url_policy() ==
942 URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT
) {
943 redirect_info
.new_first_party_for_cookies
= redirect_info
.new_url
;
945 redirect_info
.new_first_party_for_cookies
=
946 request_
->first_party_for_cookies();
949 // Alter the referrer if redirecting cross-origin (especially HTTP->HTTPS).
950 redirect_info
.new_referrer
=
951 ComputeReferrerForRedirect(request_
->referrer_policy(),
952 request_
->referrer(),
953 redirect_info
.new_url
).spec();
955 return redirect_info
;