proxygen
HPACKEncoder.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
11 
12 using std::vector;
13 
14 namespace proxygen {
15 
16 std::unique_ptr<folly::IOBuf>
17 HPACKEncoder::encode(const vector<HPACKHeader>& headers, uint32_t headroom) {
18  if (headroom) {
19  streamBuffer_.addHeadroom(headroom);
20  }
22  for (const auto& header : headers) {
23  encodeHeader(header);
24  }
25  return streamBuffer_.release();
26 }
27 
28 bool HPACKEncoder::encodeAsLiteral(const HPACKHeader& header, bool indexing) {
29  if (header.bytes() > table_.capacity()) {
30  // May want to investigate further whether or not this is wanted.
31  // Flushing the table on a large header frees up some memory,
32  // however, there will be no compression due to an empty table, and
33  // the table will fill up again fairly quickly
34  indexing = false;
35  }
36 
37  HPACK::Instruction instruction = (indexing) ?
39 
40  encodeLiteral(header, nameIndex(header.name), instruction);
41  // indexed ones need to get added to the header table
42  if (indexing) {
43  CHECK(table_.add(header.copy()));
44  }
45  return true;
46 }
47 
50  const HPACK::Instruction& instruction) {
51  // name
52  if (nameIndex) {
53  VLOG(10) << "encoding name index=" << nameIndex;
54  streamBuffer_.encodeInteger(nameIndex, instruction);
55  } else {
56  streamBuffer_.encodeInteger(0, instruction);
58  }
59  // value
61 }
62 
64  VLOG(10) << "encoding index=" << index;
66 }
67 
69  // First determine whether the header is defined as indexable using the
70  // set strategy if applicable, else assume it is indexable
71  bool indexable = !indexingStrat_ || indexingStrat_->indexHeader(header);
72 
73  // If the header was not defined as indexable, its a reasonable assumption
74  // that it does not appear in either the static or dynamic table and should
75  // not be searched. The only time this is not true is if the header indexing
76  // strat specified an exact header/value pair that is in the static header
77  // table although semantically the header indexing strategy should indeed act
78  // as an override so we assume this is desired if such a case occurs
79  uint32_t index = 0;
80  if (indexable) {
81  index = getIndex(header);
82  }
83 
84  // Finally encode the header as determined above
85  if (index) {
86  encodeAsIndex(index);
87  } else {
88  encodeAsLiteral(header, indexable);
89  }
90 }
91 
92 }
void encodeAsIndex(uint32_t index)
bool encodeAsLiteral(const HPACKHeader &header, bool indexing)
uint32_t encodeLiteral(folly::StringPiece literal)
uint32_t capacity() const
Definition: HeaderTable.h:86
std::unique_ptr< folly::IOBuf > release()
const Instruction INDEX_REF
void encodeLiteral(const HPACKHeader &header, uint32_t nameIndex, const HPACK::Instruction &instruction)
uint32_t nameIndex(const HPACKHeaderName &headerName) const
HPACKEncodeBuffer streamBuffer_
void addHeadroom(uint32_t bytes)
void encodeHeader(const HPACKHeader &header)
uint32_t bytes() const
Definition: HPACKHeader.h:49
const Instruction LITERAL_INC_INDEX
uint32_t getIndex(const HPACKHeader &header) const
virtual bool indexHeader(const HPACKHeader &header) const
void handlePendingContextUpdate(HPACKEncodeBuffer &buf, uint32_t tableCapacity)
HPACKHeaderName name
Definition: HPACKHeader.h:82
uint32_t encodeInteger(uint64_t value, uint8_t instruction, uint8_t nbit)
std::unique_ptr< folly::IOBuf > encode(const std::vector< HPACKHeader > &headers, uint32_t headroom=0)
folly::fbstring value
Definition: HPACKHeader.h:83
const HeaderIndexingStrategy * indexingStrat_
HPACKHeader copy() const
Definition: HPACKHeader.h:71
const Instruction LITERAL
virtual bool add(HPACKHeader header)
Definition: HeaderTable.cpp:33
const std::string & get() const