xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/http/quic_server_session_base.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
5 // A server specific QuicSession subclass.
6 
7 #ifndef QUICHE_QUIC_CORE_HTTP_QUIC_SERVER_SESSION_BASE_H_
8 #define QUICHE_QUIC_CORE_HTTP_QUIC_SERVER_SESSION_BASE_H_
9 
10 #include <cstdint>
11 #include <memory>
12 #include <set>
13 #include <string>
14 #include <vector>
15 
16 #include "quiche/quic/core/crypto/quic_compressed_certs_cache.h"
17 #include "quiche/quic/core/http/quic_spdy_session.h"
18 #include "quiche/quic/core/quic_crypto_server_stream_base.h"
19 #include "quiche/quic/core/quic_packets.h"
20 #include "quiche/quic/platform/api/quic_export.h"
21 
22 namespace quic {
23 
24 class QuicConfig;
25 class QuicConnection;
26 class QuicCryptoServerConfig;
27 
28 namespace test {
29 class QuicServerSessionBasePeer;
30 class QuicSimpleServerSessionPeer;
31 }  // namespace test
32 
33 class QUICHE_EXPORT QuicServerSessionBase : public QuicSpdySession {
34  public:
35   // Does not take ownership of |connection|. |crypto_config| must outlive the
36   // session. |helper| must outlive any created crypto streams.
37   QuicServerSessionBase(const QuicConfig& config,
38                         const ParsedQuicVersionVector& supported_versions,
39                         QuicConnection* connection,
40                         QuicSession::Visitor* visitor,
41                         QuicCryptoServerStreamBase::Helper* helper,
42                         const QuicCryptoServerConfig* crypto_config,
43                         QuicCompressedCertsCache* compressed_certs_cache);
44   QuicServerSessionBase(const QuicServerSessionBase&) = delete;
45   QuicServerSessionBase& operator=(const QuicServerSessionBase&) = delete;
46 
47   // Override the base class to cancel any ongoing asychronous crypto.
48   void OnConnectionClosed(const QuicConnectionCloseFrame& frame,
49                           ConnectionCloseSource source) override;
50 
51   // Sends a server config update to the client, containing new bandwidth
52   // estimate.
53   void OnCongestionWindowChange(QuicTime now) override;
54 
55   ~QuicServerSessionBase() override;
56 
57   void Initialize() override;
58 
crypto_stream()59   const QuicCryptoServerStreamBase* crypto_stream() const {
60     return crypto_stream_.get();
61   }
62 
63   // Override base class to process bandwidth related config received from
64   // client.
65   void OnConfigNegotiated() override;
66 
set_serving_region(const std::string & serving_region)67   void set_serving_region(const std::string& serving_region) {
68     serving_region_ = serving_region;
69   }
70 
serving_region()71   const std::string& serving_region() const { return serving_region_; }
72 
73   QuicSSLConfig GetSSLConfig() const override;
74 
75  protected:
76   // QuicSession methods(override them with return type of QuicSpdyStream*):
77   QuicCryptoServerStreamBase* GetMutableCryptoStream() override;
78 
79   const QuicCryptoServerStreamBase* GetCryptoStream() const override;
80 
81   std::optional<CachedNetworkParameters> GenerateCachedNetworkParameters()
82       const override;
83 
84   // If an outgoing stream can be created, return true.
85   // Return false when connection is closed or forward secure encryption hasn't
86   // established yet or number of server initiated streams already reaches the
87   // upper limit.
88   bool ShouldCreateOutgoingBidirectionalStream() override;
89   bool ShouldCreateOutgoingUnidirectionalStream() override;
90 
91   // If we should create an incoming stream, returns true. Otherwise
92   // does error handling, including communicating the error to the client and
93   // possibly closing the connection, and returns false.
94   bool ShouldCreateIncomingStream(QuicStreamId id) override;
95 
96   virtual std::unique_ptr<QuicCryptoServerStreamBase>
97   CreateQuicCryptoServerStream(
98       const QuicCryptoServerConfig* crypto_config,
99       QuicCompressedCertsCache* compressed_certs_cache) = 0;
100 
crypto_config()101   const QuicCryptoServerConfig* crypto_config() { return crypto_config_; }
102 
stream_helper()103   QuicCryptoServerStreamBase::Helper* stream_helper() { return helper_; }
104 
105  private:
106   friend class test::QuicServerSessionBasePeer;
107   friend class test::QuicSimpleServerSessionPeer;
108 
109   // Informs the QuicCryptoStream of the SETTINGS that will be used on this
110   // connection, so that the server crypto stream knows whether to accept 0-RTT
111   // data.
112   void SendSettingsToCryptoStream();
113 
114   const QuicCryptoServerConfig* crypto_config_;
115 
116   // The cache which contains most recently compressed certs.
117   // Owned by QuicDispatcher.
118   QuicCompressedCertsCache* compressed_certs_cache_;
119 
120   std::unique_ptr<QuicCryptoServerStreamBase> crypto_stream_;
121 
122   // Pointer to the helper used to create crypto server streams. Must outlive
123   // streams created via CreateQuicCryptoServerStream.
124   QuicCryptoServerStreamBase::Helper* helper_;
125 
126   // Whether bandwidth resumption is enabled for this connection.
127   bool bandwidth_resumption_enabled_;
128 
129   // The most recent bandwidth estimate sent to the client.
130   QuicBandwidth bandwidth_estimate_sent_to_client_;
131 
132   // Text describing server location. Sent to the client as part of the
133   // bandwidth estimate in the source-address token. Optional, can be left
134   // empty.
135   std::string serving_region_;
136 
137   // Time at which we send the last SCUP to the client.
138   QuicTime last_scup_time_;
139 
140   // Number of packets sent to the peer, at the time we last sent a SCUP.
141   QuicPacketNumber last_scup_packet_number_;
142 
143   // Converts QuicBandwidth to an int32 bytes/second that can be
144   // stored in CachedNetworkParameters.  TODO(jokulik): This function
145   // should go away once we fix http://b//27897982
146   int32_t BandwidthToCachedParameterBytesPerSecond(
147       const QuicBandwidth& bandwidth) const;
148 };
149 
150 }  // namespace quic
151 
152 #endif  // QUICHE_QUIC_CORE_HTTP_QUIC_SERVER_SESSION_BASE_H_
153