proxygen
HPACKCodec.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 #include <algorithm>
13 #include <folly/String.h>
14 #include <folly/io/Cursor.h>
16 #include <iosfwd>
17 
18 using folly::IOBuf;
19 using folly::io::Cursor;
23 using std::unique_ptr;
24 using std::vector;
25 
26 namespace proxygen {
27 
28 namespace compress {
29  std::pair<vector<HPACKHeader>, uint32_t> prepareHeaders(
30  vector<Header>& headers) {
31  // convert to HPACK API format
32  std::pair<vector<HPACKHeader>, uint32_t> converted;
33  converted.first.reserve(headers.size());
34  for (const auto& h : headers) {
35  // HPACKHeader automatically lowercases
36  converted.first.emplace_back(*h.name, *h.value);
37  auto& header = converted.first.back();
38  converted.second += header.name.size() + header.value.size() + 2;
39  }
40  return converted;
41 }
42 }
43 
45  : encoder_(true, HPACK::kTableSize),
46  decoder_(HPACK::kTableSize, maxUncompressed_) {}
47 
48 unique_ptr<IOBuf> HPACKCodec::encode(vector<Header>& headers) noexcept {
49  auto prepared = compress::prepareHeaders(headers);
50  encodedSize_.uncompressed = prepared.second;
51  auto buf = encoder_.encode(prepared.first, encodeHeadroom_);
52  recordCompressedSize(buf.get());
53  return buf;
54 }
55 
57  const IOBuf* stream) {
59  if (stream) {
61  }
62  if (stats_) {
64  }
65 }
66 
68  Cursor& cursor,
69  uint32_t length,
70  HPACK::StreamingCallback* streamingCb) noexcept {
71  streamingCb->stats = stats_;
72  decoder_.decodeStreaming(cursor, length, streamingCb);
73 }
74 
75 void HPACKCodec::describe(std::ostream& stream) const {
76  stream << "DecoderTable:\n" << decoder_;
77  stream << "EncoderTable:\n" << encoder_;
78 }
79 
80 std::ostream& operator<<(std::ostream& os, const HPACKCodec& codec) {
81  codec.describe(os);
82  return os;
83 }
84 
85 }
const uint32_t kTableSize
void recordCompressedSize(const folly::IOBuf *buf)
Definition: HPACKCodec.cpp:56
*than *hazptr_holder h
Definition: Hazptr.h:116
void decodeStreaming(folly::io::Cursor &cursor, uint32_t length, HPACK::StreamingCallback *streamingCb) noexcept
Definition: HPACKCodec.cpp:67
std::ostream & operator<<(std::ostream &os, const HeaderTable &table)
HPACKEncoder encoder_
Definition: HPACKCodec.h:86
CodecFactory codec
void describe(std::ostream &os) const
Definition: HPACKCodec.cpp:75
requires E e noexcept(noexcept(s.error(std::move(e))))
HPACKDecoder decoder_
Definition: HPACKCodec.h:87
HTTPHeaderSize encodedSize_
Definition: HeaderCodec.h:90
std::pair< vector< HPACKHeader >, uint32_t > prepareHeaders(vector< Header > &headers)
Definition: HPACKCodec.cpp:29
uint32_t encodeHeadroom_
Definition: HeaderCodec.h:91
std::deque< HeaderPiece > HeaderPieceList
Definition: HeaderPiece.h:59
void decodeStreaming(folly::io::Cursor &cursor, uint32_t totalBytes, HPACK::StreamingCallback *streamingCb)
std::unique_ptr< folly::IOBuf > encode(const std::vector< HPACKHeader > &headers, uint32_t headroom=0)
std::size_t computeChainDataLength() const
Definition: IOBuf.cpp:501
virtual void recordEncode(Type type, HTTPHeaderSize &size)=0
std::unique_ptr< folly::IOBuf > encode(std::vector< compress::Header > &headers) noexcept
Definition: HPACKCodec.cpp:48
HPACKCodec(TransportDirection direction)
Definition: HPACKCodec.cpp:44