xref: /aosp_15_r20/external/cronet/net/http/proxy_client_socket.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_PROXY_CLIENT_SOCKET_H_
6 #define NET_HTTP_PROXY_CLIENT_SOCKET_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "net/base/completion_once_callback.h"
12 #include "net/base/net_export.h"
13 #include "net/base/request_priority.h"
14 #include "net/socket/ssl_client_socket.h"
15 #include "net/socket/stream_socket.h"
16 
17 namespace net {
18 
19 class HostPortPair;
20 class HttpAuthController;
21 class HttpResponseInfo;
22 class HttpRequestHeaders;
23 class HttpAuthController;
24 class NetLogWithSource;
25 
26 // A common base class for a stream socket tunneled through a proxy.
27 class NET_EXPORT_PRIVATE ProxyClientSocket : public StreamSocket {
28  public:
29   ProxyClientSocket() = default;
30 
31   ProxyClientSocket(const ProxyClientSocket&) = delete;
32   ProxyClientSocket& operator=(const ProxyClientSocket&) = delete;
33 
34   ~ProxyClientSocket() override = default;
35 
36   // Returns the HttpResponseInfo (including HTTP Headers) from
37   // the response to the CONNECT request.
38   virtual const HttpResponseInfo* GetConnectResponseInfo() const = 0;
39 
40   // Returns the HttpAuthController which can be used
41   // to interact with an HTTP Proxy Authorization Required (407) request.
42   virtual const scoped_refptr<HttpAuthController>& GetAuthController() const
43       = 0;
44 
45   // If Connect (or its callback) returns PROXY_AUTH_REQUESTED, then an
46   // auth challenge was received.  If the HttpAuthController's HaveAuth()
47   // method returns true, then the request just needs to be restarted with
48   // this method to try with those credentials, and new credentials cannot
49   // be provided.  Otherwise, credentials should be added to the
50   // HttpAuthController before calling RestartWithAuth.  Not all
51   // ProxyClientSocket implementations will be restartable.  Such
52   // implementations should disconnect themselves and return OK.
53   virtual int RestartWithAuth(CompletionOnceCallback callback) = 0;
54 
55   // Set the priority of the underlying stream (for SPDY and QUIC)
56   virtual void SetStreamPriority(RequestPriority priority);
57 
58  protected:
59   // The HTTP CONNECT method for establishing a tunnel connection is documented
60   // in Section 9.3.6 of RFC 9110.
61   // https://www.rfc-editor.org/rfc/rfc9110#name-connect
62   static void BuildTunnelRequest(const HostPortPair& endpoint,
63                                  const HttpRequestHeaders& extra_headers,
64                                  const std::string& user_agent,
65                                  std::string* request_line,
66                                  HttpRequestHeaders* request_headers);
67 
68   // When an auth challenge (407 response) is received during tunnel
69   // construction/ this method should be called.
70   static int HandleProxyAuthChallenge(HttpAuthController* auth,
71                                       HttpResponseInfo* response,
72                                       const NetLogWithSource& net_log);
73 
74   // When a proxy authentication response is received during tunnel
75   // construction, this method should be called to strip everything
76   // but the auth header from the redirect response.
77   static void SanitizeProxyAuth(HttpResponseInfo& response);
78 };
79 
80 }  // namespace net
81 
82 #endif  // NET_HTTP_PROXY_CLIENT_SOCKET_H_
83