proxygen
proxygen::HPACKEncodeBuffer Class Reference

#include <HPACKEncodeBuffer.h>

Public Member Functions

 HPACKEncodeBuffer (uint32_t growthSize, bool huffmanEnabled)
 
 HPACKEncodeBuffer (uint32_t growthSize)
 
 ~HPACKEncodeBuffer ()
 
std::unique_ptr< folly::IOBufrelease ()
 
void clear ()
 
void addHeadroom (uint32_t bytes)
 
uint32_t encodeInteger (uint64_t value, uint8_t instruction, uint8_t nbit)
 
uint32_t encodeInteger (uint64_t value, const HPACK::Instruction &instruction)
 
uint32_t encodeInteger (uint64_t value)
 
uint32_t encodeLiteral (folly::StringPiece literal)
 
uint32_t encodeLiteral (uint8_t instruction, uint8_t nbit, folly::StringPiece literal)
 
uint32_t encodeHuffman (folly::StringPiece literal)
 
uint32_t encodeHuffman (uint8_t instruction, uint8_t nbit, folly::StringPiece literal)
 
std::string toBin ()
 

Private Member Functions

void append (uint8_t byte)
 

Private Attributes

folly::IOBufQueue bufQueue_
 
folly::io::QueueAppender buf_
 
uint32_t growthSize_
 
bool huffmanEnabled_
 

Detailed Description

Definition at line 20 of file HPACKEncodeBuffer.h.

Constructor & Destructor Documentation

proxygen::HPACKEncodeBuffer::HPACKEncodeBuffer ( uint32_t  growthSize,
bool  huffmanEnabled 
)

Definition at line 24 of file HPACKEncodeBuffer.cpp.

26  :
27  buf_(&bufQueue_, growthSize),
28  growthSize_(growthSize),
29  huffmanEnabled_(huffmanEnabled) {
30 }
folly::io::QueueAppender buf_
proxygen::HPACKEncodeBuffer::HPACKEncodeBuffer ( uint32_t  growthSize)
explicit

Definition at line 32 of file HPACKEncodeBuffer.cpp.

32  :
33  buf_(&bufQueue_, growthSize),
34  growthSize_(growthSize),
35  huffmanEnabled_(false) {
36 }
folly::io::QueueAppender buf_
proxygen::HPACKEncodeBuffer::~HPACKEncodeBuffer ( )
inline

Definition at line 29 of file HPACKEncodeBuffer.h.

29 {}

Member Function Documentation

void proxygen::HPACKEncodeBuffer::addHeadroom ( uint32_t  bytes)

Add headroom at the beginning of the IOBufQueue Meant to be called before encoding anything.

Definition at line 38 of file HPACKEncodeBuffer.cpp.

References folly::IOBufQueue::append(), bufQueue_, folly::IOBufQueue::front(), growthSize_, max, and folly::gen::move.

Referenced by clear(), proxygen::HPACKEncoder::encode(), and proxygen::QPACKEncoder::encode().

38  {
39  // we expect that this function is called before any encoding happens
40  CHECK(bufQueue_.front() == nullptr);
41  // create a custom IOBuf and add it to the queue
42  unique_ptr<IOBuf> buf = IOBuf::create(std::max(headroom, growthSize_));
43  buf->advance(headroom);
45 }
const folly::IOBuf * front() const
Definition: IOBufQueue.h:476
void append(std::unique_ptr< folly::IOBuf > &&buf, bool pack=false)
Definition: IOBufQueue.cpp:143
LogLevel max
Definition: LogLevel.cpp:31
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void proxygen::HPACKEncodeBuffer::append ( uint8_t  byte)
private

append one byte at the end of buffer ensuring we always have enough space

Definition at line 47 of file HPACKEncodeBuffer.cpp.

References buf_, and folly::io::detail::Writable< Derived >::push().

Referenced by clear(), and encodeInteger().

47  {
48  buf_.push(&byte, 1);
49 }
void push(const uint8_t *buf, size_t len)
Definition: Cursor.h:755
folly::io::QueueAppender buf_
void proxygen::HPACKEncodeBuffer::clear ( )
inline
uint32_t proxygen::HPACKEncodeBuffer::encodeHuffman ( folly::StringPiece  literal)

encodes a string using huffman encoding

Definition at line 95 of file HPACKEncodeBuffer.cpp.

Referenced by clear(), and encodeLiteral().

95  {
96  return encodeHuffman(0, 7, literal);
97 }
uint32_t encodeHuffman(folly::StringPiece literal)
uint32_t proxygen::HPACKEncodeBuffer::encodeHuffman ( uint8_t  instruction,
uint8_t  nbit,
folly::StringPiece  literal 
)

encodes a string using huffman encoding QPACK style, where literal length has an nbit prefix.

Definition at line 109 of file HPACKEncodeBuffer.cpp.

References buf_, count, encodeInteger(), proxygen::huffman::huffTree(), folly::size(), uint32_t, and uint8_t.

110  {
111  static const auto& huffmanTree = huffman::huffTree();
112  uint32_t size = huffmanTree.getEncodeSize(literal);
113  // add the length
114  DCHECK_LE(nbit, 7);
115  uint8_t huffmanOn = uint8_t(1 << nbit);
116  DCHECK_EQ(instruction & huffmanOn, 0);
117  uint32_t count = encodeInteger(size, instruction | huffmanOn, nbit);
118  // ensure we have enough bytes before performing the encoding
119  count += huffmanTree.encode(literal, buf_);
120  return count;
121 }
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
uint32_t encodeInteger(uint64_t value, uint8_t instruction, uint8_t nbit)
int * count
folly::io::QueueAppender buf_
const HuffTree & huffTree()
Definition: Huffman.cpp:252
uint32_t proxygen::HPACKEncodeBuffer::encodeInteger ( uint64_t  value,
uint8_t  instruction,
uint8_t  nbit 
)

Encode the integer value using variable-length layout and the given instruction using an nbit prefix. Per the spec, prefix is the portion of value that fits in one byte. The instruction is given as 1-byte value (not need for shifting) used only for the first byte. It starts from MSB.

For example for integer=3, instruction=0x80, nbit=6:

MSB LSB X X 0 0 0 0 1 1 (value) 1 0 X X X X X X (instruction) 1 0 0 0 0 0 1 1 (encoded value)

Returns
how many bytes were used to encode the value

Definition at line 61 of file HPACKEncodeBuffer.cpp.

References append(), count, proxygen::HPACK::NBIT_MASKS, uint32_t, uint8_t, and value.

Referenced by clear(), proxygen::HPACKEncoder::encodeAsIndex(), proxygen::QPACKDecoder::encodeCancelStream(), proxygen::QPACKEncoder::encodeDuplicate(), proxygen::QPACKDecoder::encodeHeaderAck(), proxygen::QPACKEncoder::encodeHeaderQ(), encodeHuffman(), encodeInteger(), proxygen::HPACKEncoder::encodeLiteral(), encodeLiteral(), proxygen::QPACKEncoder::encodeLiteralQHelper(), proxygen::QPACKEncoder::encodeQ(), proxygen::QPACKDecoder::encodeTableStateSync(), proxygen::HPACKEncoderBase::handlePendingContextUpdate(), and TEST_F().

62  {
63  CHECK(nbit > 0 && nbit <= 8);
64  uint32_t count = 0;
65  uint8_t mask = HPACK::NBIT_MASKS[nbit];
66  // The instruction should not extend into mask
67  DCHECK_EQ(instruction & mask, 0);
68 
69  // write the first byte
70  uint8_t byte = instruction;
71  if (value < mask) {
72  // fits in the first byte
73  byte |= value;
74  append(byte);
75  return 1;
76  }
77 
78  byte |= mask;
79  value -= mask;
80  ++count;
81  append(byte);
82  // variable length encoding
83  while (value >= 128) {
84  byte = 128 | (127 & value);
85  append(byte);
86  value = value >> 7;
87  ++count;
88  }
89  // last byte, which should always fit on 1 byte
90  append(value);
91  ++count;
92  return count;
93 }
static const char *const value
Definition: Conv.cpp:50
int * count
const uint8_t NBIT_MASKS[9]
uint32_t proxygen::HPACKEncodeBuffer::encodeInteger ( uint64_t  value,
const HPACK::Instruction instruction 
)

Definition at line 55 of file HPACKEncodeBuffer.cpp.

References proxygen::HPACK::Instruction::code, encodeInteger(), and proxygen::HPACK::Instruction::prefixLength.

57  {
58  return encodeInteger(value, instruction.code, instruction.prefixLength);
59 }
uint32_t encodeInteger(uint64_t value, uint8_t instruction, uint8_t nbit)
static const char *const value
Definition: Conv.cpp:50
uint32_t proxygen::HPACKEncodeBuffer::encodeInteger ( uint64_t  value)

Definition at line 51 of file HPACKEncodeBuffer.cpp.

References encodeInteger().

51  {
52  return encodeInteger(value, 0, 8);
53 }
uint32_t encodeInteger(uint64_t value, uint8_t instruction, uint8_t nbit)
static const char *const value
Definition: Conv.cpp:50
uint32_t proxygen::HPACKEncodeBuffer::encodeLiteral ( folly::StringPiece  literal)

encodes a string, either header name or header value

Returns
bytes used for encoding

Definition at line 123 of file HPACKEncodeBuffer.cpp.

Referenced by clear(), proxygen::HPACKEncoder::encodeLiteral(), proxygen::QPACKEncoder::encodeLiteralQHelper(), and TEST_F().

123  {
124  return encodeLiteral(0, 7, literal);
125 }
uint32_t encodeLiteral(folly::StringPiece literal)
uint32_t proxygen::HPACKEncodeBuffer::encodeLiteral ( uint8_t  instruction,
uint8_t  nbit,
folly::StringPiece  literal 
)

encodes a string, either header name or header value QPACK style, where literal length has an nbit prefix.

Returns
bytes used for encoding

Definition at line 127 of file HPACKEncodeBuffer.cpp.

References buf_, count, folly::Range< Iter >::data(), encodeHuffman(), encodeInteger(), huffmanEnabled_, folly::io::detail::Writable< Derived >::push(), folly::Range< Iter >::size(), uint32_t, and uint8_t.

128  {
129  if (huffmanEnabled_) {
130  return encodeHuffman(instruction, nbit, literal);
131  }
132  // otherwise use simple layout
133  uint32_t count =
134  encodeInteger(literal.size(), instruction, nbit);
135  // copy the entire string
136  buf_.push((uint8_t*)literal.data(), literal.size());
137  count += literal.size();
138  return count;
139 }
uint32_t encodeHuffman(folly::StringPiece literal)
constexpr size_type size() const
Definition: Range.h:431
void push(const uint8_t *buf, size_t len)
Definition: Cursor.h:755
constexpr Iter data() const
Definition: Range.h:446
uint32_t encodeInteger(uint64_t value, uint8_t instruction, uint8_t nbit)
int * count
folly::io::QueueAppender buf_
std::unique_ptr<folly::IOBuf> proxygen::HPACKEncodeBuffer::release ( )
inline
string proxygen::HPACKEncodeBuffer::toBin ( )

prints the content of an IOBuf in binary format. Useful for debugging.

Definition at line 141 of file HPACKEncodeBuffer.cpp.

References bufQueue_, folly::IOBufQueue::front(), and proxygen::IOBufPrinter::printBin().

Referenced by clear(), and TEST_F().

141  {
143 }
const folly::IOBuf * front() const
Definition: IOBufQueue.h:476
static std::string printBin(const folly::IOBuf *buf, bool coalesce=false)
Definition: Logging.h:140

Member Data Documentation

folly::io::QueueAppender proxygen::HPACKEncodeBuffer::buf_
private

Definition at line 111 of file HPACKEncodeBuffer.h.

Referenced by append(), encodeHuffman(), and encodeLiteral().

folly::IOBufQueue proxygen::HPACKEncodeBuffer::bufQueue_
private

Definition at line 110 of file HPACKEncodeBuffer.h.

Referenced by addHeadroom(), clear(), release(), and toBin().

uint32_t proxygen::HPACKEncodeBuffer::growthSize_
private

Definition at line 112 of file HPACKEncodeBuffer.h.

Referenced by addHeadroom().

bool proxygen::HPACKEncodeBuffer::huffmanEnabled_
private

Definition at line 113 of file HPACKEncodeBuffer.h.

Referenced by encodeLiteral().


The documentation for this class was generated from the following files: