QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 1 | // Copyright 2016 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_HTTP2_HPACK_DECODER_HPACK_WHOLE_ENTRY_BUFFER_H_ |
| 6 | #define QUICHE_HTTP2_HPACK_DECODER_HPACK_WHOLE_ENTRY_BUFFER_H_ |
| 7 | |
| 8 | // HpackWholeEntryBuffer isolates a listener from the fact that an entry may |
| 9 | // be split across multiple input buffers, providing one callback per entry. |
| 10 | // HpackWholeEntryBuffer requires that the HpackEntryDecoderListener be made in |
| 11 | // the correct order, which is tested by hpack_entry_decoder_test.cc. |
| 12 | |
| 13 | #include <stddef.h> |
| 14 | |
| 15 | #include "net/third_party/quiche/src/http2/hpack/decoder/hpack_decoder_string_buffer.h" |
bnc | 0ac0f72 | 2020-02-07 07:23:09 -0800 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/http2/hpack/decoder/hpack_decoding_error.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/http2/hpack/decoder/hpack_entry_decoder_listener.h" |
| 18 | #include "net/third_party/quiche/src/http2/hpack/decoder/hpack_whole_entry_listener.h" |
| 19 | #include "net/third_party/quiche/src/http2/hpack/http2_hpack_constants.h" |
bnc | 641ace7 | 2020-01-21 12:24:57 -0800 | [diff] [blame] | 20 | #include "net/third_party/quiche/src/common/platform/api/quiche_export.h" |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame] | 21 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 22 | |
| 23 | namespace http2 { |
| 24 | |
| 25 | // TODO(jamessynge): Consider renaming HpackEntryDecoderListener to |
| 26 | // HpackEntryPartsListener or HpackEntryFragmentsListener. |
bnc | 641ace7 | 2020-01-21 12:24:57 -0800 | [diff] [blame] | 27 | class QUICHE_EXPORT_PRIVATE HpackWholeEntryBuffer |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 28 | : public HpackEntryDecoderListener { |
| 29 | public: |
| 30 | // max_string_size specifies the maximum size of an on-the-wire string (name |
| 31 | // or value, plain or Huffman encoded) that will be accepted. See sections |
| 32 | // 5.1 and 5.2 of RFC 7541. This is a defense against OOM attacks; HTTP/2 |
| 33 | // allows a decoder to enforce any limit of the size of the header lists |
| 34 | // that it is willing decode, including less than the MAX_HEADER_LIST_SIZE |
| 35 | // setting, a setting that is initially unlimited. For example, we might |
| 36 | // choose to send a MAX_HEADER_LIST_SIZE of 64KB, and to use that same value |
| 37 | // as the upper bound for individual strings. |
| 38 | HpackWholeEntryBuffer(HpackWholeEntryListener* listener, |
| 39 | size_t max_string_size); |
| 40 | ~HpackWholeEntryBuffer() override; |
| 41 | |
| 42 | HpackWholeEntryBuffer(const HpackWholeEntryBuffer&) = delete; |
| 43 | HpackWholeEntryBuffer& operator=(const HpackWholeEntryBuffer&) = delete; |
| 44 | |
| 45 | // Set the listener to be notified when a whole entry has been decoded. |
| 46 | // The listener may be changed at any time. |
| 47 | void set_listener(HpackWholeEntryListener* listener); |
| 48 | |
| 49 | // Set how much encoded data this decoder is willing to buffer. |
| 50 | // TODO(jamessynge): Come up with consistent semantics for this protection |
| 51 | // across the various decoders; e.g. should it be for a single string or |
| 52 | // a single header entry? |
| 53 | void set_max_string_size_bytes(size_t max_string_size_bytes); |
| 54 | |
| 55 | // Ensure that decoded strings pointed to by the HpackDecoderStringBuffer |
| 56 | // instances name_ and value_ are buffered, which allows any underlying |
| 57 | // transport buffer to be freed or reused without overwriting the decoded |
| 58 | // strings. This is needed only when an HPACK entry is split across transport |
| 59 | // buffers. See HpackDecoder::DecodeFragment. |
| 60 | void BufferStringsIfUnbuffered(); |
| 61 | |
| 62 | // Was an error detected? After an error has been detected and reported, |
| 63 | // no further callbacks will be made to the listener. |
| 64 | bool error_detected() const { return error_detected_; } |
| 65 | |
| 66 | // Returns the estimate of dynamically allocated memory in bytes. |
| 67 | size_t EstimateMemoryUsage() const; |
| 68 | |
| 69 | // Implement the HpackEntryDecoderListener methods. |
| 70 | |
| 71 | void OnIndexedHeader(size_t index) override; |
| 72 | void OnStartLiteralHeader(HpackEntryType entry_type, |
| 73 | size_t maybe_name_index) override; |
| 74 | void OnNameStart(bool huffman_encoded, size_t len) override; |
| 75 | void OnNameData(const char* data, size_t len) override; |
| 76 | void OnNameEnd() override; |
| 77 | void OnValueStart(bool huffman_encoded, size_t len) override; |
| 78 | void OnValueData(const char* data, size_t len) override; |
| 79 | void OnValueEnd() override; |
| 80 | void OnDynamicTableSizeUpdate(size_t size) override; |
| 81 | |
bnc | a47c5c9 | 2020-02-20 09:10:13 -0800 | [diff] [blame^] | 82 | const HpackDecoderStringBuffer& name() const { return name_; } |
| 83 | |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 84 | private: |
bnc | 0ac0f72 | 2020-02-07 07:23:09 -0800 | [diff] [blame] | 85 | void ReportError(HpackDecodingError error); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 86 | |
| 87 | HpackWholeEntryListener* listener_; |
| 88 | HpackDecoderStringBuffer name_, value_; |
| 89 | |
| 90 | // max_string_size_bytes_ specifies the maximum allowed size of an on-the-wire |
| 91 | // string. Larger strings will be reported as errors to the listener; the |
| 92 | // endpoint should treat these as COMPRESSION errors, which are CONNECTION |
| 93 | // level errors. |
| 94 | size_t max_string_size_bytes_; |
| 95 | |
| 96 | // The name index (or zero) of the current header entry with a literal value. |
| 97 | size_t maybe_name_index_; |
| 98 | |
| 99 | // The type of the current header entry (with literals) that is being decoded. |
| 100 | HpackEntryType entry_type_; |
| 101 | |
| 102 | bool error_detected_ = false; |
| 103 | }; |
| 104 | |
| 105 | } // namespace http2 |
| 106 | |
| 107 | #endif // QUICHE_HTTP2_HPACK_DECODER_HPACK_WHOLE_ENTRY_BUFFER_H_ |