blob: 8dfc47ae55a4fca9b4ae3e876ef5281277f263e3 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2015 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_CORE_HTTP_QUIC_SPDY_SESSION_H_
6#define QUICHE_QUIC_CORE_HTTP_QUIC_SPDY_SESSION_H_
7
8#include <cstddef>
9#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -070010#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050011
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/core/http/quic_header_list.h"
13#include "net/third_party/quiche/src/quic/core/http/quic_headers_stream.h"
renjietang3a1bb802019-06-11 10:42:41 -070014#include "net/third_party/quiche/src/quic/core/http/quic_receive_control_stream.h"
15#include "net/third_party/quiche/src/quic/core/http/quic_send_control_stream.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050016#include "net/third_party/quiche/src/quic/core/http/quic_spdy_stream.h"
17#include "net/third_party/quiche/src/quic/core/qpack/qpack_decoder.h"
18#include "net/third_party/quiche/src/quic/core/qpack/qpack_decoder_stream_sender.h"
19#include "net/third_party/quiche/src/quic/core/qpack/qpack_encoder.h"
20#include "net/third_party/quiche/src/quic/core/qpack/qpack_encoder_stream_sender.h"
renjietangc2aa5cb2019-06-20 12:22:53 -070021#include "net/third_party/quiche/src/quic/core/qpack/qpack_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050022#include "net/third_party/quiche/src/quic/core/quic_session.h"
23#include "net/third_party/quiche/src/quic/core/quic_versions.h"
24#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050025#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
26#include "net/third_party/quiche/src/spdy/core/http2_frame_decoder_adapter.h"
27
28namespace quic {
29
30namespace test {
31class QuicSpdySessionPeer;
32} // namespace test
33
34// QuicHpackDebugVisitor gathers data used for understanding HPACK HoL
35// dynamics. Specifically, it is to help predict the compression
36// penalty of avoiding HoL by chagning how the dynamic table is used.
37// In chromium, the concrete instance populates an UMA
38// histogram with the data.
39class QUIC_EXPORT_PRIVATE QuicHpackDebugVisitor {
40 public:
41 QuicHpackDebugVisitor();
42 QuicHpackDebugVisitor(const QuicHpackDebugVisitor&) = delete;
43 QuicHpackDebugVisitor& operator=(const QuicHpackDebugVisitor&) = delete;
44
45 virtual ~QuicHpackDebugVisitor();
46
47 // For each HPACK indexed representation processed, |elapsed| is
48 // the time since the corresponding entry was added to the dynamic
49 // table.
50 virtual void OnUseEntry(QuicTime::Delta elapsed) = 0;
51};
52
53// A QUIC session with a headers stream.
54class QUIC_EXPORT_PRIVATE QuicSpdySession
55 : public QuicSession,
56 public QpackEncoder::DecoderStreamErrorDelegate,
renjietangc2aa5cb2019-06-20 12:22:53 -070057 public QpackDecoder::EncoderStreamErrorDelegate {
QUICHE teama6ef0a62019-03-07 20:34:33 -050058 public:
59 // Does not take ownership of |connection| or |visitor|.
60 QuicSpdySession(QuicConnection* connection,
61 QuicSession::Visitor* visitor,
62 const QuicConfig& config,
63 const ParsedQuicVersionVector& supported_versions);
64 QuicSpdySession(const QuicSpdySession&) = delete;
65 QuicSpdySession& operator=(const QuicSpdySession&) = delete;
66
67 ~QuicSpdySession() override;
68
69 void Initialize() override;
70
71 // QpackEncoder::DecoderStreamErrorDelegate implementation.
72 void OnDecoderStreamError(QuicStringPiece error_message) override;
73
QUICHE teama6ef0a62019-03-07 20:34:33 -050074 // QpackDecoder::EncoderStreamErrorDelegate implementation.
75 void OnEncoderStreamError(QuicStringPiece error_message) override;
76
QUICHE teama6ef0a62019-03-07 20:34:33 -050077 // Called by |headers_stream_| when headers with a priority have been
78 // received for a stream. This method will only be called for server streams.
79 virtual void OnStreamHeadersPriority(QuicStreamId stream_id,
80 spdy::SpdyPriority priority);
81
82 // Called by |headers_stream_| when headers have been completely received
83 // for a stream. |fin| will be true if the fin flag was set in the headers
84 // frame.
85 virtual void OnStreamHeaderList(QuicStreamId stream_id,
86 bool fin,
87 size_t frame_len,
88 const QuicHeaderList& header_list);
89
90 // Called by |headers_stream_| when push promise headers have been
91 // completely received. |fin| will be true if the fin flag was set
92 // in the headers.
93 virtual void OnPromiseHeaderList(QuicStreamId stream_id,
94 QuicStreamId promised_stream_id,
95 size_t frame_len,
96 const QuicHeaderList& header_list);
97
98 // Called by |headers_stream_| when a PRIORITY frame has been received for a
99 // stream. This method will only be called for server streams.
100 virtual void OnPriorityFrame(QuicStreamId stream_id,
101 spdy::SpdyPriority priority);
102
103 // Sends contents of |iov| to h2_deframer_, returns number of bytes processed.
104 size_t ProcessHeaderData(const struct iovec& iov);
105
106 // Writes |headers| for the stream |id| to the dedicated headers stream.
107 // If |fin| is true, then no more data will be sent for the stream |id|.
108 // If provided, |ack_notifier_delegate| will be registered to be notified when
109 // we have seen ACKs for all packets resulting from this call.
110 virtual size_t WriteHeadersOnHeadersStream(
111 QuicStreamId id,
112 spdy::SpdyHeaderBlock headers,
113 bool fin,
114 spdy::SpdyPriority priority,
115 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener);
116
117 // Writes a PRIORITY frame the to peer. Returns the size in bytes of the
118 // resulting PRIORITY frame for QUIC_VERSION_43 and above. Otherwise, does
119 // nothing and returns 0.
120 size_t WritePriority(QuicStreamId id,
121 QuicStreamId parent_stream_id,
122 int weight,
123 bool exclusive);
124
renjietang7498c8c2019-07-02 19:28:42 -0700125 // Writes a HTTP/3 PRIORITY frame to the peer.
126 void WriteH3Priority(const PriorityFrame& priority);
127
QUICHE teama6ef0a62019-03-07 20:34:33 -0500128 // Write |headers| for |promised_stream_id| on |original_stream_id| in a
129 // PUSH_PROMISE frame to peer.
renjietangf4f47122019-07-22 12:08:53 -0700130 virtual void WritePushPromise(QuicStreamId original_stream_id,
131 QuicStreamId promised_stream_id,
132 spdy::SpdyHeaderBlock headers);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500133
134 // Sends SETTINGS_MAX_HEADER_LIST_SIZE SETTINGS frame.
QUICHE team2252b702019-05-14 23:55:14 -0400135 void SendMaxHeaderListSize(size_t value);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500136
137 QpackEncoder* qpack_encoder();
138 QpackDecoder* qpack_decoder();
renjietang9818f8c2019-07-16 11:12:27 -0700139 QuicHeadersStream* headers_stream() { return headers_stream_; }
renjietangfbeb5bf2019-04-19 15:06:20 -0700140
renjietang9818f8c2019-07-16 11:12:27 -0700141 const QuicHeadersStream* headers_stream() const { return headers_stream_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500142
143 bool server_push_enabled() const { return server_push_enabled_; }
144
145 // Called by |QuicHeadersStream::UpdateEnableServerPush()| with
146 // value from SETTINGS_ENABLE_PUSH.
147 void set_server_push_enabled(bool enable) { server_push_enabled_ = enable; }
148
149 // Return true if this session wants to release headers stream's buffer
150 // aggressively.
151 virtual bool ShouldReleaseHeadersStreamSequencerBuffer();
152
153 void CloseConnectionWithDetails(QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -0700154 const std::string& details);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500155
bnc30d610c2019-07-08 18:39:59 -0700156 // Must be called before Initialize().
157 // TODO(bnc): Move to constructor argument.
QUICHE teama6ef0a62019-03-07 20:34:33 -0500158 void set_max_inbound_header_list_size(size_t max_inbound_header_list_size) {
159 max_inbound_header_list_size_ = max_inbound_header_list_size;
160 }
161
renjietang3a1bb802019-06-11 10:42:41 -0700162 void set_max_outbound_header_list_size(size_t max_outbound_header_list_size) {
163 max_outbound_header_list_size_ = max_outbound_header_list_size;
164 }
165
166 size_t max_outbound_header_list_size() const {
167 return max_outbound_header_list_size_;
168 }
169
renjietangfee2cc32019-04-05 11:32:07 -0700170 size_t max_inbound_header_list_size() const {
171 return max_inbound_header_list_size_;
172 }
173
QUICHE teamc2653c42019-03-08 13:30:06 -0800174 // Returns true if the session has active request streams.
175 bool HasActiveRequestStreams() const;
176
QUICHE teama6ef0a62019-03-07 20:34:33 -0500177 protected:
178 // Override CreateIncomingStream(), CreateOutgoingBidirectionalStream() and
179 // CreateOutgoingUnidirectionalStream() with QuicSpdyStream return type to
180 // make sure that all data streams are QuicSpdyStreams.
181 QuicSpdyStream* CreateIncomingStream(QuicStreamId id) override = 0;
renjietangbaea59c2019-05-29 15:08:14 -0700182 QuicSpdyStream* CreateIncomingStream(PendingStream* pending) override = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500183 virtual QuicSpdyStream* CreateOutgoingBidirectionalStream() = 0;
184 virtual QuicSpdyStream* CreateOutgoingUnidirectionalStream() = 0;
185
186 QuicSpdyStream* GetSpdyDataStream(const QuicStreamId stream_id);
187
188 // If an incoming stream can be created, return true.
189 virtual bool ShouldCreateIncomingStream(QuicStreamId id) = 0;
190
191 // If an outgoing bidirectional/unidirectional stream can be created, return
192 // true.
193 virtual bool ShouldCreateOutgoingBidirectionalStream() = 0;
194 virtual bool ShouldCreateOutgoingUnidirectionalStream() = 0;
195
196 // Returns true if there are open HTTP requests.
197 bool ShouldKeepConnectionAlive() const override;
198
renjietange76b2da2019-05-13 14:50:23 -0700199 // Overridden to buffer incoming unidirectional streams for version 99.
200 bool UsesPendingStreams() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500201
renjietangbb1c4892019-05-24 15:58:44 -0700202 // Overridden to Process HTTP/3 stream types. H/3 streams will be created from
203 // pending streams accordingly if the stream type can be read. Returns true if
204 // unidirectional streams are created.
205 bool ProcessPendingStream(PendingStream* pending) override;
renjietang0c558862019-05-08 13:26:23 -0700206
QUICHE teama6ef0a62019-03-07 20:34:33 -0500207 size_t WriteHeadersOnHeadersStreamImpl(
208 QuicStreamId id,
209 spdy::SpdyHeaderBlock headers,
210 bool fin,
211 QuicStreamId parent_stream_id,
212 int weight,
213 bool exclusive,
214 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener);
215
216 void OnCryptoHandshakeEvent(CryptoHandshakeEvent event) override;
217
218 bool supports_push_promise() { return supports_push_promise_; }
219
220 // Optional, enables instrumentation related to go/quic-hpack.
221 void SetHpackEncoderDebugVisitor(
222 std::unique_ptr<QuicHpackDebugVisitor> visitor);
223 void SetHpackDecoderDebugVisitor(
224 std::unique_ptr<QuicHpackDebugVisitor> visitor);
225
226 // Sets the maximum size of the header compression table spdy_framer_ is
227 // willing to use to encode header blocks.
228 void UpdateHeaderEncoderTableSize(uint32_t value);
229
230 // Called when SETTINGS_ENABLE_PUSH is received, only supported on
231 // server side.
232 void UpdateEnableServerPush(bool value);
233
234 bool IsConnected() { return connection()->connected(); }
235
dschinazi82875de2019-07-09 17:14:25 -0700236 // Sets how much encoded data the hpack decoder of h2_deframer_ is willing to
237 // buffer.
238 void set_max_decode_buffer_size_bytes(size_t max_decode_buffer_size_bytes) {
239 h2_deframer_.GetHpackDecoder()->set_max_decode_buffer_size_bytes(
240 max_decode_buffer_size_bytes);
241 }
242
243 void set_max_uncompressed_header_bytes(
244 size_t set_max_uncompressed_header_bytes);
245
QUICHE teama6ef0a62019-03-07 20:34:33 -0500246 private:
247 friend class test::QuicSpdySessionPeer;
248
249 class SpdyFramerVisitor;
250
251 // The following methods are called by the SimpleVisitor.
252
253 // Called when a HEADERS frame has been received.
254 void OnHeaders(spdy::SpdyStreamId stream_id,
255 bool has_priority,
256 spdy::SpdyPriority priority,
257 bool fin);
258
259 // Called when a PUSH_PROMISE frame has been received.
260 void OnPushPromise(spdy::SpdyStreamId stream_id,
renjietangcd8fab32019-06-17 16:22:16 -0700261 spdy::SpdyStreamId promised_stream_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500262
263 // Called when a PRIORITY frame has been received.
264 void OnPriority(spdy::SpdyStreamId stream_id, spdy::SpdyPriority priority);
265
266 // Called when the complete list of headers is available.
267 void OnHeaderList(const QuicHeaderList& header_list);
268
269 // Called when the size of the compressed frame payload is available.
270 void OnCompressedFrameSize(size_t frame_len);
271
272 std::unique_ptr<QpackEncoder> qpack_encoder_;
273 std::unique_ptr<QpackDecoder> qpack_decoder_;
274
renjietang9818f8c2019-07-16 11:12:27 -0700275 // Pointer to the header stream in stream_map_.
276 QuicHeadersStream* headers_stream_;
renjietangfbeb5bf2019-04-19 15:06:20 -0700277
renjietang3a1bb802019-06-11 10:42:41 -0700278 // HTTP/3 control streams. They are owned by QuicSession inside dynamic
279 // stream map, and can be accessed by those unowned pointers below.
280 QuicSendControlStream* send_control_stream_;
281 QuicReceiveControlStream* receive_control_stream_;
282
QUICHE teama6ef0a62019-03-07 20:34:33 -0500283 // The maximum size of a header block that will be accepted from the peer,
284 // defined per spec as key + value + overhead per field (uncompressed).
285 size_t max_inbound_header_list_size_;
286
renjietang3a1bb802019-06-11 10:42:41 -0700287 // The maximum size of a header block that can be sent to the peer. This field
288 // is informed and set by the peer via SETTINGS frame.
289 // TODO(renjietang): Honor this field when sending headers.
290 size_t max_outbound_header_list_size_;
291
QUICHE teama6ef0a62019-03-07 20:34:33 -0500292 // Set during handshake. If true, resources in x-associated-content and link
293 // headers will be pushed.
294 bool server_push_enabled_;
295
296 // Data about the stream whose headers are being processed.
297 QuicStreamId stream_id_;
298 QuicStreamId promised_stream_id_;
299 bool fin_;
300 size_t frame_len_;
301 size_t uncompressed_frame_len_;
302
303 bool supports_push_promise_;
304
305 spdy::SpdyFramer spdy_framer_;
306 http2::Http2DecoderAdapter h2_deframer_;
307 std::unique_ptr<SpdyFramerVisitor> spdy_framer_visitor_;
renjietangc2aa5cb2019-06-20 12:22:53 -0700308
309 // TODO(renjietang): Replace these two members with actual QPACK send streams.
310 NoopQpackStreamSenderDelegate encoder_stream_sender_delegate_;
311 NoopQpackStreamSenderDelegate decoder_stream_sender_delegate_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500312};
313
314} // namespace quic
315
316#endif // QUICHE_QUIC_CORE_HTTP_QUIC_SPDY_SESSION_H_