xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/spdy/core/spdy_frame_builder.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 "absl/strings/string_view.h"
13 #include "quiche/common/platform/api/quiche_bug_tracker.h"
14 #include "quiche/common/platform/api/quiche_export.h"
15 #include "quiche/common/quiche_endian.h"
16 #include "quiche/spdy/core/spdy_protocol.h"
17 #include "quiche/spdy/core/zero_copy_output_buffer.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 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, uint8_t flags, SpdyStreamId stream_id,
56                      size_t length);
57 
58   // Populates this frame with a HTTP2 frame prefix with type and length
59   // information.  |raw_frame_type| may be a defined or undefined frame type.
60   bool BeginNewUncheckedFrame(uint8_t raw_frame_type, uint8_t flags,
61                               SpdyStreamId stream_id, size_t length);
62 
63   // Takes the buffer from the SpdyFrameBuilder.
take()64   SpdySerializedFrame take() {
65     QUICHE_BUG_IF(spdy_bug_39_1, output_ != nullptr)
66         << "ZeroCopyOutputBuffer is used to build "
67         << "frames. take() shouldn't be called";
68     QUICHE_BUG_IF(spdy_bug_39_2, kMaxFrameSizeLimit < length_)
69         << "Frame length " << length_
70         << " is longer than the maximum possible allowed length.";
71     SpdySerializedFrame rv(std::move(buffer_), length());
72     capacity_ = 0;
73     length_ = 0;
74     offset_ = 0;
75     return rv;
76   }
77 
78   // Methods for adding to the payload.  These values are appended to the end
79   // of the SpdyFrameBuilder payload. Note - binary integers are converted from
80   // host to network form.
WriteUInt8(uint8_t value)81   bool WriteUInt8(uint8_t value) { return WriteBytes(&value, sizeof(value)); }
WriteUInt16(uint16_t value)82   bool WriteUInt16(uint16_t value) {
83     value = quiche::QuicheEndian::HostToNet16(value);
84     return WriteBytes(&value, sizeof(value));
85   }
WriteUInt24(uint32_t value)86   bool WriteUInt24(uint32_t value) {
87     value = quiche::QuicheEndian::HostToNet32(value);
88     return WriteBytes(reinterpret_cast<char*>(&value) + 1, sizeof(value) - 1);
89   }
WriteUInt32(uint32_t value)90   bool WriteUInt32(uint32_t value) {
91     value = quiche::QuicheEndian::HostToNet32(value);
92     return WriteBytes(&value, sizeof(value));
93   }
WriteUInt64(uint64_t value)94   bool WriteUInt64(uint64_t value) {
95     uint32_t upper =
96         quiche::QuicheEndian::HostToNet32(static_cast<uint32_t>(value >> 32));
97     uint32_t lower =
98         quiche::QuicheEndian::HostToNet32(static_cast<uint32_t>(value));
99     return (WriteBytes(&upper, sizeof(upper)) &&
100             WriteBytes(&lower, sizeof(lower)));
101   }
102   bool WriteStringPiece32(const absl::string_view value);
103   bool WriteBytes(const void* data, uint32_t data_len);
104 
105  private:
106   friend class test::SpdyFrameBuilderPeer;
107 
108   // Populates this frame with a HTTP2 frame prefix with type and length
109   // information.
110   bool BeginNewFrameInternal(uint8_t raw_frame_type, uint8_t flags,
111                              SpdyStreamId stream_id, size_t length);
112 
113   // Returns a writeable buffer of given size in bytes, to be appended to the
114   // currently written frame. Does bounds checking on length but does not
115   // increment the underlying iterator. To do so, consumers should subsequently
116   // call Seek().
117   // In general, consumers should use Write*() calls instead of this.
118   // Returns NULL on failure.
119   char* GetWritableBuffer(size_t length);
120   char* GetWritableOutput(size_t desired_length, size_t* actual_length);
121 
122   // Checks to make sure that there is an appropriate amount of space for a
123   // write of given size, in bytes.
124   bool CanWrite(size_t length) const;
125 
126   // A buffer to be created whenever a new frame needs to be written. Used only
127   // if |output_| is nullptr.
128   std::unique_ptr<char[]> buffer_;
129   // A pre-allocated buffer. If not-null, serialized frame data is written to
130   // this buffer.
131   ZeroCopyOutputBuffer* output_ = nullptr;  // Does not own.
132 
133   size_t capacity_;  // Allocation size of payload, set by constructor.
134   size_t length_;    // Length of the latest frame in the buffer.
135   size_t offset_;    // Position at which the latest frame begins.
136 };
137 
138 }  // namespace spdy
139 
140 #endif  // QUICHE_SPDY_CORE_SPDY_FRAME_BUILDER_H_
141