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/net_log.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/spdy/spdy_http_stream.h"
23 #include "net/spdy/spdy_protocol.h"
24 #include "net/spdy/spdy_session.h"
25 #include "net/spdy/spdy_stream.h"
37 class NET_EXPORT_PRIVATE SpdyProxyClientSocket
: public ProxyClientSocket
,
38 public SpdyStream::Delegate
{
40 // Create a socket on top of the |spdy_stream| by sending a SYN_STREAM
41 // CONNECT frame for |endpoint|. After the SYN_REPLY is received,
42 // any data read/written to the socket will be transferred in data
44 SpdyProxyClientSocket(SpdyStream
* spdy_stream
,
45 const std::string
& user_agent
,
46 const HostPortPair
& endpoint
,
48 const HostPortPair
& proxy_server
,
49 HttpAuthCache
* auth_cache
,
50 HttpAuthHandlerFactory
* auth_handler_factory
);
53 // On destruction Disconnect() is called.
54 virtual ~SpdyProxyClientSocket();
56 // ProxyClientSocket methods:
57 virtual const HttpResponseInfo
* GetConnectResponseInfo() const OVERRIDE
;
58 virtual HttpStream
* CreateConnectResponseStream() OVERRIDE
;
59 virtual const scoped_refptr
<HttpAuthController
>& GetAuthController() const
61 virtual int RestartWithAuth(const CompletionCallback
& callback
) OVERRIDE
;
62 virtual bool IsUsingSpdy() const OVERRIDE
;
63 virtual NextProto
GetProtocolNegotiated() const OVERRIDE
;
65 // StreamSocket implementation.
66 virtual int Connect(const CompletionCallback
& callback
) OVERRIDE
;
67 virtual void Disconnect() OVERRIDE
;
68 virtual bool IsConnected() const OVERRIDE
;
69 virtual bool IsConnectedAndIdle() const OVERRIDE
;
70 virtual const BoundNetLog
& NetLog() const OVERRIDE
;
71 virtual void SetSubresourceSpeculation() OVERRIDE
;
72 virtual void SetOmniboxSpeculation() OVERRIDE
;
73 virtual bool WasEverUsed() const OVERRIDE
;
74 virtual bool UsingTCPFastOpen() const OVERRIDE
;
75 virtual int64
NumBytesRead() const OVERRIDE
;
76 virtual base::TimeDelta
GetConnectTimeMicros() const OVERRIDE
;
77 virtual bool WasNpnNegotiated() const OVERRIDE
;
78 virtual NextProto
GetNegotiatedProtocol() const OVERRIDE
;
79 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
81 // Socket implementation.
82 virtual int Read(IOBuffer
* buf
,
84 const CompletionCallback
& callback
) OVERRIDE
;
85 virtual int Write(IOBuffer
* buf
,
87 const CompletionCallback
& callback
) OVERRIDE
;
88 virtual bool SetReceiveBufferSize(int32 size
) OVERRIDE
;
89 virtual bool SetSendBufferSize(int32 size
) OVERRIDE
;
90 virtual int GetPeerAddress(IPEndPoint
* address
) const OVERRIDE
;
91 virtual int GetLocalAddress(IPEndPoint
* address
) const OVERRIDE
;
93 // SpdyStream::Delegate implementation.
94 virtual bool OnSendHeadersComplete(int status
) OVERRIDE
;
95 virtual int OnSendBody() OVERRIDE
;
96 virtual int OnSendBodyComplete(int status
, bool* eof
) OVERRIDE
;
97 virtual int OnResponseReceived(const SpdyHeaderBlock
& response
,
98 base::Time response_time
,
100 virtual void OnHeadersSent() OVERRIDE
;
101 virtual int OnDataReceived(const char* data
, int length
) OVERRIDE
;
102 virtual void OnDataSent(int length
) OVERRIDE
;
103 virtual void OnClose(int status
) OVERRIDE
;
108 STATE_GENERATE_AUTH_TOKEN
,
109 STATE_GENERATE_AUTH_TOKEN_COMPLETE
,
111 STATE_SEND_REQUEST_COMPLETE
,
112 STATE_READ_REPLY_COMPLETE
,
117 void LogBlockedTunnelResponse() 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 int PopulateUserReadBuffer();
134 // Pointer to the SPDY Stream that this sits on top of.
135 scoped_refptr
<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 // We buffer the response body as it arrives asynchronously from the stream.
153 std::list
<scoped_refptr
<DrainableIOBuffer
> > read_buffer_
;
155 // User provided buffer for the Read() response.
156 scoped_refptr
<DrainableIOBuffer
> user_buffer_
;
158 // User specified number of bytes to be written.
159 int write_buffer_len_
;
160 // Number of bytes written which have not been confirmed
161 int write_bytes_outstanding_
;
163 // True if the transport socket has ever sent data.
166 scoped_ptr
<SpdyHttpStream
> response_stream_
;
168 base::WeakPtrFactory
<SpdyProxyClientSocket
> weak_factory_
;
170 const BoundNetLog net_log_
;
172 DISALLOW_COPY_AND_ASSIGN(SpdyProxyClientSocket
);
177 #endif // NET_SPDY_SPDY_PROXY_CLIENT_SOCKET_H_