proxygen
HTTP2Framer.h
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  */
10 #pragma once
11 
12 #include <cstdint>
13 #include <deque>
14 #include <folly/Optional.h>
15 #include <folly/Range.h>
16 #include <folly/io/Cursor.h>
21 #include <string.h>
22 
24 
25 namespace proxygen { namespace http2 {
26 
28 
31 extern const Padding kNoPadding;
32 
34 
35 enum class FrameType : uint8_t {
36  DATA = 0,
37  HEADERS = 1,
38  PRIORITY = 2,
39  RST_STREAM = 3,
40  SETTINGS = 4,
41  PUSH_PROMISE = 5,
42  PING = 6,
43  GOAWAY = 7,
44  WINDOW_UPDATE = 8,
45  CONTINUATION = 9,
46  ALTSVC = 10, // not in current draft so frame type has not been assigned
47 
48  // experimental use
49  EX_HEADERS = 0xfb,
50 
51  // For secondary certificate authentication in HTTP/2 as specified in the
52  // draft-ietf-httpbis-http2-secondary-certs-02.
53  CERTIFICATE_REQUEST = 0xf0,
54  CERTIFICATE = 0xf1,
55  CERTIFICATE_NEEDED = 0xf2,
56  USE_CERTIFICATE = 0xf3,
57 };
58 
59 enum Flags {
60  ACK = 0x1,
61  END_STREAM = 0x1,
62  END_HEADERS = 0x4,
63  PADDED = 0x8,
64  PRIORITY = 0x20,
65  // experimental flag for EX stream only
67 
68  // for secondary certificate authentication frames
69  UNSOLICITED = 0x1,
71 };
72 
73 struct FrameHeader {
74  uint32_t length; // only 24 valid bits
79 };
80 
81 static_assert(sizeof(FrameHeader) == 12, "The maths are not working");
82 
85  bool exclusive;
87 };
88 
90 
92 
94 
96 
98 
105 bool frameHasPadding(const FrameHeader& header);
106 
108 
118 ErrorCode
120  FrameHeader& header) noexcept;
121 
135 ErrorCode
137  const FrameHeader& header,
138  std::unique_ptr<folly::IOBuf>& outBuf,
139  uint16_t& padding) noexcept;
140 
141 ErrorCode
143  const FrameHeader& header,
144  size_t& parsed,
145  uint16_t& outPadding) noexcept;
146 
147 ErrorCode
149  const size_t bufLen,
150  const size_t pendingDataFramePaddingBytes,
151  size_t& toSkip) noexcept;
152 
167 ErrorCode
169  const FrameHeader& header,
170  folly::Optional<PriorityUpdate>& outPriority,
171  std::unique_ptr<folly::IOBuf>& outBuf) noexcept;
172 
173 ErrorCode
175  const FrameHeader& header,
176  HTTPCodec::ExAttributes& outExAttributes,
177  folly::Optional<PriorityUpdate>& outPriority,
178  std::unique_ptr<folly::IOBuf>& outBuf) noexcept;
179 
192 ErrorCode
194  const FrameHeader& header,
195  PriorityUpdate& outPriority) noexcept;
196 
209 ErrorCode
211  const FrameHeader& header,
212  ErrorCode& outCode) noexcept;
213 
226 ErrorCode
228  const FrameHeader& header,
229  std::deque<SettingPair>& settings) noexcept;
230 
244 ErrorCode
246  const FrameHeader& header,
247  uint32_t& outPromisedStream,
248  std::unique_ptr<folly::IOBuf>& outBuf) noexcept;
249 
261 ErrorCode
263  const FrameHeader& header,
264  uint64_t& outData) noexcept;
265 
280 ErrorCode
282  const FrameHeader& header,
283  uint32_t& outLastStreamID,
284  ErrorCode& outCode,
285  std::unique_ptr<folly::IOBuf>& outDebugData) noexcept;
286 
298 ErrorCode
300  const FrameHeader& header,
301  uint32_t& outAmount) noexcept;
302 
315 ErrorCode
317  const FrameHeader& header,
318  std::unique_ptr<folly::IOBuf>& outBuf) noexcept;
319 
335 ErrorCode
337  const FrameHeader& header,
338  uint32_t& outMaxAge,
339  uint32_t& outPort,
340  std::string& outProtocol,
341  std::string& outHost,
342  std::string& outOrigin) noexcept;
343 
357  folly::io::Cursor& cursor,
358  const FrameHeader& header,
359  uint16_t& outRequestId,
360  std::unique_ptr<folly::IOBuf>& outAuthRequest) noexcept;
361 
374 ErrorCode
376  folly::io::Cursor& cursor,
377  const FrameHeader& header,
378  uint16_t& outCertId,
379  std::unique_ptr<folly::IOBuf>& outAuthenticator) noexcept;
380 
382 
398 size_t
400  std::unique_ptr<folly::IOBuf> data,
402  folly::Optional<uint8_t> padding,
403  bool endStream,
404  bool reuseIOBufHeadroom) noexcept;
405 
422 size_t
424  std::unique_ptr<folly::IOBuf> headers,
427  folly::Optional<uint8_t> padding,
428  bool endStream,
429  bool endHeaders) noexcept;
430 
448 size_t
450  std::unique_ptr<folly::IOBuf> headers,
452  const HTTPCodec::ExAttributes& exAttributes,
453  const folly::Optional<PriorityUpdate>& priority,
454  const folly::Optional<uint8_t>& padding,
455  bool endStream,
456  bool endHeaders) noexcept;
457 
467 size_t
470  PriorityUpdate priority) noexcept;
471 
482 size_t
485  ErrorCode errorCode) noexcept;
486 
496 size_t
498  const std::deque<SettingPair>& settings);
499 
504 size_t
506 
521 size_t
523  uint32_t associatedStream,
524  uint32_t promisedStream,
525  std::unique_ptr<folly::IOBuf> headers,
526  folly::Optional<uint8_t> padding,
527  bool endHeaders) noexcept;
537 size_t
539  uint64_t data,
540  bool ack) noexcept;
541 
553 size_t
555  uint32_t lastStreamID,
556  ErrorCode errorCode,
557  std::unique_ptr<folly::IOBuf> debugData = nullptr) noexcept;
558 
569 size_t
570 writeWindowUpdate(folly::IOBufQueue& writeBuf,
572  uint32_t amount) noexcept;
573 
587 size_t
588 writeContinuation(folly::IOBufQueue& queue,
589  uint32_t stream,
590  bool endHeaders,
591  std::unique_ptr<folly::IOBuf> headers) noexcept;
606 size_t
607 writeAltSvc(folly::IOBufQueue& writeBuf,
608  uint32_t stream,
609  uint32_t maxAge,
610  uint16_t port,
611  folly::StringPiece protocol,
612  folly::StringPiece host,
613  folly::StringPiece origin) noexcept;
614 
626 size_t
627 writeCertificateRequest(folly::IOBufQueue& writeBuf,
628  uint16_t requestId,
629  std::unique_ptr<folly::IOBuf> authRequest);
630 
644 size_t writeCertificate(folly::IOBufQueue& writeBuf,
645  uint16_t certId,
646  std::unique_ptr<folly::IOBuf> authenticator,
647  bool toBeContinued);
648 
657 }}
#define FB_EXPORT
Definition: Export.h:26
ErrorCode parseAltSvc(Cursor &cursor, const FrameHeader &header, uint32_t &outMaxAge, uint32_t &outPort, std::string &outProtocol, std::string &outHost, std::string &outOrigin) noexcept
ErrorCode parseContinuation(Cursor &cursor, const FrameHeader &header, std::unique_ptr< IOBuf > &outBuf) noexcept
size_t writePing(IOBufQueue &queue, uint64_t opaqueData, bool ack) noexcept
ErrorCode parsePushPromise(Cursor &cursor, const FrameHeader &header, uint32_t &outPromisedStream, std::unique_ptr< IOBuf > &outBuf) noexcept
ErrorCode parseFrameHeader(Cursor &cursor, FrameHeader &header) noexcept
ErrorCode parseGoaway(Cursor &cursor, const FrameHeader &header, uint32_t &outLastStreamID, ErrorCode &outCode, std::unique_ptr< IOBuf > &outDebugData) noexcept
size_t writeHeaders(IOBufQueue &queue, std::unique_ptr< IOBuf > headers, uint32_t stream, folly::Optional< PriorityUpdate > priority, folly::Optional< uint8_t > padding, bool endStream, bool endHeaders) noexcept
PskType type
ErrorCode parsePriority(Cursor &cursor, const FrameHeader &header, PriorityUpdate &outPriority) noexcept
ErrorCode parseSettings(Cursor &cursor, const FrameHeader &header, std::deque< SettingPair > &settings) noexcept
STL namespace.
size_t writePushPromise(IOBufQueue &queue, uint32_t associatedStream, uint32_t promisedStream, std::unique_ptr< IOBuf > headers, folly::Optional< uint8_t > padding, bool endHeaders) noexcept
static http_parser_settings settings
Definition: test.c:1529
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
ErrorCode parseWindowUpdate(Cursor &cursor, const FrameHeader &header, uint32_t &outAmount) noexcept
size_t writeContinuation(IOBufQueue &queue, uint32_t stream, bool endHeaders, std::unique_ptr< IOBuf > headers) noexcept
ErrorCode parseCertificateRequest(folly::io::Cursor &cursor, const FrameHeader &header, uint16_t &outRequestId, std::unique_ptr< folly::IOBuf > &outAuthRequest) noexcept
bool frameHasPadding(const FrameHeader &header)
size_t writeSettings(IOBufQueue &queue, const std::deque< SettingPair > &settings)
void writeBuf(const Buf &buf, folly::io::Appender &out)
ErrorCode parseExHeaders(Cursor &cursor, const FrameHeader &header, HTTPCodec::ExAttributes &outExAttributes, folly::Optional< PriorityUpdate > &outPriority, std::unique_ptr< IOBuf > &outBuf) noexcept
size_t writePriority(IOBufQueue &queue, uint32_t stream, PriorityUpdate priority) noexcept
size_t writeCertificate(folly::IOBufQueue &writeBuf, uint16_t certId, std::unique_ptr< folly::IOBuf > authenticator, bool toBeContinued)
ErrorCode parseRstStream(Cursor &cursor, const FrameHeader &header, ErrorCode &outCode) noexcept
ErrorCode parseCertificate(folly::io::Cursor &cursor, const FrameHeader &header, uint16_t &outCertId, std::unique_ptr< folly::IOBuf > &outAuthenticator) noexcept
ErrorCode parseDataBegin(Cursor &cursor, const FrameHeader &header, size_t &, uint16_t &outPadding) noexcept
size_t writeAltSvc(IOBufQueue &queue, uint32_t stream, uint32_t maxAge, uint16_t port, StringPiece protocol, StringPiece host, StringPiece origin) noexcept
size_t writeCertificateRequest(folly::IOBufQueue &writeBuf, uint16_t requestId, std::unique_ptr< folly::IOBuf > authRequest)
ErrorCode parseHeaders(Cursor &cursor, const FrameHeader &header, folly::Optional< PriorityUpdate > &outPriority, std::unique_ptr< IOBuf > &outBuf) noexcept
size_t writeSettingsAck(IOBufQueue &queue)
const PriorityUpdate DefaultPriority
Definition: HTTP2Framer.cpp:21
size_t writeRstStream(IOBufQueue &queue, uint32_t stream, ErrorCode errorCode) noexcept
size_t writeExHeaders(IOBufQueue &queue, std::unique_ptr< IOBuf > headers, uint32_t stream, const HTTPCodec::ExAttributes &exAttributes, const folly::Optional< PriorityUpdate > &priority, const folly::Optional< uint8_t > &padding, bool endStream, bool endHeaders) noexcept
const uint8_t kMinExperimentalFrameType
Definition: HTTP2Framer.cpp:19
const char * string
Definition: Conv.cpp:212
size_t writeGoaway(IOBufQueue &queue, uint32_t lastStreamID, ErrorCode errorCode, std::unique_ptr< IOBuf > debugData) noexcept
bool isValidFrameType(FrameType type)
const
Definition: upload.py:398
const Padding kNoPadding
Definition: HTTP2Framer.cpp:20
bool frameAffectsCompression(FrameType t)
Range< const char * > StringPiece
const char * getFrameTypeString(FrameType type)
ErrorCode parseData(Cursor &cursor, const FrameHeader &header, std::unique_ptr< IOBuf > &outBuf, uint16_t &outPadding) noexcept
size_t writeData(IOBufQueue &queue, std::unique_ptr< IOBuf > data, uint32_t stream, folly::Optional< uint8_t > padding, bool endStream, bool reuseIOBufHeadroom) noexcept
size_t writeWindowUpdate(IOBufQueue &queue, uint32_t stream, uint32_t amount) noexcept
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
ErrorCode parseDataEnd(Cursor &cursor, const size_t bufLen, const size_t pendingDataFramePaddingBytes, size_t &toSkip) noexcept
ErrorCode parsePing(Cursor &cursor, const FrameHeader &header, uint64_t &outOpaqueData) noexcept