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