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 5 #ifndef NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_ 6 #define NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_ 7 8 #include <list> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/weak_ptr.h" 14 #include "net/base/completion_callback.h" 15 #include "net/base/host_port_pair.h" 16 #include "net/base/load_timing_info.h" 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" 22 #include "net/http/proxy_client_socket.h" 23 #include "net/spdy/spdy_http_stream.h" 24 #include "net/spdy/spdy_protocol.h" 25 #include "net/spdy/spdy_read_queue.h" 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; 35 class HttpStream; 36 class IOBuffer; 37 class SpdyStream; 38 39 class NET_EXPORT_PRIVATE SpdyProxyClientSocket : public ProxyClientSocket, 40 public SpdyStream::Delegate { 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 45 // frames. This object will set itself as |spdy_stream|'s delegate. 46 SpdyProxyClientSocket(const base::WeakPtr<SpdyStream>& spdy_stream, 47 const std::string& user_agent, 48 const HostPortPair& endpoint, 49 const GURL& url, 50 const HostPortPair& proxy_server, 51 const BoundNetLog& source_net_log, 52 HttpAuthCache* auth_cache, 53 HttpAuthHandlerFactory* auth_handler_factory); 54 55 56 // On destruction Disconnect() is called. 57 virtual ~SpdyProxyClientSocket(); 58 59 // ProxyClientSocket methods: 60 virtual const HttpResponseInfo* GetConnectResponseInfo() const OVERRIDE; 61 virtual HttpStream* CreateConnectResponseStream() OVERRIDE; 62 virtual const scoped_refptr<HttpAuthController>& GetAuthController() const 63 OVERRIDE; 64 virtual int RestartWithAuth(const CompletionCallback& callback) OVERRIDE; 65 virtual bool IsUsingSpdy() const OVERRIDE; 66 virtual NextProto GetProtocolNegotiated() const OVERRIDE; 67 68 // StreamSocket implementation. 69 virtual int Connect(const CompletionCallback& callback) OVERRIDE; 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; 78 virtual bool WasNpnNegotiated() const OVERRIDE; 79 virtual NextProto GetNegotiatedProtocol() const OVERRIDE; 80 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; 81 82 // Socket implementation. 83 virtual int Read(IOBuffer* buf, 84 int buf_len, 85 const CompletionCallback& callback) OVERRIDE; 86 virtual int Write(IOBuffer* buf, 87 int buf_len, 88 const CompletionCallback& callback) OVERRIDE; 89 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; 90 virtual bool SetSendBufferSize(int32 size) OVERRIDE; 91 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; 92 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; 93 94 // SpdyStream::Delegate implementation. 95 virtual void OnRequestHeadersSent() OVERRIDE; 96 virtual SpdyResponseHeadersStatus OnResponseHeadersUpdated( 97 const SpdyHeaderBlock& response_headers) OVERRIDE; 98 virtual void OnDataReceived(scoped_ptr<SpdyBuffer> buffer) OVERRIDE; 99 virtual void OnDataSent() OVERRIDE; 100 virtual void OnClose(int status) OVERRIDE; 101 102 private: 103 enum State { 104 STATE_DISCONNECTED, 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, 110 STATE_OPEN, 111 STATE_CLOSED 112 }; 113 114 void LogBlockedTunnelResponse() const; 115 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. 127 size_t PopulateUserReadBuffer(char* out, size_t len); 128 129 State next_state_; 130 131 // Pointer to the SPDY Stream that this sits on top of. 132 base::WeakPtr<SpdyStream> spdy_stream_; 133 134 // Stores the callback to the layer above, called on completing Read() or 135 // Connect(). 136 CompletionCallback read_callback_; 137 // Stores the callback to the layer above, called on completing Write(). 138 CompletionCallback write_callback_; 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. 150 SpdyReadQueue read_buffer_queue_; 151 152 // User provided buffer for the Read() response. 153 scoped_refptr<IOBuffer> user_buffer_; 154 size_t user_buffer_len_; 155 156 // User specified number of bytes to be written. 157 int write_buffer_len_; 158 159 // True if the transport socket has ever sent data. 160 bool was_ever_used_; 161 162 // Used only for redirects. 163 bool redirect_has_load_timing_info_; 164 LoadTimingInfo redirect_load_timing_info_; 165 166 base::WeakPtrFactory<SpdyProxyClientSocket> weak_factory_; 167 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_ 176