OLD | NEW |
| (Empty) |
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 NET_HTTP2_HPACK_DECODER_HPACK_DECODER_STRING_BUFFER_H_ | |
6 #define NET_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 "base/macros.h" | |
18 #include "base/strings/string_piece.h" | |
19 #include "net/base/net_export.h" | |
20 #include "net/http2/hpack/huffman/http2_hpack_huffman_decoder.h" | |
21 | |
22 namespace net { | |
23 | |
24 class NET_EXPORT_PRIVATE HpackDecoderStringBuffer { | |
25 public: | |
26 enum class State : uint8_t { RESET, COLLECTING, COMPLETE }; | |
27 enum class Backing : uint8_t { RESET, UNBUFFERED, BUFFERED, STATIC }; | |
28 | |
29 HpackDecoderStringBuffer(); | |
30 ~HpackDecoderStringBuffer(); | |
31 | |
32 void Reset(); | |
33 void Set(base::StringPiece value, bool is_static); | |
34 | |
35 // Note that for Huffman encoded strings the length of the string after | |
36 // decoding may be larger (expected), the same or even smaller; the latter | |
37 // are unlikely, but possible if the encoder makes odd choices. | |
38 void OnStart(bool huffman_encoded, size_t len); | |
39 bool OnData(const char* data, size_t len); | |
40 bool OnEnd(); | |
41 void BufferStringIfUnbuffered(); | |
42 size_t BufferedLength() const; | |
43 | |
44 base::StringPiece str() const; | |
45 | |
46 State state_for_testing() const { return state_; } | |
47 Backing backing_for_testing() const { return backing_; } | |
48 void OutputDebugStringTo(std::ostream& out) const; | |
49 | |
50 private: | |
51 // Storage for the string being buffered, if buffering is necessary | |
52 // (e.g. if Huffman encoded, buffer_ is storage for the decoded string). | |
53 std::string buffer_; | |
54 | |
55 // The StringPiece to be returned by HpackDecoderStringBuffer::str(). If a | |
56 // string has been collected, but not buffered, value_ points to that string. | |
57 base::StringPiece value_; | |
58 | |
59 // The decoder to use if the string is Huffman encoded. | |
60 HpackHuffmanDecoder decoder_; | |
61 | |
62 // Count of bytes not yet passed to OnData. | |
63 size_t remaining_len_ = 0; | |
64 | |
65 // Is the HPACK string Huffman encoded? | |
66 bool is_huffman_encoded_ = false; | |
67 | |
68 // State of the string decoding process. | |
69 State state_; | |
70 | |
71 // Where is the string stored? | |
72 Backing backing_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(HpackDecoderStringBuffer); | |
75 }; | |
76 | |
77 NET_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& out, | |
78 const HpackDecoderStringBuffer& v); | |
79 | |
80 } // namespace net | |
81 | |
82 #endif // NET_HTTP2_HPACK_DECODER_HPACK_DECODER_STRING_BUFFER_H_ | |
OLD | NEW |