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
;
75 void GetConnectionAttempts(ConnectionAttempts
* out
) const override
;
76 void ClearConnectionAttempts() override
{}
77 void AddConnectionAttempts(const ConnectionAttempts
& attempts
) 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_