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