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 int SetReceiveBufferSize(int32 size) OVERRIDE; 90 virtual int 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 // Calls |callback.Run(result)|. Used to run a callback posted to the 117 // message loop. 118 void RunCallback(const CompletionCallback& callback, int result) const; 119 120 void OnIOComplete(int result); 121 122 int DoLoop(int last_io_result); 123 int DoGenerateAuthToken(); 124 int DoGenerateAuthTokenComplete(int result); 125 int DoSendRequest(); 126 int DoSendRequestComplete(int result); 127 int DoReadReplyComplete(int result); 128 129 // Populates |user_buffer_| with as much read data as possible 130 // and returns the number of bytes read. 131 size_t PopulateUserReadBuffer(char* out, size_t len); 132 133 State next_state_; 134 135 // Pointer to the SPDY Stream that this sits on top of. 136 base::WeakPtr<SpdyStream> spdy_stream_; 137 138 // Stores the callback to the layer above, called on completing Read() or 139 // Connect(). 140 CompletionCallback read_callback_; 141 // Stores the callback to the layer above, called on completing Write(). 142 CompletionCallback write_callback_; 143 144 // CONNECT request and response. 145 HttpRequestInfo request_; 146 HttpResponseInfo response_; 147 148 // The hostname and port of the endpoint. This is not necessarily the one 149 // specified by the URL, due to Alternate-Protocol or fixed testing ports. 150 const HostPortPair endpoint_; 151 scoped_refptr<HttpAuthController> auth_; 152 153 // We buffer the response body as it arrives asynchronously from the stream. 154 SpdyReadQueue read_buffer_queue_; 155 156 // User provided buffer for the Read() response. 157 scoped_refptr<IOBuffer> user_buffer_; 158 size_t user_buffer_len_; 159 160 // User specified number of bytes to be written. 161 int write_buffer_len_; 162 163 // True if the transport socket has ever sent data. 164 bool was_ever_used_; 165 166 // Used only for redirects. 167 bool redirect_has_load_timing_info_; 168 LoadTimingInfo redirect_load_timing_info_; 169 170 const BoundNetLog net_log_; 171 172 // The default weak pointer factory. 173 base::WeakPtrFactory<SpdyProxyClientSocket> weak_factory_; 174 175 // Only used for posting write callbacks. Weak pointers created by this 176 // factory are invalidated in Disconnect(). 177 base::WeakPtrFactory<SpdyProxyClientSocket> write_callback_weak_factory_; 178 179 DISALLOW_COPY_AND_ASSIGN(SpdyProxyClientSocket); 180 }; 181 182 } // namespace net 183 184 #endif // NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_ 185