xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/tools/connect_server_backend.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 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 
5 #ifndef QUICHE_QUIC_CONNECT_PROXY_CONNECT_SERVER_BACKEND_H_
6 #define QUICHE_QUIC_CONNECT_PROXY_CONNECT_SERVER_BACKEND_H_
7 
8 #include <cstdint>
9 #include <memory>
10 #include <string>
11 #include <utility>
12 
13 #include "absl/container/flat_hash_map.h"
14 #include "absl/container/flat_hash_set.h"
15 #include "quiche/quic/core/quic_server_id.h"
16 #include "quiche/quic/core/socket_factory.h"
17 #include "quiche/quic/tools/connect_tunnel.h"
18 #include "quiche/quic/tools/connect_udp_tunnel.h"
19 #include "quiche/quic/tools/quic_simple_server_backend.h"
20 
21 namespace quic {
22 
23 // QUIC server backend that handles CONNECT and CONNECT-UDP requests.
24 // Non-CONNECT requests are delegated to a separate backend.
25 class ConnectServerBackend : public QuicSimpleServerBackend {
26  public:
27   // `server_label` is an identifier (typically randomly generated) to identify
28   // the server or backend in error headers, per the requirements of RFC 9209,
29   // Section 2.
30   ConnectServerBackend(
31       std::unique_ptr<QuicSimpleServerBackend> non_connect_backend,
32       absl::flat_hash_set<QuicServerId> acceptable_connect_destinations,
33       absl::flat_hash_set<QuicServerId> acceptable_connect_udp_targets,
34       std::string server_label);
35 
36   ConnectServerBackend(const ConnectServerBackend&) = delete;
37   ConnectServerBackend& operator=(const ConnectServerBackend&) = delete;
38 
39   ~ConnectServerBackend() override;
40 
41   // QuicSimpleServerBackend:
42   bool InitializeBackend(const std::string& backend_url) override;
43   bool IsBackendInitialized() const override;
44   void SetSocketFactory(SocketFactory* socket_factory) override;
45   void FetchResponseFromBackend(const spdy::Http2HeaderBlock& request_headers,
46                                 const std::string& request_body,
47                                 RequestHandler* request_handler) override;
48   void HandleConnectHeaders(const spdy::Http2HeaderBlock& request_headers,
49                             RequestHandler* request_handler) override;
50   void HandleConnectData(absl::string_view data, bool data_complete,
51                          RequestHandler* request_handler) override;
52   void CloseBackendResponseStream(
53       QuicSimpleServerBackend::RequestHandler* request_handler) override;
54 
55  private:
56   std::unique_ptr<QuicSimpleServerBackend> non_connect_backend_;
57   const absl::flat_hash_set<QuicServerId> acceptable_connect_destinations_;
58   const absl::flat_hash_set<QuicServerId> acceptable_connect_udp_targets_;
59   const std::string server_label_;
60 
61   SocketFactory* socket_factory_ = nullptr;  // unowned
62   absl::flat_hash_map<std::pair<QuicConnectionId, QuicStreamId>,
63                       std::unique_ptr<ConnectTunnel>>
64       connect_tunnels_;
65   absl::flat_hash_map<std::pair<QuicConnectionId, QuicStreamId>,
66                       std::unique_ptr<ConnectUdpTunnel>>
67       connect_udp_tunnels_;
68 };
69 
70 }  // namespace quic
71 
72 #endif  // QUICHE_QUIC_CONNECT_PROXY_CONNECT_SERVER_BACKEND_H_
73