spdy_proxy_client_socket.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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/spdy/spdy_proxy_client_socket.h"
  5. #include <algorithm> // min
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/callback_helpers.h"
  9. #include "base/check_op.h"
  10. #include "base/location.h"
  11. #include "base/notreached.h"
  12. #include "base/strings/string_util.h"
  13. #include "base/task/single_thread_task_runner.h"
  14. #include "base/threading/thread_task_runner_handle.h"
  15. #include "base/values.h"
  16. #include "net/base/auth.h"
  17. #include "net/base/io_buffer.h"
  18. #include "net/base/proxy_delegate.h"
  19. #include "net/http/http_auth_cache.h"
  20. #include "net/http/http_auth_handler_factory.h"
  21. #include "net/http/http_log_util.h"
  22. #include "net/http/http_request_info.h"
  23. #include "net/http/http_response_headers.h"
  24. #include "net/log/net_log_event_type.h"
  25. #include "net/log/net_log_source_type.h"
  26. #include "net/spdy/spdy_http_utils.h"
  27. #include "net/traffic_annotation/network_traffic_annotation.h"
  28. #include "url/gurl.h"
  29. namespace net {
  30. SpdyProxyClientSocket::SpdyProxyClientSocket(
  31. const base::WeakPtr<SpdyStream>& spdy_stream,
  32. const ProxyServer& proxy_server,
  33. const std::string& user_agent,
  34. const HostPortPair& endpoint,
  35. const NetLogWithSource& source_net_log,
  36. scoped_refptr<HttpAuthController> auth_controller,
  37. ProxyDelegate* proxy_delegate)
  38. : spdy_stream_(spdy_stream),
  39. endpoint_(endpoint),
  40. auth_(std::move(auth_controller)),
  41. proxy_server_(proxy_server),
  42. proxy_delegate_(proxy_delegate),
  43. user_agent_(user_agent),
  44. net_log_(NetLogWithSource::Make(spdy_stream->net_log().net_log(),
  45. NetLogSourceType::PROXY_CLIENT_SOCKET)),
  46. source_dependency_(source_net_log.source()) {
  47. request_.method = "CONNECT";
  48. request_.url = GURL("https://" + endpoint.ToString());
  49. net_log_.BeginEventReferencingSource(NetLogEventType::SOCKET_ALIVE,
  50. source_net_log.source());
  51. net_log_.AddEventReferencingSource(
  52. NetLogEventType::HTTP2_PROXY_CLIENT_SESSION,
  53. spdy_stream->net_log().source());
  54. spdy_stream_->SetDelegate(this);
  55. was_ever_used_ = spdy_stream_->WasEverUsed();
  56. }
  57. SpdyProxyClientSocket::~SpdyProxyClientSocket() {
  58. Disconnect();
  59. net_log_.EndEvent(NetLogEventType::SOCKET_ALIVE);
  60. }
  61. const HttpResponseInfo* SpdyProxyClientSocket::GetConnectResponseInfo() const {
  62. return response_.headers.get() ? &response_ : nullptr;
  63. }
  64. const scoped_refptr<HttpAuthController>&
  65. SpdyProxyClientSocket::GetAuthController() const {
  66. return auth_;
  67. }
  68. int SpdyProxyClientSocket::RestartWithAuth(CompletionOnceCallback callback) {
  69. // A SPDY Stream can only handle a single request, so the underlying
  70. // stream may not be reused and a new SpdyProxyClientSocket must be
  71. // created (possibly on top of the same SPDY Session).
  72. next_state_ = STATE_DISCONNECTED;
  73. return ERR_UNABLE_TO_REUSE_CONNECTION_FOR_PROXY_AUTH;
  74. }
  75. // Ignore priority changes, just use priority of initial request. Since multiple
  76. // requests are pooled on the SpdyProxyClientSocket, reprioritization doesn't
  77. // really work.
  78. //
  79. // TODO(mmenke): Use a single priority value for all SpdyProxyClientSockets,
  80. // regardless of what priority they're created with.
  81. void SpdyProxyClientSocket::SetStreamPriority(RequestPriority priority) {}
  82. // Sends a HEADERS frame to the proxy with a CONNECT request
  83. // for the specified endpoint. Waits for the server to send back
  84. // a HEADERS frame. OK will be returned if the status is 200.
  85. // ERR_TUNNEL_CONNECTION_FAILED will be returned for any other status.
  86. // In any of these cases, Read() may be called to retrieve the HTTP
  87. // response body. Any other return values should be considered fatal.
  88. // TODO(rch): handle 407 proxy auth requested correctly, perhaps
  89. // by creating a new stream for the subsequent request.
  90. // TODO(rch): create a more appropriate error code to disambiguate
  91. // the HTTPS Proxy tunnel failure from an HTTP Proxy tunnel failure.
  92. int SpdyProxyClientSocket::Connect(CompletionOnceCallback callback) {
  93. DCHECK(read_callback_.is_null());
  94. if (next_state_ == STATE_OPEN)
  95. return OK;
  96. DCHECK_EQ(STATE_DISCONNECTED, next_state_);
  97. next_state_ = STATE_GENERATE_AUTH_TOKEN;
  98. int rv = DoLoop(OK);
  99. if (rv == ERR_IO_PENDING)
  100. read_callback_ = std::move(callback);
  101. return rv;
  102. }
  103. void SpdyProxyClientSocket::Disconnect() {
  104. read_buffer_queue_.Clear();
  105. user_buffer_ = nullptr;
  106. user_buffer_len_ = 0;
  107. read_callback_.Reset();
  108. write_buffer_len_ = 0;
  109. write_callback_.Reset();
  110. write_callback_weak_factory_.InvalidateWeakPtrs();
  111. next_state_ = STATE_DISCONNECTED;
  112. if (spdy_stream_.get()) {
  113. // This will cause OnClose to be invoked, which takes care of
  114. // cleaning up all the internal state.
  115. spdy_stream_->Cancel(ERR_ABORTED);
  116. DCHECK(!spdy_stream_.get());
  117. }
  118. }
  119. bool SpdyProxyClientSocket::IsConnected() const {
  120. return next_state_ == STATE_OPEN;
  121. }
  122. bool SpdyProxyClientSocket::IsConnectedAndIdle() const {
  123. return IsConnected() && read_buffer_queue_.IsEmpty() &&
  124. spdy_stream_->IsOpen();
  125. }
  126. const NetLogWithSource& SpdyProxyClientSocket::NetLog() const {
  127. return net_log_;
  128. }
  129. bool SpdyProxyClientSocket::WasEverUsed() const {
  130. return was_ever_used_ || (spdy_stream_.get() && spdy_stream_->WasEverUsed());
  131. }
  132. bool SpdyProxyClientSocket::WasAlpnNegotiated() const {
  133. // Do not delegate to `spdy_stream_`. While `spdy_stream_` negotiated ALPN
  134. // with the proxy, this object represents the tunneled TCP connection to the
  135. // origin.
  136. return false;
  137. }
  138. NextProto SpdyProxyClientSocket::GetNegotiatedProtocol() const {
  139. // Do not delegate to `spdy_stream_`. While `spdy_stream_` negotiated ALPN
  140. // with the proxy, this object represents the tunneled TCP connection to the
  141. // origin.
  142. return kProtoUnknown;
  143. }
  144. bool SpdyProxyClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
  145. // Do not delegate to `spdy_stream_`. While `spdy_stream_` connected to the
  146. // proxy with TLS, this object represents the tunneled TCP connection to the
  147. // origin.
  148. return false;
  149. }
  150. int64_t SpdyProxyClientSocket::GetTotalReceivedBytes() const {
  151. NOTIMPLEMENTED();
  152. return 0;
  153. }
  154. void SpdyProxyClientSocket::ApplySocketTag(const SocketTag& tag) {
  155. // In the case of a connection to the proxy using HTTP/2 or HTTP/3 where the
  156. // underlying socket may multiplex multiple streams, applying this request's
  157. // socket tag to the multiplexed session would incorrectly apply the socket
  158. // tag to all mutliplexed streams. Fortunately socket tagging is only
  159. // supported on Android without the data reduction proxy, so only simple HTTP
  160. // proxies are supported, so proxies won't be using HTTP/2 or HTTP/3. Enforce
  161. // that a specific (non-default) tag isn't being applied.
  162. CHECK(tag == SocketTag());
  163. }
  164. int SpdyProxyClientSocket::Read(IOBuffer* buf,
  165. int buf_len,
  166. CompletionOnceCallback callback) {
  167. int rv = ReadIfReady(buf, buf_len, std::move(callback));
  168. if (rv == ERR_IO_PENDING) {
  169. user_buffer_ = buf;
  170. user_buffer_len_ = static_cast<size_t>(buf_len);
  171. }
  172. return rv;
  173. }
  174. int SpdyProxyClientSocket::ReadIfReady(IOBuffer* buf,
  175. int buf_len,
  176. CompletionOnceCallback callback) {
  177. DCHECK(!read_callback_);
  178. DCHECK(!user_buffer_);
  179. if (next_state_ == STATE_DISCONNECTED)
  180. return ERR_SOCKET_NOT_CONNECTED;
  181. if (next_state_ == STATE_CLOSED && read_buffer_queue_.IsEmpty()) {
  182. return 0;
  183. }
  184. DCHECK(next_state_ == STATE_OPEN || next_state_ == STATE_CLOSED);
  185. DCHECK(buf);
  186. size_t result = PopulateUserReadBuffer(buf->data(), buf_len);
  187. if (result == 0) {
  188. read_callback_ = std::move(callback);
  189. return ERR_IO_PENDING;
  190. }
  191. return result;
  192. }
  193. int SpdyProxyClientSocket::CancelReadIfReady() {
  194. // Only a pending ReadIfReady() can be canceled.
  195. DCHECK(!user_buffer_) << "Pending Read() cannot be canceled";
  196. read_callback_.Reset();
  197. return OK;
  198. }
  199. size_t SpdyProxyClientSocket::PopulateUserReadBuffer(char* data, size_t len) {
  200. return read_buffer_queue_.Dequeue(data, len);
  201. }
  202. int SpdyProxyClientSocket::Write(
  203. IOBuffer* buf,
  204. int buf_len,
  205. CompletionOnceCallback callback,
  206. const NetworkTrafficAnnotationTag& traffic_annotation) {
  207. DCHECK(write_callback_.is_null());
  208. if (next_state_ != STATE_OPEN)
  209. return ERR_SOCKET_NOT_CONNECTED;
  210. if (end_stream_state_ == EndStreamState::kEndStreamSent)
  211. return ERR_CONNECTION_CLOSED;
  212. DCHECK(spdy_stream_.get());
  213. spdy_stream_->SendData(buf, buf_len, MORE_DATA_TO_SEND);
  214. net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_SENT, buf_len,
  215. buf->data());
  216. write_callback_ = std::move(callback);
  217. write_buffer_len_ = buf_len;
  218. return ERR_IO_PENDING;
  219. }
  220. int SpdyProxyClientSocket::SetReceiveBufferSize(int32_t size) {
  221. // Since this StreamSocket sits on top of a shared SpdySession, it
  222. // is not safe for callers to change this underlying socket.
  223. return ERR_NOT_IMPLEMENTED;
  224. }
  225. int SpdyProxyClientSocket::SetSendBufferSize(int32_t size) {
  226. // Since this StreamSocket sits on top of a shared SpdySession, it
  227. // is not safe for callers to change this underlying socket.
  228. return ERR_NOT_IMPLEMENTED;
  229. }
  230. int SpdyProxyClientSocket::GetPeerAddress(IPEndPoint* address) const {
  231. if (!IsConnected())
  232. return ERR_SOCKET_NOT_CONNECTED;
  233. return spdy_stream_->GetPeerAddress(address);
  234. }
  235. int SpdyProxyClientSocket::GetLocalAddress(IPEndPoint* address) const {
  236. if (!IsConnected())
  237. return ERR_SOCKET_NOT_CONNECTED;
  238. return spdy_stream_->GetLocalAddress(address);
  239. }
  240. void SpdyProxyClientSocket::RunWriteCallback(CompletionOnceCallback callback,
  241. int result) const {
  242. std::move(callback).Run(result);
  243. if (end_stream_state_ == EndStreamState::kEndStreamReceived) {
  244. base::ThreadTaskRunnerHandle::Get()->PostTask(
  245. FROM_HERE, base::BindOnce(&SpdyProxyClientSocket::MaybeSendEndStream,
  246. weak_factory_.GetWeakPtr()));
  247. }
  248. }
  249. void SpdyProxyClientSocket::OnIOComplete(int result) {
  250. DCHECK_NE(STATE_DISCONNECTED, next_state_);
  251. int rv = DoLoop(result);
  252. if (rv != ERR_IO_PENDING) {
  253. std::move(read_callback_).Run(rv);
  254. }
  255. }
  256. int SpdyProxyClientSocket::DoLoop(int last_io_result) {
  257. DCHECK_NE(next_state_, STATE_DISCONNECTED);
  258. int rv = last_io_result;
  259. do {
  260. State state = next_state_;
  261. next_state_ = STATE_DISCONNECTED;
  262. switch (state) {
  263. case STATE_GENERATE_AUTH_TOKEN:
  264. DCHECK_EQ(OK, rv);
  265. rv = DoGenerateAuthToken();
  266. break;
  267. case STATE_GENERATE_AUTH_TOKEN_COMPLETE:
  268. rv = DoGenerateAuthTokenComplete(rv);
  269. break;
  270. case STATE_SEND_REQUEST:
  271. DCHECK_EQ(OK, rv);
  272. net_log_.BeginEvent(
  273. NetLogEventType::HTTP_TRANSACTION_TUNNEL_SEND_REQUEST);
  274. rv = DoSendRequest();
  275. break;
  276. case STATE_SEND_REQUEST_COMPLETE:
  277. net_log_.EndEventWithNetErrorCode(
  278. NetLogEventType::HTTP_TRANSACTION_TUNNEL_SEND_REQUEST, rv);
  279. rv = DoSendRequestComplete(rv);
  280. if (rv >= 0 || rv == ERR_IO_PENDING) {
  281. // Emit extra event so can use the same events as
  282. // HttpProxyClientSocket.
  283. net_log_.BeginEvent(
  284. NetLogEventType::HTTP_TRANSACTION_TUNNEL_READ_HEADERS);
  285. }
  286. break;
  287. case STATE_READ_REPLY_COMPLETE:
  288. rv = DoReadReplyComplete(rv);
  289. net_log_.EndEventWithNetErrorCode(
  290. NetLogEventType::HTTP_TRANSACTION_TUNNEL_READ_HEADERS, rv);
  291. break;
  292. default:
  293. NOTREACHED() << "bad state";
  294. rv = ERR_UNEXPECTED;
  295. break;
  296. }
  297. } while (rv != ERR_IO_PENDING && next_state_ != STATE_DISCONNECTED &&
  298. next_state_ != STATE_OPEN);
  299. return rv;
  300. }
  301. int SpdyProxyClientSocket::DoGenerateAuthToken() {
  302. next_state_ = STATE_GENERATE_AUTH_TOKEN_COMPLETE;
  303. return auth_->MaybeGenerateAuthToken(
  304. &request_,
  305. base::BindOnce(&SpdyProxyClientSocket::OnIOComplete,
  306. weak_factory_.GetWeakPtr()),
  307. net_log_);
  308. }
  309. int SpdyProxyClientSocket::DoGenerateAuthTokenComplete(int result) {
  310. DCHECK_NE(ERR_IO_PENDING, result);
  311. if (result == OK)
  312. next_state_ = STATE_SEND_REQUEST;
  313. return result;
  314. }
  315. int SpdyProxyClientSocket::DoSendRequest() {
  316. next_state_ = STATE_SEND_REQUEST_COMPLETE;
  317. // Add Proxy-Authentication header if necessary.
  318. HttpRequestHeaders authorization_headers;
  319. if (auth_->HaveAuth()) {
  320. auth_->AddAuthorizationHeader(&authorization_headers);
  321. }
  322. if (proxy_delegate_) {
  323. HttpRequestHeaders proxy_delegate_headers;
  324. proxy_delegate_->OnBeforeTunnelRequest(proxy_server_,
  325. &proxy_delegate_headers);
  326. request_.extra_headers.MergeFrom(proxy_delegate_headers);
  327. }
  328. std::string request_line;
  329. BuildTunnelRequest(endpoint_, authorization_headers, user_agent_,
  330. &request_line, &request_.extra_headers);
  331. NetLogRequestHeaders(net_log_,
  332. NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS,
  333. request_line, &request_.extra_headers);
  334. spdy::Http2HeaderBlock headers;
  335. CreateSpdyHeadersFromHttpRequest(request_, request_.extra_headers, &headers);
  336. return spdy_stream_->SendRequestHeaders(std::move(headers),
  337. MORE_DATA_TO_SEND);
  338. }
  339. int SpdyProxyClientSocket::DoSendRequestComplete(int result) {
  340. if (result < 0)
  341. return result;
  342. // Wait for HEADERS frame from the server
  343. next_state_ = STATE_READ_REPLY_COMPLETE;
  344. return ERR_IO_PENDING;
  345. }
  346. int SpdyProxyClientSocket::DoReadReplyComplete(int result) {
  347. // We enter this method directly from DoSendRequestComplete, since
  348. // we are notified by a callback when the HEADERS frame arrives.
  349. if (result < 0)
  350. return result;
  351. // Require the "HTTP/1.x" status line for SSL CONNECT.
  352. if (response_.headers->GetHttpVersion() < HttpVersion(1, 0))
  353. return ERR_TUNNEL_CONNECTION_FAILED;
  354. NetLogResponseHeaders(
  355. net_log_, NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS,
  356. response_.headers.get());
  357. if (proxy_delegate_) {
  358. int rv = proxy_delegate_->OnTunnelHeadersReceived(proxy_server_,
  359. *response_.headers);
  360. if (rv != OK) {
  361. DCHECK_NE(ERR_IO_PENDING, rv);
  362. return rv;
  363. }
  364. }
  365. switch (response_.headers->response_code()) {
  366. case 200: // OK
  367. next_state_ = STATE_OPEN;
  368. return OK;
  369. case 407: // Proxy Authentication Required
  370. next_state_ = STATE_OPEN;
  371. SanitizeProxyAuth(response_);
  372. return HandleProxyAuthChallenge(auth_.get(), &response_, net_log_);
  373. default:
  374. // Ignore response to avoid letting the proxy impersonate the target
  375. // server. (See http://crbug.com/137891.)
  376. return ERR_TUNNEL_CONNECTION_FAILED;
  377. }
  378. }
  379. // SpdyStream::Delegate methods:
  380. // Called when SYN frame has been sent.
  381. // Returns true if no more data to be sent after SYN frame.
  382. void SpdyProxyClientSocket::OnHeadersSent() {
  383. DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE);
  384. OnIOComplete(OK);
  385. }
  386. void SpdyProxyClientSocket::OnEarlyHintsReceived(
  387. const spdy::Http2HeaderBlock& headers) {}
  388. void SpdyProxyClientSocket::OnHeadersReceived(
  389. const spdy::Http2HeaderBlock& response_headers,
  390. const spdy::Http2HeaderBlock* pushed_request_headers) {
  391. // If we've already received the reply, existing headers are too late.
  392. // TODO(mbelshe): figure out a way to make HEADERS frames useful after the
  393. // initial response.
  394. if (next_state_ != STATE_READ_REPLY_COMPLETE)
  395. return;
  396. // Save the response
  397. const int rv = SpdyHeadersToHttpResponse(response_headers, &response_);
  398. DCHECK_NE(rv, ERR_INCOMPLETE_HTTP2_HEADERS);
  399. OnIOComplete(OK);
  400. }
  401. // Called when data is received or on EOF (if `buffer is nullptr).
  402. void SpdyProxyClientSocket::OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) {
  403. if (buffer) {
  404. net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_RECEIVED,
  405. buffer->GetRemainingSize(),
  406. buffer->GetRemainingData());
  407. read_buffer_queue_.Enqueue(std::move(buffer));
  408. } else {
  409. net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_RECEIVED, 0,
  410. nullptr);
  411. if (end_stream_state_ == EndStreamState::kNone) {
  412. // The peer sent END_STREAM. Schedule a DATA frame with END_STREAM.
  413. end_stream_state_ = EndStreamState::kEndStreamReceived;
  414. base::ThreadTaskRunnerHandle::Get()->PostTask(
  415. FROM_HERE, base::BindOnce(&SpdyProxyClientSocket::MaybeSendEndStream,
  416. weak_factory_.GetWeakPtr()));
  417. }
  418. }
  419. if (read_callback_) {
  420. if (user_buffer_) {
  421. int rv = PopulateUserReadBuffer(user_buffer_->data(), user_buffer_len_);
  422. user_buffer_ = nullptr;
  423. user_buffer_len_ = 0;
  424. std::move(read_callback_).Run(rv);
  425. } else {
  426. // If ReadIfReady() is used instead of Read(), tell the caller that data
  427. // is available for reading.
  428. std::move(read_callback_).Run(OK);
  429. }
  430. }
  431. }
  432. void SpdyProxyClientSocket::OnDataSent() {
  433. if (end_stream_state_ == EndStreamState::kEndStreamSent) {
  434. CHECK(write_callback_.is_null());
  435. return;
  436. }
  437. DCHECK(!write_callback_.is_null());
  438. int rv = write_buffer_len_;
  439. write_buffer_len_ = 0;
  440. // Proxy write callbacks result in deep callback chains. Post to allow the
  441. // stream's write callback chain to unwind (see crbug.com/355511).
  442. base::ThreadTaskRunnerHandle::Get()->PostTask(
  443. FROM_HERE, base::BindOnce(&SpdyProxyClientSocket::RunWriteCallback,
  444. write_callback_weak_factory_.GetWeakPtr(),
  445. std::move(write_callback_), rv));
  446. }
  447. void SpdyProxyClientSocket::OnTrailers(const spdy::Http2HeaderBlock& trailers) {
  448. // |spdy_stream_| is of type SPDY_BIDIRECTIONAL_STREAM, so trailers are
  449. // combined with response headers and this method will not be calld.
  450. NOTREACHED();
  451. }
  452. void SpdyProxyClientSocket::OnClose(int status) {
  453. was_ever_used_ = spdy_stream_->WasEverUsed();
  454. spdy_stream_.reset();
  455. bool connecting = next_state_ != STATE_DISCONNECTED &&
  456. next_state_ < STATE_OPEN;
  457. if (next_state_ == STATE_OPEN)
  458. next_state_ = STATE_CLOSED;
  459. else
  460. next_state_ = STATE_DISCONNECTED;
  461. base::WeakPtr<SpdyProxyClientSocket> weak_ptr = weak_factory_.GetWeakPtr();
  462. CompletionOnceCallback write_callback = std::move(write_callback_);
  463. write_buffer_len_ = 0;
  464. // If we're in the middle of connecting, we need to make sure
  465. // we invoke the connect callback.
  466. if (connecting) {
  467. DCHECK(!read_callback_.is_null());
  468. std::move(read_callback_).Run(status);
  469. } else if (!read_callback_.is_null()) {
  470. // If we have a read_callback_, the we need to make sure we call it back.
  471. OnDataReceived(std::unique_ptr<SpdyBuffer>());
  472. }
  473. // This may have been deleted by read_callback_, so check first.
  474. if (weak_ptr.get() && !write_callback.is_null())
  475. std::move(write_callback).Run(ERR_CONNECTION_CLOSED);
  476. }
  477. bool SpdyProxyClientSocket::CanGreaseFrameType() const {
  478. return false;
  479. }
  480. NetLogSource SpdyProxyClientSocket::source_dependency() const {
  481. return source_dependency_;
  482. }
  483. void SpdyProxyClientSocket::MaybeSendEndStream() {
  484. DCHECK_NE(end_stream_state_, EndStreamState::kNone);
  485. if (end_stream_state_ == EndStreamState::kEndStreamSent)
  486. return;
  487. if (!spdy_stream_)
  488. return;
  489. // When there is a pending write, wait until the write completes.
  490. if (write_callback_)
  491. return;
  492. auto buffer = base::MakeRefCounted<IOBuffer>(/*buffer_size=*/0);
  493. spdy_stream_->SendData(buffer.get(), /*length=*/0, NO_MORE_DATA_TO_SEND);
  494. end_stream_state_ = EndStreamState::kEndStreamSent;
  495. }
  496. } // namespace net