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