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_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_
6 #define NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "net/base/completion_callback.h"
13 #include "net/base/host_port_pair.h"
14 #include "net/base/load_timing_info.h"
15 #include "net/http/http_auth_controller.h"
16 #include "net/http/http_request_headers.h"
17 #include "net/http/http_request_info.h"
18 #include "net/http/http_response_info.h"
19 #include "net/http/proxy_client_socket.h"
20 #include "net/log/net_log.h"
21 #include "net/socket/ssl_client_socket.h"
26 class ClientSocketHandle
;
27 class GrowableIOBuffer
;
30 class HttpStreamParser
;
34 class HttpProxyClientSocket
: public ProxyClientSocket
{
36 // Takes ownership of |transport_socket|, which should already be connected
37 // by the time Connect() is called. If tunnel is true then on Connect()
38 // this socket will establish an Http tunnel.
39 HttpProxyClientSocket(ClientSocketHandle
* transport_socket
,
40 const std::string
& user_agent
,
41 const HostPortPair
& endpoint
,
42 const HostPortPair
& proxy_server
,
43 HttpAuthCache
* http_auth_cache
,
44 HttpAuthHandlerFactory
* http_auth_handler_factory
,
47 NextProto protocol_negotiated
,
48 ProxyDelegate
* proxy_delegate
,
51 // On destruction Disconnect() is called.
52 ~HttpProxyClientSocket() override
;
54 // ProxyClientSocket implementation.
55 const HttpResponseInfo
* GetConnectResponseInfo() const override
;
56 HttpStream
* CreateConnectResponseStream() override
;
57 int RestartWithAuth(const CompletionCallback
& callback
) override
;
58 const scoped_refptr
<HttpAuthController
>& GetAuthController() const override
;
59 bool IsUsingSpdy() const override
;
60 NextProto
GetProtocolNegotiated() const override
;
62 // StreamSocket implementation.
63 int Connect(const CompletionCallback
& callback
) override
;
64 void Disconnect() override
;
65 bool IsConnected() const override
;
66 bool IsConnectedAndIdle() const override
;
67 const BoundNetLog
& NetLog() const override
;
68 void SetSubresourceSpeculation() override
;
69 void SetOmniboxSpeculation() override
;
70 bool WasEverUsed() const override
;
71 bool UsingTCPFastOpen() const override
;
72 bool WasNpnNegotiated() const override
;
73 NextProto
GetNegotiatedProtocol() const override
;
74 bool GetSSLInfo(SSLInfo
* ssl_info
) override
;
76 // Socket implementation.
77 int Read(IOBuffer
* buf
,
79 const CompletionCallback
& callback
) override
;
80 int Write(IOBuffer
* buf
,
82 const CompletionCallback
& callback
) override
;
83 int SetReceiveBufferSize(int32 size
) override
;
84 int SetSendBufferSize(int32 size
) override
;
85 int GetPeerAddress(IPEndPoint
* address
) const override
;
86 int GetLocalAddress(IPEndPoint
* address
) const override
;
91 STATE_GENERATE_AUTH_TOKEN
,
92 STATE_GENERATE_AUTH_TOKEN_COMPLETE
,
94 STATE_SEND_REQUEST_COMPLETE
,
96 STATE_READ_HEADERS_COMPLETE
,
98 STATE_DRAIN_BODY_COMPLETE
,
100 STATE_TCP_RESTART_COMPLETE
,
104 // The size in bytes of the buffer we use to drain the response body that
105 // we want to throw away. The response body is typically a small error
106 // page just a few hundred bytes long.
107 static const int kDrainBodyBufferSize
= 1024;
109 int PrepareForAuthRestart();
110 int DidDrainBodyForAuthRestart(bool keep_alive
);
112 void LogBlockedTunnelResponse() const;
114 void DoCallback(int result
);
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
);
123 int DoReadHeadersComplete(int result
);
125 int DoDrainBodyComplete(int result
);
127 int DoTCPRestartComplete(int result
);
129 CompletionCallback io_callback_
;
132 // Stores the callback to the layer above, called on completing Connect().
133 CompletionCallback user_callback_
;
135 HttpRequestInfo request_
;
136 HttpResponseInfo response_
;
138 scoped_refptr
<GrowableIOBuffer
> parser_buf_
;
139 scoped_ptr
<HttpStreamParser
> http_stream_parser_
;
140 scoped_refptr
<IOBuffer
> drain_buf_
;
142 // Stores the underlying socket.
143 scoped_ptr
<ClientSocketHandle
> transport_
;
145 // The hostname and port of the endpoint. This is not necessarily the one
146 // specified by the URL, due to Alternate-Protocol or fixed testing ports.
147 const HostPortPair endpoint_
;
148 scoped_refptr
<HttpAuthController
> auth_
;
150 // If true, then the connection to the proxy is a SPDY connection.
151 const bool using_spdy_
;
152 // Protocol negotiated with the server.
153 NextProto protocol_negotiated_
;
154 // If true, then SSL is used to communicate with this proxy
155 const bool is_https_proxy_
;
157 std::string request_line_
;
158 HttpRequestHeaders request_headers_
;
160 // Used only for redirects.
161 bool redirect_has_load_timing_info_
;
162 LoadTimingInfo redirect_load_timing_info_
;
164 const HostPortPair proxy_server_
;
166 // This delegate must outlive this proxy client socket.
167 ProxyDelegate
* proxy_delegate_
;
169 const BoundNetLog net_log_
;
171 DISALLOW_COPY_AND_ASSIGN(HttpProxyClientSocket
);
176 #endif // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_