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
;
77 // Socket implementation.
78 int Read(IOBuffer
* buf
,
80 const CompletionCallback
& callback
) override
;
81 int Write(IOBuffer
* buf
,
83 const CompletionCallback
& callback
) override
;
84 int SetReceiveBufferSize(int32 size
) override
;
85 int SetSendBufferSize(int32 size
) override
;
86 int GetPeerAddress(IPEndPoint
* address
) const override
;
87 int GetLocalAddress(IPEndPoint
* address
) const override
;
89 // SpdyStream::Delegate implementation.
90 void OnRequestHeadersSent() override
;
91 SpdyResponseHeadersStatus
OnResponseHeadersUpdated(
92 const SpdyHeaderBlock
& response_headers
) override
;
93 void OnDataReceived(scoped_ptr
<SpdyBuffer
> buffer
) override
;
94 void OnDataSent() override
;
95 void OnClose(int status
) override
;
100 STATE_GENERATE_AUTH_TOKEN
,
101 STATE_GENERATE_AUTH_TOKEN_COMPLETE
,
103 STATE_SEND_REQUEST_COMPLETE
,
104 STATE_READ_REPLY_COMPLETE
,
109 void LogBlockedTunnelResponse() const;
111 // Calls |callback.Run(result)|. Used to run a callback posted to the
113 void RunCallback(const CompletionCallback
& callback
, int result
) const;
115 void OnIOComplete(int result
);
117 int DoLoop(int last_io_result
);
118 int DoGenerateAuthToken();
119 int DoGenerateAuthTokenComplete(int result
);
121 int DoSendRequestComplete(int result
);
122 int DoReadReplyComplete(int result
);
124 // Populates |user_buffer_| with as much read data as possible
125 // and returns the number of bytes read.
126 size_t PopulateUserReadBuffer(char* out
, size_t len
);
130 // Pointer to the SPDY Stream that this sits on top of.
131 base::WeakPtr
<SpdyStream
> spdy_stream_
;
133 // Stores the callback to the layer above, called on completing Read() or
135 CompletionCallback read_callback_
;
136 // Stores the callback to the layer above, called on completing Write().
137 CompletionCallback write_callback_
;
139 // CONNECT request and response.
140 HttpRequestInfo request_
;
141 HttpResponseInfo response_
;
143 // The hostname and port of the endpoint. This is not necessarily the one
144 // specified by the URL, due to Alternate-Protocol or fixed testing ports.
145 const HostPortPair endpoint_
;
146 scoped_refptr
<HttpAuthController
> auth_
;
148 std::string user_agent_
;
150 // We buffer the response body as it arrives asynchronously from the stream.
151 SpdyReadQueue read_buffer_queue_
;
153 // User provided buffer for the Read() response.
154 scoped_refptr
<IOBuffer
> user_buffer_
;
155 size_t user_buffer_len_
;
157 // User specified number of bytes to be written.
158 int write_buffer_len_
;
160 // True if the transport socket has ever sent data.
163 // Used only for redirects.
164 bool redirect_has_load_timing_info_
;
165 LoadTimingInfo redirect_load_timing_info_
;
167 const BoundNetLog net_log_
;
169 // The default weak pointer factory.
170 base::WeakPtrFactory
<SpdyProxyClientSocket
> weak_factory_
;
172 // Only used for posting write callbacks. Weak pointers created by this
173 // factory are invalidated in Disconnect().
174 base::WeakPtrFactory
<SpdyProxyClientSocket
> write_callback_weak_factory_
;
176 DISALLOW_COPY_AND_ASSIGN(SpdyProxyClientSocket
);
181 #endif // NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_