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
;
35 class HttpProxyClientSocket
: public ProxyClientSocket
{
37 // Takes ownership of |transport_socket|, which should already be connected
38 // by the time Connect() is called. If tunnel is true then on Connect()
39 // this socket will establish an Http tunnel.
40 HttpProxyClientSocket(ClientSocketHandle
* transport_socket
,
41 const GURL
& request_url
,
42 const std::string
& user_agent
,
43 const HostPortPair
& endpoint
,
44 const HostPortPair
& proxy_server
,
45 HttpAuthCache
* http_auth_cache
,
46 HttpAuthHandlerFactory
* http_auth_handler_factory
,
49 NextProto protocol_negotiated
,
52 // On destruction Disconnect() is called.
53 virtual ~HttpProxyClientSocket();
55 // ProxyClientSocket implementation.
56 virtual const HttpResponseInfo
* GetConnectResponseInfo() const OVERRIDE
;
57 virtual HttpStream
* CreateConnectResponseStream() OVERRIDE
;
58 virtual int RestartWithAuth(const CompletionCallback
& callback
) OVERRIDE
;
59 virtual const scoped_refptr
<HttpAuthController
>& GetAuthController() const
61 virtual bool IsUsingSpdy() const OVERRIDE
;
62 virtual NextProto
GetProtocolNegotiated() const OVERRIDE
;
64 // StreamSocket implementation.
65 virtual int Connect(const CompletionCallback
& callback
) OVERRIDE
;
66 virtual void Disconnect() OVERRIDE
;
67 virtual bool IsConnected() const OVERRIDE
;
68 virtual bool IsConnectedAndIdle() const OVERRIDE
;
69 virtual const BoundNetLog
& NetLog() const OVERRIDE
;
70 virtual void SetSubresourceSpeculation() OVERRIDE
;
71 virtual void SetOmniboxSpeculation() OVERRIDE
;
72 virtual bool WasEverUsed() const OVERRIDE
;
73 virtual bool UsingTCPFastOpen() const OVERRIDE
;
74 virtual bool WasNpnNegotiated() const OVERRIDE
;
75 virtual NextProto
GetNegotiatedProtocol() const OVERRIDE
;
76 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
78 // Socket implementation.
79 virtual int Read(IOBuffer
* buf
,
81 const CompletionCallback
& callback
) OVERRIDE
;
82 virtual int Write(IOBuffer
* buf
,
84 const CompletionCallback
& callback
) OVERRIDE
;
85 virtual int SetReceiveBufferSize(int32 size
) OVERRIDE
;
86 virtual int SetSendBufferSize(int32 size
) OVERRIDE
;
87 virtual int GetPeerAddress(IPEndPoint
* address
) const OVERRIDE
;
88 virtual int GetLocalAddress(IPEndPoint
* address
) const OVERRIDE
;
93 STATE_GENERATE_AUTH_TOKEN
,
94 STATE_GENERATE_AUTH_TOKEN_COMPLETE
,
96 STATE_SEND_REQUEST_COMPLETE
,
98 STATE_READ_HEADERS_COMPLETE
,
100 STATE_DRAIN_BODY_COMPLETE
,
102 STATE_TCP_RESTART_COMPLETE
,
106 // The size in bytes of the buffer we use to drain the response body that
107 // we want to throw away. The response body is typically a small error
108 // page just a few hundred bytes long.
109 static const int kDrainBodyBufferSize
= 1024;
111 int PrepareForAuthRestart();
112 int DidDrainBodyForAuthRestart(bool keep_alive
);
114 void LogBlockedTunnelResponse() const;
116 void DoCallback(int result
);
117 void OnIOComplete(int result
);
119 int DoLoop(int last_io_result
);
120 int DoGenerateAuthToken();
121 int DoGenerateAuthTokenComplete(int result
);
123 int DoSendRequestComplete(int result
);
125 int DoReadHeadersComplete(int result
);
127 int DoDrainBodyComplete(int result
);
129 int DoTCPRestartComplete(int result
);
131 CompletionCallback io_callback_
;
134 // Stores the callback to the layer above, called on completing Connect().
135 CompletionCallback user_callback_
;
137 HttpRequestInfo request_
;
138 HttpResponseInfo response_
;
140 scoped_refptr
<GrowableIOBuffer
> parser_buf_
;
141 scoped_ptr
<HttpStreamParser
> http_stream_parser_
;
142 scoped_refptr
<IOBuffer
> drain_buf_
;
144 // Stores the underlying socket.
145 scoped_ptr
<ClientSocketHandle
> transport_
;
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 // If true, then the connection to the proxy is a SPDY connection.
153 const bool using_spdy_
;
154 // Protocol negotiated with the server.
155 NextProto protocol_negotiated_
;
156 // If true, then SSL is used to communicate with this proxy
157 const bool is_https_proxy_
;
159 std::string request_line_
;
160 HttpRequestHeaders request_headers_
;
162 // Used only for redirects.
163 bool redirect_has_load_timing_info_
;
164 LoadTimingInfo redirect_load_timing_info_
;
166 const BoundNetLog net_log_
;
168 DISALLOW_COPY_AND_ASSIGN(HttpProxyClientSocket
);
173 #endif // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_