• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DECODER_STRING_BUFFER_H_
6 #define QUICHE_HTTP2_HPACK_DECODER_HPACK_DECODER_STRING_BUFFER_H_
7 
8 // HpackDecoderStringBuffer helps an HPACK decoder to avoid copies of a string
9 // literal (name or value) except when necessary (e.g. when split across two
10 // or more HPACK block fragments).
11 
12 #include <stddef.h>
13 
14 #include <ostream>
15 #include <string>
16 
17 #include "absl/strings/string_view.h"
18 #include "quiche/http2/hpack/huffman/hpack_huffman_decoder.h"
19 #include "quiche/common/platform/api/quiche_export.h"
20 
21 namespace http2 {
22 
23 class QUICHE_EXPORT HpackDecoderStringBuffer {
24  public:
25   enum class State : uint8_t { RESET, COLLECTING, COMPLETE };
26   enum class Backing : uint8_t { RESET, UNBUFFERED, BUFFERED, STATIC };
27 
28   HpackDecoderStringBuffer();
29   ~HpackDecoderStringBuffer();
30 
31   HpackDecoderStringBuffer(const HpackDecoderStringBuffer&) = delete;
32   HpackDecoderStringBuffer& operator=(const HpackDecoderStringBuffer&) = delete;
33 
34   void Reset();
35   void Set(absl::string_view value, bool is_static);
36 
37   // Note that for Huffman encoded strings the length of the string after
38   // decoding may be larger (expected), the same or even smaller; the latter
39   // are unlikely, but possible if the encoder makes odd choices.
40   void OnStart(bool huffman_encoded, size_t len);
41   bool OnData(const char* data, size_t len);
42   bool OnEnd();
43   void BufferStringIfUnbuffered();
44   bool IsBuffered() const;
45   size_t BufferedLength() const;
46 
47   // Accessors for the completely collected string (i.e. Set or OnEnd has just
48   // been called, and no reset of the state has occurred).
49 
50   // Returns a string_view pointing to the backing store for the string,
51   // either the internal buffer or the original transport buffer (e.g. for a
52   // literal value that wasn't Huffman encoded, and that wasn't split across
53   // transport buffers).
54   absl::string_view str() const;
55 
56   // Same as str() if state_ is COMPLETE. Otherwise, returns empty string piece.
57   absl::string_view GetStringIfComplete() const;
58 
59   // Returns the completely collected string by value, using std::move in an
60   // effort to avoid unnecessary copies. ReleaseString() must not be called
61   // unless the string has been buffered (to avoid forcing a potentially
62   // unnecessary copy). ReleaseString() also resets the instance so that it can
63   // be used to collect another string.
64   std::string ReleaseString();
65 
state_for_testing()66   State state_for_testing() const { return state_; }
backing_for_testing()67   Backing backing_for_testing() const { return backing_; }
68   void OutputDebugStringTo(std::ostream& out) const;
69 
70  private:
71   // Storage for the string being buffered, if buffering is necessary
72   // (e.g. if Huffman encoded, buffer_ is storage for the decoded string).
73   std::string buffer_;
74 
75   // The string_view to be returned by HpackDecoderStringBuffer::str(). If
76   // a string has been collected, but not buffered, value_ points to that
77   // string.
78   absl::string_view value_;
79 
80   // The decoder to use if the string is Huffman encoded.
81   HpackHuffmanDecoder decoder_;
82 
83   // Count of bytes not yet passed to OnData.
84   size_t remaining_len_;
85 
86   // Is the HPACK string Huffman encoded?
87   bool is_huffman_encoded_;
88 
89   // State of the string decoding process.
90   State state_;
91 
92   // Where is the string stored?
93   Backing backing_;
94 };
95 
96 QUICHE_EXPORT std::ostream& operator<<(std::ostream& out,
97                                        const HpackDecoderStringBuffer& v);
98 
99 }  // namespace http2
100 
101 #endif  // QUICHE_HTTP2_HPACK_DECODER_HPACK_DECODER_STRING_BUFFER_H_
102