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_TEST_TOOLS_HTTP2_FRAME_BUILDER_H_ 6 #define QUICHE_HTTP2_TEST_TOOLS_HTTP2_FRAME_BUILDER_H_ 7 8 // Http2FrameBuilder builds wire-format HTTP/2 frames (or fragments thereof) 9 // from components. 10 // 11 // For now, this is only intended for use in tests, and thus has EXPECT* in the 12 // code. If desired to use it in an encoder, it will need optimization work, 13 // especially w.r.t memory mgmt, and the EXPECT* will need to be removed or 14 // replaced with QUICHE_DCHECKs. 15 16 #include <stddef.h> // for size_t 17 18 #include <cstdint> 19 #include <string> 20 21 #include "absl/strings/string_view.h" 22 #include "quiche/http2/http2_constants.h" 23 #include "quiche/http2/http2_structures.h" 24 #include "quiche/common/platform/api/quiche_export.h" 25 26 namespace http2 { 27 namespace test { 28 29 class QUICHE_NO_EXPORT Http2FrameBuilder { 30 public: 31 Http2FrameBuilder(Http2FrameType type, uint8_t flags, uint32_t stream_id); 32 explicit Http2FrameBuilder(const Http2FrameHeader& v); Http2FrameBuilder()33 Http2FrameBuilder() {} ~Http2FrameBuilder()34 ~Http2FrameBuilder() {} 35 size()36 size_t size() const { return buffer_.size(); } buffer()37 const std::string& buffer() const { return buffer_; } 38 39 //---------------------------------------------------------------------------- 40 // Methods for appending to the end of the buffer. 41 42 // Append a sequence of bytes from various sources. 43 void Append(absl::string_view s); 44 void AppendBytes(const void* data, uint32_t num_bytes); 45 46 // Append an array of type T[N] to the string. Intended for tests with arrays 47 // initialized from literals, such as: 48 // const char kData[] = {0x12, 0x23, ...}; 49 // builder.AppendBytes(kData); 50 template <typename T, size_t N> AppendBytes(T (& buf)[N])51 void AppendBytes(T (&buf)[N]) { 52 AppendBytes(buf, N * sizeof(buf[0])); 53 } 54 55 // Support for appending padding. Does not read or write the Pad Length field. 56 void AppendZeroes(size_t num_zero_bytes); 57 58 // Append various sizes of unsigned integers. 59 void AppendUInt8(uint8_t value); 60 void AppendUInt16(uint16_t value); 61 void AppendUInt24(uint32_t value); 62 void AppendUInt31(uint32_t value); 63 void AppendUInt32(uint32_t value); 64 65 // Append various enums. 66 void Append(Http2ErrorCode error_code); 67 void Append(Http2FrameType type); 68 void Append(Http2SettingsParameter parameter); 69 70 // Append various structures. 71 void Append(const Http2FrameHeader& v); 72 void Append(const Http2PriorityFields& v); 73 void Append(const Http2RstStreamFields& v); 74 void Append(const Http2SettingFields& v); 75 void Append(const Http2PushPromiseFields& v); 76 void Append(const Http2PingFields& v); 77 void Append(const Http2GoAwayFields& v); 78 void Append(const Http2WindowUpdateFields& v); 79 void Append(const Http2AltSvcFields& v); 80 void Append(const Http2PriorityUpdateFields& v); 81 82 // Methods for changing existing buffer contents (mostly focused on updating 83 // the payload length). 84 85 void WriteAt(absl::string_view s, size_t offset); 86 void WriteBytesAt(const void* data, uint32_t num_bytes, size_t offset); 87 void WriteUInt24At(uint32_t value, size_t offset); 88 89 // Set the payload length to the specified size. 90 void SetPayloadLength(uint32_t payload_length); 91 92 // Sets the payload length to the size of the buffer minus the size of 93 // the frame header. 94 size_t SetPayloadLength(); 95 96 private: 97 std::string buffer_; 98 }; 99 100 } // namespace test 101 } // namespace http2 102 103 #endif // QUICHE_HTTP2_TEST_TOOLS_HTTP2_FRAME_BUILDER_H_ 104