ChildAccountService: get service flags from AccountTrackerService instead of fetching...
[chromium-blink-merge.git] / net / url_request / url_request_job.cc
blob40672cb3d1781143fd55aba5b8b2fc67542923dd
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"
7 #include "base/bind.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/base/network_quality_estimator.h"
22 #include "net/filter/filter.h"
23 #include "net/http/http_response_headers.h"
24 #include "net/url_request/url_request_context.h"
26 namespace net {
28 namespace {
30 // Callback for TYPE_URL_REQUEST_FILTERS_SET net-internals event.
31 scoped_ptr<base::Value> FiltersSetCallback(
32 Filter* filter,
33 NetLogCaptureMode /* capture_mode */) {
34 scoped_ptr<base::DictionaryValue> event_params(new base::DictionaryValue());
35 event_params->SetString("filters", filter->OrderedFilterList());
36 return event_params.Pass();
39 std::string ComputeMethodForRedirect(const std::string& method,
40 int http_status_code) {
41 // For 303 redirects, all request methods except HEAD are converted to GET,
42 // as per the latest httpbis draft. The draft also allows POST requests to
43 // be converted to GETs when following 301/302 redirects, for historical
44 // reasons. Most major browsers do this and so shall we. Both RFC 2616 and
45 // the httpbis draft say to prompt the user to confirm the generation of new
46 // requests, other than GET and HEAD requests, but IE omits these prompts and
47 // so shall we.
48 // See:
49 // https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#section-7.3
50 if ((http_status_code == 303 && method != "HEAD") ||
51 ((http_status_code == 301 || http_status_code == 302) &&
52 method == "POST")) {
53 return "GET";
55 return method;
58 } // namespace
60 URLRequestJob::URLRequestJob(URLRequest* request,
61 NetworkDelegate* network_delegate)
62 : request_(request),
63 done_(false),
64 prefilter_bytes_read_(0),
65 postfilter_bytes_read_(0),
66 filter_needs_more_output_space_(false),
67 filtered_read_buffer_len_(0),
68 has_handled_response_(false),
69 expected_content_size_(-1),
70 network_delegate_(network_delegate),
71 weak_factory_(this) {
72 base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
73 if (power_monitor)
74 power_monitor->AddObserver(this);
77 void URLRequestJob::SetUpload(UploadDataStream* upload) {
80 void URLRequestJob::SetExtraRequestHeaders(const HttpRequestHeaders& headers) {
83 void URLRequestJob::SetPriority(RequestPriority priority) {
86 void URLRequestJob::Kill() {
87 weak_factory_.InvalidateWeakPtrs();
88 // Make sure the request is notified that we are done. We assume that the
89 // request took care of setting its error status before calling Kill.
90 if (request_)
91 NotifyCanceled();
94 void URLRequestJob::DetachRequest() {
95 request_ = NULL;
98 // This function calls ReadData to get stream data. If a filter exists, passes
99 // the data to the attached filter. Then returns the output from filter back to
100 // the caller.
101 bool URLRequestJob::Read(IOBuffer* buf, int buf_size, int *bytes_read) {
102 bool rv = false;
104 DCHECK_LT(buf_size, 1000000); // Sanity check.
105 DCHECK(buf);
106 DCHECK(bytes_read);
107 DCHECK(filtered_read_buffer_.get() == NULL);
108 DCHECK_EQ(0, filtered_read_buffer_len_);
110 *bytes_read = 0;
112 // Skip Filter if not present.
113 if (!filter_.get()) {
114 rv = ReadRawDataHelper(buf, buf_size, bytes_read);
115 } else {
116 // Save the caller's buffers while we do IO
117 // in the filter's buffers.
118 filtered_read_buffer_ = buf;
119 filtered_read_buffer_len_ = buf_size;
121 if (ReadFilteredData(bytes_read)) {
122 rv = true; // We have data to return.
124 // It is fine to call DoneReading even if ReadFilteredData receives 0
125 // bytes from the net, but we avoid making that call if we know for
126 // sure that's the case (ReadRawDataHelper path).
127 if (*bytes_read == 0)
128 DoneReading();
129 } else {
130 rv = false; // Error, or a new IO is pending.
134 if (rv && *bytes_read == 0)
135 NotifyDone(URLRequestStatus());
136 return rv;
139 void URLRequestJob::StopCaching() {
140 // Nothing to do here.
143 bool URLRequestJob::GetFullRequestHeaders(HttpRequestHeaders* headers) const {
144 // Most job types don't send request headers.
145 return false;
148 int64 URLRequestJob::GetTotalReceivedBytes() const {
149 return 0;
152 LoadState URLRequestJob::GetLoadState() const {
153 return LOAD_STATE_IDLE;
156 UploadProgress URLRequestJob::GetUploadProgress() const {
157 return UploadProgress();
160 bool URLRequestJob::GetCharset(std::string* charset) {
161 return false;
164 void URLRequestJob::GetResponseInfo(HttpResponseInfo* info) {
167 void URLRequestJob::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
168 // Only certain request types return more than just request start times.
171 bool URLRequestJob::GetResponseCookies(std::vector<std::string>* cookies) {
172 return false;
175 Filter* URLRequestJob::SetupFilter() const {
176 return NULL;
179 bool URLRequestJob::IsRedirectResponse(GURL* location,
180 int* http_status_code) {
181 // For non-HTTP jobs, headers will be null.
182 HttpResponseHeaders* headers = request_->response_headers();
183 if (!headers)
184 return false;
186 std::string value;
187 if (!headers->IsRedirect(&value))
188 return false;
190 *location = request_->url().Resolve(value);
191 *http_status_code = headers->response_code();
192 return true;
195 bool URLRequestJob::CopyFragmentOnRedirect(const GURL& location) const {
196 return true;
199 bool URLRequestJob::IsSafeRedirect(const GURL& location) {
200 return true;
203 bool URLRequestJob::NeedsAuth() {
204 return false;
207 void URLRequestJob::GetAuthChallengeInfo(
208 scoped_refptr<AuthChallengeInfo>* auth_info) {
209 // This will only be called if NeedsAuth() returns true, in which
210 // case the derived class should implement this!
211 NOTREACHED();
214 void URLRequestJob::SetAuth(const AuthCredentials& credentials) {
215 // This will only be called if NeedsAuth() returns true, in which
216 // case the derived class should implement this!
217 NOTREACHED();
220 void URLRequestJob::CancelAuth() {
221 // This will only be called if NeedsAuth() returns true, in which
222 // case the derived class should implement this!
223 NOTREACHED();
226 void URLRequestJob::ContinueWithCertificate(
227 X509Certificate* client_cert) {
228 // The derived class should implement this!
229 NOTREACHED();
232 void URLRequestJob::ContinueDespiteLastError() {
233 // Implementations should know how to recover from errors they generate.
234 // If this code was reached, we are trying to recover from an error that
235 // we don't know how to recover from.
236 NOTREACHED();
239 void URLRequestJob::FollowDeferredRedirect() {
240 DCHECK_NE(-1, deferred_redirect_info_.status_code);
242 // NOTE: deferred_redirect_info_ may be invalid, and attempting to follow it
243 // will fail inside FollowRedirect. The DCHECK above asserts that we called
244 // OnReceivedRedirect.
246 // It is also possible that FollowRedirect will drop the last reference to
247 // this job, so we need to reset our members before calling it.
249 RedirectInfo redirect_info = deferred_redirect_info_;
250 deferred_redirect_info_ = RedirectInfo();
251 FollowRedirect(redirect_info);
254 void URLRequestJob::ResumeNetworkStart() {
255 // This should only be called for HTTP Jobs, and implemented in the derived
256 // class.
257 NOTREACHED();
260 bool URLRequestJob::GetMimeType(std::string* mime_type) const {
261 return false;
264 int URLRequestJob::GetResponseCode() const {
265 return -1;
268 HostPortPair URLRequestJob::GetSocketAddress() const {
269 return HostPortPair();
272 void URLRequestJob::OnSuspend() {
273 Kill();
276 void URLRequestJob::NotifyURLRequestDestroyed() {
279 void URLRequestJob::GetConnectionAttempts(ConnectionAttempts* out) const {
280 out->clear();
283 // static
284 GURL URLRequestJob::ComputeReferrerForRedirect(
285 URLRequest::ReferrerPolicy policy,
286 const std::string& referrer,
287 const GURL& redirect_destination) {
288 GURL original_referrer(referrer);
289 bool secure_referrer_but_insecure_destination =
290 original_referrer.SchemeIsCryptographic() &&
291 !redirect_destination.SchemeIsCryptographic();
292 bool same_origin =
293 original_referrer.GetOrigin() == redirect_destination.GetOrigin();
294 switch (policy) {
295 case URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE:
296 return secure_referrer_but_insecure_destination ? GURL()
297 : original_referrer;
299 case URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN:
300 if (same_origin) {
301 return original_referrer;
302 } else if (secure_referrer_but_insecure_destination) {
303 return GURL();
304 } else {
305 return original_referrer.GetOrigin();
308 case URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN:
309 return same_origin ? original_referrer : original_referrer.GetOrigin();
311 case URLRequest::NEVER_CLEAR_REFERRER:
312 return original_referrer;
315 NOTREACHED();
316 return GURL();
319 URLRequestJob::~URLRequestJob() {
320 base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
321 if (power_monitor)
322 power_monitor->RemoveObserver(this);
325 void URLRequestJob::NotifyCertificateRequested(
326 SSLCertRequestInfo* cert_request_info) {
327 if (!request_)
328 return; // The request was destroyed, so there is no more work to do.
330 request_->NotifyCertificateRequested(cert_request_info);
333 void URLRequestJob::NotifySSLCertificateError(const SSLInfo& ssl_info,
334 bool fatal) {
335 if (!request_)
336 return; // The request was destroyed, so there is no more work to do.
338 request_->NotifySSLCertificateError(ssl_info, fatal);
341 bool URLRequestJob::CanGetCookies(const CookieList& cookie_list) const {
342 if (!request_)
343 return false; // The request was destroyed, so there is no more work to do.
345 return request_->CanGetCookies(cookie_list);
348 bool URLRequestJob::CanSetCookie(const std::string& cookie_line,
349 CookieOptions* options) const {
350 if (!request_)
351 return false; // The request was destroyed, so there is no more work to do.
353 return request_->CanSetCookie(cookie_line, options);
356 bool URLRequestJob::CanEnablePrivacyMode() const {
357 if (!request_)
358 return false; // The request was destroyed, so there is no more work to do.
360 return request_->CanEnablePrivacyMode();
363 void URLRequestJob::NotifyBeforeNetworkStart(bool* defer) {
364 if (!request_)
365 return;
367 request_->NotifyBeforeNetworkStart(defer);
370 void URLRequestJob::NotifyHeadersComplete() {
371 if (!request_ || !request_->has_delegate())
372 return; // The request was destroyed, so there is no more work to do.
374 if (has_handled_response_)
375 return;
377 DCHECK(!request_->status().is_io_pending());
379 // Initialize to the current time, and let the subclass optionally override
380 // the time stamps if it has that information. The default request_time is
381 // set by URLRequest before it calls our Start method.
382 request_->response_info_.response_time = base::Time::Now();
383 GetResponseInfo(&request_->response_info_);
385 // When notifying the delegate, the delegate can release the request
386 // (and thus release 'this'). After calling to the delgate, we must
387 // check the request pointer to see if it still exists, and return
388 // immediately if it has been destroyed. self_preservation ensures our
389 // survival until we can get out of this method.
390 scoped_refptr<URLRequestJob> self_preservation(this);
392 if (request_)
393 request_->OnHeadersComplete();
395 GURL new_location;
396 int http_status_code;
397 if (IsRedirectResponse(&new_location, &http_status_code)) {
398 // Redirect response bodies are not read. Notify the transaction
399 // so it does not treat being stopped as an error.
400 DoneReadingRedirectResponse();
402 RedirectInfo redirect_info =
403 ComputeRedirectInfo(new_location, http_status_code);
404 bool defer_redirect = false;
405 request_->NotifyReceivedRedirect(redirect_info, &defer_redirect);
407 // Ensure that the request wasn't detached or destroyed in
408 // NotifyReceivedRedirect
409 if (!request_ || !request_->has_delegate())
410 return;
412 // If we were not cancelled, then maybe follow the redirect.
413 if (request_->status().is_success()) {
414 if (defer_redirect) {
415 deferred_redirect_info_ = redirect_info;
416 } else {
417 FollowRedirect(redirect_info);
419 return;
421 } else if (NeedsAuth()) {
422 scoped_refptr<AuthChallengeInfo> auth_info;
423 GetAuthChallengeInfo(&auth_info);
425 // Need to check for a NULL auth_info because the server may have failed
426 // to send a challenge with the 401 response.
427 if (auth_info.get()) {
428 request_->NotifyAuthRequired(auth_info.get());
429 // Wait for SetAuth or CancelAuth to be called.
430 return;
434 has_handled_response_ = true;
435 if (request_->status().is_success())
436 filter_.reset(SetupFilter());
438 if (!filter_.get()) {
439 std::string content_length;
440 request_->GetResponseHeaderByName("content-length", &content_length);
441 if (!content_length.empty())
442 base::StringToInt64(content_length, &expected_content_size_);
443 } else {
444 request_->net_log().AddEvent(
445 NetLog::TYPE_URL_REQUEST_FILTERS_SET,
446 base::Bind(&FiltersSetCallback, base::Unretained(filter_.get())));
449 request_->NotifyResponseStarted();
452 void URLRequestJob::NotifyReadComplete(int bytes_read) {
453 // TODO(cbentzel): Remove ScopedTracker below once crbug.com/475755 is fixed.
454 tracked_objects::ScopedTracker tracking_profile(
455 FROM_HERE_WITH_EXPLICIT_FUNCTION(
456 "475755 URLRequestJob::NotifyReadComplete"));
458 if (!request_ || !request_->has_delegate())
459 return; // The request was destroyed, so there is no more work to do.
461 // TODO(darin): Bug 1004233. Re-enable this test once all of the chrome
462 // unit_tests have been fixed to not trip this.
463 #if 0
464 DCHECK(!request_->status().is_io_pending());
465 #endif
466 // The headers should be complete before reads complete
467 DCHECK(has_handled_response_);
469 OnRawReadComplete(bytes_read);
471 // Don't notify if we had an error.
472 if (!request_->status().is_success())
473 return;
475 // When notifying the delegate, the delegate can release the request
476 // (and thus release 'this'). After calling to the delegate, we must
477 // check the request pointer to see if it still exists, and return
478 // immediately if it has been destroyed. self_preservation ensures our
479 // survival until we can get out of this method.
480 scoped_refptr<URLRequestJob> self_preservation(this);
482 if (filter_.get()) {
483 // Tell the filter that it has more data
484 FilteredDataRead(bytes_read);
486 // Filter the data.
487 int filter_bytes_read = 0;
488 if (ReadFilteredData(&filter_bytes_read)) {
489 if (!filter_bytes_read)
490 DoneReading();
491 request_->NotifyReadCompleted(filter_bytes_read);
493 } else {
494 request_->NotifyReadCompleted(bytes_read);
496 DVLOG(1) << __FUNCTION__ << "() "
497 << "\"" << (request_ ? request_->url().spec() : "???") << "\""
498 << " pre bytes read = " << bytes_read
499 << " pre total = " << prefilter_bytes_read_
500 << " post total = " << postfilter_bytes_read_;
503 void URLRequestJob::NotifyStartError(const URLRequestStatus &status) {
504 DCHECK(!has_handled_response_);
505 has_handled_response_ = true;
506 if (request_) {
507 // There may be relevant information in the response info even in the
508 // error case.
509 GetResponseInfo(&request_->response_info_);
511 request_->set_status(status);
512 request_->NotifyResponseStarted();
513 // We may have been deleted.
517 void URLRequestJob::NotifyDone(const URLRequestStatus &status) {
518 DCHECK(!done_) << "Job sending done notification twice";
519 if (done_)
520 return;
521 done_ = true;
523 // Unless there was an error, we should have at least tried to handle
524 // the response before getting here.
525 DCHECK(has_handled_response_ || !status.is_success());
527 // As with NotifyReadComplete, we need to take care to notice if we were
528 // destroyed during a delegate callback.
529 if (request_) {
530 request_->set_is_pending(false);
531 // With async IO, it's quite possible to have a few outstanding
532 // requests. We could receive a request to Cancel, followed shortly
533 // by a successful IO. For tracking the status(), once there is
534 // an error, we do not change the status back to success. To
535 // enforce this, only set the status if the job is so far
536 // successful.
537 if (request_->status().is_success()) {
538 if (status.status() == URLRequestStatus::FAILED) {
539 request_->net_log().AddEventWithNetErrorCode(NetLog::TYPE_FAILED,
540 status.error());
542 request_->set_status(status);
546 // Complete this notification later. This prevents us from re-entering the
547 // delegate if we're done because of a synchronous call.
548 base::MessageLoop::current()->PostTask(
549 FROM_HERE,
550 base::Bind(&URLRequestJob::CompleteNotifyDone,
551 weak_factory_.GetWeakPtr()));
554 void URLRequestJob::CompleteNotifyDone() {
555 // Check if we should notify the delegate that we're done because of an error.
556 if (request_ &&
557 !request_->status().is_success() &&
558 request_->has_delegate()) {
559 // We report the error differently depending on whether we've called
560 // OnResponseStarted yet.
561 if (has_handled_response_) {
562 // We signal the error by calling OnReadComplete with a bytes_read of -1.
563 request_->NotifyReadCompleted(-1);
564 } else {
565 has_handled_response_ = true;
566 request_->NotifyResponseStarted();
571 void URLRequestJob::NotifyCanceled() {
572 if (!done_) {
573 NotifyDone(URLRequestStatus(URLRequestStatus::CANCELED, ERR_ABORTED));
577 void URLRequestJob::NotifyRestartRequired() {
578 DCHECK(!has_handled_response_);
579 if (GetStatus().status() != URLRequestStatus::CANCELED)
580 request_->Restart();
583 void URLRequestJob::OnCallToDelegate() {
584 request_->OnCallToDelegate();
587 void URLRequestJob::OnCallToDelegateComplete() {
588 request_->OnCallToDelegateComplete();
591 bool URLRequestJob::ReadRawData(IOBuffer* buf, int buf_size,
592 int *bytes_read) {
593 DCHECK(bytes_read);
594 *bytes_read = 0;
595 return true;
598 void URLRequestJob::DoneReading() {
599 // Do nothing.
602 void URLRequestJob::DoneReadingRedirectResponse() {
605 void URLRequestJob::FilteredDataRead(int bytes_read) {
606 DCHECK(filter_);
607 filter_->FlushStreamBuffer(bytes_read);
610 bool URLRequestJob::ReadFilteredData(int* bytes_read) {
611 DCHECK(filter_);
612 DCHECK(filtered_read_buffer_.get());
613 DCHECK_GT(filtered_read_buffer_len_, 0);
614 DCHECK_LT(filtered_read_buffer_len_, 1000000); // Sanity check.
615 DCHECK(!raw_read_buffer_.get());
617 *bytes_read = 0;
618 bool rv = false;
620 for (;;) {
621 if (is_done())
622 return true;
624 if (!filter_needs_more_output_space_ && !filter_->stream_data_len()) {
625 // We don't have any raw data to work with, so read from the transaction.
626 int filtered_data_read;
627 if (ReadRawDataForFilter(&filtered_data_read)) {
628 if (filtered_data_read > 0) {
629 // Give data to filter.
630 filter_->FlushStreamBuffer(filtered_data_read);
631 } else {
632 return true; // EOF.
634 } else {
635 return false; // IO Pending (or error).
639 if ((filter_->stream_data_len() || filter_needs_more_output_space_) &&
640 !is_done()) {
641 // Get filtered data.
642 int filtered_data_len = filtered_read_buffer_len_;
643 int output_buffer_size = filtered_data_len;
644 Filter::FilterStatus status =
645 filter_->ReadData(filtered_read_buffer_->data(), &filtered_data_len);
647 if (filter_needs_more_output_space_ && !filtered_data_len) {
648 // filter_needs_more_output_space_ was mistaken... there are no more
649 // bytes and we should have at least tried to fill up the filter's input
650 // buffer. Correct the state, and try again.
651 filter_needs_more_output_space_ = false;
652 continue;
654 filter_needs_more_output_space_ =
655 (filtered_data_len == output_buffer_size);
657 switch (status) {
658 case Filter::FILTER_DONE: {
659 filter_needs_more_output_space_ = false;
660 *bytes_read = filtered_data_len;
661 postfilter_bytes_read_ += filtered_data_len;
662 rv = true;
663 break;
665 case Filter::FILTER_NEED_MORE_DATA: {
666 // We have finished filtering all data currently in the buffer.
667 // There might be some space left in the output buffer. One can
668 // consider reading more data from the stream to feed the filter
669 // and filling up the output buffer. This leads to more complicated
670 // buffer management and data notification mechanisms.
671 // We can revisit this issue if there is a real perf need.
672 if (filtered_data_len > 0) {
673 *bytes_read = filtered_data_len;
674 postfilter_bytes_read_ += filtered_data_len;
675 rv = true;
676 } else {
677 // Read again since we haven't received enough data yet (e.g., we
678 // may not have a complete gzip header yet).
679 continue;
681 break;
683 case Filter::FILTER_OK: {
684 *bytes_read = filtered_data_len;
685 postfilter_bytes_read_ += filtered_data_len;
686 rv = true;
687 break;
689 case Filter::FILTER_ERROR: {
690 DVLOG(1) << __FUNCTION__ << "() "
691 << "\"" << (request_ ? request_->url().spec() : "???")
692 << "\"" << " Filter Error";
693 filter_needs_more_output_space_ = false;
694 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
695 ERR_CONTENT_DECODING_FAILED));
696 rv = false;
697 break;
699 default: {
700 NOTREACHED();
701 filter_needs_more_output_space_ = false;
702 rv = false;
703 break;
707 // If logging all bytes is enabled, log the filtered bytes read.
708 if (rv && request() && filtered_data_len > 0 &&
709 request()->net_log().IsCapturing()) {
710 request()->net_log().AddByteTransferEvent(
711 NetLog::TYPE_URL_REQUEST_JOB_FILTERED_BYTES_READ, filtered_data_len,
712 filtered_read_buffer_->data());
714 } else {
715 // we are done, or there is no data left.
716 rv = true;
718 break;
721 if (rv) {
722 // When we successfully finished a read, we no longer need to save the
723 // caller's buffers. Release our reference.
724 filtered_read_buffer_ = NULL;
725 filtered_read_buffer_len_ = 0;
727 return rv;
730 void URLRequestJob::DestroyFilters() {
731 filter_.reset();
734 const URLRequestStatus URLRequestJob::GetStatus() {
735 if (request_)
736 return request_->status();
737 // If the request is gone, we must be cancelled.
738 return URLRequestStatus(URLRequestStatus::CANCELED,
739 ERR_ABORTED);
742 void URLRequestJob::SetStatus(const URLRequestStatus &status) {
743 if (request_)
744 request_->set_status(status);
747 void URLRequestJob::SetProxyServer(const HostPortPair& proxy_server) {
748 request_->proxy_server_ = proxy_server;
751 bool URLRequestJob::ReadRawDataForFilter(int* bytes_read) {
752 bool rv = false;
754 DCHECK(bytes_read);
755 DCHECK(filter_.get());
757 *bytes_read = 0;
759 // Get more pre-filtered data if needed.
760 // TODO(mbelshe): is it possible that the filter needs *MORE* data
761 // when there is some data already in the buffer?
762 if (!filter_->stream_data_len() && !is_done()) {
763 IOBuffer* stream_buffer = filter_->stream_buffer();
764 int stream_buffer_size = filter_->stream_buffer_size();
765 rv = ReadRawDataHelper(stream_buffer, stream_buffer_size, bytes_read);
767 return rv;
770 bool URLRequestJob::ReadRawDataHelper(IOBuffer* buf, int buf_size,
771 int* bytes_read) {
772 DCHECK(!request_->status().is_io_pending());
773 DCHECK(raw_read_buffer_.get() == NULL);
775 // Keep a pointer to the read buffer, so we have access to it in the
776 // OnRawReadComplete() callback in the event that the read completes
777 // asynchronously.
778 raw_read_buffer_ = buf;
779 bool rv = ReadRawData(buf, buf_size, bytes_read);
781 if (!request_->status().is_io_pending()) {
782 // If the read completes synchronously, either success or failure,
783 // invoke the OnRawReadComplete callback so we can account for the
784 // completed read.
785 OnRawReadComplete(*bytes_read);
787 return rv;
790 void URLRequestJob::FollowRedirect(const RedirectInfo& redirect_info) {
791 int rv = request_->Redirect(redirect_info);
792 if (rv != OK)
793 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
796 void URLRequestJob::OnRawReadComplete(int bytes_read) {
797 DCHECK(raw_read_buffer_.get());
798 // If |filter_| is non-NULL, bytes will be logged after it is applied instead.
799 if (!filter_.get() && request() && bytes_read > 0 &&
800 request()->net_log().IsCapturing()) {
801 request()->net_log().AddByteTransferEvent(
802 NetLog::TYPE_URL_REQUEST_JOB_BYTES_READ,
803 bytes_read, raw_read_buffer_->data());
806 if (bytes_read > 0) {
807 RecordBytesRead(bytes_read);
809 raw_read_buffer_ = NULL;
812 void URLRequestJob::RecordBytesRead(int bytes_read) {
813 DCHECK_GT(bytes_read, 0);
814 prefilter_bytes_read_ += bytes_read;
816 // Notify NetworkQualityEstimator.
817 // TODO(tbansal): Move this to url_request_http_job.cc. This may catch
818 // Service Worker jobs twice.
819 if (request_ && request_->context()->network_quality_estimator()) {
820 request_->context()->network_quality_estimator()->NotifyDataReceived(
821 *request_, prefilter_bytes_read_);
824 if (!filter_.get())
825 postfilter_bytes_read_ += bytes_read;
826 DVLOG(2) << __FUNCTION__ << "() "
827 << "\"" << (request_ ? request_->url().spec() : "???") << "\""
828 << " pre bytes read = " << bytes_read
829 << " pre total = " << prefilter_bytes_read_
830 << " post total = " << postfilter_bytes_read_;
831 UpdatePacketReadTimes(); // Facilitate stats recording if it is active.
832 if (network_delegate_)
833 network_delegate_->NotifyRawBytesRead(*request_, bytes_read);
836 bool URLRequestJob::FilterHasData() {
837 return filter_.get() && filter_->stream_data_len();
840 void URLRequestJob::UpdatePacketReadTimes() {
843 RedirectInfo URLRequestJob::ComputeRedirectInfo(const GURL& location,
844 int http_status_code) {
845 const GURL& url = request_->url();
847 RedirectInfo redirect_info;
849 redirect_info.status_code = http_status_code;
851 // The request method may change, depending on the status code.
852 redirect_info.new_method =
853 ComputeMethodForRedirect(request_->method(), http_status_code);
855 // Move the reference fragment of the old location to the new one if the
856 // new one has none. This duplicates mozilla's behavior.
857 if (url.is_valid() && url.has_ref() && !location.has_ref() &&
858 CopyFragmentOnRedirect(location)) {
859 GURL::Replacements replacements;
860 // Reference the |ref| directly out of the original URL to avoid a
861 // malloc.
862 replacements.SetRef(url.spec().data(),
863 url.parsed_for_possibly_invalid_spec().ref);
864 redirect_info.new_url = location.ReplaceComponents(replacements);
865 } else {
866 redirect_info.new_url = location;
869 // Update the first-party URL if appropriate.
870 if (request_->first_party_url_policy() ==
871 URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT) {
872 redirect_info.new_first_party_for_cookies = redirect_info.new_url;
873 } else {
874 redirect_info.new_first_party_for_cookies =
875 request_->first_party_for_cookies();
878 // Alter the referrer if redirecting cross-origin (especially HTTP->HTTPS).
879 redirect_info.new_referrer =
880 ComputeReferrerForRedirect(request_->referrer_policy(),
881 request_->referrer(),
882 redirect_info.new_url).spec();
884 return redirect_info;
887 } // namespace net