1 // Copyright (c) 2012 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_SPDY_CORE_SPDY_FRAME_BUILDER_H_ 6 #define QUICHE_SPDY_CORE_SPDY_FRAME_BUILDER_H_ 7 8 #include <cstddef> 9 #include <cstdint> 10 #include <memory> 11 12 #include "net/third_party/quiche/src/common/platform/api/quiche_export.h" 13 #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" 14 #include "net/third_party/quiche/src/spdy/core/spdy_protocol.h" 15 #include "net/third_party/quiche/src/spdy/core/zero_copy_output_buffer.h" 16 #include "net/third_party/quiche/src/spdy/platform/api/spdy_bug_tracker.h" 17 #include "net/third_party/quiche/src/spdy/platform/api/spdy_endianness_util.h" 18 19 namespace spdy { 20 21 namespace test { 22 class SpdyFrameBuilderPeer; 23 } // namespace test 24 25 // This class provides facilities for basic binary value packing 26 // into Spdy frames. 27 // 28 // The SpdyFrameBuilder supports appending primitive values (int, string, etc) 29 // to a frame instance. The SpdyFrameBuilder grows its internal memory buffer 30 // dynamically to hold the sequence of primitive values. The internal memory 31 // buffer is exposed as the "data" of the SpdyFrameBuilder. 32 class QUICHE_EXPORT_PRIVATE SpdyFrameBuilder { 33 public: 34 // Initializes a SpdyFrameBuilder with a buffer of given size 35 explicit SpdyFrameBuilder(size_t size); 36 // Doesn't take ownership of output. 37 SpdyFrameBuilder(size_t size, ZeroCopyOutputBuffer* output); 38 39 ~SpdyFrameBuilder(); 40 41 // Returns the total size of the SpdyFrameBuilder's data, which may include 42 // multiple frames. length()43 size_t length() const { return offset_ + length_; } 44 45 // Seeks forward by the given number of bytes. Useful in conjunction with 46 // GetWriteableBuffer() above. 47 bool Seek(size_t length); 48 49 // Populates this frame with a HTTP2 frame prefix using length information 50 // from |capacity_|. The given type must be a control frame type. 51 bool BeginNewFrame(SpdyFrameType type, uint8_t flags, SpdyStreamId stream_id); 52 53 // Populates this frame with a HTTP2 frame prefix with type and length 54 // information. |type| must be a defined frame type. 55 bool BeginNewFrame(SpdyFrameType type, 56 uint8_t flags, 57 SpdyStreamId stream_id, 58 size_t length); 59 60 // Populates this frame with a HTTP2 frame prefix with type and length 61 // information. |raw_frame_type| may be a defined or undefined frame type. 62 bool BeginNewUncheckedFrame(uint8_t raw_frame_type, 63 uint8_t flags, 64 SpdyStreamId stream_id, 65 size_t length); 66 67 // Takes the buffer from the SpdyFrameBuilder. take()68 SpdySerializedFrame take() { 69 SPDY_BUG_IF(output_ != nullptr) << "ZeroCopyOutputBuffer is used to build " 70 << "frames. take() shouldn't be called"; 71 SPDY_BUG_IF(kMaxFrameSizeLimit < length_) 72 << "Frame length " << length_ 73 << " is longer than the maximum possible allowed length."; 74 SpdySerializedFrame rv(buffer_.release(), length(), true); 75 capacity_ = 0; 76 length_ = 0; 77 offset_ = 0; 78 return rv; 79 } 80 81 // Methods for adding to the payload. These values are appended to the end 82 // of the SpdyFrameBuilder payload. Note - binary integers are converted from 83 // host to network form. WriteUInt8(uint8_t value)84 bool WriteUInt8(uint8_t value) { return WriteBytes(&value, sizeof(value)); } WriteUInt16(uint16_t value)85 bool WriteUInt16(uint16_t value) { 86 value = SpdyHostToNet16(value); 87 return WriteBytes(&value, sizeof(value)); 88 } WriteUInt24(uint32_t value)89 bool WriteUInt24(uint32_t value) { 90 value = SpdyHostToNet32(value); 91 return WriteBytes(reinterpret_cast<char*>(&value) + 1, sizeof(value) - 1); 92 } WriteUInt32(uint32_t value)93 bool WriteUInt32(uint32_t value) { 94 value = SpdyHostToNet32(value); 95 return WriteBytes(&value, sizeof(value)); 96 } WriteUInt64(uint64_t value)97 bool WriteUInt64(uint64_t value) { 98 uint32_t upper = SpdyHostToNet32(static_cast<uint32_t>(value >> 32)); 99 uint32_t lower = SpdyHostToNet32(static_cast<uint32_t>(value)); 100 return (WriteBytes(&upper, sizeof(upper)) && 101 WriteBytes(&lower, sizeof(lower))); 102 } 103 bool WriteStringPiece32(const quiche::QuicheStringPiece value); 104 bool WriteBytes(const void* data, uint32_t data_len); 105 106 private: 107 friend class test::SpdyFrameBuilderPeer; 108 109 // Populates this frame with a HTTP2 frame prefix with type and length 110 // information. 111 bool BeginNewFrameInternal(uint8_t raw_frame_type, 112 uint8_t flags, 113 SpdyStreamId stream_id, 114 size_t length); 115 116 // Returns a writeable buffer of given size in bytes, to be appended to the 117 // currently written frame. Does bounds checking on length but does not 118 // increment the underlying iterator. To do so, consumers should subsequently 119 // call Seek(). 120 // In general, consumers should use Write*() calls instead of this. 121 // Returns NULL on failure. 122 char* GetWritableBuffer(size_t length); 123 char* GetWritableOutput(size_t desired_length, size_t* actual_length); 124 125 // Checks to make sure that there is an appropriate amount of space for a 126 // write of given size, in bytes. 127 bool CanWrite(size_t length) const; 128 129 // A buffer to be created whenever a new frame needs to be written. Used only 130 // if |output_| is nullptr. 131 std::unique_ptr<char[]> buffer_; 132 // A pre-allocated buffer. If not-null, serialized frame data is written to 133 // this buffer. 134 ZeroCopyOutputBuffer* output_ = nullptr; // Does not own. 135 136 size_t capacity_; // Allocation size of payload, set by constructor. 137 size_t length_; // Length of the latest frame in the buffer. 138 size_t offset_; // Position at which the latest frame begins. 139 }; 140 141 } // namespace spdy 142 143 #endif // QUICHE_SPDY_CORE_SPDY_FRAME_BUILDER_H_ 144