http_network_transaction.cc 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  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/http/http_network_transaction.h"
  5. #include <set>
  6. #include <utility>
  7. #include <vector>
  8. #include "base/base64url.h"
  9. #include "base/bind.h"
  10. #include "base/callback_helpers.h"
  11. #include "base/compiler_specific.h"
  12. #include "base/feature_list.h"
  13. #include "base/format_macros.h"
  14. #include "base/metrics/field_trial.h"
  15. #include "base/metrics/histogram_functions.h"
  16. #include "base/metrics/histogram_macros.h"
  17. #include "base/metrics/sparse_histogram.h"
  18. #include "base/strings/string_number_conversions.h"
  19. #include "base/strings/string_util.h"
  20. #include "base/time/time.h"
  21. #include "base/values.h"
  22. #include "build/build_config.h"
  23. #include "net/base/auth.h"
  24. #include "net/base/features.h"
  25. #include "net/base/host_port_pair.h"
  26. #include "net/base/io_buffer.h"
  27. #include "net/base/load_flags.h"
  28. #include "net/base/load_timing_info.h"
  29. #include "net/base/net_errors.h"
  30. #include "net/base/proxy_server.h"
  31. #include "net/base/transport_info.h"
  32. #include "net/base/upload_data_stream.h"
  33. #include "net/base/url_util.h"
  34. #include "net/cert/cert_status_flags.h"
  35. #include "net/filter/filter_source_stream.h"
  36. #include "net/http/bidirectional_stream_impl.h"
  37. #include "net/http/http_auth.h"
  38. #include "net/http/http_auth_controller.h"
  39. #include "net/http/http_auth_handler.h"
  40. #include "net/http/http_auth_handler_factory.h"
  41. #include "net/http/http_basic_stream.h"
  42. #include "net/http/http_chunked_decoder.h"
  43. #include "net/http/http_log_util.h"
  44. #include "net/http/http_network_session.h"
  45. #include "net/http/http_request_headers.h"
  46. #include "net/http/http_request_info.h"
  47. #include "net/http/http_response_headers.h"
  48. #include "net/http/http_response_info.h"
  49. #include "net/http/http_server_properties.h"
  50. #include "net/http/http_status_code.h"
  51. #include "net/http/http_stream.h"
  52. #include "net/http/http_stream_factory.h"
  53. #include "net/http/http_util.h"
  54. #include "net/http/transport_security_state.h"
  55. #include "net/http/url_security_manager.h"
  56. #include "net/log/net_log_event_type.h"
  57. #include "net/socket/client_socket_factory.h"
  58. #include "net/socket/next_proto.h"
  59. #include "net/socket/transport_client_socket_pool.h"
  60. #include "net/spdy/spdy_http_stream.h"
  61. #include "net/spdy/spdy_session.h"
  62. #include "net/spdy/spdy_session_pool.h"
  63. #include "net/ssl/ssl_cert_request_info.h"
  64. #include "net/ssl/ssl_connection_status_flags.h"
  65. #include "net/ssl/ssl_info.h"
  66. #include "net/ssl/ssl_private_key.h"
  67. #include "url/gurl.h"
  68. #include "url/origin.h"
  69. #include "url/scheme_host_port.h"
  70. #include "url/url_canon.h"
  71. #if BUILDFLAG(ENABLE_REPORTING)
  72. #include "net/network_error_logging/network_error_logging_service.h"
  73. #include "net/reporting/reporting_header_parser.h"
  74. #include "net/reporting/reporting_service.h"
  75. #endif // BUILDFLAG(ENABLE_REPORTING)
  76. namespace net {
  77. namespace {
  78. // Max number of |retry_attempts| (excluding the initial request) after which
  79. // we give up and show an error page.
  80. const size_t kMaxRetryAttempts = 2;
  81. // Max number of calls to RestartWith* allowed for a single connection. A single
  82. // HttpNetworkTransaction should not signal very many restartable errors, but it
  83. // may occur due to a bug (e.g. https://crbug.com/823387 or
  84. // https://crbug.com/488043) or simply if the server or proxy requests
  85. // authentication repeatedly. Although these calls are often associated with a
  86. // user prompt, in other scenarios (remembered preferences, extensions,
  87. // multi-leg authentication), they may be triggered automatically. To avoid
  88. // looping forever, bound the number of restarts.
  89. const size_t kMaxRestarts = 32;
  90. void SetProxyInfoInReponse(const ProxyInfo& proxy_info,
  91. HttpResponseInfo* response_info) {
  92. response_info->was_fetched_via_proxy = !proxy_info.is_direct();
  93. if (response_info->was_fetched_via_proxy && !proxy_info.is_empty())
  94. response_info->proxy_server = proxy_info.proxy_server();
  95. else if (!response_info->was_fetched_via_proxy && proxy_info.is_direct())
  96. response_info->proxy_server = ProxyServer::Direct();
  97. else
  98. response_info->proxy_server = ProxyServer();
  99. }
  100. } // namespace
  101. const int HttpNetworkTransaction::kDrainBodyBufferSize;
  102. HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
  103. HttpNetworkSession* session)
  104. : io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete,
  105. base::Unretained(this))),
  106. session_(session),
  107. priority_(priority) {}
  108. HttpNetworkTransaction::~HttpNetworkTransaction() {
  109. #if BUILDFLAG(ENABLE_REPORTING)
  110. // If no error or success report has been generated yet at this point, then
  111. // this network transaction was prematurely cancelled.
  112. GenerateNetworkErrorLoggingReport(ERR_ABORTED);
  113. #endif // BUILDFLAG(ENABLE_REPORTING)
  114. if (stream_.get()) {
  115. // TODO(mbelshe): The stream_ should be able to compute whether or not the
  116. // stream should be kept alive. No reason to compute here
  117. // and pass it in.
  118. if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE ||
  119. close_connection_on_destruction_) {
  120. stream_->Close(true /* not reusable */);
  121. } else if (stream_->IsResponseBodyComplete()) {
  122. // If the response body is complete, we can just reuse the socket.
  123. stream_->Close(false /* reusable */);
  124. } else {
  125. // Otherwise, we try to drain the response body.
  126. HttpStream* stream = stream_.release();
  127. stream->Drain(session_);
  128. }
  129. }
  130. if (request_ && request_->upload_data_stream)
  131. request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
  132. }
  133. int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
  134. CompletionOnceCallback callback,
  135. const NetLogWithSource& net_log) {
  136. if (request_info->load_flags & LOAD_ONLY_FROM_CACHE)
  137. return ERR_CACHE_MISS;
  138. DCHECK(request_info->traffic_annotation.is_valid());
  139. net_log_ = net_log;
  140. request_ = request_info;
  141. url_ = request_->url;
  142. network_isolation_key_ = request_->network_isolation_key;
  143. #if BUILDFLAG(ENABLE_REPORTING)
  144. // Store values for later use in NEL report generation.
  145. request_method_ = request_->method;
  146. request_->extra_headers.GetHeader(HttpRequestHeaders::kReferer,
  147. &request_referrer_);
  148. request_->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent,
  149. &request_user_agent_);
  150. request_reporting_upload_depth_ = request_->reporting_upload_depth;
  151. start_timeticks_ = base::TimeTicks::Now();
  152. #endif // BUILDFLAG(ENABLE_REPORTING)
  153. if (request_->load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES) {
  154. server_ssl_config_.disable_cert_verification_network_fetches = true;
  155. proxy_ssl_config_.disable_cert_verification_network_fetches = true;
  156. }
  157. if (request_->idempotency == IDEMPOTENT ||
  158. (request_->idempotency == DEFAULT_IDEMPOTENCY &&
  159. HttpUtil::IsMethodSafe(request_info->method))) {
  160. can_send_early_data_ = true;
  161. }
  162. if (request_->load_flags & LOAD_PREFETCH) {
  163. response_.unused_since_prefetch = true;
  164. }
  165. if (request_->load_flags & LOAD_RESTRICTED_PREFETCH) {
  166. DCHECK(response_.unused_since_prefetch);
  167. response_.restricted_prefetch = true;
  168. }
  169. next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
  170. int rv = DoLoop(OK);
  171. if (rv == ERR_IO_PENDING)
  172. callback_ = std::move(callback);
  173. // This always returns ERR_IO_PENDING because DoCreateStream() does, but
  174. // GenerateNetworkErrorLoggingReportIfError() should be called here if any
  175. // other net::Error can be returned.
  176. DCHECK_EQ(rv, ERR_IO_PENDING);
  177. return rv;
  178. }
  179. int HttpNetworkTransaction::RestartIgnoringLastError(
  180. CompletionOnceCallback callback) {
  181. DCHECK(!stream_.get());
  182. DCHECK(!stream_request_.get());
  183. DCHECK_EQ(STATE_NONE, next_state_);
  184. if (!CheckMaxRestarts())
  185. return ERR_TOO_MANY_RETRIES;
  186. next_state_ = STATE_CREATE_STREAM;
  187. int rv = DoLoop(OK);
  188. if (rv == ERR_IO_PENDING)
  189. callback_ = std::move(callback);
  190. // This always returns ERR_IO_PENDING because DoCreateStream() does, but
  191. // GenerateNetworkErrorLoggingReportIfError() should be called here if any
  192. // other net::Error can be returned.
  193. DCHECK_EQ(rv, ERR_IO_PENDING);
  194. return rv;
  195. }
  196. int HttpNetworkTransaction::RestartWithCertificate(
  197. scoped_refptr<X509Certificate> client_cert,
  198. scoped_refptr<SSLPrivateKey> client_private_key,
  199. CompletionOnceCallback callback) {
  200. // When we receive ERR_SSL_CLIENT_AUTH_CERT_NEEDED, we always tear down
  201. // existing streams and stream requests to force a new connection.
  202. DCHECK(!stream_request_.get());
  203. DCHECK(!stream_.get());
  204. DCHECK_EQ(STATE_NONE, next_state_);
  205. if (!CheckMaxRestarts())
  206. return ERR_TOO_MANY_RETRIES;
  207. // Add the credentials to the client auth cache. The next stream request will
  208. // then pick them up.
  209. session_->ssl_client_context()->SetClientCertificate(
  210. response_.cert_request_info->host_and_port, std::move(client_cert),
  211. std::move(client_private_key));
  212. if (!response_.cert_request_info->is_proxy)
  213. configured_client_cert_for_server_ = true;
  214. // Reset the other member variables.
  215. // Note: this is necessary only with SSL renegotiation.
  216. ResetStateForRestart();
  217. next_state_ = STATE_CREATE_STREAM;
  218. int rv = DoLoop(OK);
  219. if (rv == ERR_IO_PENDING)
  220. callback_ = std::move(callback);
  221. // This always returns ERR_IO_PENDING because DoCreateStream() does, but
  222. // GenerateNetworkErrorLoggingReportIfError() should be called here if any
  223. // other net::Error can be returned.
  224. DCHECK_EQ(rv, ERR_IO_PENDING);
  225. return rv;
  226. }
  227. int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials,
  228. CompletionOnceCallback callback) {
  229. if (!CheckMaxRestarts())
  230. return ERR_TOO_MANY_RETRIES;
  231. HttpAuth::Target target = pending_auth_target_;
  232. if (target == HttpAuth::AUTH_NONE) {
  233. NOTREACHED();
  234. return ERR_UNEXPECTED;
  235. }
  236. pending_auth_target_ = HttpAuth::AUTH_NONE;
  237. auth_controllers_[target]->ResetAuth(credentials);
  238. DCHECK(callback_.is_null());
  239. int rv = OK;
  240. if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
  241. // In this case, we've gathered credentials for use with proxy
  242. // authentication of a tunnel.
  243. DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
  244. DCHECK(stream_request_ != nullptr);
  245. auth_controllers_[target] = nullptr;
  246. ResetStateForRestart();
  247. rv = stream_request_->RestartTunnelWithProxyAuth();
  248. } else {
  249. // In this case, we've gathered credentials for the server or the proxy
  250. // but it is not during the tunneling phase.
  251. DCHECK(stream_request_ == nullptr);
  252. PrepareForAuthRestart(target);
  253. rv = DoLoop(OK);
  254. // Note: If an error is encountered while draining the old response body, no
  255. // Network Error Logging report will be generated, because the error was
  256. // with the old request, which will already have had a NEL report generated
  257. // for it due to the auth challenge (so we don't report a second error for
  258. // that request).
  259. }
  260. if (rv == ERR_IO_PENDING)
  261. callback_ = std::move(callback);
  262. return rv;
  263. }
  264. void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
  265. DCHECK(HaveAuth(target));
  266. DCHECK(!stream_request_.get());
  267. // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
  268. if (target == HttpAuth::AUTH_SERVER &&
  269. auth_controllers_[target]->NeedsHTTP11()) {
  270. session_->http_server_properties()->SetHTTP11Required(
  271. url::SchemeHostPort(request_->url), network_isolation_key_);
  272. }
  273. bool keep_alive = false;
  274. // Even if the server says the connection is keep-alive, we have to be
  275. // able to find the end of each response in order to reuse the connection.
  276. if (stream_->CanReuseConnection()) {
  277. // If the response body hasn't been completely read, we need to drain
  278. // it first.
  279. if (!stream_->IsResponseBodyComplete()) {
  280. next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
  281. read_buf_ = base::MakeRefCounted<IOBuffer>(
  282. kDrainBodyBufferSize); // A bit bucket.
  283. read_buf_len_ = kDrainBodyBufferSize;
  284. return;
  285. }
  286. keep_alive = true;
  287. }
  288. // We don't need to drain the response body, so we act as if we had drained
  289. // the response body.
  290. DidDrainBodyForAuthRestart(keep_alive);
  291. }
  292. void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
  293. DCHECK(!stream_request_.get());
  294. if (stream_.get()) {
  295. total_received_bytes_ += stream_->GetTotalReceivedBytes();
  296. total_sent_bytes_ += stream_->GetTotalSentBytes();
  297. std::unique_ptr<HttpStream> new_stream;
  298. if (keep_alive && stream_->CanReuseConnection()) {
  299. // We should call connection_->set_idle_time(), but this doesn't occur
  300. // often enough to be worth the trouble.
  301. stream_->SetConnectionReused();
  302. new_stream = stream_->RenewStreamForAuth();
  303. }
  304. if (!new_stream) {
  305. // Close the stream and mark it as not_reusable. Even in the
  306. // keep_alive case, we've determined that the stream_ is not
  307. // reusable if new_stream is NULL.
  308. stream_->Close(true);
  309. next_state_ = STATE_CREATE_STREAM;
  310. } else {
  311. // Renewed streams shouldn't carry over sent or received bytes.
  312. DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
  313. DCHECK_EQ(0, new_stream->GetTotalSentBytes());
  314. next_state_ = STATE_CONNECTED_CALLBACK;
  315. }
  316. stream_ = std::move(new_stream);
  317. }
  318. // Reset the other member variables.
  319. ResetStateForAuthRestart();
  320. }
  321. bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
  322. return pending_auth_target_ != HttpAuth::AUTH_NONE &&
  323. HaveAuth(pending_auth_target_);
  324. }
  325. int HttpNetworkTransaction::Read(IOBuffer* buf,
  326. int buf_len,
  327. CompletionOnceCallback callback) {
  328. DCHECK(buf);
  329. DCHECK_LT(0, buf_len);
  330. scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
  331. if (headers_valid_ && headers.get() && stream_request_.get()) {
  332. // We're trying to read the body of the response but we're still trying
  333. // to establish an SSL tunnel through an HTTP proxy. We can't read these
  334. // bytes when establishing a tunnel because they might be controlled by
  335. // an active network attacker. We don't worry about this for HTTP
  336. // because an active network attacker can already control HTTP sessions.
  337. // We reach this case when the user cancels a 407 proxy auth prompt. We
  338. // also don't worry about this for an HTTPS Proxy, because the
  339. // communication with the proxy is secure.
  340. // See http://crbug.com/8473.
  341. DCHECK(proxy_info_.is_http_like());
  342. DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
  343. return ERR_TUNNEL_CONNECTION_FAILED;
  344. }
  345. // Are we using SPDY or HTTP?
  346. next_state_ = STATE_READ_BODY;
  347. read_buf_ = buf;
  348. read_buf_len_ = buf_len;
  349. int rv = DoLoop(OK);
  350. if (rv == ERR_IO_PENDING)
  351. callback_ = std::move(callback);
  352. return rv;
  353. }
  354. void HttpNetworkTransaction::StopCaching() {}
  355. int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
  356. int64_t total_received_bytes = total_received_bytes_;
  357. if (stream_)
  358. total_received_bytes += stream_->GetTotalReceivedBytes();
  359. return total_received_bytes;
  360. }
  361. int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
  362. int64_t total_sent_bytes = total_sent_bytes_;
  363. if (stream_)
  364. total_sent_bytes += stream_->GetTotalSentBytes();
  365. return total_sent_bytes;
  366. }
  367. void HttpNetworkTransaction::DoneReading() {}
  368. const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
  369. return &response_;
  370. }
  371. LoadState HttpNetworkTransaction::GetLoadState() const {
  372. // TODO(wtc): Define a new LoadState value for the
  373. // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
  374. switch (next_state_) {
  375. case STATE_CREATE_STREAM:
  376. return LOAD_STATE_WAITING_FOR_DELEGATE;
  377. case STATE_CREATE_STREAM_COMPLETE:
  378. return stream_request_->GetLoadState();
  379. case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
  380. case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
  381. case STATE_SEND_REQUEST_COMPLETE:
  382. return LOAD_STATE_SENDING_REQUEST;
  383. case STATE_READ_HEADERS_COMPLETE:
  384. return LOAD_STATE_WAITING_FOR_RESPONSE;
  385. case STATE_READ_BODY_COMPLETE:
  386. return LOAD_STATE_READING_RESPONSE;
  387. default:
  388. return LOAD_STATE_IDLE;
  389. }
  390. }
  391. void HttpNetworkTransaction::SetQuicServerInfo(
  392. QuicServerInfo* quic_server_info) {}
  393. bool HttpNetworkTransaction::GetLoadTimingInfo(
  394. LoadTimingInfo* load_timing_info) const {
  395. if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
  396. return false;
  397. load_timing_info->proxy_resolve_start =
  398. proxy_info_.proxy_resolve_start_time();
  399. load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
  400. load_timing_info->send_start = send_start_time_;
  401. load_timing_info->send_end = send_end_time_;
  402. return true;
  403. }
  404. bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
  405. if (remote_endpoint_.address().empty())
  406. return false;
  407. *endpoint = remote_endpoint_;
  408. return true;
  409. }
  410. void HttpNetworkTransaction::PopulateNetErrorDetails(
  411. NetErrorDetails* details) const {
  412. *details = net_error_details_;
  413. if (stream_)
  414. stream_->PopulateNetErrorDetails(details);
  415. }
  416. void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
  417. priority_ = priority;
  418. if (stream_request_)
  419. stream_request_->SetPriority(priority);
  420. if (stream_)
  421. stream_->SetPriority(priority);
  422. // The above call may have resulted in deleting |*this|.
  423. }
  424. void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
  425. WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
  426. websocket_handshake_stream_base_create_helper_ = create_helper;
  427. }
  428. void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
  429. BeforeNetworkStartCallback callback) {
  430. before_network_start_callback_ = std::move(callback);
  431. }
  432. void HttpNetworkTransaction::SetConnectedCallback(
  433. const ConnectedCallback& callback) {
  434. connected_callback_ = callback;
  435. }
  436. void HttpNetworkTransaction::SetRequestHeadersCallback(
  437. RequestHeadersCallback callback) {
  438. DCHECK(!stream_);
  439. request_headers_callback_ = std::move(callback);
  440. }
  441. void HttpNetworkTransaction::SetEarlyResponseHeadersCallback(
  442. ResponseHeadersCallback callback) {
  443. DCHECK(!stream_);
  444. early_response_headers_callback_ = std::move(callback);
  445. }
  446. void HttpNetworkTransaction::SetResponseHeadersCallback(
  447. ResponseHeadersCallback callback) {
  448. DCHECK(!stream_);
  449. response_headers_callback_ = std::move(callback);
  450. }
  451. int HttpNetworkTransaction::ResumeNetworkStart() {
  452. DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
  453. return DoLoop(OK);
  454. }
  455. void HttpNetworkTransaction::ResumeAfterConnected(int result) {
  456. DCHECK_EQ(next_state_, STATE_CONNECTED_CALLBACK_COMPLETE);
  457. OnIOComplete(result);
  458. }
  459. void HttpNetworkTransaction::CloseConnectionOnDestruction() {
  460. close_connection_on_destruction_ = true;
  461. }
  462. void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
  463. const ProxyInfo& used_proxy_info,
  464. std::unique_ptr<HttpStream> stream) {
  465. DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
  466. DCHECK(stream_request_.get());
  467. if (stream_) {
  468. total_received_bytes_ += stream_->GetTotalReceivedBytes();
  469. total_sent_bytes_ += stream_->GetTotalSentBytes();
  470. }
  471. stream_ = std::move(stream);
  472. stream_->SetRequestHeadersCallback(request_headers_callback_);
  473. server_ssl_config_ = used_ssl_config;
  474. proxy_info_ = used_proxy_info;
  475. response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
  476. response_.alpn_negotiated_protocol =
  477. NextProtoToString(stream_request_->negotiated_protocol());
  478. response_.was_fetched_via_spdy = stream_request_->using_spdy();
  479. response_.dns_aliases = stream_->GetDnsAliases();
  480. SetProxyInfoInReponse(used_proxy_info, &response_);
  481. OnIOComplete(OK);
  482. }
  483. void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
  484. const SSLConfig& used_ssl_config,
  485. const ProxyInfo& used_proxy_info,
  486. std::unique_ptr<BidirectionalStreamImpl> stream) {
  487. NOTREACHED();
  488. }
  489. void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
  490. const SSLConfig& used_ssl_config,
  491. const ProxyInfo& used_proxy_info,
  492. std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
  493. OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
  494. }
  495. void HttpNetworkTransaction::OnStreamFailed(
  496. int result,
  497. const NetErrorDetails& net_error_details,
  498. const SSLConfig& used_ssl_config,
  499. const ProxyInfo& used_proxy_info,
  500. ResolveErrorInfo resolve_error_info) {
  501. DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
  502. DCHECK_NE(OK, result);
  503. DCHECK(stream_request_.get());
  504. DCHECK(!stream_.get());
  505. server_ssl_config_ = used_ssl_config;
  506. net_error_details_ = net_error_details;
  507. proxy_info_ = used_proxy_info;
  508. SetProxyInfoInReponse(used_proxy_info, &response_);
  509. response_.resolve_error_info = resolve_error_info;
  510. OnIOComplete(result);
  511. }
  512. void HttpNetworkTransaction::OnCertificateError(
  513. int result,
  514. const SSLConfig& used_ssl_config,
  515. const SSLInfo& ssl_info) {
  516. DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
  517. DCHECK_NE(OK, result);
  518. DCHECK(stream_request_.get());
  519. DCHECK(!stream_.get());
  520. response_.ssl_info = ssl_info;
  521. server_ssl_config_ = used_ssl_config;
  522. // TODO(mbelshe): For now, we're going to pass the error through, and that
  523. // will close the stream_request in all cases. This means that we're always
  524. // going to restart an entire STATE_CREATE_STREAM, even if the connection is
  525. // good and the user chooses to ignore the error. This is not ideal, but not
  526. // the end of the world either.
  527. OnIOComplete(result);
  528. }
  529. void HttpNetworkTransaction::OnNeedsProxyAuth(
  530. const HttpResponseInfo& proxy_response,
  531. const SSLConfig& used_ssl_config,
  532. const ProxyInfo& used_proxy_info,
  533. HttpAuthController* auth_controller) {
  534. DCHECK(stream_request_.get());
  535. DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
  536. establishing_tunnel_ = true;
  537. response_.headers = proxy_response.headers;
  538. response_.auth_challenge = proxy_response.auth_challenge;
  539. response_.did_use_http_auth = proxy_response.did_use_http_auth;
  540. SetProxyInfoInReponse(used_proxy_info, &response_);
  541. if (!ContentEncodingsValid()) {
  542. DoCallback(ERR_CONTENT_DECODING_FAILED);
  543. return;
  544. }
  545. headers_valid_ = true;
  546. server_ssl_config_ = used_ssl_config;
  547. proxy_info_ = used_proxy_info;
  548. auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
  549. pending_auth_target_ = HttpAuth::AUTH_PROXY;
  550. DoCallback(OK);
  551. }
  552. void HttpNetworkTransaction::OnNeedsClientAuth(
  553. const SSLConfig& used_ssl_config,
  554. SSLCertRequestInfo* cert_info) {
  555. DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
  556. server_ssl_config_ = used_ssl_config;
  557. response_.cert_request_info = cert_info;
  558. OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
  559. }
  560. void HttpNetworkTransaction::OnQuicBroken() {
  561. net_error_details_.quic_broken = true;
  562. }
  563. ConnectionAttempts HttpNetworkTransaction::GetConnectionAttempts() const {
  564. return connection_attempts_;
  565. }
  566. bool HttpNetworkTransaction::IsSecureRequest() const {
  567. return request_->url.SchemeIsCryptographic();
  568. }
  569. bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
  570. return proxy_info_.is_http_like() &&
  571. !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
  572. }
  573. void HttpNetworkTransaction::DoCallback(int rv) {
  574. DCHECK_NE(rv, ERR_IO_PENDING);
  575. DCHECK(!callback_.is_null());
  576. #if BUILDFLAG(ENABLE_REPORTING)
  577. // Just before invoking the caller's completion callback, generate a NEL
  578. // report about this network request if the result was an error.
  579. GenerateNetworkErrorLoggingReportIfError(rv);
  580. #endif // BUILDFLAG(ENABLE_REPORTING)
  581. // Since Run may result in Read being called, clear user_callback_ up front.
  582. std::move(callback_).Run(rv);
  583. }
  584. void HttpNetworkTransaction::OnIOComplete(int result) {
  585. int rv = DoLoop(result);
  586. if (rv != ERR_IO_PENDING)
  587. DoCallback(rv);
  588. }
  589. int HttpNetworkTransaction::DoLoop(int result) {
  590. DCHECK(next_state_ != STATE_NONE);
  591. int rv = result;
  592. do {
  593. State state = next_state_;
  594. next_state_ = STATE_NONE;
  595. switch (state) {
  596. case STATE_NOTIFY_BEFORE_CREATE_STREAM:
  597. DCHECK_EQ(OK, rv);
  598. rv = DoNotifyBeforeCreateStream();
  599. break;
  600. case STATE_CREATE_STREAM:
  601. DCHECK_EQ(OK, rv);
  602. rv = DoCreateStream();
  603. break;
  604. case STATE_CREATE_STREAM_COMPLETE:
  605. rv = DoCreateStreamComplete(rv);
  606. break;
  607. case STATE_INIT_STREAM:
  608. DCHECK_EQ(OK, rv);
  609. rv = DoInitStream();
  610. break;
  611. case STATE_INIT_STREAM_COMPLETE:
  612. rv = DoInitStreamComplete(rv);
  613. break;
  614. case STATE_CONNECTED_CALLBACK:
  615. rv = DoConnectedCallback();
  616. break;
  617. case STATE_CONNECTED_CALLBACK_COMPLETE:
  618. rv = DoConnectedCallbackComplete(rv);
  619. break;
  620. case STATE_GENERATE_PROXY_AUTH_TOKEN:
  621. DCHECK_EQ(OK, rv);
  622. rv = DoGenerateProxyAuthToken();
  623. break;
  624. case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
  625. rv = DoGenerateProxyAuthTokenComplete(rv);
  626. break;
  627. case STATE_GENERATE_SERVER_AUTH_TOKEN:
  628. DCHECK_EQ(OK, rv);
  629. rv = DoGenerateServerAuthToken();
  630. break;
  631. case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
  632. rv = DoGenerateServerAuthTokenComplete(rv);
  633. break;
  634. case STATE_INIT_REQUEST_BODY:
  635. DCHECK_EQ(OK, rv);
  636. rv = DoInitRequestBody();
  637. break;
  638. case STATE_INIT_REQUEST_BODY_COMPLETE:
  639. rv = DoInitRequestBodyComplete(rv);
  640. break;
  641. case STATE_BUILD_REQUEST:
  642. DCHECK_EQ(OK, rv);
  643. net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
  644. rv = DoBuildRequest();
  645. break;
  646. case STATE_BUILD_REQUEST_COMPLETE:
  647. rv = DoBuildRequestComplete(rv);
  648. break;
  649. case STATE_SEND_REQUEST:
  650. DCHECK_EQ(OK, rv);
  651. rv = DoSendRequest();
  652. break;
  653. case STATE_SEND_REQUEST_COMPLETE:
  654. rv = DoSendRequestComplete(rv);
  655. net_log_.EndEventWithNetErrorCode(
  656. NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
  657. break;
  658. case STATE_READ_HEADERS:
  659. DCHECK_EQ(OK, rv);
  660. net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
  661. rv = DoReadHeaders();
  662. break;
  663. case STATE_READ_HEADERS_COMPLETE:
  664. rv = DoReadHeadersComplete(rv);
  665. net_log_.EndEventWithNetErrorCode(
  666. NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
  667. break;
  668. case STATE_READ_BODY:
  669. DCHECK_EQ(OK, rv);
  670. net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
  671. rv = DoReadBody();
  672. break;
  673. case STATE_READ_BODY_COMPLETE:
  674. rv = DoReadBodyComplete(rv);
  675. net_log_.EndEventWithNetErrorCode(
  676. NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
  677. break;
  678. case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
  679. DCHECK_EQ(OK, rv);
  680. net_log_.BeginEvent(
  681. NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
  682. rv = DoDrainBodyForAuthRestart();
  683. break;
  684. case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
  685. rv = DoDrainBodyForAuthRestartComplete(rv);
  686. net_log_.EndEventWithNetErrorCode(
  687. NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
  688. break;
  689. default:
  690. NOTREACHED() << "bad state";
  691. rv = ERR_FAILED;
  692. break;
  693. }
  694. } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
  695. return rv;
  696. }
  697. int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
  698. next_state_ = STATE_CREATE_STREAM;
  699. bool defer = false;
  700. if (!before_network_start_callback_.is_null())
  701. std::move(before_network_start_callback_).Run(&defer);
  702. if (!defer)
  703. return OK;
  704. return ERR_IO_PENDING;
  705. }
  706. int HttpNetworkTransaction::DoCreateStream() {
  707. response_.network_accessed = true;
  708. next_state_ = STATE_CREATE_STREAM_COMPLETE;
  709. // IP based pooling is only enabled on a retry after 421 Misdirected Request
  710. // is received. Alternative Services are also disabled in this case (though
  711. // they can also be disabled when retrying after a QUIC error).
  712. if (!enable_ip_based_pooling_)
  713. DCHECK(!enable_alternative_services_);
  714. if (ForWebSocketHandshake()) {
  715. stream_request_ =
  716. session_->http_stream_factory()->RequestWebSocketHandshakeStream(
  717. *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
  718. websocket_handshake_stream_base_create_helper_,
  719. enable_ip_based_pooling_, enable_alternative_services_, net_log_);
  720. } else {
  721. stream_request_ = session_->http_stream_factory()->RequestStream(
  722. *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
  723. enable_ip_based_pooling_, enable_alternative_services_, net_log_);
  724. }
  725. DCHECK(stream_request_.get());
  726. return ERR_IO_PENDING;
  727. }
  728. int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
  729. CopyConnectionAttemptsFromStreamRequest();
  730. if (result == OK) {
  731. next_state_ = STATE_CONNECTED_CALLBACK;
  732. DCHECK(stream_.get());
  733. } else if (result == ERR_HTTP_1_1_REQUIRED ||
  734. result == ERR_PROXY_HTTP_1_1_REQUIRED) {
  735. return HandleHttp11Required(result);
  736. }
  737. // Handle possible client certificate errors that may have occurred if the
  738. // stream used SSL for one or more of the layers.
  739. result = HandleSSLClientAuthError(result);
  740. // At this point we are done with the stream_request_.
  741. stream_request_.reset();
  742. return result;
  743. }
  744. int HttpNetworkTransaction::DoInitStream() {
  745. DCHECK(stream_.get());
  746. next_state_ = STATE_INIT_STREAM_COMPLETE;
  747. return stream_->InitializeStream(can_send_early_data_, priority_, net_log_,
  748. io_callback_);
  749. }
  750. int HttpNetworkTransaction::DoInitStreamComplete(int result) {
  751. if (result != OK) {
  752. if (result < 0)
  753. result = HandleIOError(result);
  754. // The stream initialization failed, so this stream will never be useful.
  755. if (stream_) {
  756. total_received_bytes_ += stream_->GetTotalReceivedBytes();
  757. total_sent_bytes_ += stream_->GetTotalSentBytes();
  758. }
  759. CacheNetErrorDetailsAndResetStream();
  760. return result;
  761. }
  762. next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
  763. return result;
  764. }
  765. int HttpNetworkTransaction::DoConnectedCallback() {
  766. // Register the HttpRequestInfo object on the stream here so that it's
  767. // available when invoking the `connected_callback_`, as
  768. // HttpStream::GetAcceptChViaAlps() needs the HttpRequestInfo to retrieve
  769. // the ACCEPT_CH frame payload.
  770. stream_->RegisterRequest(request_);
  771. next_state_ = STATE_CONNECTED_CALLBACK_COMPLETE;
  772. int result = stream_->GetRemoteEndpoint(&remote_endpoint_);
  773. if (result != OK) {
  774. // `GetRemoteEndpoint()` fails when the underlying socket is not connected
  775. // anymore, even though the peer's address is known. This can happen when
  776. // we picked a socket from socket pools while it was still connected, but
  777. // the remote side closes it before we get a chance to send our request.
  778. // See if we should retry the request based on the error code we got.
  779. return HandleIOError(result);
  780. }
  781. if (connected_callback_.is_null()) {
  782. return OK;
  783. }
  784. // Fire off notification that we have successfully connected.
  785. TransportType type = TransportType::kDirect;
  786. if (!proxy_info_.is_direct()) {
  787. type = TransportType::kProxied;
  788. }
  789. return connected_callback_.Run(
  790. TransportInfo(type, remote_endpoint_,
  791. std::string{stream_->GetAcceptChViaAlps()}),
  792. base::BindOnce(&HttpNetworkTransaction::ResumeAfterConnected,
  793. base::Unretained(this)));
  794. }
  795. int HttpNetworkTransaction::DoConnectedCallbackComplete(int result) {
  796. if (result != OK) {
  797. if (stream_) {
  798. stream_->Close(/*not_reusable=*/false);
  799. }
  800. // Stop the state machine here if the call failed.
  801. return result;
  802. }
  803. next_state_ = STATE_INIT_STREAM;
  804. return OK;
  805. }
  806. int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
  807. next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
  808. if (!ShouldApplyProxyAuth())
  809. return OK;
  810. HttpAuth::Target target = HttpAuth::AUTH_PROXY;
  811. if (!auth_controllers_[target].get())
  812. auth_controllers_[target] = base::MakeRefCounted<HttpAuthController>(
  813. target, AuthURL(target), request_->network_isolation_key,
  814. session_->http_auth_cache(), session_->http_auth_handler_factory(),
  815. session_->host_resolver());
  816. return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
  817. io_callback_,
  818. net_log_);
  819. }
  820. int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
  821. DCHECK_NE(ERR_IO_PENDING, rv);
  822. if (rv == OK)
  823. next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
  824. return rv;
  825. }
  826. int HttpNetworkTransaction::DoGenerateServerAuthToken() {
  827. next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
  828. HttpAuth::Target target = HttpAuth::AUTH_SERVER;
  829. if (!auth_controllers_[target].get()) {
  830. auth_controllers_[target] = base::MakeRefCounted<HttpAuthController>(
  831. target, AuthURL(target), request_->network_isolation_key,
  832. session_->http_auth_cache(), session_->http_auth_handler_factory(),
  833. session_->host_resolver());
  834. if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
  835. auth_controllers_[target]->DisableEmbeddedIdentity();
  836. }
  837. if (!ShouldApplyServerAuth())
  838. return OK;
  839. return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
  840. io_callback_,
  841. net_log_);
  842. }
  843. int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
  844. DCHECK_NE(ERR_IO_PENDING, rv);
  845. if (rv == OK)
  846. next_state_ = STATE_INIT_REQUEST_BODY;
  847. return rv;
  848. }
  849. int HttpNetworkTransaction::BuildRequestHeaders(
  850. bool using_http_proxy_without_tunnel) {
  851. request_headers_.SetHeader(HttpRequestHeaders::kHost,
  852. GetHostAndOptionalPort(request_->url));
  853. // For compat with HTTP/1.0 servers and proxies:
  854. if (using_http_proxy_without_tunnel) {
  855. request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
  856. "keep-alive");
  857. } else {
  858. request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
  859. }
  860. // Add a content length header?
  861. if (request_->upload_data_stream) {
  862. if (request_->upload_data_stream->is_chunked()) {
  863. request_headers_.SetHeader(
  864. HttpRequestHeaders::kTransferEncoding, "chunked");
  865. } else {
  866. request_headers_.SetHeader(
  867. HttpRequestHeaders::kContentLength,
  868. base::NumberToString(request_->upload_data_stream->size()));
  869. }
  870. } else if (request_->method == "POST" || request_->method == "PUT") {
  871. // An empty POST/PUT request still needs a content length. As for HEAD,
  872. // IE and Safari also add a content length header. Presumably it is to
  873. // support sending a HEAD request to an URL that only expects to be sent a
  874. // POST or some other method that normally would have a message body.
  875. // Firefox (40.0) does not send the header, and RFC 7230 & 7231
  876. // specify that it should not be sent due to undefined behavior.
  877. request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
  878. }
  879. // Honor load flags that impact proxy caches.
  880. if (request_->load_flags & LOAD_BYPASS_CACHE) {
  881. request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
  882. request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
  883. } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
  884. request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
  885. }
  886. if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
  887. auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
  888. &request_headers_);
  889. if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
  890. auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
  891. &request_headers_);
  892. request_headers_.MergeFrom(request_->extra_headers);
  893. response_.did_use_http_auth =
  894. request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
  895. request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
  896. return OK;
  897. }
  898. int HttpNetworkTransaction::DoInitRequestBody() {
  899. next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
  900. int rv = OK;
  901. if (request_->upload_data_stream)
  902. rv = request_->upload_data_stream->Init(
  903. base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
  904. base::Unretained(this)),
  905. net_log_);
  906. return rv;
  907. }
  908. int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
  909. if (result == OK)
  910. next_state_ = STATE_BUILD_REQUEST;
  911. return result;
  912. }
  913. int HttpNetworkTransaction::DoBuildRequest() {
  914. next_state_ = STATE_BUILD_REQUEST_COMPLETE;
  915. headers_valid_ = false;
  916. // This is constructed lazily (instead of within our Start method), so that
  917. // we have proxy info available.
  918. if (request_headers_.IsEmpty()) {
  919. bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
  920. return BuildRequestHeaders(using_http_proxy_without_tunnel);
  921. }
  922. return OK;
  923. }
  924. int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
  925. if (result == OK)
  926. next_state_ = STATE_SEND_REQUEST;
  927. return result;
  928. }
  929. int HttpNetworkTransaction::DoSendRequest() {
  930. send_start_time_ = base::TimeTicks::Now();
  931. next_state_ = STATE_SEND_REQUEST_COMPLETE;
  932. stream_->SetRequestIdempotency(request_->idempotency);
  933. return stream_->SendRequest(request_headers_, &response_, io_callback_);
  934. }
  935. int HttpNetworkTransaction::DoSendRequestComplete(int result) {
  936. send_end_time_ = base::TimeTicks::Now();
  937. if (result == ERR_HTTP_1_1_REQUIRED ||
  938. result == ERR_PROXY_HTTP_1_1_REQUIRED) {
  939. return HandleHttp11Required(result);
  940. }
  941. if (result < 0)
  942. return HandleIOError(result);
  943. next_state_ = STATE_READ_HEADERS;
  944. return OK;
  945. }
  946. int HttpNetworkTransaction::DoReadHeaders() {
  947. next_state_ = STATE_READ_HEADERS_COMPLETE;
  948. return stream_->ReadResponseHeaders(io_callback_);
  949. }
  950. int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
  951. // We can get a ERR_SSL_CLIENT_AUTH_CERT_NEEDED here due to SSL renegotiation.
  952. // Server certificate errors are impossible. Rather than reverify the new
  953. // server certificate, BoringSSL forbids server certificates from changing.
  954. DCHECK(!IsCertificateError(result));
  955. if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
  956. DCHECK(stream_.get());
  957. DCHECK(IsSecureRequest());
  958. response_.cert_request_info = base::MakeRefCounted<SSLCertRequestInfo>();
  959. stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
  960. total_received_bytes_ += stream_->GetTotalReceivedBytes();
  961. total_sent_bytes_ += stream_->GetTotalSentBytes();
  962. stream_->Close(true);
  963. CacheNetErrorDetailsAndResetStream();
  964. }
  965. if (result == ERR_HTTP_1_1_REQUIRED ||
  966. result == ERR_PROXY_HTTP_1_1_REQUIRED) {
  967. return HandleHttp11Required(result);
  968. }
  969. // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
  970. // response headers were received, we do the best we can to make sense of it
  971. // and send it back up the stack.
  972. //
  973. // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
  974. // bizarre for SPDY. Assuming this logic is useful at all.
  975. // TODO(davidben): Bubble the error code up so we do not cache?
  976. if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
  977. result = OK;
  978. if (result < 0)
  979. return HandleIOError(result);
  980. DCHECK(response_.headers.get());
  981. // Check for a 103 Early Hints response.
  982. if (response_.headers->response_code() == HTTP_EARLY_HINTS) {
  983. NetLogResponseHeaders(
  984. net_log_,
  985. NetLogEventType::HTTP_TRANSACTION_READ_EARLY_HINTS_RESPONSE_HEADERS,
  986. response_.headers.get());
  987. // Early Hints does not make sense for a WebSocket handshake.
  988. if (ForWebSocketHandshake())
  989. return ERR_FAILED;
  990. // TODO(crbug.com/671310): Validate headers? It seems that
  991. // "Content-Encoding" etc should not appear.
  992. if (early_response_headers_callback_)
  993. early_response_headers_callback_.Run(std::move(response_.headers));
  994. response_.headers =
  995. base::MakeRefCounted<HttpResponseHeaders>(std::string());
  996. next_state_ = STATE_READ_HEADERS;
  997. return OK;
  998. }
  999. if (!ContentEncodingsValid())
  1000. return ERR_CONTENT_DECODING_FAILED;
  1001. // On a 408 response from the server ("Request Timeout") on a stale socket,
  1002. // retry the request for HTTP/1.1 but not HTTP/2 or QUIC because those
  1003. // multiplex requests and have no need for 408.
  1004. if (response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
  1005. HttpResponseInfo::ConnectionInfoToCoarse(response_.connection_info) ==
  1006. HttpResponseInfo::CONNECTION_INFO_COARSE_HTTP1 &&
  1007. stream_->IsConnectionReused()) {
  1008. #if BUILDFLAG(ENABLE_REPORTING)
  1009. GenerateNetworkErrorLoggingReport(OK);
  1010. #endif // BUILDFLAG(ENABLE_REPORTING)
  1011. net_log_.AddEventWithNetErrorCode(
  1012. NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
  1013. response_.headers->response_code());
  1014. // This will close the socket - it would be weird to try and reuse it, even
  1015. // if the server doesn't actually close it.
  1016. ResetConnectionAndRequestForResend();
  1017. return OK;
  1018. }
  1019. NetLogResponseHeaders(net_log_,
  1020. NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
  1021. response_.headers.get());
  1022. if (response_headers_callback_)
  1023. response_headers_callback_.Run(response_.headers);
  1024. if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
  1025. // HTTP/0.9 doesn't support the PUT method, so lack of response headers
  1026. // indicates a buggy server. See:
  1027. // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
  1028. if (request_->method == "PUT")
  1029. return ERR_METHOD_NOT_SUPPORTED;
  1030. }
  1031. if (can_send_early_data_ &&
  1032. response_.headers->response_code() == HTTP_TOO_EARLY) {
  1033. return HandleIOError(ERR_EARLY_DATA_REJECTED);
  1034. }
  1035. // Check for an intermediate 100 Continue response. An origin server is
  1036. // allowed to send this response even if we didn't ask for it, so we just
  1037. // need to skip over it.
  1038. // We treat any other 1xx in this same way (although in practice getting
  1039. // a 1xx that isn't a 100 is rare).
  1040. // Unless this is a WebSocket request, in which case we pass it on up.
  1041. if (response_.headers->response_code() / 100 == 1 &&
  1042. !ForWebSocketHandshake()) {
  1043. response_.headers =
  1044. base::MakeRefCounted<HttpResponseHeaders>(std::string());
  1045. next_state_ = STATE_READ_HEADERS;
  1046. return OK;
  1047. }
  1048. const bool has_body_with_null_source =
  1049. request_->upload_data_stream &&
  1050. request_->upload_data_stream->has_null_source();
  1051. if (response_.headers->response_code() == 421 &&
  1052. (enable_ip_based_pooling_ || enable_alternative_services_) &&
  1053. !has_body_with_null_source) {
  1054. #if BUILDFLAG(ENABLE_REPORTING)
  1055. GenerateNetworkErrorLoggingReport(OK);
  1056. #endif // BUILDFLAG(ENABLE_REPORTING)
  1057. // Retry the request with both IP based pooling and Alternative Services
  1058. // disabled.
  1059. enable_ip_based_pooling_ = false;
  1060. enable_alternative_services_ = false;
  1061. net_log_.AddEvent(
  1062. NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
  1063. ResetConnectionAndRequestForResend();
  1064. return OK;
  1065. }
  1066. if (IsSecureRequest()) {
  1067. stream_->GetSSLInfo(&response_.ssl_info);
  1068. if (response_.ssl_info.is_valid() &&
  1069. !IsCertStatusError(response_.ssl_info.cert_status)) {
  1070. session_->http_stream_factory()->ProcessAlternativeServices(
  1071. session_, network_isolation_key_, response_.headers.get(),
  1072. url::SchemeHostPort(request_->url));
  1073. }
  1074. }
  1075. int rv = HandleAuthChallenge();
  1076. if (rv != OK)
  1077. return rv;
  1078. #if BUILDFLAG(ENABLE_REPORTING)
  1079. // Note: This just handles the legacy Report-To header, which is still
  1080. // required for NEL. The newer Reporting-Endpoints header is processed in
  1081. // network::PopulateParsedHeaders().
  1082. ProcessReportToHeader();
  1083. // Note: Unless there is a pre-existing NEL policy for this origin, any NEL
  1084. // reports generated before the NEL header is processed here will just be
  1085. // dropped by the NetworkErrorLoggingService.
  1086. ProcessNetworkErrorLoggingHeader();
  1087. // Generate NEL report here if we have to report an HTTP error (4xx or 5xx
  1088. // code), or if the response body will not be read, or on a redirect.
  1089. // Note: This will report a success for a redirect even if an error is
  1090. // encountered later while draining the body.
  1091. int response_code = response_.headers->response_code();
  1092. if ((response_code >= 400 && response_code < 600) ||
  1093. response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT ||
  1094. response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" ||
  1095. response_.headers->GetContentLength() == 0 ||
  1096. response_.headers->IsRedirect(nullptr /* location */)) {
  1097. GenerateNetworkErrorLoggingReport(OK);
  1098. }
  1099. #endif // BUILDFLAG(ENABLE_REPORTING)
  1100. headers_valid_ = true;
  1101. // We have reached the end of Start state machine, set the RequestInfo to
  1102. // null.
  1103. // RequestInfo is a member of the HttpTransaction's consumer and is useful
  1104. // only until the final response headers are received. Clearing it will ensure
  1105. // that HttpRequestInfo is only used up until final response headers are
  1106. // received. Clearing is allowed so that the transaction can be disassociated
  1107. // from its creating consumer in cases where it is shared for writing to the
  1108. // cache. It is also safe to set it to null at this point since
  1109. // upload_data_stream is also not used in the Read state machine.
  1110. if (pending_auth_target_ == HttpAuth::AUTH_NONE)
  1111. request_ = nullptr;
  1112. return OK;
  1113. }
  1114. int HttpNetworkTransaction::DoReadBody() {
  1115. DCHECK(read_buf_.get());
  1116. // TODO(https://crbug.com/1335423): Change to DCHECK_GT() or remove after bug
  1117. // is fixed.
  1118. CHECK_GT(read_buf_len_, 0);
  1119. DCHECK(stream_ != nullptr);
  1120. next_state_ = STATE_READ_BODY_COMPLETE;
  1121. return stream_->ReadResponseBody(
  1122. read_buf_.get(), read_buf_len_, io_callback_);
  1123. }
  1124. int HttpNetworkTransaction::DoReadBodyComplete(int result) {
  1125. // We are done with the Read call.
  1126. bool done = false;
  1127. if (result <= 0) {
  1128. DCHECK_NE(ERR_IO_PENDING, result);
  1129. done = true;
  1130. }
  1131. // Clean up connection if we are done.
  1132. if (done) {
  1133. // Note: Just because IsResponseBodyComplete is true, we're not
  1134. // necessarily "done". We're only "done" when it is the last
  1135. // read on this HttpNetworkTransaction, which will be signified
  1136. // by a zero-length read.
  1137. // TODO(mbelshe): The keep-alive property is really a property of
  1138. // the stream. No need to compute it here just to pass back
  1139. // to the stream's Close function.
  1140. bool keep_alive =
  1141. stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
  1142. stream_->Close(!keep_alive);
  1143. // Note: we don't reset the stream here. We've closed it, but we still
  1144. // need it around so that callers can call methods such as
  1145. // GetUploadProgress() and have them be meaningful.
  1146. // TODO(mbelshe): This means we closed the stream here, and we close it
  1147. // again in ~HttpNetworkTransaction. Clean that up.
  1148. // The next Read call will return 0 (EOF).
  1149. // This transaction was successful. If it had been retried because of an
  1150. // error with an alternative service, mark that alternative service broken.
  1151. if (!enable_alternative_services_ &&
  1152. retried_alternative_service_.protocol != kProtoUnknown) {
  1153. HistogramBrokenAlternateProtocolLocation(
  1154. BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_NETWORK_TRANSACTION);
  1155. session_->http_server_properties()->MarkAlternativeServiceBroken(
  1156. retried_alternative_service_, network_isolation_key_);
  1157. }
  1158. #if BUILDFLAG(ENABLE_REPORTING)
  1159. GenerateNetworkErrorLoggingReport(result);
  1160. #endif // BUILDFLAG(ENABLE_REPORTING)
  1161. }
  1162. // Clear these to avoid leaving around old state.
  1163. read_buf_ = nullptr;
  1164. read_buf_len_ = 0;
  1165. return result;
  1166. }
  1167. int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
  1168. // This method differs from DoReadBody only in the next_state_. So we just
  1169. // call DoReadBody and override the next_state_. Perhaps there is a more
  1170. // elegant way for these two methods to share code.
  1171. int rv = DoReadBody();
  1172. DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
  1173. next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
  1174. return rv;
  1175. }
  1176. // TODO(wtc): This method and the DoReadBodyComplete method are almost
  1177. // the same. Figure out a good way for these two methods to share code.
  1178. int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
  1179. // keep_alive defaults to true because the very reason we're draining the
  1180. // response body is to reuse the connection for auth restart.
  1181. bool done = false, keep_alive = true;
  1182. if (result < 0) {
  1183. // Error or closed connection while reading the socket.
  1184. // Note: No Network Error Logging report is generated here because a report
  1185. // will have already been generated for the original request due to the auth
  1186. // challenge, so a second report is not generated for the same request here.
  1187. done = true;
  1188. keep_alive = false;
  1189. } else if (stream_->IsResponseBodyComplete()) {
  1190. done = true;
  1191. }
  1192. if (done) {
  1193. DidDrainBodyForAuthRestart(keep_alive);
  1194. } else {
  1195. // Keep draining.
  1196. next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
  1197. }
  1198. return OK;
  1199. }
  1200. #if BUILDFLAG(ENABLE_REPORTING)
  1201. void HttpNetworkTransaction::ProcessReportToHeader() {
  1202. std::string value;
  1203. if (!response_.headers->GetNormalizedHeader("Report-To", &value))
  1204. return;
  1205. ReportingService* reporting_service = session_->reporting_service();
  1206. if (!reporting_service)
  1207. return;
  1208. // Only accept Report-To headers on HTTPS connections that have no
  1209. // certificate errors.
  1210. if (!response_.ssl_info.is_valid())
  1211. return;
  1212. if (IsCertStatusError(response_.ssl_info.cert_status))
  1213. return;
  1214. reporting_service->ProcessReportToHeader(url::Origin::Create(url_),
  1215. network_isolation_key_, value);
  1216. }
  1217. void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() {
  1218. std::string value;
  1219. if (!response_.headers->GetNormalizedHeader(
  1220. NetworkErrorLoggingService::kHeaderName, &value)) {
  1221. return;
  1222. }
  1223. NetworkErrorLoggingService* network_error_logging_service =
  1224. session_->network_error_logging_service();
  1225. if (!network_error_logging_service)
  1226. return;
  1227. // Don't accept NEL headers received via a proxy, because the IP address of
  1228. // the destination server is not known.
  1229. if (response_.was_fetched_via_proxy)
  1230. return;
  1231. // Only accept NEL headers on HTTPS connections that have no certificate
  1232. // errors.
  1233. if (!response_.ssl_info.is_valid() ||
  1234. IsCertStatusError(response_.ssl_info.cert_status)) {
  1235. return;
  1236. }
  1237. if (remote_endpoint_.address().empty())
  1238. return;
  1239. network_error_logging_service->OnHeader(network_isolation_key_,
  1240. url::Origin::Create(url_),
  1241. remote_endpoint_.address(), value);
  1242. }
  1243. void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) {
  1244. if (rv < 0 && rv != ERR_IO_PENDING)
  1245. GenerateNetworkErrorLoggingReport(rv);
  1246. }
  1247. void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) {
  1248. // |rv| should be a valid net::Error
  1249. DCHECK_NE(rv, ERR_IO_PENDING);
  1250. DCHECK_LE(rv, 0);
  1251. if (network_error_logging_report_generated_)
  1252. return;
  1253. network_error_logging_report_generated_ = true;
  1254. NetworkErrorLoggingService* service =
  1255. session_->network_error_logging_service();
  1256. if (!service)
  1257. return;
  1258. // Don't report on proxy auth challenges.
  1259. if (response_.headers && response_.headers->response_code() ==
  1260. HTTP_PROXY_AUTHENTICATION_REQUIRED) {
  1261. return;
  1262. }
  1263. // Don't generate NEL reports if we are behind a proxy, to avoid leaking
  1264. // internal network details.
  1265. if (response_.was_fetched_via_proxy)
  1266. return;
  1267. // Ignore errors from non-HTTPS origins.
  1268. if (!url_.SchemeIsCryptographic())
  1269. return;
  1270. NetworkErrorLoggingService::RequestDetails details;
  1271. details.network_isolation_key = network_isolation_key_;
  1272. details.uri = url_;
  1273. if (!request_referrer_.empty())
  1274. details.referrer = GURL(request_referrer_);
  1275. details.user_agent = request_user_agent_;
  1276. if (!remote_endpoint_.address().empty()) {
  1277. details.server_ip = remote_endpoint_.address();
  1278. } else if (!connection_attempts_.empty()) {
  1279. // When we failed to connect to the server, `remote_endpoint_` is not set.
  1280. // In such case, we use the last endpoint address of `connection_attempts_`
  1281. // for the NEL report. This address information is important for the
  1282. // downgrade step to protect against port scan attack.
  1283. // https://www.w3.org/TR/network-error-logging/#generate-a-network-error-report
  1284. details.server_ip = connection_attempts_.back().endpoint.address();
  1285. } else {
  1286. details.server_ip = IPAddress();
  1287. }
  1288. // HttpResponseHeaders::response_code() returns 0 if response code couldn't
  1289. // be parsed, which is also how NEL represents the same.
  1290. if (response_.headers) {
  1291. details.status_code = response_.headers->response_code();
  1292. } else {
  1293. details.status_code = 0;
  1294. }
  1295. // If we got response headers, assume that the connection used HTTP/1.1
  1296. // unless ALPN negotiation tells us otherwise (handled below).
  1297. if (response_.was_alpn_negotiated) {
  1298. details.protocol = response_.alpn_negotiated_protocol;
  1299. } else {
  1300. details.protocol = "http/1.1";
  1301. }
  1302. details.method = request_method_;
  1303. details.elapsed_time = base::TimeTicks::Now() - start_timeticks_;
  1304. details.type = static_cast<Error>(rv);
  1305. details.reporting_upload_depth = request_reporting_upload_depth_;
  1306. service->OnRequest(std::move(details));
  1307. }
  1308. #endif // BUILDFLAG(ENABLE_REPORTING)
  1309. int HttpNetworkTransaction::HandleHttp11Required(int error) {
  1310. DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
  1311. error == ERR_PROXY_HTTP_1_1_REQUIRED);
  1312. // HttpServerProperties should have been updated, so when the request is sent
  1313. // again, it will automatically use HTTP/1.1.
  1314. ResetConnectionAndRequestForResend();
  1315. return OK;
  1316. }
  1317. int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
  1318. // Client certificate errors may come from either the origin server or the
  1319. // proxy.
  1320. //
  1321. // Origin errors are handled here, while most proxy errors are handled in the
  1322. // HttpStreamFactory and below, while handshaking with the proxy. However, in
  1323. // TLS 1.2 with False Start, or TLS 1.3, client certificate errors are
  1324. // reported immediately after the handshake. The error will then surface out
  1325. // of the first Read() rather than Connect().
  1326. //
  1327. // If the request is tunneled (i.e. the origin is HTTPS), this first Read()
  1328. // occurs while establishing the tunnel and HttpStreamFactory handles the
  1329. // proxy error. However, if the request is not tunneled (i.e. the origin is
  1330. // HTTP), this first Read() happens late and is ultimately surfaced out of
  1331. // DoReadHeadersComplete(). This method will then be responsible for both
  1332. // origin and proxy errors.
  1333. //
  1334. // See https://crbug.com/828965.
  1335. bool is_server = !UsingHttpProxyWithoutTunnel();
  1336. HostPortPair host_port_pair =
  1337. is_server ? HostPortPair::FromURL(request_->url)
  1338. : proxy_info_.proxy_server().host_port_pair();
  1339. if (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error)) {
  1340. DCHECK((is_server && IsSecureRequest()) ||
  1341. proxy_info_.is_secure_http_like());
  1342. if (session_->ssl_client_context()->ClearClientCertificate(
  1343. host_port_pair)) {
  1344. // The private key handle may have gone stale due to, e.g., the user
  1345. // unplugging their smartcard. Operating systems do not provide reliable
  1346. // notifications for this, so if the signature failed and the user was
  1347. // not already prompted for certificate on this request, retry to ask
  1348. // the user for a new one.
  1349. //
  1350. // TODO(davidben): There is no corresponding feature for proxy client
  1351. // certificates. Ideally this would live at a lower level, common to both,
  1352. // but |configured_client_cert_for_server_| is not accessible below the
  1353. // socket pools.
  1354. if (is_server && error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
  1355. !configured_client_cert_for_server_ && !HasExceededMaxRetries()) {
  1356. retry_attempts_++;
  1357. net_log_.AddEventWithNetErrorCode(
  1358. NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
  1359. ResetConnectionAndRequestForResend();
  1360. return OK;
  1361. }
  1362. }
  1363. }
  1364. return error;
  1365. }
  1366. // This method determines whether it is safe to resend the request after an
  1367. // IO error. It should only be called in response to errors received before
  1368. // final set of response headers have been successfully parsed, that the
  1369. // transaction may need to be retried on.
  1370. // It should not be used in other cases, such as a Connect error.
  1371. int HttpNetworkTransaction::HandleIOError(int error) {
  1372. // Because the peer may request renegotiation with client authentication at
  1373. // any time, check and handle client authentication errors.
  1374. error = HandleSSLClientAuthError(error);
  1375. #if BUILDFLAG(ENABLE_REPORTING)
  1376. GenerateNetworkErrorLoggingReportIfError(error);
  1377. #endif // BUILDFLAG(ENABLE_REPORTING)
  1378. switch (error) {
  1379. // If we try to reuse a connection that the server is in the process of
  1380. // closing, we may end up successfully writing out our request (or a
  1381. // portion of our request) only to find a connection error when we try to
  1382. // read from (or finish writing to) the socket.
  1383. case ERR_CONNECTION_RESET:
  1384. case ERR_CONNECTION_CLOSED:
  1385. case ERR_CONNECTION_ABORTED:
  1386. // There can be a race between the socket pool checking checking whether a
  1387. // socket is still connected, receiving the FIN, and sending/reading data
  1388. // on a reused socket. If we receive the FIN between the connectedness
  1389. // check and writing/reading from the socket, we may first learn the socket
  1390. // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
  1391. // likely happen when trying to retrieve its IP address.
  1392. // See http://crbug.com/105824 for more details.
  1393. case ERR_SOCKET_NOT_CONNECTED:
  1394. // If a socket is closed on its initial request, HttpStreamParser returns
  1395. // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
  1396. // preconnected but failed to be used before the server timed it out.
  1397. case ERR_EMPTY_RESPONSE:
  1398. if (ShouldResendRequest()) {
  1399. net_log_.AddEventWithNetErrorCode(
  1400. NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
  1401. ResetConnectionAndRequestForResend();
  1402. error = OK;
  1403. }
  1404. break;
  1405. case ERR_EARLY_DATA_REJECTED:
  1406. case ERR_WRONG_VERSION_ON_EARLY_DATA:
  1407. net_log_.AddEventWithNetErrorCode(
  1408. NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
  1409. // Disable early data on the SSLConfig on a reset.
  1410. can_send_early_data_ = false;
  1411. ResetConnectionAndRequestForResend();
  1412. error = OK;
  1413. break;
  1414. case ERR_HTTP2_PING_FAILED:
  1415. case ERR_HTTP2_SERVER_REFUSED_STREAM:
  1416. case ERR_HTTP2_PUSHED_STREAM_NOT_AVAILABLE:
  1417. case ERR_HTTP2_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
  1418. case ERR_HTTP2_PUSHED_RESPONSE_DOES_NOT_MATCH:
  1419. case ERR_QUIC_HANDSHAKE_FAILED:
  1420. case ERR_QUIC_GOAWAY_REQUEST_CAN_BE_RETRIED:
  1421. if (HasExceededMaxRetries())
  1422. break;
  1423. net_log_.AddEventWithNetErrorCode(
  1424. NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
  1425. retry_attempts_++;
  1426. ResetConnectionAndRequestForResend();
  1427. error = OK;
  1428. break;
  1429. case ERR_QUIC_PROTOCOL_ERROR:
  1430. if (GetResponseHeaders() != nullptr ||
  1431. !stream_->GetAlternativeService(&retried_alternative_service_)) {
  1432. // If the response headers have already been recieved and passed up
  1433. // then the request can not be retried. Also, if there was no
  1434. // alternative service used for this request, then there is no
  1435. // alternative service to be disabled.
  1436. break;
  1437. }
  1438. if (HasExceededMaxRetries())
  1439. break;
  1440. if (session_->http_server_properties()->IsAlternativeServiceBroken(
  1441. retried_alternative_service_, network_isolation_key_)) {
  1442. // If the alternative service was marked as broken while the request
  1443. // was in flight, retry the request which will not use the broken
  1444. // alternative service.
  1445. net_log_.AddEventWithNetErrorCode(
  1446. NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
  1447. retry_attempts_++;
  1448. ResetConnectionAndRequestForResend();
  1449. error = OK;
  1450. } else if (session_->context()
  1451. .quic_context->params()
  1452. ->retry_without_alt_svc_on_quic_errors) {
  1453. // Disable alternative services for this request and retry it. If the
  1454. // retry succeeds, then the alternative service will be marked as
  1455. // broken then.
  1456. enable_alternative_services_ = false;
  1457. net_log_.AddEventWithNetErrorCode(
  1458. NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
  1459. retry_attempts_++;
  1460. ResetConnectionAndRequestForResend();
  1461. error = OK;
  1462. }
  1463. break;
  1464. }
  1465. return error;
  1466. }
  1467. void HttpNetworkTransaction::ResetStateForRestart() {
  1468. ResetStateForAuthRestart();
  1469. if (stream_) {
  1470. total_received_bytes_ += stream_->GetTotalReceivedBytes();
  1471. total_sent_bytes_ += stream_->GetTotalSentBytes();
  1472. }
  1473. CacheNetErrorDetailsAndResetStream();
  1474. }
  1475. void HttpNetworkTransaction::ResetStateForAuthRestart() {
  1476. send_start_time_ = base::TimeTicks();
  1477. send_end_time_ = base::TimeTicks();
  1478. pending_auth_target_ = HttpAuth::AUTH_NONE;
  1479. read_buf_ = nullptr;
  1480. read_buf_len_ = 0;
  1481. headers_valid_ = false;
  1482. request_headers_.Clear();
  1483. response_ = HttpResponseInfo();
  1484. SetProxyInfoInReponse(proxy_info_, &response_);
  1485. establishing_tunnel_ = false;
  1486. remote_endpoint_ = IPEndPoint();
  1487. net_error_details_.quic_broken = false;
  1488. net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR;
  1489. #if BUILDFLAG(ENABLE_REPORTING)
  1490. network_error_logging_report_generated_ = false;
  1491. start_timeticks_ = base::TimeTicks::Now();
  1492. #endif // BUILDFLAG(ENABLE_REPORTING)
  1493. }
  1494. void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
  1495. if (stream_)
  1496. stream_->PopulateNetErrorDetails(&net_error_details_);
  1497. stream_.reset();
  1498. }
  1499. HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
  1500. return response_.headers.get();
  1501. }
  1502. bool HttpNetworkTransaction::ShouldResendRequest() const {
  1503. bool connection_is_proven = stream_->IsConnectionReused();
  1504. bool has_received_headers = GetResponseHeaders() != nullptr;
  1505. // NOTE: we resend a request only if we reused a keep-alive connection.
  1506. // This automatically prevents an infinite resend loop because we'll run
  1507. // out of the cached keep-alive connections eventually.
  1508. return connection_is_proven && !has_received_headers;
  1509. }
  1510. bool HttpNetworkTransaction::HasExceededMaxRetries() const {
  1511. return (retry_attempts_ >= kMaxRetryAttempts);
  1512. }
  1513. bool HttpNetworkTransaction::CheckMaxRestarts() {
  1514. num_restarts_++;
  1515. return num_restarts_ < kMaxRestarts;
  1516. }
  1517. void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
  1518. if (stream_.get()) {
  1519. stream_->Close(true);
  1520. CacheNetErrorDetailsAndResetStream();
  1521. }
  1522. // We need to clear request_headers_ because it contains the real request
  1523. // headers, but we may need to resend the CONNECT request first to recreate
  1524. // the SSL tunnel.
  1525. request_headers_.Clear();
  1526. next_state_ = STATE_CREATE_STREAM; // Resend the request.
  1527. #if BUILDFLAG(ENABLE_REPORTING)
  1528. // Reset for new request.
  1529. network_error_logging_report_generated_ = false;
  1530. start_timeticks_ = base::TimeTicks::Now();
  1531. #endif // BUILDFLAG(ENABLE_REPORTING)
  1532. ResetStateForRestart();
  1533. }
  1534. bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
  1535. return UsingHttpProxyWithoutTunnel();
  1536. }
  1537. bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
  1538. return request_->privacy_mode == PRIVACY_MODE_DISABLED;
  1539. }
  1540. int HttpNetworkTransaction::HandleAuthChallenge() {
  1541. scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
  1542. DCHECK(headers.get());
  1543. int status = headers->response_code();
  1544. if (status != HTTP_UNAUTHORIZED &&
  1545. status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
  1546. return OK;
  1547. HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
  1548. HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
  1549. if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
  1550. return ERR_UNEXPECTED_PROXY_AUTH;
  1551. // This case can trigger when an HTTPS server responds with a "Proxy
  1552. // authentication required" status code through a non-authenticating
  1553. // proxy.
  1554. if (!auth_controllers_[target].get())
  1555. return ERR_UNEXPECTED_PROXY_AUTH;
  1556. int rv = auth_controllers_[target]->HandleAuthChallenge(
  1557. headers, response_.ssl_info, !ShouldApplyServerAuth(), false, net_log_);
  1558. if (auth_controllers_[target]->HaveAuthHandler())
  1559. pending_auth_target_ = target;
  1560. auth_controllers_[target]->TakeAuthInfo(&response_.auth_challenge);
  1561. return rv;
  1562. }
  1563. bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
  1564. return auth_controllers_[target].get() &&
  1565. auth_controllers_[target]->HaveAuth();
  1566. }
  1567. GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
  1568. switch (target) {
  1569. case HttpAuth::AUTH_PROXY: {
  1570. if (!proxy_info_.proxy_server().is_valid() ||
  1571. proxy_info_.proxy_server().is_direct()) {
  1572. return GURL(); // There is no proxy server.
  1573. }
  1574. // TODO(https://crbug.com/1103768): Mapping proxy addresses to
  1575. // URLs is a lossy conversion, shouldn't do this.
  1576. const char* scheme =
  1577. proxy_info_.is_secure_http_like() ? "https://" : "http://";
  1578. return GURL(scheme +
  1579. proxy_info_.proxy_server().host_port_pair().ToString());
  1580. }
  1581. case HttpAuth::AUTH_SERVER:
  1582. if (ForWebSocketHandshake()) {
  1583. return net::ChangeWebSocketSchemeToHttpScheme(request_->url);
  1584. }
  1585. return request_->url;
  1586. default:
  1587. return GURL();
  1588. }
  1589. }
  1590. bool HttpNetworkTransaction::ForWebSocketHandshake() const {
  1591. return websocket_handshake_stream_base_create_helper_ &&
  1592. request_->url.SchemeIsWSOrWSS();
  1593. }
  1594. void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
  1595. DCHECK(stream_request_);
  1596. // Since the transaction can restart with auth credentials, it may create a
  1597. // stream more than once. Accumulate all of the connection attempts across
  1598. // those streams by appending them to the vector:
  1599. for (const auto& attempt : stream_request_->connection_attempts())
  1600. connection_attempts_.push_back(attempt);
  1601. }
  1602. bool HttpNetworkTransaction::ContentEncodingsValid() const {
  1603. HttpResponseHeaders* headers = GetResponseHeaders();
  1604. DCHECK(headers);
  1605. std::string accept_encoding;
  1606. request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
  1607. &accept_encoding);
  1608. std::set<std::string> allowed_encodings;
  1609. if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings))
  1610. return false;
  1611. std::string content_encoding;
  1612. headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
  1613. std::set<std::string> used_encodings;
  1614. if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings))
  1615. return false;
  1616. // When "Accept-Encoding" is not specified, it is parsed as "*".
  1617. // If "*" encoding is advertised, then any encoding should be "accepted".
  1618. // This does not mean, that it will be successfully decoded.
  1619. if (allowed_encodings.find("*") != allowed_encodings.end())
  1620. return true;
  1621. bool result = true;
  1622. for (auto const& encoding : used_encodings) {
  1623. SourceStream::SourceType source_type =
  1624. FilterSourceStream::ParseEncodingType(encoding);
  1625. // We don't reject encodings we are not aware. They just will not decode.
  1626. if (source_type == SourceStream::TYPE_UNKNOWN)
  1627. continue;
  1628. if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
  1629. result = false;
  1630. break;
  1631. }
  1632. }
  1633. // Temporary workaround for http://crbug.com/714514
  1634. if (headers->IsRedirect(nullptr)) {
  1635. UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
  1636. !result);
  1637. return true;
  1638. }
  1639. return result;
  1640. }
  1641. } // namespace net