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_TOOLS_HPACK_BLOCK_BUILDER_H_ |
| 6 #define NET_HTTP2_HPACK_TOOLS_HPACK_BLOCK_BUILDER_H_ |
| 7 |
| 8 // HpackBlockBuilder builds wire-format HPACK blocks (or fragments thereof) |
| 9 // from components. |
| 10 |
| 11 // Supports very large varints to enable tests to create HPACK blocks with |
| 12 // values that the decoder should reject. For now, this is only intended for |
| 13 // use in tests, and thus has EXPECT* in the code. If desired to use it in an |
| 14 // encoder, it will need optimization work, especially w.r.t memory mgmt, and |
| 15 // the EXPECT* will need to be removed or replaced with DCHECKs. And of course |
| 16 // the support for very large varints will not be needed in production code. |
| 17 |
| 18 #include <stddef.h> |
| 19 #include <string> |
| 20 |
| 21 #include "base/strings/string_piece.h" |
| 22 #include "net/http2/hpack/http2_hpack_constants.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 |
| 25 namespace net { |
| 26 namespace test { |
| 27 |
| 28 class HpackBlockBuilder { |
| 29 public: |
| 30 explicit HpackBlockBuilder(base::StringPiece initial_contents) { |
| 31 initial_contents.AppendToString(&buffer_); |
| 32 } |
| 33 HpackBlockBuilder() {} |
| 34 ~HpackBlockBuilder() {} |
| 35 |
| 36 size_t size() const { return buffer_.size(); } |
| 37 const std::string& buffer() const { return buffer_; } |
| 38 |
| 39 //---------------------------------------------------------------------------- |
| 40 // Methods for appending a valid HPACK entry. |
| 41 |
| 42 void AppendIndexedHeader(uint64_t index) { |
| 43 AppendEntryTypeAndVarint(HpackEntryType::kIndexedHeader, index); |
| 44 } |
| 45 |
| 46 void AppendDynamicTableSizeUpdate(uint64_t size) { |
| 47 AppendEntryTypeAndVarint(HpackEntryType::kDynamicTableSizeUpdate, size); |
| 48 } |
| 49 |
| 50 void AppendNameIndexAndLiteralValue(HpackEntryType entry_type, |
| 51 uint64_t name_index, |
| 52 bool value_is_huffman_encoded, |
| 53 base::StringPiece value) { |
| 54 // name_index==0 would indicate that the entry includes a literal name. |
| 55 // Call AppendLiteralNameAndValue in that case. |
| 56 EXPECT_NE(0u, name_index); |
| 57 AppendEntryTypeAndVarint(entry_type, name_index); |
| 58 AppendString(value_is_huffman_encoded, value); |
| 59 } |
| 60 |
| 61 void AppendLiteralNameAndValue(HpackEntryType entry_type, |
| 62 bool name_is_huffman_encoded, |
| 63 base::StringPiece name, |
| 64 bool value_is_huffman_encoded, |
| 65 base::StringPiece value) { |
| 66 AppendEntryTypeAndVarint(entry_type, 0); |
| 67 AppendString(name_is_huffman_encoded, name); |
| 68 AppendString(value_is_huffman_encoded, value); |
| 69 } |
| 70 |
| 71 //---------------------------------------------------------------------------- |
| 72 // Primitive methods that are not guaranteed to write a valid HPACK entry. |
| 73 |
| 74 // Appends a varint, with the specified high_bits above the prefix of the |
| 75 // varint. |
| 76 void AppendHighBitsAndVarint(uint8_t high_bits, |
| 77 uint8_t prefix_length, |
| 78 uint64_t varint); |
| 79 |
| 80 // Append the start of an HPACK entry for the specified type, with the |
| 81 // specified varint. |
| 82 void AppendEntryTypeAndVarint(HpackEntryType entry_type, uint64_t varint); |
| 83 |
| 84 // Append a header string (i.e. a header name or value) in HPACK format. |
| 85 // Does NOT perform Huffman encoding. |
| 86 void AppendString(bool is_huffman_encoded, base::StringPiece str); |
| 87 |
| 88 private: |
| 89 std::string buffer_; |
| 90 }; |
| 91 |
| 92 } // namespace test |
| 93 } // namespace net |
| 94 |
| 95 #endif // NET_HTTP2_HPACK_TOOLS_HPACK_BLOCK_BUILDER_H_ |
OLD | NEW |