[email protected] | c0fe941d | 2012-02-25 00:15:32 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_ |
| 6 | #define NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_ |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 7 | |
[email protected] | ba34436 | 2013-04-17 10:16:33 | [diff] [blame] | 8 | #include <list> |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 9 | #include <string> |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 10 | |
| 11 | #include "base/basictypes.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
[email protected] | 6af4e41 | 2011-11-29 23:39:18 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 14 | #include "net/base/completion_callback.h" |
| 15 | #include "net/base/host_port_pair.h" |
[email protected] | 57d2dfa | 2013-06-24 06:04:12 | [diff] [blame] | 16 | #include "net/base/load_timing_info.h" |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 17 | #include "net/base/net_log.h" |
| 18 | #include "net/http/http_auth_controller.h" |
| 19 | #include "net/http/http_request_headers.h" |
| 20 | #include "net/http/http_request_info.h" |
| 21 | #include "net/http/http_response_info.h" |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 22 | #include "net/http/proxy_client_socket.h" |
| 23 | #include "net/spdy/spdy_http_stream.h" |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 24 | #include "net/spdy/spdy_protocol.h" |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 25 | #include "net/spdy/spdy_read_queue.h" |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 26 | #include "net/spdy/spdy_session.h" |
| 27 | #include "net/spdy/spdy_stream.h" |
| 28 | |
| 29 | |
| 30 | class GURL; |
| 31 | |
| 32 | namespace net { |
| 33 | |
| 34 | class AddressList; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 35 | class HttpStream; |
| 36 | class IOBuffer; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 37 | class SpdyStream; |
| 38 | |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 39 | class NET_EXPORT_PRIVATE SpdyProxyClientSocket : public ProxyClientSocket, |
| 40 | public SpdyStream::Delegate { |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 41 | public: |
| 42 | // Create a socket on top of the |spdy_stream| by sending a SYN_STREAM |
| 43 | // CONNECT frame for |endpoint|. After the SYN_REPLY is received, |
| 44 | // any data read/written to the socket will be transferred in data |
[email protected] | f6a7829 | 2013-03-09 14:36:34 | [diff] [blame] | 45 | // frames. This object will set itself as |spdy_stream|'s delegate. |
[email protected] | d26ff35 | 2013-05-13 08:48:28 | [diff] [blame] | 46 | SpdyProxyClientSocket(const base::WeakPtr<SpdyStream>& spdy_stream, |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 47 | const std::string& user_agent, |
| 48 | const HostPortPair& endpoint, |
| 49 | const GURL& url, |
| 50 | const HostPortPair& proxy_server, |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 51 | const BoundNetLog& source_net_log, |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 52 | HttpAuthCache* auth_cache, |
| 53 | HttpAuthHandlerFactory* auth_handler_factory); |
| 54 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 55 | |
| 56 | // On destruction Disconnect() is called. |
| 57 | virtual ~SpdyProxyClientSocket(); |
| 58 | |
[email protected] | 76725780 | 2011-12-14 17:33:18 | [diff] [blame] | 59 | // ProxyClientSocket methods: |
[email protected] | f2cbbc8 | 2011-11-16 01:10:29 | [diff] [blame] | 60 | virtual const HttpResponseInfo* GetConnectResponseInfo() const OVERRIDE; |
[email protected] | f2cbbc8 | 2011-11-16 01:10:29 | [diff] [blame] | 61 | virtual HttpStream* CreateConnectResponseStream() OVERRIDE; |
[email protected] | c0fe941d | 2012-02-25 00:15:32 | [diff] [blame] | 62 | virtual const scoped_refptr<HttpAuthController>& GetAuthController() const |
| 63 | OVERRIDE; |
| 64 | virtual int RestartWithAuth(const CompletionCallback& callback) OVERRIDE; |
| 65 | virtual bool IsUsingSpdy() const OVERRIDE; |
[email protected] | 8e3c78cb | 2012-03-31 03:58:46 | [diff] [blame] | 66 | virtual NextProto GetProtocolNegotiated() const OVERRIDE; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 67 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 68 | // StreamSocket implementation. |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 69 | virtual int Connect(const CompletionCallback& callback) OVERRIDE; |
[email protected] | f2cbbc8 | 2011-11-16 01:10:29 | [diff] [blame] | 70 | virtual void Disconnect() OVERRIDE; |
| 71 | virtual bool IsConnected() const OVERRIDE; |
| 72 | virtual bool IsConnectedAndIdle() const OVERRIDE; |
| 73 | virtual const BoundNetLog& NetLog() const OVERRIDE; |
| 74 | virtual void SetSubresourceSpeculation() OVERRIDE; |
| 75 | virtual void SetOmniboxSpeculation() OVERRIDE; |
| 76 | virtual bool WasEverUsed() const OVERRIDE; |
| 77 | virtual bool UsingTCPFastOpen() const OVERRIDE; |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 78 | virtual bool WasNpnNegotiated() const OVERRIDE; |
[email protected] | 33661e48 | 2012-04-03 16:16:26 | [diff] [blame] | 79 | virtual NextProto GetNegotiatedProtocol() const OVERRIDE; |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 80 | virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 81 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 82 | // Socket implementation. |
[email protected] | f2cbbc8 | 2011-11-16 01:10:29 | [diff] [blame] | 83 | virtual int Read(IOBuffer* buf, |
| 84 | int buf_len, |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 85 | const CompletionCallback& callback) OVERRIDE; |
[email protected] | 6a60c8c | 2011-11-11 05:27:26 | [diff] [blame] | 86 | virtual int Write(IOBuffer* buf, |
| 87 | int buf_len, |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 88 | const CompletionCallback& callback) OVERRIDE; |
[email protected] | f2cbbc8 | 2011-11-16 01:10:29 | [diff] [blame] | 89 | virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; |
| 90 | virtual bool SetSendBufferSize(int32 size) OVERRIDE; |
[email protected] | a352869 | 2012-06-08 00:11:42 | [diff] [blame] | 91 | virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; |
[email protected] | f2cbbc8 | 2011-11-16 01:10:29 | [diff] [blame] | 92 | virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 93 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 94 | // SpdyStream::Delegate implementation. |
[email protected] | edbfa8c | 2013-05-29 00:22:33 | [diff] [blame] | 95 | virtual void OnRequestHeadersSent() OVERRIDE; |
[email protected] | 6d116e1a | 2013-06-24 07:42:15 | [diff] [blame] | 96 | virtual SpdyResponseHeadersStatus OnResponseHeadersUpdated( |
| 97 | const SpdyHeaderBlock& response_headers) OVERRIDE; |
| 98 | virtual void OnDataReceived(scoped_ptr<SpdyBuffer> buffer) OVERRIDE; |
[email protected] | aa19cfc | 2013-05-23 16:41:38 | [diff] [blame] | 99 | virtual void OnDataSent() OVERRIDE; |
[email protected] | f2cbbc8 | 2011-11-16 01:10:29 | [diff] [blame] | 100 | virtual void OnClose(int status) OVERRIDE; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 101 | |
| 102 | private: |
| 103 | enum State { |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 104 | STATE_DISCONNECTED, |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 105 | STATE_GENERATE_AUTH_TOKEN, |
| 106 | STATE_GENERATE_AUTH_TOKEN_COMPLETE, |
| 107 | STATE_SEND_REQUEST, |
| 108 | STATE_SEND_REQUEST_COMPLETE, |
| 109 | STATE_READ_REPLY_COMPLETE, |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 110 | STATE_OPEN, |
| 111 | STATE_CLOSED |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 112 | }; |
| 113 | |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 114 | void LogBlockedTunnelResponse() const; |
| 115 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 116 | void OnIOComplete(int result); |
| 117 | |
| 118 | int DoLoop(int last_io_result); |
| 119 | int DoGenerateAuthToken(); |
| 120 | int DoGenerateAuthTokenComplete(int result); |
| 121 | int DoSendRequest(); |
| 122 | int DoSendRequestComplete(int result); |
| 123 | int DoReadReplyComplete(int result); |
| 124 | |
| 125 | // Populates |user_buffer_| with as much read data as possible |
| 126 | // and returns the number of bytes read. |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 127 | size_t PopulateUserReadBuffer(char* out, size_t len); |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 128 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 129 | State next_state_; |
| 130 | |
| 131 | // Pointer to the SPDY Stream that this sits on top of. |
[email protected] | d26ff35 | 2013-05-13 08:48:28 | [diff] [blame] | 132 | base::WeakPtr<SpdyStream> spdy_stream_; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 133 | |
| 134 | // Stores the callback to the layer above, called on completing Read() or |
| 135 | // Connect(). |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 136 | CompletionCallback read_callback_; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 137 | // Stores the callback to the layer above, called on completing Write(). |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 138 | CompletionCallback write_callback_; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 139 | |
| 140 | // CONNECT request and response. |
| 141 | HttpRequestInfo request_; |
| 142 | HttpResponseInfo response_; |
| 143 | |
| 144 | // The hostname and port of the endpoint. This is not necessarily the one |
| 145 | // specified by the URL, due to Alternate-Protocol or fixed testing ports. |
| 146 | const HostPortPair endpoint_; |
| 147 | scoped_refptr<HttpAuthController> auth_; |
| 148 | |
| 149 | // We buffer the response body as it arrives asynchronously from the stream. |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 150 | SpdyReadQueue read_buffer_queue_; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 151 | |
| 152 | // User provided buffer for the Read() response. |
[email protected] | ca690b0 | 2013-04-17 10:38:43 | [diff] [blame] | 153 | scoped_refptr<IOBuffer> user_buffer_; |
| 154 | size_t user_buffer_len_; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 155 | |
| 156 | // User specified number of bytes to be written. |
| 157 | int write_buffer_len_; |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 158 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 159 | // True if the transport socket has ever sent data. |
| 160 | bool was_ever_used_; |
| 161 | |
[email protected] | 57d2dfa | 2013-06-24 06:04:12 | [diff] [blame] | 162 | // Used only for redirects. |
| 163 | bool redirect_has_load_timing_info_; |
| 164 | LoadTimingInfo redirect_load_timing_info_; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 165 | |
[email protected] | 6af4e41 | 2011-11-29 23:39:18 | [diff] [blame] | 166 | base::WeakPtrFactory<SpdyProxyClientSocket> weak_factory_; |
| 167 | |
[email protected] | fe2f62a | 2010-10-01 03:34:07 | [diff] [blame] | 168 | const BoundNetLog net_log_; |
| 169 | |
| 170 | DISALLOW_COPY_AND_ASSIGN(SpdyProxyClientSocket); |
| 171 | }; |
| 172 | |
| 173 | } // namespace net |
| 174 | |
| 175 | #endif // NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_ |