• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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_MASQUE_MASQUE_SERVER_BACKEND_H_
6 #define QUICHE_QUIC_MASQUE_MASQUE_SERVER_BACKEND_H_
7 
8 #include "absl/container/flat_hash_map.h"
9 #include "quiche/quic/masque/masque_utils.h"
10 #include "quiche/quic/platform/api/quic_export.h"
11 #include "quiche/quic/tools/quic_memory_cache_backend.h"
12 #include "quiche/spdy/core/http2_header_block.h"
13 
14 namespace quic {
15 
16 // QUIC server backend that understands MASQUE requests, but otherwise answers
17 // HTTP queries using an in-memory cache.
18 class QUIC_NO_EXPORT MasqueServerBackend : public QuicMemoryCacheBackend {
19  public:
20   // Interface meant to be implemented by the owner of the MasqueServerBackend
21   // instance.
22   class QUIC_NO_EXPORT BackendClient {
23    public:
24     virtual std::unique_ptr<QuicBackendResponse> HandleMasqueRequest(
25         const spdy::Http2HeaderBlock& request_headers,
26         QuicSimpleServerBackend::RequestHandler* request_handler) = 0;
27     virtual ~BackendClient() = default;
28   };
29 
30   explicit MasqueServerBackend(MasqueMode masque_mode,
31                                const std::string& server_authority,
32                                const std::string& cache_directory);
33 
34   // Disallow copy and assign.
35   MasqueServerBackend(const MasqueServerBackend&) = delete;
36   MasqueServerBackend& operator=(const MasqueServerBackend&) = delete;
37 
38   // From QuicMemoryCacheBackend.
39   void FetchResponseFromBackend(
40       const spdy::Http2HeaderBlock& request_headers,
41       const std::string& request_body,
42       QuicSimpleServerBackend::RequestHandler* request_handler) override;
43   void HandleConnectHeaders(const spdy::Http2HeaderBlock& request_headers,
44                             RequestHandler* request_handler) override;
45 
46   void CloseBackendResponseStream(
47       QuicSimpleServerBackend::RequestHandler* request_handler) override;
48 
49   // Register backend client that can handle MASQUE requests.
50   void RegisterBackendClient(QuicConnectionId connection_id,
51                              BackendClient* backend_client);
52 
53   // Unregister backend client.
54   void RemoveBackendClient(QuicConnectionId connection_id);
55 
56   // Provides a unique client IP address for each CONNECT-IP client.
57   QuicIpAddress GetNextClientIpAddress();
58 
59  private:
60   // Handle MASQUE request.
61   bool MaybeHandleMasqueRequest(
62       const spdy::Http2HeaderBlock& request_headers,
63       QuicSimpleServerBackend::RequestHandler* request_handler);
64 
65   MasqueMode masque_mode_;
66   std::string server_authority_;
67 
68   struct QUIC_NO_EXPORT BackendClientState {
69     BackendClient* backend_client;
70     std::vector<std::unique_ptr<QuicBackendResponse>> responses;
71   };
72   absl::flat_hash_map<QuicConnectionId, BackendClientState,
73                       QuicConnectionIdHash>
74       backend_client_states_;
75   uint8_t connect_ip_next_client_ip_[4];
76 };
77 
78 }  // namespace quic
79 
80 #endif  // QUICHE_QUIC_MASQUE_MASQUE_SERVER_BACKEND_H_
81