QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 2013 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 | // The base class for streams which deliver data to/from an application. |
| 6 | // In each direction, the data on such a stream first contains compressed |
| 7 | // headers then body data. |
| 8 | |
| 9 | #ifndef QUICHE_QUIC_CORE_HTTP_QUIC_SPDY_STREAM_H_ |
| 10 | #define QUICHE_QUIC_CORE_HTTP_QUIC_SPDY_STREAM_H_ |
| 11 | |
| 12 | #include <sys/types.h> |
| 13 | |
| 14 | #include <cstddef> |
| 15 | #include <list> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 16 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | #include "net/third_party/quiche/src/quic/core/http/http_decoder.h" |
| 19 | #include "net/third_party/quiche/src/quic/core/http/http_encoder.h" |
| 20 | #include "net/third_party/quiche/src/quic/core/http/quic_header_list.h" |
| 21 | #include "net/third_party/quiche/src/quic/core/http/quic_spdy_stream_body_buffer.h" |
| 22 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
| 23 | #include "net/third_party/quiche/src/quic/core/quic_stream.h" |
| 24 | #include "net/third_party/quiche/src/quic/core/quic_stream_sequencer.h" |
| 25 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
| 26 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 27 | #include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 28 | #include "net/third_party/quiche/src/spdy/core/spdy_framer.h" |
| 29 | |
| 30 | namespace quic { |
| 31 | |
| 32 | namespace test { |
| 33 | class QuicSpdyStreamPeer; |
| 34 | class QuicStreamPeer; |
| 35 | } // namespace test |
| 36 | |
| 37 | class QuicSpdySession; |
| 38 | |
| 39 | // A QUIC stream that can send and receive HTTP2 (SPDY) headers. |
| 40 | class QUIC_EXPORT_PRIVATE QuicSpdyStream : public QuicStream { |
| 41 | public: |
| 42 | // Visitor receives callbacks from the stream. |
| 43 | class QUIC_EXPORT_PRIVATE Visitor { |
| 44 | public: |
| 45 | Visitor() {} |
| 46 | Visitor(const Visitor&) = delete; |
| 47 | Visitor& operator=(const Visitor&) = delete; |
| 48 | |
| 49 | // Called when the stream is closed. |
| 50 | virtual void OnClose(QuicSpdyStream* stream) = 0; |
| 51 | |
| 52 | // Allows subclasses to override and do work. |
| 53 | virtual void OnPromiseHeadersComplete(QuicStreamId promised_id, |
| 54 | size_t frame_len) {} |
| 55 | |
| 56 | protected: |
| 57 | virtual ~Visitor() {} |
| 58 | }; |
| 59 | |
| 60 | QuicSpdyStream(QuicStreamId id, |
| 61 | QuicSpdySession* spdy_session, |
| 62 | StreamType type); |
| 63 | QuicSpdyStream(PendingStream pending, |
| 64 | QuicSpdySession* spdy_session, |
| 65 | StreamType type); |
| 66 | QuicSpdyStream(const QuicSpdyStream&) = delete; |
| 67 | QuicSpdyStream& operator=(const QuicSpdyStream&) = delete; |
| 68 | ~QuicSpdyStream() override; |
| 69 | |
| 70 | // QuicStream implementation |
| 71 | void OnClose() override; |
| 72 | |
| 73 | // Override to maybe close the write side after writing. |
| 74 | void OnCanWrite() override; |
| 75 | |
| 76 | // Called by the session when headers with a priority have been received |
| 77 | // for this stream. This method will only be called for server streams. |
| 78 | virtual void OnStreamHeadersPriority(spdy::SpdyPriority priority); |
| 79 | |
| 80 | // Called by the session when decompressed headers have been completely |
| 81 | // delivered to this stream. If |fin| is true, then this stream |
| 82 | // should be closed; no more data will be sent by the peer. |
| 83 | virtual void OnStreamHeaderList(bool fin, |
| 84 | size_t frame_len, |
| 85 | const QuicHeaderList& header_list); |
| 86 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 87 | // Called by the session when decompressed push promise headers have |
| 88 | // been completely delivered to this stream. |
| 89 | virtual void OnPromiseHeaderList(QuicStreamId promised_id, |
| 90 | size_t frame_len, |
| 91 | const QuicHeaderList& header_list); |
| 92 | |
| 93 | // Called by the session when a PRIORITY frame has been been received for this |
| 94 | // stream. This method will only be called for server streams. |
| 95 | void OnPriorityFrame(spdy::SpdyPriority priority); |
| 96 | |
| 97 | // Override the base class to not discard response when receiving |
| 98 | // QUIC_STREAM_NO_ERROR. |
| 99 | void OnStreamReset(const QuicRstStreamFrame& frame) override; |
| 100 | |
| 101 | // Called by the sequencer when new data is available. Decodes the data and |
| 102 | // calls OnBodyAvailable() to pass to the upper layer. |
| 103 | void OnDataAvailable() override; |
| 104 | |
| 105 | // Called in OnDataAvailable() after it finishes the decoding job. |
| 106 | virtual void OnBodyAvailable() = 0; |
| 107 | |
| 108 | // Writes the headers contained in |header_block| to the dedicated |
| 109 | // headers stream. |
| 110 | virtual size_t WriteHeaders( |
| 111 | spdy::SpdyHeaderBlock header_block, |
| 112 | bool fin, |
| 113 | QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener); |
| 114 | |
| 115 | // Sends |data| to the peer, or buffers if it can't be sent immediately. |
| 116 | void WriteOrBufferBody(QuicStringPiece data, bool fin); |
| 117 | |
| 118 | // Writes the trailers contained in |trailer_block| to the dedicated |
| 119 | // headers stream. Trailers will always have the FIN set. |
| 120 | virtual size_t WriteTrailers( |
| 121 | spdy::SpdyHeaderBlock trailer_block, |
| 122 | QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener); |
| 123 | |
| 124 | // Override to report newly acked bytes via ack_listener_. |
| 125 | bool OnStreamFrameAcked(QuicStreamOffset offset, |
| 126 | QuicByteCount data_length, |
| 127 | bool fin_acked, |
| 128 | QuicTime::Delta ack_delay_time, |
| 129 | QuicByteCount* newly_acked_length) override; |
| 130 | |
| 131 | // Override to report bytes retransmitted via ack_listener_. |
| 132 | void OnStreamFrameRetransmitted(QuicStreamOffset offset, |
| 133 | QuicByteCount data_length, |
| 134 | bool fin_retransmitted) override; |
| 135 | |
| 136 | // Does the same thing as WriteOrBufferBody except this method takes iovec |
| 137 | // as the data input. Right now it only calls WritevData. |
| 138 | // TODO(renjietang): Write data frame header before writing body. |
| 139 | QuicConsumedData WritevBody(const struct iovec* iov, int count, bool fin); |
| 140 | |
| 141 | // Does the same thing as WriteOrBufferBody except this method takes |
| 142 | // memslicespan as the data input. Right now it only calls WriteMemSlices. |
| 143 | // TODO(renjietang): Write data frame header before writing body. |
| 144 | QuicConsumedData WriteBodySlices(QuicMemSliceSpan slices, bool fin); |
| 145 | |
| 146 | // Marks the trailers as consumed. This applies to the case where this object |
| 147 | // receives headers and trailers as QuicHeaderLists via calls to |
| 148 | // OnStreamHeaderList(). |
| 149 | void MarkTrailersConsumed(); |
| 150 | |
| 151 | // Clears |header_list_|. |
| 152 | void ConsumeHeaderList(); |
| 153 | |
| 154 | // This block of functions wraps the sequencer's functions of the same |
| 155 | // name. These methods return uncompressed data until that has |
| 156 | // been fully processed. Then they simply delegate to the sequencer. |
| 157 | virtual size_t Readv(const struct iovec* iov, size_t iov_len); |
| 158 | virtual int GetReadableRegions(iovec* iov, size_t iov_len) const; |
| 159 | void MarkConsumed(size_t num_bytes); |
| 160 | |
| 161 | // Returns true if header contains a valid 3-digit status and parse the status |
| 162 | // code to |status_code|. |
| 163 | bool ParseHeaderStatusCode(const spdy::SpdyHeaderBlock& header, |
| 164 | int* status_code) const; |
| 165 | |
| 166 | // Returns true when all data has been read from the peer, including the fin. |
| 167 | bool IsDoneReading() const; |
| 168 | bool HasBytesToRead() const; |
| 169 | |
| 170 | void set_visitor(Visitor* visitor) { visitor_ = visitor; } |
| 171 | |
| 172 | bool headers_decompressed() const { return headers_decompressed_; } |
| 173 | |
| 174 | // Returns total amount of body bytes that have been read. |
| 175 | uint64_t total_body_bytes_read() const; |
| 176 | |
| 177 | const QuicHeaderList& header_list() const { return header_list_; } |
| 178 | |
| 179 | bool trailers_decompressed() const { return trailers_decompressed_; } |
| 180 | |
| 181 | // Returns whatever trailers have been received for this stream. |
| 182 | const spdy::SpdyHeaderBlock& received_trailers() const { |
| 183 | return received_trailers_; |
| 184 | } |
| 185 | |
| 186 | // Returns true if headers have been fully read and consumed. |
| 187 | bool FinishedReadingHeaders() const; |
| 188 | |
| 189 | // Returns true if trailers have been fully read and consumed, or FIN has |
| 190 | // been received and there are no trailers. |
| 191 | bool FinishedReadingTrailers() const; |
| 192 | |
| 193 | // Called when owning session is getting deleted to avoid subsequent |
| 194 | // use of the spdy_session_ member. |
| 195 | void ClearSession(); |
| 196 | |
| 197 | // Returns true if the sequencer has delivered the FIN, and no more body bytes |
| 198 | // will be available. |
| 199 | bool IsClosed() { return sequencer()->IsClosed(); } |
| 200 | |
QUICHE team | b478bb1 | 2019-03-18 06:48:21 -0700 | [diff] [blame] | 201 | using QuicStream::CloseWriteSide; |
| 202 | |
| 203 | protected: |
| 204 | // HTTP/3 |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 205 | void OnDataFrameStart(Http3FrameLengths frame_lengths); |
| 206 | void OnDataFramePayload(QuicStringPiece payload); |
| 207 | void OnDataFrameEnd(); |
bnc | 62446bc | 2019-03-14 06:11:25 -0700 | [diff] [blame] | 208 | void OnHeadersFrameStart(Http3FrameLengths frame_length); |
| 209 | void OnHeadersFramePayload(QuicStringPiece payload); |
bnc | a322c04 | 2019-04-12 08:56:59 -0700 | [diff] [blame] | 210 | void OnHeadersFrameEnd(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 211 | |
QUICHE team | b478bb1 | 2019-03-18 06:48:21 -0700 | [diff] [blame] | 212 | // Called when the received headers are too large. By default this will |
| 213 | // reset the stream. |
| 214 | virtual void OnHeadersTooLarge(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 215 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 216 | virtual void OnInitialHeadersComplete(bool fin, |
| 217 | size_t frame_len, |
| 218 | const QuicHeaderList& header_list); |
| 219 | virtual void OnTrailingHeadersComplete(bool fin, |
| 220 | size_t frame_len, |
| 221 | const QuicHeaderList& header_list); |
| 222 | virtual size_t WriteHeadersImpl( |
| 223 | spdy::SpdyHeaderBlock header_block, |
| 224 | bool fin, |
| 225 | QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener); |
| 226 | |
| 227 | QuicSpdySession* spdy_session() const { return spdy_session_; } |
| 228 | Visitor* visitor() { return visitor_; } |
| 229 | |
| 230 | void set_headers_decompressed(bool val) { headers_decompressed_ = val; } |
| 231 | |
| 232 | void set_ack_listener( |
| 233 | QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener) { |
| 234 | ack_listener_ = std::move(ack_listener); |
| 235 | } |
| 236 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 237 | private: |
| 238 | friend class test::QuicSpdyStreamPeer; |
| 239 | friend class test::QuicStreamPeer; |
| 240 | friend class QuicStreamUtils; |
| 241 | class HttpDecoderVisitor; |
| 242 | |
| 243 | // Given the interval marked by [|offset|, |offset| + |data_length|), return |
| 244 | // the number of frame header bytes contained in it. |
| 245 | QuicByteCount GetNumFrameHeadersInInterval(QuicStreamOffset offset, |
| 246 | QuicByteCount data_length) const; |
| 247 | |
| 248 | QuicSpdySession* spdy_session_; |
| 249 | |
| 250 | Visitor* visitor_; |
| 251 | // True if the headers have been completely decompressed. |
| 252 | bool headers_decompressed_; |
| 253 | // Contains a copy of the decompressed header (name, value) pairs until they |
| 254 | // are consumed via Readv. |
| 255 | QuicHeaderList header_list_; |
| 256 | |
| 257 | // True if the trailers have been completely decompressed. |
| 258 | bool trailers_decompressed_; |
| 259 | // True if the trailers have been consumed. |
| 260 | bool trailers_consumed_; |
| 261 | // The parsed trailers received from the peer. |
| 262 | spdy::SpdyHeaderBlock received_trailers_; |
| 263 | |
| 264 | // Http encoder for writing streams. |
| 265 | HttpEncoder encoder_; |
| 266 | // Http decoder for processing raw incoming stream frames. |
| 267 | HttpDecoder decoder_; |
| 268 | // Visitor of the HttpDecoder. |
| 269 | std::unique_ptr<HttpDecoderVisitor> http_decoder_visitor_; |
| 270 | // Buffer that contains decoded data of the stream. |
| 271 | QuicSpdyStreamBodyBuffer body_buffer_; |
| 272 | |
| 273 | // Ack listener of this stream, and it is notified when any of written bytes |
| 274 | // are acked or retransmitted. |
| 275 | QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener_; |
| 276 | |
| 277 | // Offset of unacked frame headers. |
| 278 | QuicIntervalSet<QuicStreamOffset> unacked_frame_headers_offsets_; |
| 279 | }; |
| 280 | |
| 281 | } // namespace quic |
| 282 | |
| 283 | #endif // QUICHE_QUIC_CORE_HTTP_QUIC_SPDY_STREAM_H_ |