url_request.cc 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "base/callback_helpers.h"
  9. #include "base/compiler_specific.h"
  10. #include "base/metrics/histogram_macros.h"
  11. #include "base/rand_util.h"
  12. #include "base/strings/utf_string_conversions.h"
  13. #include "base/synchronization/lock.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/load_flags.h"
  18. #include "net/base/load_timing_info.h"
  19. #include "net/base/net_errors.h"
  20. #include "net/base/network_change_notifier.h"
  21. #include "net/base/network_delegate.h"
  22. #include "net/base/upload_data_stream.h"
  23. #include "net/cert/x509_certificate.h"
  24. #include "net/cookies/cookie_store.h"
  25. #include "net/cookies/cookie_util.h"
  26. #include "net/dns/public/secure_dns_policy.h"
  27. #include "net/http/http_log_util.h"
  28. #include "net/http/http_util.h"
  29. #include "net/log/net_log.h"
  30. #include "net/log/net_log_event_type.h"
  31. #include "net/log/net_log_source_type.h"
  32. #include "net/socket/next_proto.h"
  33. #include "net/ssl/ssl_cert_request_info.h"
  34. #include "net/ssl/ssl_private_key.h"
  35. #include "net/url_request/redirect_info.h"
  36. #include "net/url_request/redirect_util.h"
  37. #include "net/url_request/url_request_context.h"
  38. #include "net/url_request/url_request_error_job.h"
  39. #include "net/url_request/url_request_job.h"
  40. #include "net/url_request/url_request_job_factory.h"
  41. #include "net/url_request/url_request_netlog_params.h"
  42. #include "net/url_request/url_request_redirect_job.h"
  43. #include "url/gurl.h"
  44. #include "url/origin.h"
  45. using base::Time;
  46. using std::string;
  47. namespace net {
  48. namespace {
  49. // True once the first URLRequest was started.
  50. bool g_url_requests_started = false;
  51. // True if cookies are accepted by default.
  52. bool g_default_can_use_cookies = true;
  53. // When the URLRequest first assempts load timing information, it has the times
  54. // at which each event occurred. The API requires the time which the request
  55. // was blocked on each phase. This function handles the conversion.
  56. //
  57. // In the case of reusing a SPDY session, old proxy results may have been
  58. // reused, so proxy resolution times may be before the request was started.
  59. //
  60. // Due to preconnect and late binding, it is also possible for the connection
  61. // attempt to start before a request has been started, or proxy resolution
  62. // completed.
  63. //
  64. // This functions fixes both those cases.
  65. void ConvertRealLoadTimesToBlockingTimes(LoadTimingInfo* load_timing_info) {
  66. DCHECK(!load_timing_info->request_start.is_null());
  67. // Earliest time possible for the request to be blocking on connect events.
  68. base::TimeTicks block_on_connect = load_timing_info->request_start;
  69. if (!load_timing_info->proxy_resolve_start.is_null()) {
  70. DCHECK(!load_timing_info->proxy_resolve_end.is_null());
  71. // Make sure the proxy times are after request start.
  72. if (load_timing_info->proxy_resolve_start < load_timing_info->request_start)
  73. load_timing_info->proxy_resolve_start = load_timing_info->request_start;
  74. if (load_timing_info->proxy_resolve_end < load_timing_info->request_start)
  75. load_timing_info->proxy_resolve_end = load_timing_info->request_start;
  76. // Connect times must also be after the proxy times.
  77. block_on_connect = load_timing_info->proxy_resolve_end;
  78. }
  79. if (!load_timing_info->receive_headers_start.is_null() &&
  80. load_timing_info->receive_headers_start < block_on_connect) {
  81. load_timing_info->receive_headers_start = block_on_connect;
  82. }
  83. if (!load_timing_info->receive_non_informational_headers_start.is_null() &&
  84. load_timing_info->receive_non_informational_headers_start <
  85. block_on_connect) {
  86. load_timing_info->receive_non_informational_headers_start =
  87. block_on_connect;
  88. }
  89. // Make sure connection times are after start and proxy times.
  90. LoadTimingInfo::ConnectTiming* connect_timing =
  91. &load_timing_info->connect_timing;
  92. if (!connect_timing->dns_start.is_null()) {
  93. DCHECK(!connect_timing->dns_end.is_null());
  94. if (connect_timing->dns_start < block_on_connect)
  95. connect_timing->dns_start = block_on_connect;
  96. if (connect_timing->dns_end < block_on_connect)
  97. connect_timing->dns_end = block_on_connect;
  98. }
  99. if (!connect_timing->connect_start.is_null()) {
  100. DCHECK(!connect_timing->connect_end.is_null());
  101. if (connect_timing->connect_start < block_on_connect)
  102. connect_timing->connect_start = block_on_connect;
  103. if (connect_timing->connect_end < block_on_connect)
  104. connect_timing->connect_end = block_on_connect;
  105. }
  106. if (!connect_timing->ssl_start.is_null()) {
  107. DCHECK(!connect_timing->ssl_end.is_null());
  108. if (connect_timing->ssl_start < block_on_connect)
  109. connect_timing->ssl_start = block_on_connect;
  110. if (connect_timing->ssl_end < block_on_connect)
  111. connect_timing->ssl_end = block_on_connect;
  112. }
  113. }
  114. NetLogWithSource CreateNetLogWithSource(
  115. NetLog* net_log,
  116. absl::optional<net::NetLogSource> net_log_source) {
  117. if (net_log_source) {
  118. return NetLogWithSource::Make(net_log, net_log_source.value());
  119. }
  120. return NetLogWithSource::Make(net_log, NetLogSourceType::URL_REQUEST);
  121. }
  122. } // namespace
  123. ///////////////////////////////////////////////////////////////////////////////
  124. // URLRequest::Delegate
  125. int URLRequest::Delegate::OnConnected(URLRequest* request,
  126. const TransportInfo& info,
  127. CompletionOnceCallback callback) {
  128. return OK;
  129. }
  130. void URLRequest::Delegate::OnReceivedRedirect(URLRequest* request,
  131. const RedirectInfo& redirect_info,
  132. bool* defer_redirect) {}
  133. void URLRequest::Delegate::OnAuthRequired(URLRequest* request,
  134. const AuthChallengeInfo& auth_info) {
  135. request->CancelAuth();
  136. }
  137. void URLRequest::Delegate::OnCertificateRequested(
  138. URLRequest* request,
  139. SSLCertRequestInfo* cert_request_info) {
  140. request->CancelWithError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
  141. }
  142. void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request,
  143. int net_error,
  144. const SSLInfo& ssl_info,
  145. bool is_hsts_ok) {
  146. request->Cancel();
  147. }
  148. void URLRequest::Delegate::OnResponseStarted(URLRequest* request,
  149. int net_error) {
  150. NOTREACHED();
  151. }
  152. ///////////////////////////////////////////////////////////////////////////////
  153. // URLRequest
  154. URLRequest::~URLRequest() {
  155. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  156. // Log the redirect count during destruction, to ensure that it is only
  157. // recorded at the end of following all redirect chains.
  158. UMA_HISTOGRAM_EXACT_LINEAR("Net.RedirectChainLength",
  159. kMaxRedirects - redirect_limit_,
  160. kMaxRedirects + 1);
  161. Cancel();
  162. if (network_delegate()) {
  163. network_delegate()->NotifyURLRequestDestroyed(this);
  164. if (job_.get())
  165. job_->NotifyURLRequestDestroyed();
  166. }
  167. // Delete job before |this|, since subclasses may do weird things, like depend
  168. // on UserData associated with |this| and poke at it during teardown.
  169. job_.reset();
  170. DCHECK_EQ(1u, context_->url_requests()->count(this));
  171. context_->url_requests()->erase(this);
  172. int net_error = OK;
  173. // Log error only on failure, not cancellation, as even successful requests
  174. // are "cancelled" on destruction.
  175. if (status_ != ERR_ABORTED)
  176. net_error = status_;
  177. net_log_.EndEventWithNetErrorCode(NetLogEventType::REQUEST_ALIVE, net_error);
  178. }
  179. void URLRequest::set_upload(std::unique_ptr<UploadDataStream> upload) {
  180. upload_data_stream_ = std::move(upload);
  181. }
  182. const UploadDataStream* URLRequest::get_upload_for_testing() const {
  183. return upload_data_stream_.get();
  184. }
  185. bool URLRequest::has_upload() const {
  186. return upload_data_stream_.get() != nullptr;
  187. }
  188. void URLRequest::SetExtraRequestHeaderByName(const string& name,
  189. const string& value,
  190. bool overwrite) {
  191. DCHECK(!is_pending_ || is_redirecting_);
  192. if (overwrite) {
  193. extra_request_headers_.SetHeader(name, value);
  194. } else {
  195. extra_request_headers_.SetHeaderIfMissing(name, value);
  196. }
  197. }
  198. void URLRequest::RemoveRequestHeaderByName(const string& name) {
  199. DCHECK(!is_pending_ || is_redirecting_);
  200. extra_request_headers_.RemoveHeader(name);
  201. }
  202. void URLRequest::SetExtraRequestHeaders(const HttpRequestHeaders& headers) {
  203. DCHECK(!is_pending_);
  204. extra_request_headers_ = headers;
  205. // NOTE: This method will likely become non-trivial once the other setters
  206. // for request headers are implemented.
  207. }
  208. int64_t URLRequest::GetTotalReceivedBytes() const {
  209. if (!job_.get())
  210. return 0;
  211. return job_->GetTotalReceivedBytes();
  212. }
  213. int64_t URLRequest::GetTotalSentBytes() const {
  214. if (!job_.get())
  215. return 0;
  216. return job_->GetTotalSentBytes();
  217. }
  218. int64_t URLRequest::GetRawBodyBytes() const {
  219. if (!job_.get())
  220. return 0;
  221. return job_->prefilter_bytes_read();
  222. }
  223. LoadStateWithParam URLRequest::GetLoadState() const {
  224. // The !blocked_by_.empty() check allows |this| to report it's blocked on a
  225. // delegate before it has been started.
  226. if (calling_delegate_ || !blocked_by_.empty()) {
  227. return LoadStateWithParam(LOAD_STATE_WAITING_FOR_DELEGATE,
  228. use_blocked_by_as_load_param_
  229. ? base::UTF8ToUTF16(blocked_by_)
  230. : std::u16string());
  231. }
  232. return LoadStateWithParam(job_.get() ? job_->GetLoadState() : LOAD_STATE_IDLE,
  233. std::u16string());
  234. }
  235. base::Value URLRequest::GetStateAsValue() const {
  236. base::Value::Dict dict;
  237. dict.Set("url", original_url().possibly_invalid_spec());
  238. if (url_chain_.size() > 1) {
  239. base::Value list(base::Value::Type::LIST);
  240. for (const GURL& url : url_chain_) {
  241. list.Append(url.possibly_invalid_spec());
  242. }
  243. dict.Set("url_chain", std::move(list));
  244. }
  245. dict.Set("load_flags", load_flags_);
  246. LoadStateWithParam load_state = GetLoadState();
  247. dict.Set("load_state", load_state.state);
  248. if (!load_state.param.empty())
  249. dict.Set("load_state_param", load_state.param);
  250. if (!blocked_by_.empty())
  251. dict.Set("delegate_blocked_by", blocked_by_);
  252. dict.Set("method", method_);
  253. dict.Set("network_isolation_key",
  254. isolation_info_.network_isolation_key().ToDebugString());
  255. dict.Set("has_upload", has_upload());
  256. dict.Set("is_pending", is_pending_);
  257. dict.Set("traffic_annotation", traffic_annotation_.unique_id_hash_code);
  258. if (status_ != OK)
  259. dict.Set("net_error", status_);
  260. return base::Value(std::move(dict));
  261. }
  262. void URLRequest::LogBlockedBy(const char* blocked_by) {
  263. DCHECK(blocked_by);
  264. DCHECK_GT(strlen(blocked_by), 0u);
  265. // Only log information to NetLog during startup and certain deferring calls
  266. // to delegates. For all reads but the first, do nothing.
  267. if (!calling_delegate_ && !response_info_.request_time.is_null())
  268. return;
  269. LogUnblocked();
  270. blocked_by_ = blocked_by;
  271. use_blocked_by_as_load_param_ = false;
  272. net_log_.BeginEventWithStringParams(NetLogEventType::DELEGATE_INFO,
  273. "delegate_blocked_by", blocked_by_);
  274. }
  275. void URLRequest::LogAndReportBlockedBy(const char* source) {
  276. LogBlockedBy(source);
  277. use_blocked_by_as_load_param_ = true;
  278. }
  279. void URLRequest::LogUnblocked() {
  280. if (blocked_by_.empty())
  281. return;
  282. net_log_.EndEvent(NetLogEventType::DELEGATE_INFO);
  283. blocked_by_.clear();
  284. }
  285. UploadProgress URLRequest::GetUploadProgress() const {
  286. if (!job_.get()) {
  287. // We haven't started or the request was cancelled
  288. return UploadProgress();
  289. }
  290. if (final_upload_progress_.position()) {
  291. // The first job completed and none of the subsequent series of
  292. // GETs when following redirects will upload anything, so we return the
  293. // cached results from the initial job, the POST.
  294. return final_upload_progress_;
  295. }
  296. if (upload_data_stream_)
  297. return upload_data_stream_->GetUploadProgress();
  298. return UploadProgress();
  299. }
  300. void URLRequest::GetResponseHeaderByName(const string& name,
  301. string* value) const {
  302. DCHECK(value);
  303. if (response_info_.headers.get()) {
  304. response_info_.headers->GetNormalizedHeader(name, value);
  305. } else {
  306. value->clear();
  307. }
  308. }
  309. IPEndPoint URLRequest::GetResponseRemoteEndpoint() const {
  310. DCHECK(job_.get());
  311. return job_->GetResponseRemoteEndpoint();
  312. }
  313. HttpResponseHeaders* URLRequest::response_headers() const {
  314. return response_info_.headers.get();
  315. }
  316. const absl::optional<AuthChallengeInfo>& URLRequest::auth_challenge_info()
  317. const {
  318. return response_info_.auth_challenge;
  319. }
  320. void URLRequest::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
  321. *load_timing_info = load_timing_info_;
  322. }
  323. void URLRequest::PopulateNetErrorDetails(NetErrorDetails* details) const {
  324. if (!job_)
  325. return;
  326. return job_->PopulateNetErrorDetails(details);
  327. }
  328. bool URLRequest::GetTransactionRemoteEndpoint(IPEndPoint* endpoint) const {
  329. if (!job_)
  330. return false;
  331. return job_->GetTransactionRemoteEndpoint(endpoint);
  332. }
  333. void URLRequest::GetMimeType(string* mime_type) const {
  334. DCHECK(job_.get());
  335. job_->GetMimeType(mime_type);
  336. }
  337. void URLRequest::GetCharset(string* charset) const {
  338. DCHECK(job_.get());
  339. job_->GetCharset(charset);
  340. }
  341. int URLRequest::GetResponseCode() const {
  342. DCHECK(job_.get());
  343. return job_->GetResponseCode();
  344. }
  345. void URLRequest::set_maybe_sent_cookies(CookieAccessResultList cookies) {
  346. maybe_sent_cookies_ = std::move(cookies);
  347. }
  348. void URLRequest::set_maybe_stored_cookies(
  349. CookieAndLineAccessResultList cookies) {
  350. maybe_stored_cookies_ = std::move(cookies);
  351. }
  352. void URLRequest::SetLoadFlags(int flags) {
  353. if ((load_flags_ & LOAD_IGNORE_LIMITS) != (flags & LOAD_IGNORE_LIMITS)) {
  354. DCHECK(!job_.get());
  355. DCHECK(flags & LOAD_IGNORE_LIMITS);
  356. DCHECK_EQ(priority_, MAXIMUM_PRIORITY);
  357. }
  358. load_flags_ = flags;
  359. // This should be a no-op given the above DCHECKs, but do this
  360. // anyway for release mode.
  361. if ((load_flags_ & LOAD_IGNORE_LIMITS) != 0)
  362. SetPriority(MAXIMUM_PRIORITY);
  363. }
  364. void URLRequest::SetSecureDnsPolicy(SecureDnsPolicy secure_dns_policy) {
  365. secure_dns_policy_ = secure_dns_policy;
  366. }
  367. // static
  368. void URLRequest::SetDefaultCookiePolicyToBlock() {
  369. CHECK(!g_url_requests_started);
  370. g_default_can_use_cookies = false;
  371. }
  372. void URLRequest::SetURLChain(const std::vector<GURL>& url_chain) {
  373. DCHECK(!job_);
  374. DCHECK(!is_pending_);
  375. DCHECK_EQ(url_chain_.size(), 1u);
  376. if (url_chain.size() < 2)
  377. return;
  378. // In most cases the current request URL will match the last URL in the
  379. // explicitly set URL chain. In some cases, however, a throttle will modify
  380. // the request URL resulting in a different request URL. We handle this by
  381. // using previous values from the explicitly set URL chain, but with the
  382. // request URL as the final entry in the chain.
  383. url_chain_.insert(url_chain_.begin(), url_chain.begin(),
  384. url_chain.begin() + url_chain.size() - 1);
  385. }
  386. void URLRequest::set_site_for_cookies(const SiteForCookies& site_for_cookies) {
  387. DCHECK(!is_pending_);
  388. site_for_cookies_ = site_for_cookies;
  389. }
  390. void URLRequest::set_first_party_url_policy(
  391. RedirectInfo::FirstPartyURLPolicy first_party_url_policy) {
  392. DCHECK(!is_pending_);
  393. first_party_url_policy_ = first_party_url_policy;
  394. }
  395. void URLRequest::set_initiator(const absl::optional<url::Origin>& initiator) {
  396. DCHECK(!is_pending_);
  397. DCHECK(!initiator.has_value() || initiator.value().opaque() ||
  398. initiator.value().GetURL().is_valid());
  399. initiator_ = initiator;
  400. }
  401. void URLRequest::set_method(const std::string& method) {
  402. DCHECK(!is_pending_);
  403. method_ = method;
  404. }
  405. #if BUILDFLAG(ENABLE_REPORTING)
  406. void URLRequest::set_reporting_upload_depth(int reporting_upload_depth) {
  407. DCHECK(!is_pending_);
  408. reporting_upload_depth_ = reporting_upload_depth;
  409. }
  410. #endif
  411. void URLRequest::SetReferrer(const std::string& referrer) {
  412. DCHECK(!is_pending_);
  413. GURL referrer_url(referrer);
  414. if (referrer_url.is_valid()) {
  415. referrer_ = referrer_url.GetAsReferrer().spec();
  416. } else {
  417. referrer_ = referrer;
  418. }
  419. }
  420. void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) {
  421. DCHECK(!is_pending_);
  422. referrer_policy_ = referrer_policy;
  423. }
  424. void URLRequest::set_allow_credentials(bool allow_credentials) {
  425. allow_credentials_ = allow_credentials;
  426. if (allow_credentials) {
  427. load_flags_ &= ~LOAD_DO_NOT_SAVE_COOKIES;
  428. } else {
  429. load_flags_ |= LOAD_DO_NOT_SAVE_COOKIES;
  430. }
  431. }
  432. void URLRequest::Start() {
  433. DCHECK(delegate_);
  434. if (status_ != OK)
  435. return;
  436. if (context_->require_network_isolation_key())
  437. DCHECK(!isolation_info_.IsEmpty());
  438. // Some values can be NULL, but the job factory must not be.
  439. DCHECK(context_->job_factory());
  440. // Anything that sets |blocked_by_| before start should have cleaned up after
  441. // itself.
  442. DCHECK(blocked_by_.empty());
  443. g_url_requests_started = true;
  444. response_info_.request_time = base::Time::Now();
  445. load_timing_info_ = LoadTimingInfo();
  446. load_timing_info_.request_start_time = response_info_.request_time;
  447. load_timing_info_.request_start = base::TimeTicks::Now();
  448. if (network_delegate()) {
  449. OnCallToDelegate(NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST);
  450. int error = network_delegate()->NotifyBeforeURLRequest(
  451. this,
  452. base::BindOnce(&URLRequest::BeforeRequestComplete,
  453. base::Unretained(this)),
  454. &delegate_redirect_url_);
  455. // If ERR_IO_PENDING is returned, the delegate will invoke
  456. // |BeforeRequestComplete| later.
  457. if (error != ERR_IO_PENDING)
  458. BeforeRequestComplete(error);
  459. return;
  460. }
  461. StartJob(context_->job_factory()->CreateJob(this));
  462. }
  463. ///////////////////////////////////////////////////////////////////////////////
  464. URLRequest::URLRequest(const GURL& url,
  465. RequestPriority priority,
  466. Delegate* delegate,
  467. const URLRequestContext* context,
  468. NetworkTrafficAnnotationTag traffic_annotation,
  469. bool is_for_websockets,
  470. absl::optional<net::NetLogSource> net_log_source)
  471. : context_(context),
  472. net_log_(CreateNetLogWithSource(context->net_log(), net_log_source)),
  473. url_chain_(1, url),
  474. method_("GET"),
  475. delegate_(delegate),
  476. is_for_websockets_(is_for_websockets),
  477. redirect_limit_(kMaxRedirects),
  478. priority_(priority),
  479. creation_time_(base::TimeTicks::Now()),
  480. traffic_annotation_(traffic_annotation) {
  481. // Sanity check out environment.
  482. DCHECK(base::ThreadTaskRunnerHandle::IsSet());
  483. context->url_requests()->insert(this);
  484. net_log_.BeginEvent(NetLogEventType::REQUEST_ALIVE, [&] {
  485. return NetLogURLRequestConstructorParams(url, priority_,
  486. traffic_annotation_);
  487. });
  488. }
  489. void URLRequest::BeforeRequestComplete(int error) {
  490. DCHECK(!job_.get());
  491. DCHECK_NE(ERR_IO_PENDING, error);
  492. // Check that there are no callbacks to already failed or canceled requests.
  493. DCHECK(!failed());
  494. OnCallToDelegateComplete();
  495. if (error != OK) {
  496. net_log_.AddEventWithStringParams(NetLogEventType::CANCELLED, "source",
  497. "delegate");
  498. StartJob(std::make_unique<URLRequestErrorJob>(this, error));
  499. } else if (!delegate_redirect_url_.is_empty()) {
  500. GURL new_url;
  501. new_url.Swap(&delegate_redirect_url_);
  502. StartJob(std::make_unique<URLRequestRedirectJob>(
  503. this, new_url,
  504. // Use status code 307 to preserve the method, so POST requests work.
  505. RedirectUtil::ResponseCode::REDIRECT_307_TEMPORARY_REDIRECT,
  506. "Delegate"));
  507. } else {
  508. StartJob(context_->job_factory()->CreateJob(this));
  509. }
  510. }
  511. void URLRequest::StartJob(std::unique_ptr<URLRequestJob> job) {
  512. DCHECK(!is_pending_);
  513. DCHECK(!job_);
  514. net_log_.BeginEvent(NetLogEventType::URL_REQUEST_START_JOB, [&] {
  515. return NetLogURLRequestStartParams(
  516. url(), method_, load_flags_, isolation_info_, site_for_cookies_,
  517. initiator_,
  518. upload_data_stream_ ? upload_data_stream_->identifier() : -1);
  519. });
  520. job_ = std::move(job);
  521. job_->SetExtraRequestHeaders(extra_request_headers_);
  522. job_->SetPriority(priority_);
  523. job_->SetRequestHeadersCallback(request_headers_callback_);
  524. job_->SetEarlyResponseHeadersCallback(early_response_headers_callback_);
  525. job_->SetResponseHeadersCallback(response_headers_callback_);
  526. if (upload_data_stream_.get())
  527. job_->SetUpload(upload_data_stream_.get());
  528. is_pending_ = true;
  529. is_redirecting_ = false;
  530. response_info_.was_cached = false;
  531. maybe_sent_cookies_.clear();
  532. maybe_stored_cookies_.clear();
  533. has_partitioned_cookie_ = false;
  534. GURL referrer_url(referrer_);
  535. bool same_origin_for_metrics;
  536. if (referrer_url !=
  537. URLRequestJob::ComputeReferrerForPolicy(
  538. referrer_policy_, referrer_url, url(), &same_origin_for_metrics)) {
  539. if (!network_delegate() ||
  540. !network_delegate()->CancelURLRequestWithPolicyViolatingReferrerHeader(
  541. *this, url(), referrer_url)) {
  542. referrer_.clear();
  543. } else {
  544. // We need to clear the referrer anyway to avoid an infinite recursion
  545. // when starting the error job.
  546. referrer_.clear();
  547. net_log_.AddEventWithStringParams(NetLogEventType::CANCELLED, "source",
  548. "delegate");
  549. RestartWithJob(
  550. std::make_unique<URLRequestErrorJob>(this, ERR_BLOCKED_BY_CLIENT));
  551. return;
  552. }
  553. }
  554. RecordReferrerGranularityMetrics(same_origin_for_metrics);
  555. // Start() always completes asynchronously.
  556. //
  557. // Status is generally set by URLRequestJob itself, but Start() calls
  558. // directly into the URLRequestJob subclass, so URLRequestJob can't set it
  559. // here.
  560. // TODO(mmenke): Make the URLRequest manage its own status.
  561. status_ = ERR_IO_PENDING;
  562. job_->Start();
  563. }
  564. void URLRequest::RestartWithJob(std::unique_ptr<URLRequestJob> job) {
  565. DCHECK(job->request() == this);
  566. PrepareToRestart();
  567. StartJob(std::move(job));
  568. }
  569. int URLRequest::Cancel() {
  570. return DoCancel(ERR_ABORTED, SSLInfo());
  571. }
  572. int URLRequest::CancelWithError(int error) {
  573. return DoCancel(error, SSLInfo());
  574. }
  575. void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
  576. // This should only be called on a started request.
  577. if (!is_pending_ || !job_.get() || job_->has_response_started()) {
  578. NOTREACHED();
  579. return;
  580. }
  581. DoCancel(error, ssl_info);
  582. }
  583. int URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
  584. DCHECK_LT(error, 0);
  585. // If cancelled while calling a delegate, clear delegate info.
  586. if (calling_delegate_) {
  587. LogUnblocked();
  588. OnCallToDelegateComplete();
  589. }
  590. // If the URL request already has an error status, then canceling is a no-op.
  591. // Plus, we don't want to change the error status once it has been set.
  592. if (!failed()) {
  593. status_ = error;
  594. response_info_.ssl_info = ssl_info;
  595. // If the request hasn't already been completed, log a cancellation event.
  596. if (!has_notified_completion_) {
  597. // Don't log an error code on ERR_ABORTED, since that's redundant.
  598. net_log_.AddEventWithNetErrorCode(NetLogEventType::CANCELLED,
  599. error == ERR_ABORTED ? OK : error);
  600. }
  601. }
  602. if (is_pending_ && job_.get())
  603. job_->Kill();
  604. // We need to notify about the end of this job here synchronously. The
  605. // Job sends an asynchronous notification but by the time this is processed,
  606. // our |context_| is NULL.
  607. NotifyRequestCompleted();
  608. // The Job will call our NotifyDone method asynchronously. This is done so
  609. // that the Delegate implementation can call Cancel without having to worry
  610. // about being called recursively.
  611. return status_;
  612. }
  613. int URLRequest::Read(IOBuffer* dest, int dest_size) {
  614. DCHECK(job_.get());
  615. DCHECK_NE(ERR_IO_PENDING, status_);
  616. // If this is the first read, end the delegate call that may have started in
  617. // OnResponseStarted.
  618. OnCallToDelegateComplete();
  619. // If the request has failed, Read() will return actual network error code.
  620. if (status_ != OK)
  621. return status_;
  622. // This handles reads after the request already completed successfully.
  623. // TODO(ahendrickson): DCHECK() that it is not done after
  624. // http://crbug.com/115705 is fixed.
  625. if (job_->is_done())
  626. return status_;
  627. if (dest_size == 0) {
  628. // Caller is not too bright. I guess we've done what they asked.
  629. return OK;
  630. }
  631. int rv = job_->Read(dest, dest_size);
  632. if (rv == ERR_IO_PENDING) {
  633. set_status(ERR_IO_PENDING);
  634. } else if (rv <= 0) {
  635. NotifyRequestCompleted();
  636. }
  637. // If rv is not 0 or actual bytes read, the status cannot be success.
  638. DCHECK(rv >= 0 || status_ != OK);
  639. return rv;
  640. }
  641. void URLRequest::set_status(int status) {
  642. DCHECK_LE(status, 0);
  643. DCHECK(!failed() || (status != OK && status != ERR_IO_PENDING));
  644. status_ = status;
  645. }
  646. bool URLRequest::failed() const {
  647. return (status_ != OK && status_ != ERR_IO_PENDING);
  648. }
  649. int URLRequest::NotifyConnected(const TransportInfo& info,
  650. CompletionOnceCallback callback) {
  651. OnCallToDelegate(NetLogEventType::URL_REQUEST_DELEGATE_CONNECTED);
  652. int result = delegate_->OnConnected(
  653. this, info,
  654. base::BindOnce(
  655. [](URLRequest* request, CompletionOnceCallback callback, int result) {
  656. request->OnCallToDelegateComplete(result);
  657. std::move(callback).Run(result);
  658. },
  659. this, std::move(callback)));
  660. if (result != ERR_IO_PENDING)
  661. OnCallToDelegateComplete(result);
  662. return result;
  663. }
  664. void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info,
  665. bool* defer_redirect) {
  666. DCHECK_EQ(OK, status_);
  667. is_redirecting_ = true;
  668. OnCallToDelegate(NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT);
  669. delegate_->OnReceivedRedirect(this, redirect_info, defer_redirect);
  670. // |this| may be have been destroyed here.
  671. }
  672. void URLRequest::NotifyResponseStarted(int net_error) {
  673. DCHECK_LE(net_error, 0);
  674. // Change status if there was an error.
  675. if (net_error != OK)
  676. set_status(net_error);
  677. // |status_| should not be ERR_IO_PENDING when calling into the
  678. // URLRequest::Delegate().
  679. DCHECK_NE(ERR_IO_PENDING, status_);
  680. net_log_.EndEventWithNetErrorCode(NetLogEventType::URL_REQUEST_START_JOB,
  681. net_error);
  682. // In some cases (e.g. an event was canceled), we might have sent the
  683. // completion event and receive a NotifyResponseStarted() later.
  684. if (!has_notified_completion_ && net_error == OK) {
  685. if (network_delegate())
  686. network_delegate()->NotifyResponseStarted(this, net_error);
  687. }
  688. // Notify in case the entire URL Request has been finished.
  689. if (!has_notified_completion_ && net_error != OK)
  690. NotifyRequestCompleted();
  691. OnCallToDelegate(NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED);
  692. delegate_->OnResponseStarted(this, net_error);
  693. // Nothing may appear below this line as OnResponseStarted may delete
  694. // |this|.
  695. }
  696. void URLRequest::FollowDeferredRedirect(
  697. const absl::optional<std::vector<std::string>>& removed_headers,
  698. const absl::optional<net::HttpRequestHeaders>& modified_headers) {
  699. DCHECK(job_.get());
  700. DCHECK_EQ(OK, status_);
  701. maybe_sent_cookies_.clear();
  702. maybe_stored_cookies_.clear();
  703. has_partitioned_cookie_ = false;
  704. status_ = ERR_IO_PENDING;
  705. job_->FollowDeferredRedirect(removed_headers, modified_headers);
  706. }
  707. void URLRequest::SetAuth(const AuthCredentials& credentials) {
  708. DCHECK(job_.get());
  709. DCHECK(job_->NeedsAuth());
  710. maybe_sent_cookies_.clear();
  711. maybe_stored_cookies_.clear();
  712. has_partitioned_cookie_ = false;
  713. status_ = ERR_IO_PENDING;
  714. job_->SetAuth(credentials);
  715. }
  716. void URLRequest::CancelAuth() {
  717. DCHECK(job_.get());
  718. DCHECK(job_->NeedsAuth());
  719. status_ = ERR_IO_PENDING;
  720. job_->CancelAuth();
  721. }
  722. void URLRequest::ContinueWithCertificate(
  723. scoped_refptr<X509Certificate> client_cert,
  724. scoped_refptr<SSLPrivateKey> client_private_key) {
  725. DCHECK(job_.get());
  726. // Matches the call in NotifyCertificateRequested.
  727. OnCallToDelegateComplete();
  728. status_ = ERR_IO_PENDING;
  729. job_->ContinueWithCertificate(std::move(client_cert),
  730. std::move(client_private_key));
  731. }
  732. void URLRequest::ContinueDespiteLastError() {
  733. DCHECK(job_.get());
  734. // Matches the call in NotifySSLCertificateError.
  735. OnCallToDelegateComplete();
  736. status_ = ERR_IO_PENDING;
  737. job_->ContinueDespiteLastError();
  738. }
  739. void URLRequest::AbortAndCloseConnection() {
  740. DCHECK_EQ(OK, status_);
  741. DCHECK(!has_notified_completion_);
  742. DCHECK(job_);
  743. job_->CloseConnectionOnDestruction();
  744. job_.reset();
  745. }
  746. void URLRequest::PrepareToRestart() {
  747. DCHECK(job_.get());
  748. // Close the current URL_REQUEST_START_JOB, since we will be starting a new
  749. // one.
  750. net_log_.EndEvent(NetLogEventType::URL_REQUEST_START_JOB);
  751. job_.reset();
  752. response_info_ = HttpResponseInfo();
  753. response_info_.request_time = base::Time::Now();
  754. load_timing_info_ = LoadTimingInfo();
  755. load_timing_info_.request_start_time = response_info_.request_time;
  756. load_timing_info_.request_start = base::TimeTicks::Now();
  757. status_ = OK;
  758. is_pending_ = false;
  759. proxy_server_ = ProxyServer();
  760. }
  761. void URLRequest::Redirect(
  762. const RedirectInfo& redirect_info,
  763. const absl::optional<std::vector<std::string>>& removed_headers,
  764. const absl::optional<net::HttpRequestHeaders>& modified_headers) {
  765. // This method always succeeds. Whether |job_| is allowed to redirect to
  766. // |redirect_info| is checked in URLRequestJob::CanFollowRedirect, before
  767. // NotifyReceivedRedirect. This means the delegate can assume that, if it
  768. // accepted the redirect, future calls to OnResponseStarted correspond to
  769. // |redirect_info.new_url|.
  770. OnCallToDelegateComplete();
  771. if (net_log_.IsCapturing()) {
  772. net_log_.AddEventWithStringParams(
  773. NetLogEventType::URL_REQUEST_REDIRECTED, "location",
  774. redirect_info.new_url.possibly_invalid_spec());
  775. }
  776. if (network_delegate())
  777. network_delegate()->NotifyBeforeRedirect(this, redirect_info.new_url);
  778. if (!final_upload_progress_.position() && upload_data_stream_)
  779. final_upload_progress_ = upload_data_stream_->GetUploadProgress();
  780. PrepareToRestart();
  781. bool clear_body = false;
  782. net::RedirectUtil::UpdateHttpRequest(url(), method_, redirect_info,
  783. removed_headers, modified_headers,
  784. &extra_request_headers_, &clear_body);
  785. if (clear_body)
  786. upload_data_stream_.reset();
  787. method_ = redirect_info.new_method;
  788. referrer_ = redirect_info.new_referrer;
  789. referrer_policy_ = redirect_info.new_referrer_policy;
  790. site_for_cookies_ = redirect_info.new_site_for_cookies;
  791. isolation_info_ = isolation_info_.CreateForRedirect(
  792. url::Origin::Create(redirect_info.new_url));
  793. url_chain_.push_back(redirect_info.new_url);
  794. --redirect_limit_;
  795. Start();
  796. }
  797. // static
  798. bool URLRequest::DefaultCanUseCookies() {
  799. return g_default_can_use_cookies;
  800. }
  801. const URLRequestContext* URLRequest::context() const {
  802. return context_;
  803. }
  804. NetworkDelegate* URLRequest::network_delegate() const {
  805. return context_->network_delegate();
  806. }
  807. int64_t URLRequest::GetExpectedContentSize() const {
  808. int64_t expected_content_size = -1;
  809. if (job_.get())
  810. expected_content_size = job_->expected_content_size();
  811. return expected_content_size;
  812. }
  813. void URLRequest::SetPriority(RequestPriority priority) {
  814. DCHECK_GE(priority, MINIMUM_PRIORITY);
  815. DCHECK_LE(priority, MAXIMUM_PRIORITY);
  816. if ((load_flags_ & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) {
  817. NOTREACHED();
  818. // Maintain the invariant that requests with IGNORE_LIMITS set
  819. // have MAXIMUM_PRIORITY for release mode.
  820. return;
  821. }
  822. if (priority_ == priority)
  823. return;
  824. priority_ = priority;
  825. net_log_.AddEventWithStringParams(NetLogEventType::URL_REQUEST_SET_PRIORITY,
  826. "priority",
  827. RequestPriorityToString(priority_));
  828. if (job_.get())
  829. job_->SetPriority(priority_);
  830. }
  831. void URLRequest::NotifyAuthRequired(
  832. std::unique_ptr<AuthChallengeInfo> auth_info) {
  833. DCHECK_EQ(OK, status_);
  834. DCHECK(auth_info);
  835. // Check that there are no callbacks to already failed or cancelled requests.
  836. DCHECK(!failed());
  837. delegate_->OnAuthRequired(this, *auth_info.get());
  838. }
  839. void URLRequest::NotifyCertificateRequested(
  840. SSLCertRequestInfo* cert_request_info) {
  841. status_ = OK;
  842. OnCallToDelegate(NetLogEventType::URL_REQUEST_DELEGATE_CERTIFICATE_REQUESTED);
  843. delegate_->OnCertificateRequested(this, cert_request_info);
  844. }
  845. void URLRequest::NotifySSLCertificateError(int net_error,
  846. const SSLInfo& ssl_info,
  847. bool fatal) {
  848. status_ = OK;
  849. OnCallToDelegate(NetLogEventType::URL_REQUEST_DELEGATE_SSL_CERTIFICATE_ERROR);
  850. delegate_->OnSSLCertificateError(this, net_error, ssl_info, fatal);
  851. }
  852. bool URLRequest::CanSetCookie(const net::CanonicalCookie& cookie,
  853. CookieOptions* options) const {
  854. DCHECK(!(load_flags_ & LOAD_DO_NOT_SAVE_COOKIES));
  855. bool can_set_cookies = g_default_can_use_cookies;
  856. if (network_delegate()) {
  857. can_set_cookies = network_delegate()->CanSetCookie(*this, cookie, options);
  858. }
  859. if (!can_set_cookies)
  860. net_log_.AddEvent(NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE);
  861. return can_set_cookies;
  862. }
  863. void URLRequest::NotifyReadCompleted(int bytes_read) {
  864. if (bytes_read > 0)
  865. set_status(OK);
  866. // Notify in case the entire URL Request has been finished.
  867. if (bytes_read <= 0)
  868. NotifyRequestCompleted();
  869. // When URLRequestJob notices there was an error in URLRequest's |status_|,
  870. // it calls this method with |bytes_read| set to -1. Set it to a real error
  871. // here.
  872. // TODO(maksims): NotifyReadCompleted take the error code as an argument on
  873. // failure, rather than -1.
  874. if (bytes_read == -1) {
  875. // |status_| should indicate an error.
  876. DCHECK(failed());
  877. bytes_read = status_;
  878. }
  879. delegate_->OnReadCompleted(this, bytes_read);
  880. // Nothing below this line as OnReadCompleted may delete |this|.
  881. }
  882. void URLRequest::OnHeadersComplete() {
  883. // The URLRequest status should still be IO_PENDING, which it was set to
  884. // before the URLRequestJob was started. On error or cancellation, this
  885. // method should not be called.
  886. DCHECK_EQ(ERR_IO_PENDING, status_);
  887. set_status(OK);
  888. // Cache load timing information now, as information will be lost once the
  889. // socket is closed and the ClientSocketHandle is Reset, which will happen
  890. // once the body is complete. The start times should already be populated.
  891. if (job_.get()) {
  892. // Keep a copy of the two times the URLRequest sets.
  893. base::TimeTicks request_start = load_timing_info_.request_start;
  894. base::Time request_start_time = load_timing_info_.request_start_time;
  895. // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
  896. // consistent place to start from.
  897. load_timing_info_ = LoadTimingInfo();
  898. job_->GetLoadTimingInfo(&load_timing_info_);
  899. load_timing_info_.request_start = request_start;
  900. load_timing_info_.request_start_time = request_start_time;
  901. ConvertRealLoadTimesToBlockingTimes(&load_timing_info_);
  902. }
  903. }
  904. void URLRequest::NotifyRequestCompleted() {
  905. // TODO(battre): Get rid of this check, according to willchan it should
  906. // not be needed.
  907. if (has_notified_completion_)
  908. return;
  909. is_pending_ = false;
  910. is_redirecting_ = false;
  911. has_notified_completion_ = true;
  912. if (network_delegate())
  913. network_delegate()->NotifyCompleted(this, job_.get() != nullptr, status_);
  914. }
  915. void URLRequest::OnCallToDelegate(NetLogEventType type) {
  916. DCHECK(!calling_delegate_);
  917. DCHECK(blocked_by_.empty());
  918. calling_delegate_ = true;
  919. delegate_event_type_ = type;
  920. net_log_.BeginEvent(type);
  921. }
  922. void URLRequest::OnCallToDelegateComplete(int error) {
  923. // This should have been cleared before resuming the request.
  924. DCHECK(blocked_by_.empty());
  925. if (!calling_delegate_)
  926. return;
  927. calling_delegate_ = false;
  928. net_log_.EndEventWithNetErrorCode(delegate_event_type_, error);
  929. delegate_event_type_ = NetLogEventType::FAILED;
  930. }
  931. void URLRequest::RecordReferrerGranularityMetrics(
  932. bool request_is_same_origin) const {
  933. GURL referrer_url(referrer_);
  934. bool referrer_more_descriptive_than_its_origin =
  935. referrer_url.is_valid() && referrer_url.PathForRequestPiece().size() > 1;
  936. // To avoid renaming the existing enum, we have to use the three-argument
  937. // histogram macro.
  938. if (request_is_same_origin) {
  939. UMA_HISTOGRAM_ENUMERATION(
  940. "Net.URLRequest.ReferrerPolicyForRequest.SameOrigin", referrer_policy_,
  941. static_cast<int>(ReferrerPolicy::MAX) + 1);
  942. UMA_HISTOGRAM_BOOLEAN(
  943. "Net.URLRequest.ReferrerHasInformativePath.SameOrigin",
  944. referrer_more_descriptive_than_its_origin);
  945. } else {
  946. UMA_HISTOGRAM_ENUMERATION(
  947. "Net.URLRequest.ReferrerPolicyForRequest.CrossOrigin", referrer_policy_,
  948. static_cast<int>(ReferrerPolicy::MAX) + 1);
  949. UMA_HISTOGRAM_BOOLEAN(
  950. "Net.URLRequest.ReferrerHasInformativePath.CrossOrigin",
  951. referrer_more_descriptive_than_its_origin);
  952. }
  953. }
  954. ConnectionAttempts URLRequest::GetConnectionAttempts() const {
  955. if (job_)
  956. return job_->GetConnectionAttempts();
  957. return {};
  958. }
  959. void URLRequest::SetRequestHeadersCallback(RequestHeadersCallback callback) {
  960. DCHECK(!job_.get());
  961. DCHECK(request_headers_callback_.is_null());
  962. request_headers_callback_ = std::move(callback);
  963. }
  964. void URLRequest::SetResponseHeadersCallback(ResponseHeadersCallback callback) {
  965. DCHECK(!job_.get());
  966. DCHECK(response_headers_callback_.is_null());
  967. response_headers_callback_ = std::move(callback);
  968. }
  969. void URLRequest::SetEarlyResponseHeadersCallback(
  970. ResponseHeadersCallback callback) {
  971. DCHECK(!job_.get());
  972. DCHECK(early_response_headers_callback_.is_null());
  973. early_response_headers_callback_ = std::move(callback);
  974. }
  975. void URLRequest::set_socket_tag(const SocketTag& socket_tag) {
  976. DCHECK(!is_pending_);
  977. DCHECK(url().SchemeIsHTTPOrHTTPS());
  978. socket_tag_ = socket_tag;
  979. }
  980. base::WeakPtr<URLRequest> URLRequest::GetWeakPtr() {
  981. return weak_factory_.GetWeakPtr();
  982. }
  983. } // namespace net