http_stream_request.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_STREAM_REQUEST_H_
  5. #define NET_HTTP_HTTP_STREAM_REQUEST_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "net/base/load_states.h"
  9. #include "net/base/net_error_details.h"
  10. #include "net/base/net_export.h"
  11. #include "net/base/request_priority.h"
  12. #include "net/http/http_response_info.h"
  13. #include "net/log/net_log_source.h"
  14. #include "net/log/net_log_with_source.h"
  15. #include "net/proxy_resolution/proxy_info.h"
  16. #include "net/socket/connection_attempts.h"
  17. #include "net/socket/next_proto.h"
  18. #include "net/spdy/spdy_session_key.h"
  19. #include "net/spdy/spdy_session_pool.h"
  20. #include "net/ssl/ssl_config.h"
  21. #include "net/ssl/ssl_info.h"
  22. #include "net/websockets/websocket_handshake_stream_base.h"
  23. #include "url/gurl.h"
  24. namespace net {
  25. class BidirectionalStreamImpl;
  26. class HttpAuthController;
  27. class HttpStream;
  28. class SSLCertRequestInfo;
  29. // The HttpStreamRequest is the client's handle to the worker object which
  30. // handles the creation of an HttpStream. While the HttpStream is being
  31. // created, this object is the creator's handle for interacting with the
  32. // HttpStream creation process. The request is cancelled by deleting it, after
  33. // which no callbacks will be invoked.
  34. class NET_EXPORT_PRIVATE HttpStreamRequest {
  35. public:
  36. // Indicates which type of stream is requested.
  37. enum StreamType {
  38. BIDIRECTIONAL_STREAM,
  39. HTTP_STREAM,
  40. };
  41. // The HttpStreamRequest::Delegate is a set of callback methods for a
  42. // HttpStreamRequestJob. Generally, only one of these methods will be
  43. // called as a result of a stream request.
  44. class NET_EXPORT_PRIVATE Delegate {
  45. public:
  46. virtual ~Delegate() = default;
  47. // This is the success case for RequestStream.
  48. // |stream| is now owned by the delegate.
  49. // |used_ssl_config| indicates the actual SSL configuration used for this
  50. // stream, since the HttpStreamRequest may have modified the configuration
  51. // during stream processing.
  52. // |used_proxy_info| indicates the actual ProxyInfo used for this stream,
  53. // since the HttpStreamRequest performs the proxy resolution.
  54. virtual void OnStreamReady(const SSLConfig& used_ssl_config,
  55. const ProxyInfo& used_proxy_info,
  56. std::unique_ptr<HttpStream> stream) = 0;
  57. // This is the success case for RequestWebSocketHandshakeStream.
  58. // |stream| is now owned by the delegate.
  59. // |used_ssl_config| indicates the actual SSL configuration used for this
  60. // stream, since the HttpStreamRequest may have modified the configuration
  61. // during stream processing.
  62. // |used_proxy_info| indicates the actual ProxyInfo used for this stream,
  63. // since the HttpStreamRequest performs the proxy resolution.
  64. virtual void OnWebSocketHandshakeStreamReady(
  65. const SSLConfig& used_ssl_config,
  66. const ProxyInfo& used_proxy_info,
  67. std::unique_ptr<WebSocketHandshakeStreamBase> stream) = 0;
  68. virtual void OnBidirectionalStreamImplReady(
  69. const SSLConfig& used_ssl_config,
  70. const ProxyInfo& used_proxy_info,
  71. std::unique_ptr<BidirectionalStreamImpl> stream) = 0;
  72. // This is the failure to create a stream case.
  73. // |used_ssl_config| indicates the actual SSL configuration used for this
  74. // stream, since the HttpStreamRequest may have modified the configuration
  75. // during stream processing.
  76. // |used_proxy_info| indicates the actual ProxyInfo used for this stream,
  77. // since the HttpStreamRequest performs the proxy resolution.
  78. virtual void OnStreamFailed(int status,
  79. const NetErrorDetails& net_error_details,
  80. const SSLConfig& used_ssl_config,
  81. const ProxyInfo& used_proxy_info,
  82. ResolveErrorInfo resolve_error_info) = 0;
  83. // Called when we have a certificate error for the request.
  84. // |used_ssl_config| indicates the actual SSL configuration used for this
  85. // stream, since the HttpStreamRequest may have modified the configuration
  86. // during stream processing.
  87. virtual void OnCertificateError(int status,
  88. const SSLConfig& used_ssl_config,
  89. const SSLInfo& ssl_info) = 0;
  90. // This is the failure case where we need proxy authentication during
  91. // proxy tunnel establishment. For the tunnel case, we were unable to
  92. // create the HttpStream, so the caller provides the auth and then resumes
  93. // the HttpStreamRequest.
  94. //
  95. // For the non-tunnel case, the caller will discover the authentication
  96. // failure when reading response headers. At that point, it will handle the
  97. // authentication failure and restart the HttpStreamRequest entirely.
  98. //
  99. // Ownership of |auth_controller| and |proxy_response| are owned
  100. // by the HttpStreamRequest. |proxy_response| is not guaranteed to be usable
  101. // after the lifetime of this callback. The delegate may take a reference
  102. // to |auth_controller| if it is needed beyond the lifetime of this
  103. // callback.
  104. //
  105. // |used_ssl_config| indicates the actual SSL configuration used for this
  106. // stream, since the HttpStreamRequest may have modified the configuration
  107. // during stream processing.
  108. virtual void OnNeedsProxyAuth(const HttpResponseInfo& proxy_response,
  109. const SSLConfig& used_ssl_config,
  110. const ProxyInfo& used_proxy_info,
  111. HttpAuthController* auth_controller) = 0;
  112. // This is the failure for SSL Client Auth
  113. // Ownership of |cert_info| is retained by the HttpStreamRequest. The
  114. // delegate may take a reference if it needs the cert_info beyond the
  115. // lifetime of this callback.
  116. virtual void OnNeedsClientAuth(const SSLConfig& used_ssl_config,
  117. SSLCertRequestInfo* cert_info) = 0;
  118. // Called when finding all QUIC alternative services are marked broken for
  119. // the origin in this request which advertises supporting QUIC.
  120. virtual void OnQuicBroken() = 0;
  121. };
  122. class NET_EXPORT_PRIVATE Helper {
  123. public:
  124. virtual ~Helper() = default;
  125. // Returns the LoadState for Request.
  126. virtual LoadState GetLoadState() const = 0;
  127. // Called when Request is destructed.
  128. virtual void OnRequestComplete() = 0;
  129. // Called to resume the HttpStream creation process when necessary
  130. // Proxy authentication credentials are collected.
  131. virtual int RestartTunnelWithProxyAuth() = 0;
  132. // Called when the priority of transaction changes.
  133. virtual void SetPriority(RequestPriority priority) = 0;
  134. };
  135. // Request will notify |job_controller| when it's destructed.
  136. // Thus |job_controller| is valid for the lifetime of the |this| Request.
  137. HttpStreamRequest(const GURL& url,
  138. Helper* helper,
  139. HttpStreamRequest::Delegate* delegate,
  140. WebSocketHandshakeStreamBase::CreateHelper*
  141. websocket_handshake_stream_create_helper,
  142. const NetLogWithSource& net_log,
  143. StreamType stream_type);
  144. HttpStreamRequest(const HttpStreamRequest&) = delete;
  145. HttpStreamRequest& operator=(const HttpStreamRequest&) = delete;
  146. ~HttpStreamRequest();
  147. // When a HttpStream creation process is stalled due to necessity
  148. // of Proxy authentication credentials, the delegate OnNeedsProxyAuth
  149. // will have been called. It now becomes the delegate's responsibility
  150. // to collect the necessary credentials, and then call this method to
  151. // resume the HttpStream creation process.
  152. int RestartTunnelWithProxyAuth();
  153. // Called when the priority of the parent transaction changes.
  154. void SetPriority(RequestPriority priority);
  155. // Marks completion of the request. Must be called before OnStreamReady().
  156. void Complete(bool was_alpn_negotiated,
  157. NextProto negotiated_protocol,
  158. bool using_spdy);
  159. // Called by |helper_| to record connection attempts made by the socket
  160. // layer in an attached Job for this stream request.
  161. void AddConnectionAttempts(const ConnectionAttempts& attempts);
  162. // Returns the LoadState for the request.
  163. LoadState GetLoadState() const;
  164. // Returns true if TLS/ALPN was negotiated for this stream.
  165. bool was_alpn_negotiated() const;
  166. // Protocol negotiated with the server.
  167. NextProto negotiated_protocol() const;
  168. // Returns true if this stream is being fetched over SPDY.
  169. bool using_spdy() const;
  170. // Returns socket-layer connection attempts made for this stream request.
  171. const ConnectionAttempts& connection_attempts() const;
  172. // Returns the WebSocketHandshakeStreamBase::CreateHelper for this stream
  173. // request.
  174. WebSocketHandshakeStreamBase::CreateHelper*
  175. websocket_handshake_stream_create_helper() const;
  176. // The GURL from the HttpRequestInfo the started the Request.
  177. const GURL& url() const { return url_; }
  178. const NetLogWithSource& net_log() const { return net_log_; }
  179. StreamType stream_type() const { return stream_type_; }
  180. bool completed() const { return completed_; }
  181. private:
  182. const GURL url_;
  183. // Unowned. The helper must not be destroyed before this object is.
  184. raw_ptr<Helper> helper_;
  185. const raw_ptr<WebSocketHandshakeStreamBase::CreateHelper>
  186. websocket_handshake_stream_create_helper_;
  187. const NetLogWithSource net_log_;
  188. bool completed_ = false;
  189. bool was_alpn_negotiated_ = false;
  190. // Protocol negotiated with the server.
  191. NextProto negotiated_protocol_ = kProtoUnknown;
  192. bool using_spdy_ = false;
  193. ConnectionAttempts connection_attempts_;
  194. const StreamType stream_type_;
  195. };
  196. } // namespace net
  197. #endif // NET_HTTP_HTTP_STREAM_REQUEST_H_