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 NetLogCaptureMode
/* capture_mode */) {
31 scoped_ptr
<base::DictionaryValue
> event_params(new base::DictionaryValue());
32 event_params
->SetString("filters", filter
->OrderedFilterList());
33 return event_params
.release();
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_needs_more_output_space_(false),
64 filtered_read_buffer_len_(0),
65 has_handled_response_(false),
66 expected_content_size_(-1),
67 network_delegate_(network_delegate
),
69 base::PowerMonitor
* power_monitor
= base::PowerMonitor::Get();
71 power_monitor
->AddObserver(this);
74 void URLRequestJob::SetUpload(UploadDataStream
* upload
) {
77 void URLRequestJob::SetExtraRequestHeaders(const HttpRequestHeaders
& headers
) {
80 void URLRequestJob::SetPriority(RequestPriority priority
) {
83 void URLRequestJob::Kill() {
84 weak_factory_
.InvalidateWeakPtrs();
85 // Make sure the request is notified that we are done. We assume that the
86 // request took care of setting its error status before calling Kill.
91 void URLRequestJob::DetachRequest() {
95 // This function calls ReadData to get stream data. If a filter exists, passes
96 // the data to the attached filter. Then returns the output from filter back to
98 bool URLRequestJob::Read(IOBuffer
* buf
, int buf_size
, int *bytes_read
) {
101 DCHECK_LT(buf_size
, 1000000); // Sanity check.
104 DCHECK(filtered_read_buffer_
.get() == NULL
);
105 DCHECK_EQ(0, filtered_read_buffer_len_
);
109 // Skip Filter if not present.
110 if (!filter_
.get()) {
111 rv
= ReadRawDataHelper(buf
, buf_size
, bytes_read
);
113 // Save the caller's buffers while we do IO
114 // in the filter's buffers.
115 filtered_read_buffer_
= buf
;
116 filtered_read_buffer_len_
= buf_size
;
118 if (ReadFilteredData(bytes_read
)) {
119 rv
= true; // We have data to return.
121 // It is fine to call DoneReading even if ReadFilteredData receives 0
122 // bytes from the net, but we avoid making that call if we know for
123 // sure that's the case (ReadRawDataHelper path).
124 if (*bytes_read
== 0)
127 rv
= false; // Error, or a new IO is pending.
131 if (rv
&& *bytes_read
== 0)
132 NotifyDone(URLRequestStatus());
136 void URLRequestJob::StopCaching() {
137 // Nothing to do here.
140 bool URLRequestJob::GetFullRequestHeaders(HttpRequestHeaders
* headers
) const {
141 // Most job types don't send request headers.
145 int64
URLRequestJob::GetTotalReceivedBytes() const {
149 LoadState
URLRequestJob::GetLoadState() const {
150 return LOAD_STATE_IDLE
;
153 UploadProgress
URLRequestJob::GetUploadProgress() const {
154 return UploadProgress();
157 bool URLRequestJob::GetCharset(std::string
* charset
) {
161 void URLRequestJob::GetResponseInfo(HttpResponseInfo
* info
) {
164 void URLRequestJob::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
165 // Only certain request types return more than just request start times.
168 bool URLRequestJob::GetResponseCookies(std::vector
<std::string
>* cookies
) {
172 Filter
* URLRequestJob::SetupFilter() const {
176 bool URLRequestJob::IsRedirectResponse(GURL
* location
,
177 int* http_status_code
) {
178 // For non-HTTP jobs, headers will be null.
179 HttpResponseHeaders
* headers
= request_
->response_headers();
184 if (!headers
->IsRedirect(&value
))
187 *location
= request_
->url().Resolve(value
);
188 *http_status_code
= headers
->response_code();
192 bool URLRequestJob::CopyFragmentOnRedirect(const GURL
& location
) const {
196 bool URLRequestJob::IsSafeRedirect(const GURL
& location
) {
200 bool URLRequestJob::NeedsAuth() {
204 void URLRequestJob::GetAuthChallengeInfo(
205 scoped_refptr
<AuthChallengeInfo
>* auth_info
) {
206 // This will only be called if NeedsAuth() returns true, in which
207 // case the derived class should implement this!
211 void URLRequestJob::SetAuth(const AuthCredentials
& credentials
) {
212 // This will only be called if NeedsAuth() returns true, in which
213 // case the derived class should implement this!
217 void URLRequestJob::CancelAuth() {
218 // This will only be called if NeedsAuth() returns true, in which
219 // case the derived class should implement this!
223 void URLRequestJob::ContinueWithCertificate(
224 X509Certificate
* client_cert
) {
225 // The derived class should implement this!
229 void URLRequestJob::ContinueDespiteLastError() {
230 // Implementations should know how to recover from errors they generate.
231 // If this code was reached, we are trying to recover from an error that
232 // we don't know how to recover from.
236 void URLRequestJob::FollowDeferredRedirect() {
237 DCHECK_NE(-1, deferred_redirect_info_
.status_code
);
239 // NOTE: deferred_redirect_info_ may be invalid, and attempting to follow it
240 // will fail inside FollowRedirect. The DCHECK above asserts that we called
241 // OnReceivedRedirect.
243 // It is also possible that FollowRedirect will drop the last reference to
244 // this job, so we need to reset our members before calling it.
246 RedirectInfo redirect_info
= deferred_redirect_info_
;
247 deferred_redirect_info_
= RedirectInfo();
248 FollowRedirect(redirect_info
);
251 void URLRequestJob::ResumeNetworkStart() {
252 // This should only be called for HTTP Jobs, and implemented in the derived
257 bool URLRequestJob::GetMimeType(std::string
* mime_type
) const {
261 int URLRequestJob::GetResponseCode() const {
265 HostPortPair
URLRequestJob::GetSocketAddress() const {
266 return HostPortPair();
269 void URLRequestJob::OnSuspend() {
273 void URLRequestJob::NotifyURLRequestDestroyed() {
276 void URLRequestJob::GetConnectionAttempts(ConnectionAttempts
* out
) const {
281 GURL
URLRequestJob::ComputeReferrerForRedirect(
282 URLRequest::ReferrerPolicy policy
,
283 const std::string
& referrer
,
284 const GURL
& redirect_destination
) {
285 GURL
original_referrer(referrer
);
286 bool secure_referrer_but_insecure_destination
=
287 original_referrer
.SchemeIsCryptographic() &&
288 !redirect_destination
.SchemeIsCryptographic();
290 original_referrer
.GetOrigin() == redirect_destination
.GetOrigin();
292 case URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
:
293 return secure_referrer_but_insecure_destination
? GURL()
296 case URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN
:
298 return original_referrer
;
299 } else if (secure_referrer_but_insecure_destination
) {
302 return original_referrer
.GetOrigin();
305 case URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN
:
306 return same_origin
? original_referrer
: original_referrer
.GetOrigin();
308 case URLRequest::NEVER_CLEAR_REFERRER
:
309 return original_referrer
;
316 URLRequestJob::~URLRequestJob() {
317 base::PowerMonitor
* power_monitor
= base::PowerMonitor::Get();
319 power_monitor
->RemoveObserver(this);
322 void URLRequestJob::NotifyCertificateRequested(
323 SSLCertRequestInfo
* cert_request_info
) {
325 return; // The request was destroyed, so there is no more work to do.
327 request_
->NotifyCertificateRequested(cert_request_info
);
330 void URLRequestJob::NotifySSLCertificateError(const SSLInfo
& ssl_info
,
333 return; // The request was destroyed, so there is no more work to do.
335 request_
->NotifySSLCertificateError(ssl_info
, fatal
);
338 bool URLRequestJob::CanGetCookies(const CookieList
& cookie_list
) const {
340 return false; // The request was destroyed, so there is no more work to do.
342 return request_
->CanGetCookies(cookie_list
);
345 bool URLRequestJob::CanSetCookie(const std::string
& cookie_line
,
346 CookieOptions
* options
) const {
348 return false; // The request was destroyed, so there is no more work to do.
350 return request_
->CanSetCookie(cookie_line
, options
);
353 bool URLRequestJob::CanEnablePrivacyMode() const {
355 return false; // The request was destroyed, so there is no more work to do.
357 return request_
->CanEnablePrivacyMode();
360 void URLRequestJob::NotifyBeforeNetworkStart(bool* defer
) {
364 request_
->NotifyBeforeNetworkStart(defer
);
367 void URLRequestJob::NotifyHeadersComplete() {
368 if (!request_
|| !request_
->has_delegate())
369 return; // The request was destroyed, so there is no more work to do.
371 if (has_handled_response_
)
374 DCHECK(!request_
->status().is_io_pending());
376 // Initialize to the current time, and let the subclass optionally override
377 // the time stamps if it has that information. The default request_time is
378 // set by URLRequest before it calls our Start method.
379 request_
->response_info_
.response_time
= base::Time::Now();
380 GetResponseInfo(&request_
->response_info_
);
382 // When notifying the delegate, the delegate can release the request
383 // (and thus release 'this'). After calling to the delgate, we must
384 // check the request pointer to see if it still exists, and return
385 // immediately if it has been destroyed. self_preservation ensures our
386 // survival until we can get out of this method.
387 scoped_refptr
<URLRequestJob
> self_preservation(this);
390 request_
->OnHeadersComplete();
393 int http_status_code
;
394 if (IsRedirectResponse(&new_location
, &http_status_code
)) {
395 // Redirect response bodies are not read. Notify the transaction
396 // so it does not treat being stopped as an error.
397 DoneReadingRedirectResponse();
399 RedirectInfo redirect_info
=
400 ComputeRedirectInfo(new_location
, http_status_code
);
401 bool defer_redirect
= false;
402 request_
->NotifyReceivedRedirect(redirect_info
, &defer_redirect
);
404 // Ensure that the request wasn't detached or destroyed in
405 // NotifyReceivedRedirect
406 if (!request_
|| !request_
->has_delegate())
409 // If we were not cancelled, then maybe follow the redirect.
410 if (request_
->status().is_success()) {
411 if (defer_redirect
) {
412 deferred_redirect_info_
= redirect_info
;
414 FollowRedirect(redirect_info
);
418 } else if (NeedsAuth()) {
419 scoped_refptr
<AuthChallengeInfo
> auth_info
;
420 GetAuthChallengeInfo(&auth_info
);
422 // Need to check for a NULL auth_info because the server may have failed
423 // to send a challenge with the 401 response.
424 if (auth_info
.get()) {
425 request_
->NotifyAuthRequired(auth_info
.get());
426 // Wait for SetAuth or CancelAuth to be called.
431 has_handled_response_
= true;
432 if (request_
->status().is_success())
433 filter_
.reset(SetupFilter());
435 if (!filter_
.get()) {
436 std::string content_length
;
437 request_
->GetResponseHeaderByName("content-length", &content_length
);
438 if (!content_length
.empty())
439 base::StringToInt64(content_length
, &expected_content_size_
);
441 request_
->net_log().AddEvent(
442 NetLog::TYPE_URL_REQUEST_FILTERS_SET
,
443 base::Bind(&FiltersSetCallback
, base::Unretained(filter_
.get())));
446 request_
->NotifyResponseStarted();
449 void URLRequestJob::NotifyReadComplete(int bytes_read
) {
450 // TODO(cbentzel): Remove ScopedTracker below once crbug.com/475755 is fixed.
451 tracked_objects::ScopedTracker
tracking_profile(
452 FROM_HERE_WITH_EXPLICIT_FUNCTION(
453 "475755 URLRequestJob::NotifyReadComplete"));
455 if (!request_
|| !request_
->has_delegate())
456 return; // The request was destroyed, so there is no more work to do.
458 // TODO(darin): Bug 1004233. Re-enable this test once all of the chrome
459 // unit_tests have been fixed to not trip this.
461 DCHECK(!request_
->status().is_io_pending());
463 // The headers should be complete before reads complete
464 DCHECK(has_handled_response_
);
466 OnRawReadComplete(bytes_read
);
468 // Don't notify if we had an error.
469 if (!request_
->status().is_success())
472 // When notifying the delegate, the delegate can release the request
473 // (and thus release 'this'). After calling to the delegate, we must
474 // check the request pointer to see if it still exists, and return
475 // immediately if it has been destroyed. self_preservation ensures our
476 // survival until we can get out of this method.
477 scoped_refptr
<URLRequestJob
> self_preservation(this);
480 // Tell the filter that it has more data
481 FilteredDataRead(bytes_read
);
484 int filter_bytes_read
= 0;
485 if (ReadFilteredData(&filter_bytes_read
)) {
486 if (!filter_bytes_read
)
488 request_
->NotifyReadCompleted(filter_bytes_read
);
491 request_
->NotifyReadCompleted(bytes_read
);
493 DVLOG(1) << __FUNCTION__
<< "() "
494 << "\"" << (request_
? request_
->url().spec() : "???") << "\""
495 << " pre bytes read = " << bytes_read
496 << " pre total = " << prefilter_bytes_read_
497 << " post total = " << postfilter_bytes_read_
;
500 void URLRequestJob::NotifyStartError(const URLRequestStatus
&status
) {
501 DCHECK(!has_handled_response_
);
502 has_handled_response_
= true;
504 // There may be relevant information in the response info even in the
506 GetResponseInfo(&request_
->response_info_
);
508 request_
->set_status(status
);
509 request_
->NotifyResponseStarted();
510 // We may have been deleted.
514 void URLRequestJob::NotifyDone(const URLRequestStatus
&status
) {
515 DCHECK(!done_
) << "Job sending done notification twice";
520 // Unless there was an error, we should have at least tried to handle
521 // the response before getting here.
522 DCHECK(has_handled_response_
|| !status
.is_success());
524 // As with NotifyReadComplete, we need to take care to notice if we were
525 // destroyed during a delegate callback.
527 request_
->set_is_pending(false);
528 // With async IO, it's quite possible to have a few outstanding
529 // requests. We could receive a request to Cancel, followed shortly
530 // by a successful IO. For tracking the status(), once there is
531 // an error, we do not change the status back to success. To
532 // enforce this, only set the status if the job is so far
534 if (request_
->status().is_success()) {
535 if (status
.status() == URLRequestStatus::FAILED
) {
536 request_
->net_log().AddEventWithNetErrorCode(NetLog::TYPE_FAILED
,
539 request_
->set_status(status
);
543 // Complete this notification later. This prevents us from re-entering the
544 // delegate if we're done because of a synchronous call.
545 base::MessageLoop::current()->PostTask(
547 base::Bind(&URLRequestJob::CompleteNotifyDone
,
548 weak_factory_
.GetWeakPtr()));
551 void URLRequestJob::CompleteNotifyDone() {
552 // Check if we should notify the delegate that we're done because of an error.
554 !request_
->status().is_success() &&
555 request_
->has_delegate()) {
556 // We report the error differently depending on whether we've called
557 // OnResponseStarted yet.
558 if (has_handled_response_
) {
559 // We signal the error by calling OnReadComplete with a bytes_read of -1.
560 request_
->NotifyReadCompleted(-1);
562 has_handled_response_
= true;
563 request_
->NotifyResponseStarted();
568 void URLRequestJob::NotifyCanceled() {
570 NotifyDone(URLRequestStatus(URLRequestStatus::CANCELED
, ERR_ABORTED
));
574 void URLRequestJob::NotifyRestartRequired() {
575 DCHECK(!has_handled_response_
);
576 if (GetStatus().status() != URLRequestStatus::CANCELED
)
580 void URLRequestJob::OnCallToDelegate() {
581 request_
->OnCallToDelegate();
584 void URLRequestJob::OnCallToDelegateComplete() {
585 request_
->OnCallToDelegateComplete();
588 bool URLRequestJob::ReadRawData(IOBuffer
* buf
, int buf_size
,
595 void URLRequestJob::DoneReading() {
599 void URLRequestJob::DoneReadingRedirectResponse() {
602 void URLRequestJob::FilteredDataRead(int bytes_read
) {
604 filter_
->FlushStreamBuffer(bytes_read
);
607 bool URLRequestJob::ReadFilteredData(int* bytes_read
) {
609 DCHECK(filtered_read_buffer_
.get());
610 DCHECK_GT(filtered_read_buffer_len_
, 0);
611 DCHECK_LT(filtered_read_buffer_len_
, 1000000); // Sanity check.
612 DCHECK(!raw_read_buffer_
.get());
621 if (!filter_needs_more_output_space_
&& !filter_
->stream_data_len()) {
622 // We don't have any raw data to work with, so read from the transaction.
623 int filtered_data_read
;
624 if (ReadRawDataForFilter(&filtered_data_read
)) {
625 if (filtered_data_read
> 0) {
626 // Give data to filter.
627 filter_
->FlushStreamBuffer(filtered_data_read
);
632 return false; // IO Pending (or error).
636 if ((filter_
->stream_data_len() || filter_needs_more_output_space_
) &&
638 // Get filtered data.
639 int filtered_data_len
= filtered_read_buffer_len_
;
640 int output_buffer_size
= filtered_data_len
;
641 Filter::FilterStatus status
=
642 filter_
->ReadData(filtered_read_buffer_
->data(), &filtered_data_len
);
644 if (filter_needs_more_output_space_
&& !filtered_data_len
) {
645 // filter_needs_more_output_space_ was mistaken... there are no more
646 // bytes and we should have at least tried to fill up the filter's input
647 // buffer. Correct the state, and try again.
648 filter_needs_more_output_space_
= false;
651 filter_needs_more_output_space_
=
652 (filtered_data_len
== output_buffer_size
);
655 case Filter::FILTER_DONE
: {
656 filter_needs_more_output_space_
= false;
657 *bytes_read
= filtered_data_len
;
658 postfilter_bytes_read_
+= filtered_data_len
;
662 case Filter::FILTER_NEED_MORE_DATA
: {
663 // We have finished filtering all data currently in the buffer.
664 // There might be some space left in the output buffer. One can
665 // consider reading more data from the stream to feed the filter
666 // and filling up the output buffer. This leads to more complicated
667 // buffer management and data notification mechanisms.
668 // We can revisit this issue if there is a real perf need.
669 if (filtered_data_len
> 0) {
670 *bytes_read
= filtered_data_len
;
671 postfilter_bytes_read_
+= filtered_data_len
;
674 // Read again since we haven't received enough data yet (e.g., we
675 // may not have a complete gzip header yet).
680 case Filter::FILTER_OK
: {
681 *bytes_read
= filtered_data_len
;
682 postfilter_bytes_read_
+= filtered_data_len
;
686 case Filter::FILTER_ERROR
: {
687 DVLOG(1) << __FUNCTION__
<< "() "
688 << "\"" << (request_
? request_
->url().spec() : "???")
689 << "\"" << " Filter Error";
690 filter_needs_more_output_space_
= false;
691 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
692 ERR_CONTENT_DECODING_FAILED
));
698 filter_needs_more_output_space_
= false;
704 // If logging all bytes is enabled, log the filtered bytes read.
705 if (rv
&& request() && filtered_data_len
> 0 &&
706 request()->net_log().IsCapturing()) {
707 request()->net_log().AddByteTransferEvent(
708 NetLog::TYPE_URL_REQUEST_JOB_FILTERED_BYTES_READ
, filtered_data_len
,
709 filtered_read_buffer_
->data());
712 // we are done, or there is no data left.
719 // When we successfully finished a read, we no longer need to save the
720 // caller's buffers. Release our reference.
721 filtered_read_buffer_
= NULL
;
722 filtered_read_buffer_len_
= 0;
727 void URLRequestJob::DestroyFilters() {
731 const URLRequestStatus
URLRequestJob::GetStatus() {
733 return request_
->status();
734 // If the request is gone, we must be cancelled.
735 return URLRequestStatus(URLRequestStatus::CANCELED
,
739 void URLRequestJob::SetStatus(const URLRequestStatus
&status
) {
741 request_
->set_status(status
);
744 void URLRequestJob::SetProxyServer(const HostPortPair
& proxy_server
) {
745 request_
->proxy_server_
= proxy_server
;
748 bool URLRequestJob::ReadRawDataForFilter(int* bytes_read
) {
752 DCHECK(filter_
.get());
756 // Get more pre-filtered data if needed.
757 // TODO(mbelshe): is it possible that the filter needs *MORE* data
758 // when there is some data already in the buffer?
759 if (!filter_
->stream_data_len() && !is_done()) {
760 IOBuffer
* stream_buffer
= filter_
->stream_buffer();
761 int stream_buffer_size
= filter_
->stream_buffer_size();
762 rv
= ReadRawDataHelper(stream_buffer
, stream_buffer_size
, bytes_read
);
767 bool URLRequestJob::ReadRawDataHelper(IOBuffer
* buf
, int buf_size
,
769 DCHECK(!request_
->status().is_io_pending());
770 DCHECK(raw_read_buffer_
.get() == NULL
);
772 // Keep a pointer to the read buffer, so we have access to it in the
773 // OnRawReadComplete() callback in the event that the read completes
775 raw_read_buffer_
= buf
;
776 bool rv
= ReadRawData(buf
, buf_size
, bytes_read
);
778 if (!request_
->status().is_io_pending()) {
779 // If the read completes synchronously, either success or failure,
780 // invoke the OnRawReadComplete callback so we can account for the
782 OnRawReadComplete(*bytes_read
);
787 void URLRequestJob::FollowRedirect(const RedirectInfo
& redirect_info
) {
788 int rv
= request_
->Redirect(redirect_info
);
790 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
, rv
));
793 void URLRequestJob::OnRawReadComplete(int bytes_read
) {
794 DCHECK(raw_read_buffer_
.get());
795 // If |filter_| is non-NULL, bytes will be logged after it is applied instead.
796 if (!filter_
.get() && request() && bytes_read
> 0 &&
797 request()->net_log().IsCapturing()) {
798 request()->net_log().AddByteTransferEvent(
799 NetLog::TYPE_URL_REQUEST_JOB_BYTES_READ
,
800 bytes_read
, raw_read_buffer_
->data());
803 if (bytes_read
> 0) {
804 RecordBytesRead(bytes_read
);
806 raw_read_buffer_
= NULL
;
809 void URLRequestJob::RecordBytesRead(int bytes_read
) {
810 prefilter_bytes_read_
+= bytes_read
;
812 postfilter_bytes_read_
+= bytes_read
;
813 DVLOG(2) << __FUNCTION__
<< "() "
814 << "\"" << (request_
? request_
->url().spec() : "???") << "\""
815 << " pre bytes read = " << bytes_read
816 << " pre total = " << prefilter_bytes_read_
817 << " post total = " << postfilter_bytes_read_
;
818 UpdatePacketReadTimes(); // Facilitate stats recording if it is active.
819 if (network_delegate_
)
820 network_delegate_
->NotifyRawBytesRead(*request_
, bytes_read
);
823 bool URLRequestJob::FilterHasData() {
824 return filter_
.get() && filter_
->stream_data_len();
827 void URLRequestJob::UpdatePacketReadTimes() {
830 RedirectInfo
URLRequestJob::ComputeRedirectInfo(const GURL
& location
,
831 int http_status_code
) {
832 const GURL
& url
= request_
->url();
834 RedirectInfo redirect_info
;
836 redirect_info
.status_code
= http_status_code
;
838 // The request method may change, depending on the status code.
839 redirect_info
.new_method
=
840 ComputeMethodForRedirect(request_
->method(), http_status_code
);
842 // Move the reference fragment of the old location to the new one if the
843 // new one has none. This duplicates mozilla's behavior.
844 if (url
.is_valid() && url
.has_ref() && !location
.has_ref() &&
845 CopyFragmentOnRedirect(location
)) {
846 GURL::Replacements replacements
;
847 // Reference the |ref| directly out of the original URL to avoid a
849 replacements
.SetRef(url
.spec().data(),
850 url
.parsed_for_possibly_invalid_spec().ref
);
851 redirect_info
.new_url
= location
.ReplaceComponents(replacements
);
853 redirect_info
.new_url
= location
;
856 // Update the first-party URL if appropriate.
857 if (request_
->first_party_url_policy() ==
858 URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT
) {
859 redirect_info
.new_first_party_for_cookies
= redirect_info
.new_url
;
861 redirect_info
.new_first_party_for_cookies
=
862 request_
->first_party_for_cookies();
865 // Alter the referrer if redirecting cross-origin (especially HTTP->HTTPS).
866 redirect_info
.new_referrer
=
867 ComputeReferrerForRedirect(request_
->referrer_policy(),
868 request_
->referrer(),
869 redirect_info
.new_url
).spec();
871 return redirect_info
;