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 OnTrailers(const SpdyHeaderBlock
& trailers
) override
;
99 void OnClose(int status
) override
;
104 STATE_GENERATE_AUTH_TOKEN
,
105 STATE_GENERATE_AUTH_TOKEN_COMPLETE
,
107 STATE_SEND_REQUEST_COMPLETE
,
108 STATE_READ_REPLY_COMPLETE
,
113 void LogBlockedTunnelResponse() const;
115 // Calls |callback.Run(result)|. Used to run a callback posted to the
117 void RunCallback(const CompletionCallback
& callback
, int result
) const;
119 void OnIOComplete(int result
);
121 int DoLoop(int last_io_result
);
122 int DoGenerateAuthToken();
123 int DoGenerateAuthTokenComplete(int result
);
125 int DoSendRequestComplete(int result
);
126 int DoReadReplyComplete(int result
);
128 // Populates |user_buffer_| with as much read data as possible
129 // and returns the number of bytes read.
130 size_t PopulateUserReadBuffer(char* out
, size_t len
);
134 // Pointer to the SPDY Stream that this sits on top of.
135 base::WeakPtr
<SpdyStream
> spdy_stream_
;
137 // Stores the callback to the layer above, called on completing Read() or
139 CompletionCallback read_callback_
;
140 // Stores the callback to the layer above, called on completing Write().
141 CompletionCallback write_callback_
;
143 // CONNECT request and response.
144 HttpRequestInfo request_
;
145 HttpResponseInfo response_
;
147 // The hostname and port of the endpoint. This is not necessarily the one
148 // specified by the URL, due to Alternate-Protocol or fixed testing ports.
149 const HostPortPair endpoint_
;
150 scoped_refptr
<HttpAuthController
> auth_
;
152 std::string user_agent_
;
154 // We buffer the response body as it arrives asynchronously from the stream.
155 SpdyReadQueue read_buffer_queue_
;
157 // User provided buffer for the Read() response.
158 scoped_refptr
<IOBuffer
> user_buffer_
;
159 size_t user_buffer_len_
;
161 // User specified number of bytes to be written.
162 int write_buffer_len_
;
164 // True if the transport socket has ever sent data.
167 // Used only for redirects.
168 bool redirect_has_load_timing_info_
;
169 LoadTimingInfo redirect_load_timing_info_
;
171 const BoundNetLog net_log_
;
173 // The default weak pointer factory.
174 base::WeakPtrFactory
<SpdyProxyClientSocket
> weak_factory_
;
176 // Only used for posting write callbacks. Weak pointers created by this
177 // factory are invalidated in Disconnect().
178 base::WeakPtrFactory
<SpdyProxyClientSocket
> write_callback_weak_factory_
;
180 DISALLOW_COPY_AND_ASSIGN(SpdyProxyClientSocket
);
185 #endif // NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_