http_proxy_client_socket.h 5.7 KB

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