xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/spdy/core/hpack/hpack_output_stream.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 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_HPACK_HPACK_OUTPUT_STREAM_H_
6 #define QUICHE_SPDY_CORE_HPACK_HPACK_OUTPUT_STREAM_H_
7 
8 #include <cstddef>
9 #include <cstdint>
10 #include <string>
11 
12 #include "absl/strings/string_view.h"
13 #include "quiche/common/platform/api/quiche_export.h"
14 #include "quiche/spdy/core/hpack/hpack_constants.h"
15 
16 // All section references below are to
17 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-08
18 
19 namespace spdy {
20 
21 // An HpackOutputStream handles all the low-level details of encoding
22 // header fields.
23 class QUICHE_EXPORT HpackOutputStream {
24  public:
25   HpackOutputStream();
26   HpackOutputStream(const HpackOutputStream&) = delete;
27   HpackOutputStream& operator=(const HpackOutputStream&) = delete;
28   ~HpackOutputStream();
29 
30   // Appends the lower |bit_size| bits of |bits| to the internal buffer.
31   //
32   // |bit_size| must be > 0 and <= 8. |bits| must not have any bits
33   // set other than the lower |bit_size| bits.
34   void AppendBits(uint8_t bits, size_t bit_size);
35 
36   // Simply forwards to AppendBits(prefix.bits, prefix.bit-size).
37   void AppendPrefix(HpackPrefix prefix);
38 
39   // Directly appends |buffer|.
40   void AppendBytes(absl::string_view buffer);
41 
42   // Appends the given integer using the representation described in
43   // 6.1. If the internal buffer ends on a byte boundary, the prefix
44   // length N is taken to be 8; otherwise, it is taken to be the
45   // number of bits to the next byte boundary.
46   //
47   // It is guaranteed that the internal buffer will end on a byte
48   // boundary after this function is called.
49   void AppendUint32(uint32_t I);
50 
51   // Return pointer to internal buffer.  |bit_offset_| needs to be zero.
52   std::string* MutableString();
53 
54   // Returns the internal buffer as a string, then resets state.
55   std::string TakeString();
56 
57   // Returns up to |max_size| bytes of the internal buffer. Resets
58   // internal state with the overflow.
59   std::string BoundedTakeString(size_t max_size);
60 
61   // Size in bytes of stream's internal buffer.
size()62   size_t size() const { return buffer_.size(); }
63 
64  private:
65   // The internal bit buffer.
66   std::string buffer_;
67 
68   // If 0, the buffer ends on a byte boundary. If non-zero, the buffer
69   // ends on the nth most significant bit. Guaranteed to be < 8.
70   size_t bit_offset_;
71 };
72 
73 }  // namespace spdy
74 
75 #endif  // QUICHE_SPDY_CORE_HPACK_HPACK_OUTPUT_STREAM_H_
76