blob: 2cf3c6ae6f5ff13be9f2c9f055043e9c3eb1e905 [file] [log] [blame]
[email protected]a7ea8832010-07-12 17:54:541// Copyright (c) 2010 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.
4
5#ifndef NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_
6#define NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]a7ea8832010-07-12 17:54:548
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/ref_counted.h"
13#include "net/base/completion_callback.h"
14#include "net/base/host_port_pair.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"
[email protected]511f6f52010-12-17 03:58:2920#include "net/http/proxy_client_socket.h"
[email protected]a7ea8832010-07-12 17:54:5421
22class GURL;
23
24namespace net {
25
26class AddressList;
27class ClientSocketHandle;
[email protected]4d4a5162010-09-21 22:44:0428class GrowableIOBuffer;
[email protected]3598c6022010-09-17 23:13:0929class HttpAuthCache;
30class HttpAuthHandleFactory;
[email protected]a7ea8832010-07-12 17:54:5431class HttpStream;
[email protected]4d4a5162010-09-21 22:44:0432class HttpStreamParser;
[email protected]8e6441ca2010-08-19 05:56:3833class IOBuffer;
[email protected]a7ea8832010-07-12 17:54:5434
[email protected]511f6f52010-12-17 03:58:2935class HttpProxyClientSocket : public ProxyClientSocket {
[email protected]a7ea8832010-07-12 17:54:5436 public:
[email protected]e772db3f2010-07-12 18:11:1337 // 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.
[email protected]a7ea8832010-07-12 17:54:5440 HttpProxyClientSocket(ClientSocketHandle* transport_socket,
[email protected]da81f132010-08-18 23:39:2941 const GURL& request_url,
42 const std::string& user_agent,
43 const HostPortPair& endpoint,
[email protected]394816e92010-08-03 07:38:5944 const HostPortPair& proxy_server,
[email protected]3598c6022010-09-17 23:13:0945 HttpAuthCache* http_auth_cache,
46 HttpAuthHandlerFactory* http_auth_handler_factory,
[email protected]7642b5ae2010-09-01 20:55:1747 bool tunnel,
[email protected]511f6f52010-12-17 03:58:2948 bool using_spdy,
49 bool is_https_proxy);
[email protected]a7ea8832010-07-12 17:54:5450
51 // On destruction Disconnect() is called.
52 virtual ~HttpProxyClientSocket();
53
54 // If Connect (or its callback) returns PROXY_AUTH_REQUESTED, then
[email protected]e772db3f2010-07-12 18:11:1355 // credentials should be added to the HttpAuthController before calling
56 // RestartWithAuth.
57 int RestartWithAuth(CompletionCallback* callback);
[email protected]a7ea8832010-07-12 17:54:5458
[email protected]394816e92010-08-03 07:38:5959 const scoped_refptr<HttpAuthController>& auth_controller() {
60 return auth_;
[email protected]a7ea8832010-07-12 17:54:5461 }
62
[email protected]7642b5ae2010-09-01 20:55:1763 bool using_spdy() {
64 return using_spdy_;
65 }
66
[email protected]be1a48b2011-01-20 00:12:1367 // ProxyClientSocket methods:
68 virtual const HttpResponseInfo* GetConnectResponseInfo() const;
69 virtual HttpStream* CreateConnectResponseStream();
[email protected]a7ea8832010-07-12 17:54:5470
[email protected]be1a48b2011-01-20 00:12:1371 // ClientSocket methods:
[email protected]a7ea8832010-07-12 17:54:5472 virtual int Connect(CompletionCallback* callback);
73 virtual void Disconnect();
74 virtual bool IsConnected() const;
75 virtual bool IsConnectedAndIdle() const;
[email protected]ddb1e5a2010-12-13 20:10:4576 virtual const BoundNetLog& NetLog() const;
[email protected]9b5614a2010-08-25 20:29:4577 virtual void SetSubresourceSpeculation();
78 virtual void SetOmniboxSpeculation();
[email protected]0f873e82010-09-02 16:09:0179 virtual bool WasEverUsed() const;
[email protected]7f7e92392010-10-26 18:29:2980 virtual bool UsingTCPFastOpen() const;
[email protected]a7ea8832010-07-12 17:54:5481
82 // Socket methods:
83 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
84 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
[email protected]a7ea8832010-07-12 17:54:5485 virtual bool SetReceiveBufferSize(int32 size);
86 virtual bool SetSendBufferSize(int32 size);
[email protected]a7ea8832010-07-12 17:54:5487 virtual int GetPeerAddress(AddressList* address) const;
88
89 private:
90 enum State {
91 STATE_NONE,
92 STATE_GENERATE_AUTH_TOKEN,
93 STATE_GENERATE_AUTH_TOKEN_COMPLETE,
94 STATE_SEND_REQUEST,
95 STATE_SEND_REQUEST_COMPLETE,
96 STATE_READ_HEADERS,
97 STATE_READ_HEADERS_COMPLETE,
[email protected]a7ea8832010-07-12 17:54:5498 STATE_DRAIN_BODY,
99 STATE_DRAIN_BODY_COMPLETE,
[email protected]394816e92010-08-03 07:38:59100 STATE_TCP_RESTART,
101 STATE_TCP_RESTART_COMPLETE,
[email protected]a7ea8832010-07-12 17:54:54102 STATE_DONE,
103 };
104
105 // The size in bytes of the buffer we use to drain the response body that
106 // we want to throw away. The response body is typically a small error
107 // page just a few hundred bytes long.
[email protected]81854c42010-09-22 03:39:36108 static const int kDrainBodyBufferSize = 1024;
[email protected]a7ea8832010-07-12 17:54:54109
110 int PrepareForAuthRestart();
111 int DidDrainBodyForAuthRestart(bool keep_alive);
112
113 int HandleAuthChallenge();
114
115 void LogBlockedTunnelResponse(int response_code) const;
116
117 void DoCallback(int result);
118 void OnIOComplete(int result);
119
120 int DoLoop(int last_io_result);
121 int DoGenerateAuthToken();
122 int DoGenerateAuthTokenComplete(int result);
123 int DoSendRequest();
124 int DoSendRequestComplete(int result);
125 int DoReadHeaders();
126 int DoReadHeadersComplete(int result);
127 int DoDrainBody();
128 int DoDrainBodyComplete(int result);
[email protected]394816e92010-08-03 07:38:59129 int DoTCPRestart();
130 int DoTCPRestartComplete(int result);
[email protected]a7ea8832010-07-12 17:54:54131
132 CompletionCallbackImpl<HttpProxyClientSocket> io_callback_;
133 State next_state_;
134
135 // Stores the callback to the layer above, called on completing Connect().
136 CompletionCallback* user_callback_;
137
[email protected]a7ea8832010-07-12 17:54:54138 HttpRequestInfo request_;
139 HttpResponseInfo response_;
[email protected]560c0432010-07-13 20:45:31140
[email protected]4d4a5162010-09-21 22:44:04141 scoped_refptr<GrowableIOBuffer> parser_buf_;
142 scoped_ptr<HttpStreamParser> http_stream_parser_;
[email protected]560c0432010-07-13 20:45:31143 scoped_refptr<IOBuffer> drain_buf_;
144
145 // Stores the underlying socket.
[email protected]511f6f52010-12-17 03:58:29146 scoped_ptr<ClientSocketHandle> transport_;
[email protected]a7ea8832010-07-12 17:54:54147
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.
[email protected]560c0432010-07-13 20:45:31150 const HostPortPair endpoint_;
151 scoped_refptr<HttpAuthController> auth_;
152 const bool tunnel_;
[email protected]7642b5ae2010-09-01 20:55:17153 // If true, then the connection to the proxy is a SPDY connection.
154 const bool using_spdy_;
[email protected]511f6f52010-12-17 03:58:29155 // If true, then SSL is used to communicate with this proxy
156 const bool is_https_proxy_;
[email protected]a7ea8832010-07-12 17:54:54157
[email protected]3deb9a52010-11-11 00:24:40158 std::string request_line_;
159 HttpRequestHeaders request_headers_;
[email protected]a7ea8832010-07-12 17:54:54160
[email protected]560c0432010-07-13 20:45:31161 const BoundNetLog net_log_;
[email protected]a7ea8832010-07-12 17:54:54162
163 DISALLOW_COPY_AND_ASSIGN(HttpProxyClientSocket);
164};
165
166} // namespace net
167
168#endif // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_