url_request_job.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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.
  4. #include "net/url_request/url_request_job.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/callback_helpers.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/location.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/metrics/histogram_macros.h"
  12. #include "base/strings/string_number_conversions.h"
  13. #include "base/task/single_thread_task_runner.h"
  14. #include "base/threading/thread_task_runner_handle.h"
  15. #include "base/values.h"
  16. #include "net/base/auth.h"
  17. #include "net/base/features.h"
  18. #include "net/base/io_buffer.h"
  19. #include "net/base/load_flags.h"
  20. #include "net/base/load_states.h"
  21. #include "net/base/net_errors.h"
  22. #include "net/base/network_delegate.h"
  23. #include "net/base/proxy_server.h"
  24. #include "net/cert/x509_certificate.h"
  25. #include "net/log/net_log.h"
  26. #include "net/log/net_log_capture_mode.h"
  27. #include "net/log/net_log_event_type.h"
  28. #include "net/log/net_log_with_source.h"
  29. #include "net/nqe/network_quality_estimator.h"
  30. #include "net/ssl/ssl_private_key.h"
  31. #include "net/url_request/redirect_util.h"
  32. #include "net/url_request/url_request_context.h"
  33. namespace net {
  34. namespace {
  35. // Callback for TYPE_URL_REQUEST_FILTERS_SET net-internals event.
  36. base::Value SourceStreamSetParams(SourceStream* source_stream) {
  37. base::Value::Dict event_params;
  38. event_params.Set("filters", source_stream->Description());
  39. return base::Value(std::move(event_params));
  40. }
  41. } // namespace
  42. // Each SourceStreams own the previous SourceStream in the chain, but the
  43. // ultimate source is URLRequestJob, which has other ownership semantics, so
  44. // this class is a proxy for URLRequestJob that is owned by the first stream
  45. // (in dataflow order).
  46. class URLRequestJob::URLRequestJobSourceStream : public SourceStream {
  47. public:
  48. explicit URLRequestJobSourceStream(URLRequestJob* job)
  49. : SourceStream(SourceStream::TYPE_NONE), job_(job) {
  50. DCHECK(job_);
  51. }
  52. URLRequestJobSourceStream(const URLRequestJobSourceStream&) = delete;
  53. URLRequestJobSourceStream& operator=(const URLRequestJobSourceStream&) =
  54. delete;
  55. ~URLRequestJobSourceStream() override = default;
  56. // SourceStream implementation:
  57. int Read(IOBuffer* dest_buffer,
  58. int buffer_size,
  59. CompletionOnceCallback callback) override {
  60. DCHECK(job_);
  61. return job_->ReadRawDataHelper(dest_buffer, buffer_size,
  62. std::move(callback));
  63. }
  64. std::string Description() const override { return std::string(); }
  65. bool MayHaveMoreBytes() const override { return true; }
  66. private:
  67. // It is safe to keep a raw pointer because |job_| owns the last stream which
  68. // indirectly owns |this|. Therefore, |job_| will not be destroyed when |this|
  69. // is alive.
  70. const raw_ptr<URLRequestJob> job_;
  71. };
  72. URLRequestJob::URLRequestJob(URLRequest* request) : request_(request) {}
  73. URLRequestJob::~URLRequestJob() = default;
  74. void URLRequestJob::SetUpload(UploadDataStream* upload) {
  75. }
  76. void URLRequestJob::SetExtraRequestHeaders(const HttpRequestHeaders& headers) {
  77. }
  78. void URLRequestJob::SetPriority(RequestPriority priority) {
  79. }
  80. void URLRequestJob::Kill() {
  81. weak_factory_.InvalidateWeakPtrs();
  82. // Make sure the URLRequest is notified that the job is done. This assumes
  83. // that the URLRequest took care of setting its error status before calling
  84. // Kill().
  85. // TODO(mmenke): The URLRequest is currently deleted before this method
  86. // invokes its async callback whenever this is called by the URLRequest.
  87. // Try to simplify how cancellation works.
  88. NotifyCanceled();
  89. }
  90. // This method passes reads down the filter chain, where they eventually end up
  91. // at URLRequestJobSourceStream::Read, which calls back into
  92. // URLRequestJob::ReadRawData.
  93. int URLRequestJob::Read(IOBuffer* buf, int buf_size) {
  94. DCHECK(buf);
  95. pending_read_buffer_ = buf;
  96. int result = source_stream_->Read(
  97. buf, buf_size,
  98. base::BindOnce(&URLRequestJob::SourceStreamReadComplete,
  99. weak_factory_.GetWeakPtr(), false));
  100. if (result == ERR_IO_PENDING)
  101. return ERR_IO_PENDING;
  102. SourceStreamReadComplete(true, result);
  103. return result;
  104. }
  105. int64_t URLRequestJob::GetTotalReceivedBytes() const {
  106. return 0;
  107. }
  108. int64_t URLRequestJob::GetTotalSentBytes() const {
  109. return 0;
  110. }
  111. LoadState URLRequestJob::GetLoadState() const {
  112. return LOAD_STATE_IDLE;
  113. }
  114. bool URLRequestJob::GetCharset(std::string* charset) {
  115. return false;
  116. }
  117. void URLRequestJob::GetResponseInfo(HttpResponseInfo* info) {
  118. }
  119. void URLRequestJob::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
  120. // Only certain request types return more than just request start times.
  121. }
  122. bool URLRequestJob::GetTransactionRemoteEndpoint(IPEndPoint* endpoint) const {
  123. return false;
  124. }
  125. void URLRequestJob::PopulateNetErrorDetails(NetErrorDetails* details) const {
  126. return;
  127. }
  128. bool URLRequestJob::IsRedirectResponse(GURL* location,
  129. int* http_status_code,
  130. bool* insecure_scheme_was_upgraded) {
  131. // For non-HTTP jobs, headers will be null.
  132. HttpResponseHeaders* headers = request_->response_headers();
  133. if (!headers)
  134. return false;
  135. std::string value;
  136. if (!headers->IsRedirect(&value))
  137. return false;
  138. *insecure_scheme_was_upgraded = false;
  139. *location = request_->url().Resolve(value);
  140. // If this a redirect to HTTP of a request that had the
  141. // 'upgrade-insecure-requests' policy set, upgrade it to HTTPS.
  142. if (request_->upgrade_if_insecure()) {
  143. if (location->SchemeIs("http")) {
  144. *insecure_scheme_was_upgraded = true;
  145. GURL::Replacements replacements;
  146. replacements.SetSchemeStr("https");
  147. *location = location->ReplaceComponents(replacements);
  148. }
  149. }
  150. *http_status_code = headers->response_code();
  151. return true;
  152. }
  153. bool URLRequestJob::CopyFragmentOnRedirect(const GURL& location) const {
  154. return true;
  155. }
  156. bool URLRequestJob::IsSafeRedirect(const GURL& location) {
  157. return true;
  158. }
  159. bool URLRequestJob::NeedsAuth() {
  160. return false;
  161. }
  162. std::unique_ptr<AuthChallengeInfo> URLRequestJob::GetAuthChallengeInfo() {
  163. // This will only be called if NeedsAuth() returns true, in which
  164. // case the derived class should implement this!
  165. NOTREACHED();
  166. return nullptr;
  167. }
  168. void URLRequestJob::SetAuth(const AuthCredentials& credentials) {
  169. // This will only be called if NeedsAuth() returns true, in which
  170. // case the derived class should implement this!
  171. NOTREACHED();
  172. }
  173. void URLRequestJob::CancelAuth() {
  174. // This will only be called if NeedsAuth() returns true, in which
  175. // case the derived class should implement this!
  176. NOTREACHED();
  177. }
  178. void URLRequestJob::ContinueWithCertificate(
  179. scoped_refptr<X509Certificate> client_cert,
  180. scoped_refptr<SSLPrivateKey> client_private_key) {
  181. // The derived class should implement this!
  182. NOTREACHED();
  183. }
  184. void URLRequestJob::ContinueDespiteLastError() {
  185. // Implementations should know how to recover from errors they generate.
  186. // If this code was reached, we are trying to recover from an error that
  187. // we don't know how to recover from.
  188. NOTREACHED();
  189. }
  190. void URLRequestJob::FollowDeferredRedirect(
  191. const absl::optional<std::vector<std::string>>& removed_headers,
  192. const absl::optional<net::HttpRequestHeaders>& modified_headers) {
  193. // OnReceivedRedirect must have been called.
  194. DCHECK(deferred_redirect_info_);
  195. // It is possible that FollowRedirect will delete |this|, so it is not safe to
  196. // pass along a reference to |deferred_redirect_info_|.
  197. absl::optional<RedirectInfo> redirect_info =
  198. std::move(deferred_redirect_info_);
  199. FollowRedirect(*redirect_info, removed_headers, modified_headers);
  200. }
  201. int64_t URLRequestJob::prefilter_bytes_read() const {
  202. return prefilter_bytes_read_;
  203. }
  204. bool URLRequestJob::GetMimeType(std::string* mime_type) const {
  205. return false;
  206. }
  207. int URLRequestJob::GetResponseCode() const {
  208. HttpResponseHeaders* headers = request_->response_headers();
  209. if (!headers)
  210. return -1;
  211. return headers->response_code();
  212. }
  213. IPEndPoint URLRequestJob::GetResponseRemoteEndpoint() const {
  214. return IPEndPoint();
  215. }
  216. void URLRequestJob::NotifyURLRequestDestroyed() {
  217. }
  218. ConnectionAttempts URLRequestJob::GetConnectionAttempts() const {
  219. return {};
  220. }
  221. void URLRequestJob::CloseConnectionOnDestruction() {}
  222. namespace {
  223. // Assuming |url| has already been stripped for use as a referrer, if
  224. // |should_strip_to_origin| is true, this method returns the output of the
  225. // "Strip `url` for use as a referrer" algorithm from the Referrer Policy spec
  226. // with its "origin-only" flag set to true:
  227. // https://w3c.github.io/webappsec-referrer-policy/#strip-url
  228. GURL MaybeStripToOrigin(GURL url, bool should_strip_to_origin) {
  229. if (!should_strip_to_origin)
  230. return url;
  231. return url.DeprecatedGetOriginAsURL();
  232. }
  233. } // namespace
  234. // static
  235. GURL URLRequestJob::ComputeReferrerForPolicy(
  236. ReferrerPolicy policy,
  237. const GURL& original_referrer,
  238. const GURL& destination,
  239. bool* same_origin_out_for_metrics) {
  240. // Here and below, numbered lines are from the Referrer Policy spec's
  241. // "Determine request's referrer" algorithm:
  242. // https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer
  243. //
  244. // 4. Let referrerURL be the result of stripping referrerSource for use as a
  245. // referrer.
  246. GURL stripped_referrer = original_referrer.GetAsReferrer();
  247. // 5. Let referrerOrigin be the result of stripping referrerSource for use as
  248. // a referrer, with the origin-only flag set to true.
  249. //
  250. // (We use a boolean instead of computing the URL right away in order to avoid
  251. // constructing a new GURL when it's not necessary.)
  252. bool should_strip_to_origin = false;
  253. // 6. If the result of serializing referrerURL is a string whose length is
  254. // greater than 4096, set referrerURL to referrerOrigin.
  255. if (stripped_referrer.spec().size() > 4096)
  256. should_strip_to_origin = true;
  257. bool same_origin = url::IsSameOriginWith(original_referrer, destination);
  258. if (same_origin_out_for_metrics)
  259. *same_origin_out_for_metrics = same_origin;
  260. // 7. The user agent MAY alter referrerURL or referrerOrigin at this point to
  261. // enforce arbitrary policy considerations in the interests of minimizing data
  262. // leakage. For example, the user agent could strip the URL down to an origin,
  263. // modify its host, replace it with an empty string, etc.
  264. if (base::FeatureList::IsEnabled(
  265. features::kCapReferrerToOriginOnCrossOrigin) &&
  266. !same_origin) {
  267. should_strip_to_origin = true;
  268. }
  269. bool secure_referrer_but_insecure_destination =
  270. original_referrer.SchemeIsCryptographic() &&
  271. !destination.SchemeIsCryptographic();
  272. switch (policy) {
  273. case ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE:
  274. if (secure_referrer_but_insecure_destination)
  275. return GURL();
  276. return MaybeStripToOrigin(std::move(stripped_referrer),
  277. should_strip_to_origin);
  278. case ReferrerPolicy::REDUCE_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN:
  279. if (secure_referrer_but_insecure_destination)
  280. return GURL();
  281. if (!same_origin)
  282. should_strip_to_origin = true;
  283. return MaybeStripToOrigin(std::move(stripped_referrer),
  284. should_strip_to_origin);
  285. case ReferrerPolicy::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN:
  286. if (!same_origin)
  287. should_strip_to_origin = true;
  288. return MaybeStripToOrigin(std::move(stripped_referrer),
  289. should_strip_to_origin);
  290. case ReferrerPolicy::NEVER_CLEAR:
  291. return MaybeStripToOrigin(std::move(stripped_referrer),
  292. should_strip_to_origin);
  293. case ReferrerPolicy::ORIGIN:
  294. should_strip_to_origin = true;
  295. return MaybeStripToOrigin(std::move(stripped_referrer),
  296. should_strip_to_origin);
  297. case ReferrerPolicy::CLEAR_ON_TRANSITION_CROSS_ORIGIN:
  298. if (!same_origin)
  299. return GURL();
  300. return MaybeStripToOrigin(std::move(stripped_referrer),
  301. should_strip_to_origin);
  302. case ReferrerPolicy::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE:
  303. if (secure_referrer_but_insecure_destination)
  304. return GURL();
  305. should_strip_to_origin = true;
  306. return MaybeStripToOrigin(std::move(stripped_referrer),
  307. should_strip_to_origin);
  308. case ReferrerPolicy::NO_REFERRER:
  309. return GURL();
  310. }
  311. NOTREACHED();
  312. return GURL();
  313. }
  314. int URLRequestJob::NotifyConnected(const TransportInfo& info,
  315. CompletionOnceCallback callback) {
  316. return request_->NotifyConnected(info, std::move(callback));
  317. }
  318. void URLRequestJob::NotifyCertificateRequested(
  319. SSLCertRequestInfo* cert_request_info) {
  320. request_->NotifyCertificateRequested(cert_request_info);
  321. }
  322. void URLRequestJob::NotifySSLCertificateError(int net_error,
  323. const SSLInfo& ssl_info,
  324. bool fatal) {
  325. request_->NotifySSLCertificateError(net_error, ssl_info, fatal);
  326. }
  327. bool URLRequestJob::CanSetCookie(const net::CanonicalCookie& cookie,
  328. CookieOptions* options) const {
  329. return request_->CanSetCookie(cookie, options);
  330. }
  331. void URLRequestJob::NotifyHeadersComplete() {
  332. if (has_handled_response_)
  333. return;
  334. // Initialize to the current time, and let the subclass optionally override
  335. // the time stamps if it has that information. The default request_time is
  336. // set by URLRequest before it calls our Start method.
  337. request_->response_info_.response_time = base::Time::Now();
  338. GetResponseInfo(&request_->response_info_);
  339. request_->OnHeadersComplete();
  340. GURL new_location;
  341. int http_status_code;
  342. bool insecure_scheme_was_upgraded;
  343. if (IsRedirectResponse(&new_location, &http_status_code,
  344. &insecure_scheme_was_upgraded)) {
  345. // Redirect response bodies are not read. Notify the transaction
  346. // so it does not treat being stopped as an error.
  347. DoneReadingRedirectResponse();
  348. // Invalid redirect targets are failed early before
  349. // NotifyReceivedRedirect. This means the delegate can assume that, if it
  350. // accepts the redirect, future calls to OnResponseStarted correspond to
  351. // |redirect_info.new_url|.
  352. int redirect_check_result = CanFollowRedirect(new_location);
  353. if (redirect_check_result != OK) {
  354. OnDone(redirect_check_result, true /* notify_done */);
  355. return;
  356. }
  357. // When notifying the URLRequest::Delegate, it can destroy the request,
  358. // which will destroy |this|. After calling to the URLRequest::Delegate,
  359. // pointer must be checked to see if |this| still exists, and if not, the
  360. // code must return immediately.
  361. base::WeakPtr<URLRequestJob> weak_this(weak_factory_.GetWeakPtr());
  362. RedirectInfo redirect_info = RedirectInfo::ComputeRedirectInfo(
  363. request_->method(), request_->url(), request_->site_for_cookies(),
  364. request_->first_party_url_policy(), request_->referrer_policy(),
  365. request_->referrer(), http_status_code, new_location,
  366. net::RedirectUtil::GetReferrerPolicyHeader(
  367. request_->response_headers()),
  368. insecure_scheme_was_upgraded, CopyFragmentOnRedirect(new_location));
  369. bool defer_redirect = false;
  370. request_->NotifyReceivedRedirect(redirect_info, &defer_redirect);
  371. // Ensure that the request wasn't detached, destroyed, or canceled in
  372. // NotifyReceivedRedirect.
  373. if (!weak_this || request_->failed())
  374. return;
  375. if (defer_redirect) {
  376. deferred_redirect_info_ = std::move(redirect_info);
  377. } else {
  378. FollowRedirect(redirect_info, absl::nullopt, /* removed_headers */
  379. absl::nullopt /* modified_headers */);
  380. }
  381. return;
  382. }
  383. if (NeedsAuth()) {
  384. std::unique_ptr<AuthChallengeInfo> auth_info = GetAuthChallengeInfo();
  385. // Need to check for a NULL auth_info because the server may have failed
  386. // to send a challenge with the 401 response.
  387. if (auth_info) {
  388. request_->NotifyAuthRequired(std::move(auth_info));
  389. // Wait for SetAuth or CancelAuth to be called.
  390. return;
  391. }
  392. }
  393. NotifyFinalHeadersReceived();
  394. // |this| may be destroyed at this point.
  395. }
  396. void URLRequestJob::NotifyFinalHeadersReceived() {
  397. DCHECK(!NeedsAuth() || !GetAuthChallengeInfo());
  398. if (has_handled_response_)
  399. return;
  400. // While the request's status is normally updated in NotifyHeadersComplete(),
  401. // URLRequestHttpJob::CancelAuth() posts a task to invoke this method
  402. // directly, which bypasses that logic.
  403. if (request_->status() == ERR_IO_PENDING)
  404. request_->set_status(OK);
  405. has_handled_response_ = true;
  406. if (request_->status() == OK) {
  407. DCHECK(!source_stream_);
  408. source_stream_ = SetUpSourceStream();
  409. if (!source_stream_) {
  410. OnDone(ERR_CONTENT_DECODING_INIT_FAILED, true /* notify_done */);
  411. return;
  412. }
  413. if (source_stream_->type() == SourceStream::TYPE_NONE) {
  414. // If the subclass didn't set |expected_content_size|, and there are
  415. // headers, and the response body is not compressed, try to get the
  416. // expected content size from the headers.
  417. if (expected_content_size_ == -1 && request_->response_headers()) {
  418. // This sets |expected_content_size_| to its previous value of -1 if
  419. // there's no Content-Length header.
  420. expected_content_size_ =
  421. request_->response_headers()->GetContentLength();
  422. }
  423. } else {
  424. request_->net_log().AddEvent(
  425. NetLogEventType::URL_REQUEST_FILTERS_SET,
  426. [&] { return SourceStreamSetParams(source_stream_.get()); });
  427. }
  428. }
  429. request_->NotifyResponseStarted(OK);
  430. // |this| may be destroyed at this point.
  431. }
  432. void URLRequestJob::ConvertResultToError(int result, Error* error, int* count) {
  433. if (result >= 0) {
  434. *error = OK;
  435. *count = result;
  436. } else {
  437. *error = static_cast<Error>(result);
  438. *count = 0;
  439. }
  440. }
  441. void URLRequestJob::ReadRawDataComplete(int result) {
  442. DCHECK_EQ(ERR_IO_PENDING, request_->status());
  443. DCHECK_NE(ERR_IO_PENDING, result);
  444. // The headers should be complete before reads complete
  445. DCHECK(has_handled_response_);
  446. GatherRawReadStats(result);
  447. // Notify SourceStream.
  448. DCHECK(!read_raw_callback_.is_null());
  449. std::move(read_raw_callback_).Run(result);
  450. // |this| may be destroyed at this point.
  451. }
  452. void URLRequestJob::NotifyStartError(int net_error) {
  453. DCHECK(!has_handled_response_);
  454. DCHECK_EQ(ERR_IO_PENDING, request_->status());
  455. has_handled_response_ = true;
  456. // There may be relevant information in the response info even in the
  457. // error case.
  458. GetResponseInfo(&request_->response_info_);
  459. request_->NotifyResponseStarted(net_error);
  460. // |this| may have been deleted here.
  461. }
  462. void URLRequestJob::OnDone(int net_error, bool notify_done) {
  463. DCHECK_NE(ERR_IO_PENDING, net_error);
  464. DCHECK(!done_) << "Job sending done notification twice";
  465. if (done_)
  466. return;
  467. done_ = true;
  468. // Unless there was an error, we should have at least tried to handle
  469. // the response before getting here.
  470. DCHECK(has_handled_response_ || net_error != OK);
  471. request_->set_is_pending(false);
  472. // With async IO, it's quite possible to have a few outstanding
  473. // requests. We could receive a request to Cancel, followed shortly
  474. // by a successful IO. For tracking the status(), once there is
  475. // an error, we do not change the status back to success. To
  476. // enforce this, only set the status if the job is so far
  477. // successful.
  478. if (!request_->failed()) {
  479. if (net_error != net::OK && net_error != ERR_ABORTED) {
  480. request_->net_log().AddEventWithNetErrorCode(NetLogEventType::FAILED,
  481. net_error);
  482. }
  483. request_->set_status(net_error);
  484. }
  485. if (notify_done) {
  486. // Complete this notification later. This prevents us from re-entering the
  487. // delegate if we're done because of a synchronous call.
  488. base::ThreadTaskRunnerHandle::Get()->PostTask(
  489. FROM_HERE,
  490. base::BindOnce(&URLRequestJob::NotifyDone, weak_factory_.GetWeakPtr()));
  491. }
  492. }
  493. void URLRequestJob::NotifyDone() {
  494. // Check if we should notify the URLRequest that we're done because of an
  495. // error.
  496. if (request_->failed()) {
  497. // We report the error differently depending on whether we've called
  498. // OnResponseStarted yet.
  499. if (has_handled_response_) {
  500. // We signal the error by calling OnReadComplete with a bytes_read of -1.
  501. request_->NotifyReadCompleted(-1);
  502. } else {
  503. has_handled_response_ = true;
  504. // Error code doesn't actually matter here, since the status has already
  505. // been updated.
  506. request_->NotifyResponseStarted(request_->status());
  507. }
  508. }
  509. }
  510. void URLRequestJob::NotifyCanceled() {
  511. if (!done_)
  512. OnDone(ERR_ABORTED, true /* notify_done */);
  513. }
  514. void URLRequestJob::OnCallToDelegate(NetLogEventType type) {
  515. request_->OnCallToDelegate(type);
  516. }
  517. void URLRequestJob::OnCallToDelegateComplete() {
  518. request_->OnCallToDelegateComplete();
  519. }
  520. int URLRequestJob::ReadRawData(IOBuffer* buf, int buf_size) {
  521. return 0;
  522. }
  523. void URLRequestJob::DoneReading() {
  524. // Do nothing.
  525. }
  526. void URLRequestJob::DoneReadingRedirectResponse() {
  527. }
  528. std::unique_ptr<SourceStream> URLRequestJob::SetUpSourceStream() {
  529. return std::make_unique<URLRequestJobSourceStream>(this);
  530. }
  531. void URLRequestJob::SetProxyServer(const ProxyServer& proxy_server) {
  532. request_->proxy_server_ = proxy_server;
  533. }
  534. void URLRequestJob::SourceStreamReadComplete(bool synchronous, int result) {
  535. DCHECK_NE(ERR_IO_PENDING, result);
  536. if (result > 0 && request()->net_log().IsCapturing()) {
  537. request()->net_log().AddByteTransferEvent(
  538. NetLogEventType::URL_REQUEST_JOB_FILTERED_BYTES_READ, result,
  539. pending_read_buffer_->data());
  540. }
  541. pending_read_buffer_ = nullptr;
  542. if (result < 0) {
  543. OnDone(result, !synchronous /* notify_done */);
  544. return;
  545. }
  546. if (result > 0) {
  547. postfilter_bytes_read_ += result;
  548. } else {
  549. DCHECK_EQ(0, result);
  550. DoneReading();
  551. // In the synchronous case, the caller will notify the URLRequest of
  552. // completion. In the async case, the NotifyReadCompleted call will.
  553. // TODO(mmenke): Can this be combined with the error case?
  554. OnDone(OK, false /* notify_done */);
  555. }
  556. if (!synchronous)
  557. request_->NotifyReadCompleted(result);
  558. }
  559. int URLRequestJob::ReadRawDataHelper(IOBuffer* buf,
  560. int buf_size,
  561. CompletionOnceCallback callback) {
  562. DCHECK(!raw_read_buffer_);
  563. // Keep a pointer to the read buffer, so URLRequestJob::GatherRawReadStats()
  564. // has access to it to log stats.
  565. raw_read_buffer_ = buf;
  566. // TODO(xunjieli): Make ReadRawData take in a callback rather than requiring
  567. // subclass to call ReadRawDataComplete upon asynchronous completion.
  568. int result = ReadRawData(buf, buf_size);
  569. if (result != ERR_IO_PENDING) {
  570. // If the read completes synchronously, either success or failure, invoke
  571. // GatherRawReadStats so we can account for the completed read.
  572. GatherRawReadStats(result);
  573. } else {
  574. read_raw_callback_ = std::move(callback);
  575. }
  576. return result;
  577. }
  578. int URLRequestJob::CanFollowRedirect(const GURL& new_url) {
  579. if (request_->redirect_limit_ <= 0) {
  580. DVLOG(1) << "disallowing redirect: exceeds limit";
  581. return ERR_TOO_MANY_REDIRECTS;
  582. }
  583. if (!new_url.is_valid())
  584. return ERR_INVALID_REDIRECT;
  585. if (!IsSafeRedirect(new_url)) {
  586. DVLOG(1) << "disallowing redirect: unsafe protocol";
  587. return ERR_UNSAFE_REDIRECT;
  588. }
  589. return OK;
  590. }
  591. void URLRequestJob::FollowRedirect(
  592. const RedirectInfo& redirect_info,
  593. const absl::optional<std::vector<std::string>>& removed_headers,
  594. const absl::optional<net::HttpRequestHeaders>& modified_headers) {
  595. request_->Redirect(redirect_info, removed_headers, modified_headers);
  596. }
  597. void URLRequestJob::GatherRawReadStats(int bytes_read) {
  598. DCHECK(raw_read_buffer_ || bytes_read == 0);
  599. DCHECK_NE(ERR_IO_PENDING, bytes_read);
  600. if (bytes_read > 0) {
  601. // If there is a filter, bytes will be logged after the filter is applied.
  602. if (source_stream_->type() != SourceStream::TYPE_NONE &&
  603. request()->net_log().IsCapturing()) {
  604. request()->net_log().AddByteTransferEvent(
  605. NetLogEventType::URL_REQUEST_JOB_BYTES_READ, bytes_read,
  606. raw_read_buffer_->data());
  607. }
  608. RecordBytesRead(bytes_read);
  609. }
  610. raw_read_buffer_ = nullptr;
  611. }
  612. void URLRequestJob::RecordBytesRead(int bytes_read) {
  613. DCHECK_GT(bytes_read, 0);
  614. prefilter_bytes_read_ += base::checked_cast<size_t>(bytes_read);
  615. // On first read, notify NetworkQualityEstimator that response headers have
  616. // been received.
  617. // TODO(tbansal): Move this to url_request_http_job.cc. This may catch
  618. // Service Worker jobs twice.
  619. // If prefilter_bytes_read_ is equal to bytes_read, it indicates this is the
  620. // first raw read of the response body. This is used as the signal that
  621. // response headers have been received.
  622. if (request_->context()->network_quality_estimator()) {
  623. if (prefilter_bytes_read() == bytes_read) {
  624. request_->context()->network_quality_estimator()->NotifyHeadersReceived(
  625. *request_, prefilter_bytes_read());
  626. } else {
  627. request_->context()->network_quality_estimator()->NotifyBytesRead(
  628. *request_, prefilter_bytes_read());
  629. }
  630. }
  631. DVLOG(2) << __FUNCTION__ << "() "
  632. << "\"" << request_->url().spec() << "\""
  633. << " pre bytes read = " << bytes_read
  634. << " pre total = " << prefilter_bytes_read()
  635. << " post total = " << postfilter_bytes_read();
  636. }
  637. } // namespace net