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 <stddef.h> 9 #include <stdint.h> 10 11 #include <list> 12 #include <memory> 13 #include <string> 14 15 #include "base/macros.h" 16 #include "base/memory/ref_counted.h" 17 #include "base/memory/weak_ptr.h" 18 #include "net/base/completion_once_callback.h" 19 #include "net/base/host_port_pair.h" 20 #include "net/base/net_export.h" 21 #include "net/base/proxy_server.h" 22 #include "net/http/http_auth_controller.h" 23 #include "net/http/http_request_headers.h" 24 #include "net/http/http_request_info.h" 25 #include "net/http/http_response_info.h" 26 #include "net/http/proxy_client_socket.h" 27 #include "net/log/net_log_source.h" 28 #include "net/log/net_log_with_source.h" 29 #include "net/spdy/spdy_http_stream.h" 30 #include "net/spdy/spdy_read_queue.h" 31 #include "net/spdy/spdy_session.h" 32 #include "net/spdy/spdy_stream.h" 33 #include "net/third_party/quiche/src/spdy/core/spdy_protocol.h" 34 #include "net/traffic_annotation/network_traffic_annotation.h" 35 36 namespace net { 37 38 class IOBuffer; 39 class ProxyDelegate; 40 class SpdyStream; 41 42 class NET_EXPORT_PRIVATE SpdyProxyClientSocket : public ProxyClientSocket, 43 public SpdyStream::Delegate { 44 public: 45 // Create a socket on top of the |spdy_stream| by sending a HEADERS CONNECT 46 // frame for |endpoint|. After the response HEADERS frame is received, any 47 // data read/written to the socket will be transferred in data frames. This 48 // object will set itself as |spdy_stream|'s delegate. 49 SpdyProxyClientSocket(const base::WeakPtr<SpdyStream>& spdy_stream, 50 const ProxyServer& proxy_server, 51 const std::string& user_agent, 52 const HostPortPair& endpoint, 53 const NetLogWithSource& source_net_log, 54 HttpAuthController* auth_controller, 55 ProxyDelegate* proxy_delegate); 56 57 // On destruction Disconnect() is called. 58 ~SpdyProxyClientSocket() override; 59 60 // ProxyClientSocket methods: 61 const HttpResponseInfo* GetConnectResponseInfo() const override; 62 const scoped_refptr<HttpAuthController>& GetAuthController() const override; 63 int RestartWithAuth(CompletionOnceCallback callback) override; 64 bool IsUsingSpdy() const override; 65 NextProto GetProxyNegotiatedProtocol() const override; 66 void SetStreamPriority(RequestPriority priority) override; 67 68 // StreamSocket implementation. 69 int Connect(CompletionOnceCallback callback) override; 70 void Disconnect() override; 71 bool IsConnected() const override; 72 bool IsConnectedAndIdle() const override; 73 const NetLogWithSource& NetLog() const override; 74 bool WasEverUsed() const override; 75 bool WasAlpnNegotiated() const override; 76 NextProto GetNegotiatedProtocol() const override; 77 bool GetSSLInfo(SSLInfo* ssl_info) override; 78 void GetConnectionAttempts(ConnectionAttempts* out) const override; ClearConnectionAttempts()79 void ClearConnectionAttempts() override {} AddConnectionAttempts(const ConnectionAttempts & attempts)80 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} 81 int64_t GetTotalReceivedBytes() const override; 82 void ApplySocketTag(const SocketTag& tag) override; 83 84 // Socket implementation. 85 int Read(IOBuffer* buf, 86 int buf_len, 87 CompletionOnceCallback callback) override; 88 int ReadIfReady(IOBuffer* buf, 89 int buf_len, 90 CompletionOnceCallback callback) override; 91 int CancelReadIfReady() override; 92 int Write(IOBuffer* buf, 93 int buf_len, 94 CompletionOnceCallback callback, 95 const NetworkTrafficAnnotationTag& traffic_annotation) override; 96 int SetReceiveBufferSize(int32_t size) override; 97 int SetSendBufferSize(int32_t size) override; 98 int GetPeerAddress(IPEndPoint* address) const override; 99 int GetLocalAddress(IPEndPoint* address) const override; 100 101 // SpdyStream::Delegate implementation. 102 void OnHeadersSent() override; 103 void OnHeadersReceived( 104 const spdy::SpdyHeaderBlock& response_headers, 105 const spdy::SpdyHeaderBlock* pushed_request_headers) override; 106 void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override; 107 void OnDataSent() override; 108 void OnTrailers(const spdy::SpdyHeaderBlock& trailers) override; 109 void OnClose(int status) override; 110 bool CanGreaseFrameType() const override; 111 NetLogSource source_dependency() const override; 112 113 private: 114 enum State { 115 STATE_DISCONNECTED, 116 STATE_GENERATE_AUTH_TOKEN, 117 STATE_GENERATE_AUTH_TOKEN_COMPLETE, 118 STATE_SEND_REQUEST, 119 STATE_SEND_REQUEST_COMPLETE, 120 STATE_READ_REPLY_COMPLETE, 121 STATE_OPEN, 122 STATE_CLOSED 123 }; 124 125 // Calls |callback.Run(result)|. Used to run a callback posted to the 126 // message loop. 127 void RunCallback(CompletionOnceCallback callback, int result) const; 128 129 void OnIOComplete(int result); 130 131 int DoLoop(int last_io_result); 132 int DoGenerateAuthToken(); 133 int DoGenerateAuthTokenComplete(int result); 134 int DoSendRequest(); 135 int DoSendRequestComplete(int result); 136 int DoReadReplyComplete(int result); 137 138 // Populates |user_buffer_| with as much read data as possible 139 // and returns the number of bytes read. 140 size_t PopulateUserReadBuffer(char* out, size_t len); 141 142 State next_state_; 143 144 // Pointer to the SPDY Stream that this sits on top of. 145 base::WeakPtr<SpdyStream> spdy_stream_; 146 147 // Stores the callback to the layer above, called on completing Read() or 148 // Connect(). 149 CompletionOnceCallback read_callback_; 150 // Stores the callback to the layer above, called on completing Write(). 151 CompletionOnceCallback write_callback_; 152 153 // CONNECT request and response. 154 HttpRequestInfo request_; 155 HttpResponseInfo response_; 156 157 // The hostname and port of the endpoint. This is not necessarily the one 158 // specified by the URL, due to Alternate-Protocol or fixed testing ports. 159 const HostPortPair endpoint_; 160 scoped_refptr<HttpAuthController> auth_; 161 162 const ProxyServer proxy_server_; 163 164 // This delegate must outlive this proxy client socket. 165 ProxyDelegate* const proxy_delegate_; 166 167 std::string user_agent_; 168 169 // We buffer the response body as it arrives asynchronously from the stream. 170 SpdyReadQueue read_buffer_queue_; 171 172 // User provided buffer for the Read() response. 173 scoped_refptr<IOBuffer> user_buffer_; 174 size_t user_buffer_len_; 175 176 // User specified number of bytes to be written. 177 int write_buffer_len_; 178 179 // True if the transport socket has ever sent data. 180 bool was_ever_used_; 181 182 const NetLogWithSource net_log_; 183 const NetLogSource source_dependency_; 184 185 // The default weak pointer factory. 186 base::WeakPtrFactory<SpdyProxyClientSocket> weak_factory_{this}; 187 188 // Only used for posting write callbacks. Weak pointers created by this 189 // factory are invalidated in Disconnect(). 190 base::WeakPtrFactory<SpdyProxyClientSocket> write_callback_weak_factory_{ 191 this}; 192 193 DISALLOW_COPY_AND_ASSIGN(SpdyProxyClientSocket); 194 }; 195 196 } // namespace net 197 198 #endif // NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_ 199