• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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_SPDY_CORE_HPACK_HPACK_DECODER_ADAPTER_H_
6 #define QUICHE_SPDY_CORE_HPACK_HPACK_DECODER_ADAPTER_H_
7 
8 // HpackDecoderAdapter uses http2::HpackDecoder to decode HPACK blocks into
9 // HTTP/2 header lists as outlined in http://tools.ietf.org/html/rfc7541.
10 
11 #include <stddef.h>
12 
13 #include <string>
14 
15 #include "absl/strings/string_view.h"
16 #include "quiche/http2/hpack/decoder/hpack_decoder.h"
17 #include "quiche/http2/hpack/decoder/hpack_decoder_listener.h"
18 #include "quiche/http2/hpack/decoder/hpack_decoding_error.h"
19 #include "quiche/common/platform/api/quiche_export.h"
20 #include "quiche/spdy/core/http2_header_block.h"
21 #include "quiche/spdy/core/spdy_headers_handler_interface.h"
22 
23 namespace spdy {
24 namespace test {
25 class HpackDecoderAdapterPeer;
26 }  // namespace test
27 
28 class QUICHE_EXPORT HpackDecoderAdapter {
29  public:
30   friend test::HpackDecoderAdapterPeer;
31   HpackDecoderAdapter();
32   HpackDecoderAdapter(const HpackDecoderAdapter&) = delete;
33   HpackDecoderAdapter& operator=(const HpackDecoderAdapter&) = delete;
34   ~HpackDecoderAdapter();
35 
36   // Called upon acknowledgement of SETTINGS_HEADER_TABLE_SIZE.
37   void ApplyHeaderTableSizeSetting(size_t size_setting);
38 
39   // Returns the most recently applied value of SETTINGS_HEADER_TABLE_SIZE.
40   size_t GetCurrentHeaderTableSizeSetting() const;
41 
42   // If a SpdyHeadersHandlerInterface is provided, the decoder will emit
43   // headers to it rather than accumulating them in a Http2HeaderBlock.
44   // Does not take ownership of the handler, but does use the pointer until
45   // the current HPACK block is completely decoded.
46   void HandleControlFrameHeadersStart(SpdyHeadersHandlerInterface* handler);
47 
48   // Called as HPACK block fragments arrive. Returns false if an error occurred
49   // while decoding the block. Does not take ownership of headers_data.
50   bool HandleControlFrameHeadersData(const char* headers_data,
51                                      size_t headers_data_length);
52 
53   // Called after a HPACK block has been completely delivered via
54   // HandleControlFrameHeadersData(). Returns false if an error occurred.
55   // |compressed_len| if non-null will be set to the size of the encoded
56   // buffered block that was accumulated in HandleControlFrameHeadersData(),
57   // to support subsequent calculation of compression percentage.
58   // Discards the handler supplied at the start of decoding the block.
59   bool HandleControlFrameHeadersComplete();
60 
61   // Accessor for the most recently decoded headers block. Valid until the next
62   // call to HandleControlFrameHeadersData().
63   // TODO(birenroy): Remove this method when all users of HpackDecoder specify
64   // a SpdyHeadersHandlerInterface.
65   const Http2HeaderBlock& decoded_block() const;
66 
67   // Returns the current dynamic table size, including the 32 bytes per entry
68   // overhead mentioned in RFC 7541 section 4.1.
GetDynamicTableSize()69   size_t GetDynamicTableSize() const {
70     return hpack_decoder_.GetDynamicTableSize();
71   }
72 
73   // Set how much encoded data this decoder is willing to buffer.
74   // TODO(jamessynge): Resolve definition of this value, as it is currently
75   // too tied to a single implementation. We probably want to limit one or more
76   // of these: individual name or value strings, header entries, the entire
77   // header list, or the HPACK block; we probably shouldn't care about the size
78   // of individual transport buffers.
79   void set_max_decode_buffer_size_bytes(size_t max_decode_buffer_size_bytes);
80 
81   // Specifies the maximum size of an on-the-wire header block that will be
82   // accepted.
83   void set_max_header_block_bytes(size_t max_header_block_bytes);
84 
85   // Error code if an error has occurred, Error::kOk otherwise.
error()86   http2::HpackDecodingError error() const { return error_; }
87 
detailed_error()88   std::string detailed_error() const { return detailed_error_; }
89 
90  private:
91   class QUICHE_EXPORT ListenerAdapter : public http2::HpackDecoderListener {
92    public:
93     ListenerAdapter();
94     ~ListenerAdapter() override;
95 
96     // If a SpdyHeadersHandlerInterface is provided, the decoder will emit
97     // headers to it rather than accumulating them in a Http2HeaderBlock.
98     // Does not take ownership of the handler, but does use the pointer until
99     // the current HPACK block is completely decoded.
100     void set_handler(SpdyHeadersHandlerInterface* handler);
decoded_block()101     const Http2HeaderBlock& decoded_block() const { return decoded_block_; }
102 
103     // Override the HpackDecoderListener methods:
104     void OnHeaderListStart() override;
105     void OnHeader(const std::string& name, const std::string& value) override;
106     void OnHeaderListEnd() override;
107     void OnHeaderErrorDetected(absl::string_view error_message) override;
108 
AddToTotalHpackBytes(size_t delta)109     void AddToTotalHpackBytes(size_t delta) { total_hpack_bytes_ += delta; }
total_hpack_bytes()110     size_t total_hpack_bytes() const { return total_hpack_bytes_; }
111 
112    private:
113     // If the caller doesn't provide a handler, the header list is stored in
114     // this Http2HeaderBlock.
115     Http2HeaderBlock decoded_block_;
116 
117     // If non-NULL, handles decoded headers. Not owned.
118     SpdyHeadersHandlerInterface* handler_;
119 
120     // Total bytes that have been received as input (i.e. HPACK encoded)
121     // in the current HPACK block.
122     size_t total_hpack_bytes_;
123 
124     // Total bytes of the name and value strings in the current HPACK block.
125     size_t total_uncompressed_bytes_;
126   };
127 
128   // Converts calls to HpackDecoderListener into calls to
129   // SpdyHeadersHandlerInterface.
130   ListenerAdapter listener_adapter_;
131 
132   // The actual decoder.
133   http2::HpackDecoder hpack_decoder_;
134 
135   // How much encoded data this decoder is willing to buffer.
136   size_t max_decode_buffer_size_bytes_;
137 
138   // How much encoded data this decoder is willing to process.
139   size_t max_header_block_bytes_;
140 
141   // Flag to keep track of having seen the header block start. Needed at the
142   // moment because HandleControlFrameHeadersStart won't be called if a handler
143   // is not being provided by the caller.
144   bool header_block_started_;
145 
146   // Error code if an error has occurred, Error::kOk otherwise.
147   http2::HpackDecodingError error_;
148   std::string detailed_error_;
149 };
150 
151 }  // namespace spdy
152 
153 #endif  // QUICHE_SPDY_CORE_HPACK_HPACK_DECODER_ADAPTER_H_
154