blob: 27eb718f25184f804de9df533aa8241fca98c3f5 [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// 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"
bnc0ac0f722020-02-07 07:23:09 -080016#include "net/third_party/quiche/src/http2/hpack/decoder/hpack_decoding_error.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050017#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"
bnc641ace72020-01-21 12:24:57 -080020#include "net/third_party/quiche/src/common/platform/api/quiche_export.h"
bnc74646d12019-12-13 09:21:19 -080021#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050022
23namespace http2 {
24
25// TODO(jamessynge): Consider renaming HpackEntryDecoderListener to
26// HpackEntryPartsListener or HpackEntryFragmentsListener.
bnc641ace72020-01-21 12:24:57 -080027class QUICHE_EXPORT_PRIVATE HpackWholeEntryBuffer
QUICHE teamfd50a402018-12-07 22:54:05 -050028 : 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
bnca47c5c92020-02-20 09:10:13 -080082 const HpackDecoderStringBuffer& name() const { return name_; }
83
QUICHE teamfd50a402018-12-07 22:54:05 -050084 private:
bnc0ac0f722020-02-07 07:23:09 -080085 void ReportError(HpackDecodingError error);
QUICHE teamfd50a402018-12-07 22:54:05 -050086
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_