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.
5 #ifndef NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_
6 #define NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_
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/http/http_auth_controller.h"
18 #include "net/http/http_request_headers.h"
19 #include "net/http/http_request_info.h"
20 #include "net/http/http_response_info.h"
21 #include "net/http/proxy_client_socket.h"
22 #include "net/log/net_log.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"
36 class NET_EXPORT_PRIVATE SpdyProxyClientSocket
: public ProxyClientSocket
,
37 public SpdyStream::Delegate
{
39 // Create a socket on top of the |spdy_stream| by sending a SYN_STREAM
40 // CONNECT frame for |endpoint|. After the SYN_REPLY is received,
41 // any data read/written to the socket will be transferred in data
42 // frames. This object will set itself as |spdy_stream|'s delegate.
43 SpdyProxyClientSocket(const base::WeakPtr
<SpdyStream
>& spdy_stream
,
44 const std::string
& user_agent
,
45 const HostPortPair
& endpoint
,
46 const HostPortPair
& proxy_server
,
47 const BoundNetLog
& source_net_log
,
48 HttpAuthCache
* auth_cache
,
49 HttpAuthHandlerFactory
* auth_handler_factory
);
52 // On destruction Disconnect() is called.
53 ~SpdyProxyClientSocket() override
;
55 // ProxyClientSocket methods:
56 const HttpResponseInfo
* GetConnectResponseInfo() const override
;
57 HttpStream
* CreateConnectResponseStream() override
;
58 const scoped_refptr
<HttpAuthController
>& GetAuthController() const override
;
59 int RestartWithAuth(const CompletionCallback
& callback
) override
;
60 bool IsUsingSpdy() const override
;
61 NextProto
GetProtocolNegotiated() const override
;
63 // StreamSocket implementation.
64 int Connect(const CompletionCallback
& callback
) override
;
65 void Disconnect() override
;
66 bool IsConnected() const override
;
67 bool IsConnectedAndIdle() const override
;
68 const BoundNetLog
& NetLog() const override
;
69 void SetSubresourceSpeculation() override
;
70 void SetOmniboxSpeculation() override
;
71 bool WasEverUsed() const override
;
72 bool UsingTCPFastOpen() const override
;
73 bool WasNpnNegotiated() const override
;
74 NextProto
GetNegotiatedProtocol() const override
;
75 bool GetSSLInfo(SSLInfo
* ssl_info
) override
;
76 void GetConnectionAttempts(ConnectionAttempts
* out
) const override
;
77 void ClearConnectionAttempts() override
{}
78 void AddConnectionAttempts(const ConnectionAttempts
& attempts
) override
{}
80 // Socket implementation.
81 int Read(IOBuffer
* buf
,
83 const CompletionCallback
& callback
) override
;
84 int Write(IOBuffer
* buf
,
86 const CompletionCallback
& callback
) override
;
87 int SetReceiveBufferSize(int32 size
) override
;
88 int SetSendBufferSize(int32 size
) override
;
89 int GetPeerAddress(IPEndPoint
* address
) const override
;
90 int GetLocalAddress(IPEndPoint
* address
) const override
;
92 // SpdyStream::Delegate implementation.
93 void OnRequestHeadersSent() override
;
94 SpdyResponseHeadersStatus
OnResponseHeadersUpdated(
95 const SpdyHeaderBlock
& response_headers
) override
;
96 void OnDataReceived(scoped_ptr
<SpdyBuffer
> buffer
) override
;
97 void OnDataSent() override
;
98 void OnClose(int status
) override
;
103 STATE_GENERATE_AUTH_TOKEN
,
104 STATE_GENERATE_AUTH_TOKEN_COMPLETE
,
106 STATE_SEND_REQUEST_COMPLETE
,
107 STATE_READ_REPLY_COMPLETE
,
112 void LogBlockedTunnelResponse() const;
114 // Calls |callback.Run(result)|. Used to run a callback posted to the
116 void RunCallback(const CompletionCallback
& callback
, int result
) const;
118 void OnIOComplete(int result
);
120 int DoLoop(int last_io_result
);
121 int DoGenerateAuthToken();
122 int DoGenerateAuthTokenComplete(int result
);
124 int DoSendRequestComplete(int result
);
125 int DoReadReplyComplete(int result
);
127 // Populates |user_buffer_| with as much read data as possible
128 // and returns the number of bytes read.
129 size_t PopulateUserReadBuffer(char* out
, size_t len
);
133 // Pointer to the SPDY Stream that this sits on top of.
134 base::WeakPtr
<SpdyStream
> spdy_stream_
;
136 // Stores the callback to the layer above, called on completing Read() or
138 CompletionCallback read_callback_
;
139 // Stores the callback to the layer above, called on completing Write().
140 CompletionCallback write_callback_
;
142 // CONNECT request and response.
143 HttpRequestInfo request_
;
144 HttpResponseInfo response_
;
146 // The hostname and port of the endpoint. This is not necessarily the one
147 // specified by the URL, due to Alternate-Protocol or fixed testing ports.
148 const HostPortPair endpoint_
;
149 scoped_refptr
<HttpAuthController
> auth_
;
151 std::string user_agent_
;
153 // We buffer the response body as it arrives asynchronously from the stream.
154 SpdyReadQueue read_buffer_queue_
;
156 // User provided buffer for the Read() response.
157 scoped_refptr
<IOBuffer
> user_buffer_
;
158 size_t user_buffer_len_
;
160 // User specified number of bytes to be written.
161 int write_buffer_len_
;
163 // True if the transport socket has ever sent data.
166 // Used only for redirects.
167 bool redirect_has_load_timing_info_
;
168 LoadTimingInfo redirect_load_timing_info_
;
170 const BoundNetLog net_log_
;
172 // The default weak pointer factory.
173 base::WeakPtrFactory
<SpdyProxyClientSocket
> weak_factory_
;
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_
;
179 DISALLOW_COPY_AND_ASSIGN(SpdyProxyClientSocket
);
184 #endif // NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_