1 // Copyright 2012 The Chromium Authors 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_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 #include <string> 12 13 #include "base/memory/raw_ptr.h" 14 #include "base/memory/scoped_refptr.h" 15 #include "net/base/completion_once_callback.h" 16 #include "net/base/completion_repeating_callback.h" 17 #include "net/base/host_port_pair.h" 18 #include "net/base/net_export.h" 19 #include "net/base/proxy_server.h" 20 #include "net/http/http_auth_controller.h" 21 #include "net/http/http_request_headers.h" 22 #include "net/http/http_request_info.h" 23 #include "net/http/http_response_info.h" 24 #include "net/http/proxy_client_socket.h" 25 #include "net/log/net_log_with_source.h" 26 #include "net/socket/ssl_client_socket.h" 27 #include "net/traffic_annotation/network_traffic_annotation.h" 28 29 namespace net { 30 31 class GrowableIOBuffer; 32 class HttpStreamParser; 33 class IOBuffer; 34 class ProxyDelegate; 35 class StreamSocket; 36 37 // Tunnels a stream socket over an HTTP/1.1 connection. 38 class NET_EXPORT_PRIVATE HttpProxyClientSocket : public ProxyClientSocket { 39 public: 40 // Takes ownership of |socket|, which should already be connected by the time 41 // Connect() is called. |socket| is assumed to be a fresh socket. If tunnel 42 // is true then on Connect() this socket will establish an Http tunnel. 43 HttpProxyClientSocket(std::unique_ptr<StreamSocket> socket, 44 const std::string& user_agent, 45 const HostPortPair& endpoint, 46 const ProxyServer& proxy_server, 47 scoped_refptr<HttpAuthController> http_auth_controller, 48 ProxyDelegate* proxy_delegate, 49 const NetworkTrafficAnnotationTag& traffic_annotation); 50 51 HttpProxyClientSocket(const HttpProxyClientSocket&) = delete; 52 HttpProxyClientSocket& operator=(const HttpProxyClientSocket&) = delete; 53 54 // On destruction Disconnect() is called. 55 ~HttpProxyClientSocket() override; 56 57 // ProxyClientSocket implementation. 58 const HttpResponseInfo* GetConnectResponseInfo() const override; 59 int RestartWithAuth(CompletionOnceCallback callback) override; 60 const scoped_refptr<HttpAuthController>& GetAuthController() const override; 61 62 // StreamSocket implementation. 63 int Connect(CompletionOnceCallback callback) override; 64 void Disconnect() override; 65 bool IsConnected() const override; 66 bool IsConnectedAndIdle() const override; 67 const NetLogWithSource& NetLog() const override; 68 bool WasEverUsed() const override; 69 bool WasAlpnNegotiated() const override; 70 NextProto GetNegotiatedProtocol() const override; 71 bool GetSSLInfo(SSLInfo* ssl_info) override; 72 int64_t GetTotalReceivedBytes() const override; 73 void ApplySocketTag(const SocketTag& tag) override; 74 75 // Socket implementation. 76 int Read(IOBuffer* buf, 77 int buf_len, 78 CompletionOnceCallback callback) override; 79 int ReadIfReady(IOBuffer* buf, 80 int buf_len, 81 CompletionOnceCallback callback) override; 82 int CancelReadIfReady() override; 83 int Write(IOBuffer* buf, 84 int buf_len, 85 CompletionOnceCallback callback, 86 const NetworkTrafficAnnotationTag& traffic_annotation) override; 87 int SetReceiveBufferSize(int32_t size) override; 88 int SetSendBufferSize(int32_t size) override; 89 int GetPeerAddress(IPEndPoint* address) const override; 90 int GetLocalAddress(IPEndPoint* address) const override; 91 92 private: 93 enum State { 94 STATE_NONE, 95 STATE_GENERATE_AUTH_TOKEN, 96 STATE_GENERATE_AUTH_TOKEN_COMPLETE, 97 STATE_SEND_REQUEST, 98 STATE_SEND_REQUEST_COMPLETE, 99 STATE_READ_HEADERS, 100 STATE_READ_HEADERS_COMPLETE, 101 STATE_DRAIN_BODY, 102 STATE_DRAIN_BODY_COMPLETE, 103 STATE_DONE, 104 }; 105 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; 110 111 int PrepareForAuthRestart(); 112 int DidDrainBodyForAuthRestart(); 113 114 void DoCallback(int result); 115 void OnIOComplete(int result); 116 117 int DoLoop(int last_io_result); 118 int DoGenerateAuthToken(); 119 int DoGenerateAuthTokenComplete(int result); 120 int DoSendRequest(); 121 int DoSendRequestComplete(int result); 122 int DoReadHeaders(); 123 int DoReadHeadersComplete(int result); 124 int DoDrainBody(); 125 int DoDrainBodyComplete(int result); 126 127 // Returns whether |next_state_| is STATE_DONE. 128 bool CheckDone(); 129 130 CompletionRepeatingCallback io_callback_; 131 State next_state_ = STATE_NONE; 132 133 // Stores the callback provided by the caller of async operations. 134 CompletionOnceCallback user_callback_; 135 136 HttpRequestInfo request_; 137 HttpResponseInfo response_; 138 139 scoped_refptr<GrowableIOBuffer> parser_buf_; 140 std::unique_ptr<HttpStreamParser> http_stream_parser_; 141 scoped_refptr<IOBuffer> drain_buf_; 142 143 std::unique_ptr<StreamSocket> socket_; 144 145 // Whether or not |socket_| has been previously used. Once auth credentials 146 // are sent, set to true. 147 bool is_reused_ = false; 148 149 // The hostname and port of the endpoint. This is not necessarily the one 150 // specified by the URL, due to Alternate-Protocol or fixed testing ports. 151 const HostPortPair endpoint_; 152 scoped_refptr<HttpAuthController> auth_; 153 154 std::string request_line_; 155 HttpRequestHeaders request_headers_; 156 157 const ProxyServer proxy_server_; 158 159 // This delegate must outlive this proxy client socket. 160 raw_ptr<ProxyDelegate> proxy_delegate_; 161 162 // Network traffic annotation for handshaking and setup. 163 const NetworkTrafficAnnotationTag traffic_annotation_; 164 165 const NetLogWithSource net_log_; 166 }; 167 168 } // namespace net 169 170 #endif // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_H_ 171