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/base/net_log.h"
16 #include "net/http/http_auth_controller.h"
17 #include "net/http/http_request_headers.h"
18 #include "net/http/http_request_info.h"
19 #include "net/http/http_response_info.h"
20 #include "net/http/proxy_client_socket.h"
21 #include "net/socket/ssl_client_socket.h"
28 class ClientSocketHandle
;
29 class GrowableIOBuffer
;
32 class HttpStreamParser
;
36 class HttpProxyClientSocket
: public ProxyClientSocket
{
38 // Takes ownership of |transport_socket|, which should already be connected
39 // by the time Connect() is called. If tunnel is true then on Connect()
40 // this socket will establish an Http tunnel.
41 HttpProxyClientSocket(ClientSocketHandle
* transport_socket
,
42 const GURL
& request_url
,
43 const std::string
& user_agent
,
44 const HostPortPair
& endpoint
,
45 const HostPortPair
& proxy_server
,
46 HttpAuthCache
* http_auth_cache
,
47 HttpAuthHandlerFactory
* http_auth_handler_factory
,
50 NextProto protocol_negotiated
,
51 ProxyDelegate
* proxy_delegate
,
54 // On destruction Disconnect() is called.
55 ~HttpProxyClientSocket() override
;
57 // ProxyClientSocket implementation.
58 const HttpResponseInfo
* GetConnectResponseInfo() const override
;
59 HttpStream
* CreateConnectResponseStream() override
;
60 int RestartWithAuth(const CompletionCallback
& callback
) override
;
61 const scoped_refptr
<HttpAuthController
>& GetAuthController() const override
;
62 bool IsUsingSpdy() const override
;
63 NextProto
GetProtocolNegotiated() const override
;
65 // StreamSocket implementation.
66 int Connect(const CompletionCallback
& callback
) override
;
67 void Disconnect() override
;
68 bool IsConnected() const override
;
69 bool IsConnectedAndIdle() const override
;
70 const BoundNetLog
& NetLog() const override
;
71 void SetSubresourceSpeculation() override
;
72 void SetOmniboxSpeculation() override
;
73 bool WasEverUsed() const override
;
74 bool UsingTCPFastOpen() const override
;
75 bool WasNpnNegotiated() const override
;
76 NextProto
GetNegotiatedProtocol() const override
;
77 bool GetSSLInfo(SSLInfo
* ssl_info
) override
;
79 // Socket implementation.
80 int Read(IOBuffer
* buf
,
82 const CompletionCallback
& callback
) override
;
83 int Write(IOBuffer
* buf
,
85 const CompletionCallback
& callback
) override
;
86 int SetReceiveBufferSize(int32 size
) override
;
87 int SetSendBufferSize(int32 size
) override
;
88 int GetPeerAddress(IPEndPoint
* address
) const override
;
89 int GetLocalAddress(IPEndPoint
* address
) const override
;
94 STATE_GENERATE_AUTH_TOKEN
,
95 STATE_GENERATE_AUTH_TOKEN_COMPLETE
,
97 STATE_SEND_REQUEST_COMPLETE
,
99 STATE_READ_HEADERS_COMPLETE
,
101 STATE_DRAIN_BODY_COMPLETE
,
103 STATE_TCP_RESTART_COMPLETE
,
107 // The size in bytes of the buffer we use to drain the response body that
108 // we want to throw away. The response body is typically a small error
109 // page just a few hundred bytes long.
110 static const int kDrainBodyBufferSize
= 1024;
112 int PrepareForAuthRestart();
113 int DidDrainBodyForAuthRestart(bool keep_alive
);
115 void LogBlockedTunnelResponse() const;
117 void DoCallback(int result
);
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
);
126 int DoReadHeadersComplete(int result
);
128 int DoDrainBodyComplete(int result
);
130 int DoTCPRestartComplete(int result
);
132 CompletionCallback io_callback_
;
135 // Stores the callback to the layer above, called on completing Connect().
136 CompletionCallback user_callback_
;
138 HttpRequestInfo request_
;
139 HttpResponseInfo response_
;
141 scoped_refptr
<GrowableIOBuffer
> parser_buf_
;
142 scoped_ptr
<HttpStreamParser
> http_stream_parser_
;
143 scoped_refptr
<IOBuffer
> drain_buf_
;
145 // Stores the underlying socket.
146 scoped_ptr
<ClientSocketHandle
> transport_
;
148 // The hostname and port of the endpoint. This is not necessarily the one
149 // specified by the URL, due to Alternate-Protocol or fixed testing ports.
150 const HostPortPair endpoint_
;
151 scoped_refptr
<HttpAuthController
> auth_
;
153 // If true, then the connection to the proxy is a SPDY connection.
154 const bool using_spdy_
;
155 // Protocol negotiated with the server.
156 NextProto protocol_negotiated_
;
157 // If true, then SSL is used to communicate with this proxy
158 const bool is_https_proxy_
;
160 std::string request_line_
;
161 HttpRequestHeaders request_headers_
;
163 // Used only for redirects.
164 bool redirect_has_load_timing_info_
;
165 LoadTimingInfo redirect_load_timing_info_
;
167 const HostPortPair proxy_server_
;
169 // This delegate must outlive this proxy client socket.
170 ProxyDelegate
* proxy_delegate_
;
172 const BoundNetLog net_log_
;
174 DISALLOW_COPY_AND_ASSIGN(HttpProxyClientSocket
);
179 #endif // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_