proxygen
HTTP1xCodec.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 
15 #include <string>
16 
18 
19 namespace proxygen {
20 
21 class HTTP1xCodec : public HTTPCodec {
22  public:
23  explicit HTTP1xCodec(TransportDirection direction,
24  bool forceUpstream1_1 = false);
25  ~HTTP1xCodec() override;
26 
27  // HTTPCodec API
28  CodecProtocol getProtocol() const override {
30  }
31 
32  const std::string& getUserAgent() const override {
33  return userAgent_;
34  }
35 
37  return transportDirection_;
38  }
39  StreamID createStream() override;
40  void setCallback(Callback* callback) override { callback_ = callback; }
41  bool isBusy() const override;
42  void setParserPaused(bool paused) override;
43  size_t onIngress(const folly::IOBuf& buf) override;
44  void onIngressEOF() override;
45  bool isReusable() const override;
46  bool isWaitingToDrain() const override { return false; }
47  // If the session has been upgraded we will send EOF (or RST if needed)
48  // on egress complete
49  bool closeOnEgressComplete() const override { return egressUpgrade_; }
50  bool supportsParallelRequests() const override { return false; }
51  bool supportsPushTransactions() const override { return false; }
53  StreamID txn,
54  const HTTPMessage& msg,
55  bool eom = false,
56  HTTPHeaderSize* size = nullptr) override;
57  size_t generateBody(folly::IOBufQueue& writeBuf,
58  StreamID txn,
59  std::unique_ptr<folly::IOBuf> chain,
61  bool eom) override;
62  size_t generateChunkHeader(folly::IOBufQueue& writeBuf,
63  StreamID txn,
64  size_t length) override;
66  StreamID txn) override;
67  size_t generateTrailers(folly::IOBufQueue& writeBuf,
68  StreamID txn,
69  const HTTPHeaders& trailers) override;
70  size_t generateEOM(folly::IOBufQueue& writeBuf,
71  StreamID txn) override;
72  size_t generateRstStream(folly::IOBufQueue& writeBuf,
73  StreamID txn,
74  ErrorCode statusCode) override;
75  size_t generateGoaway(
76  folly::IOBufQueue& writeBuf,
77  StreamID lastStream,
78  ErrorCode statusCode,
79  std::unique_ptr<folly::IOBuf> debugData = nullptr) override;
80 
81  void setAllowedUpgradeProtocols(std::list<std::string> protocols);
83 
87  static bool supportsNextProtocol(const std::string& npn);
88 
89  private:
91  enum class HeaderParseState : uint8_t {
99  };
100 
102  std::string generateWebsocketAccept(const std::string& acceptKey) const;
104 
109  UNSET,
110  ENABLED, // incoming message requested keepalive
111  DISABLED, // incoming message disabled keepalive
112  };
113 
114  void addDateHeader(folly::IOBufQueue& writeBuf, size_t& len);
115 
117  bool isParsingHeaders() const {
120  }
121 
126  }
127 
132  void onParserError(const char* what = nullptr);
133 
136 
138  void serializeWebsocketHeader(folly::IOBufQueue& writeBuf, size_t& len,
139  bool upstream);
140 
141  // Parser callbacks
142  int onMessageBegin();
143  int onURL(const char* buf, size_t len);
144  int onReason(const char* buf, size_t len);
145  int onHeaderField(const char* buf, size_t len);
146  int onHeaderValue(const char* buf, size_t len);
147  int onHeadersComplete(size_t len);
148  int onBody(const char* buf, size_t len);
149  int onChunkHeader(size_t len);
150  int onChunkComplete();
151  int onMessageComplete();
152 
158  std::unique_ptr<HTTPMessage> msg_;
159  std::unique_ptr<HTTPMessage> upgradeRequest_;
160  std::unique_ptr<HTTPHeaders> trailers_;
167  std::string upgradeHeader_; // last sent/received client upgrade header
172  KeepaliveRequested keepaliveRequested_; // only used in DOWNSTREAM mode
173  std::pair<CodecProtocol, std::string> upgradeResult_; // DOWNSTREAM only
174  bool forceUpstream1_1_:1; // Use HTTP/1.1 upstream even if msg is 1.0
176  bool pendingEOF_:1;
178  bool parserError_:1;
182  bool inChunk_:1;
184  bool keepalive_:1;
186  // TODO: replace the 2 booleans below with an enum "request method"
188  bool headRequest_:1;
198 
199  // C-callable wrappers for the http_parser callbacks
200  static int onMessageBeginCB(http_parser* parser);
201  static int onPathCB(http_parser* parser, const char* buf, size_t len);
202  static int onQueryStringCB(http_parser* parser, const char* buf, size_t len);
203  static int onUrlCB(http_parser* parser, const char* buf, size_t len);
204  static int onReasonCB(http_parser* parser, const char* buf, size_t len);
205  static int onHeaderFieldCB(http_parser* parser, const char* buf, size_t len);
206  static int onHeaderValueCB(http_parser* parser, const char* buf, size_t len);
208  const char* buf, size_t len);
209  static int onBodyCB(http_parser* parser, const char* buf, size_t len);
210  static int onChunkHeaderCB(http_parser* parser);
211  static int onChunkCompleteCB(http_parser* parser);
213 
214  static const http_parser_settings* getParserSettings();
215 };
216 
217 } // proxygen
std::pair< CodecProtocol, std::string > upgradeResult_
Definition: HTTP1xCodec.h:173
size_t generateTrailers(folly::IOBufQueue &writeBuf, StreamID txn, const HTTPHeaders &trailers) override
int onBody(const char *buf, size_t len)
std::string currentHeaderValue_
Definition: HTTP1xCodec.h:163
TransportDirection transportDirection_
Definition: HTTP1xCodec.h:171
spdy::GoawayStatusCode statusCode
Definition: SPDYCodec.cpp:110
std::unique_ptr< HTTPMessage > msg_
Definition: HTTP1xCodec.h:158
void serializeWebsocketHeader(folly::IOBufQueue &writeBuf, size_t &len, bool upstream)
bool isBusy() const override
const std::string & getAllowedUpgradeProtocols()
std::unique_ptr< HTTPMessage > upgradeRequest_
Definition: HTTP1xCodec.h:159
void setAllowedUpgradeProtocols(std::list< std::string > protocols)
HeaderParseState headerParseState_
Definition: HTTP1xCodec.h:170
std::string userAgent_
Definition: HTTP1xCodec.h:165
CodecProtocol getProtocol() const override
Definition: HTTP1xCodec.h:28
void generateHeader(folly::IOBufQueue &writeBuf, StreamID txn, const HTTPMessage &msg, bool eom=false, HTTPHeaderSize *size=nullptr) override
bool supportsParallelRequests() const override
Definition: HTTP1xCodec.h:50
std::string upgradeHeader_
Definition: HTTP1xCodec.h:167
static int onChunkHeaderCB(http_parser *parser)
static int onQueryStringCB(http_parser *parser, const char *buf, size_t len)
static bool supportsNextProtocol(const std::string &npn)
folly::StringPiece currentHeaderNameStringPiece_
Definition: HTTP1xCodec.h:162
bool closeOnEgressComplete() const override
Definition: HTTP1xCodec.h:49
int onHeaderValue(const char *buf, size_t len)
static int onMessageCompleteCB(http_parser *parser)
static int onHeaderValueCB(http_parser *parser, const char *buf, size_t len)
std::string generateWebsocketKey() const
static int onMessageBeginCB(http_parser *parser)
static http_parser * parser
Definition: test.c:40
static int onHeadersCompleteCB(http_parser *parser, const char *buf, size_t len)
const std::string & getUserAgent() const override
Definition: HTTP1xCodec.h:32
std::string currentHeaderName_
Definition: HTTP1xCodec.h:161
static int onChunkCompleteCB(http_parser *parser)
static int onUrlCB(http_parser *parser, const char *buf, size_t len)
HTTPCodec::Callback * callback_
Definition: HTTP1xCodec.h:153
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
TransportDirection getTransportDirection() const override
Definition: HTTP1xCodec.h:36
void writeBuf(const Buf &buf, folly::io::Appender &out)
HTTPHeaderSize headerSize_
Definition: HTTP1xCodec.h:169
void setCallback(Callback *callback) override
Definition: HTTP1xCodec.h:40
void addDateHeader(folly::IOBufQueue &writeBuf, size_t &len)
bool isReusable() const override
size_t onIngress(const folly::IOBuf &buf) override
static int onReasonCB(http_parser *parser, const char *buf, size_t len)
static int onBodyCB(http_parser *parser, const char *buf, size_t len)
bool isParsingHeaderOrTrailerName() const
Definition: HTTP1xCodec.h:123
bool isParsingHeaders() const
Definition: HTTP1xCodec.h:117
int onHeaderField(const char *buf, size_t len)
int onHeadersComplete(size_t len)
void onParserError(const char *what=nullptr)
std::string allowedNativeUpgrades_
Definition: HTTP1xCodec.h:168
const char * string
Definition: Conv.cpp:212
void setParserPaused(bool paused) override
std::string websockAcceptKey_
Definition: HTTP1xCodec.h:103
int onReason(const char *buf, size_t len)
void pushHeaderNameAndValue(HTTPHeaders &hdrs)
uint64_t StreamID
Definition: HTTPCodec.h:49
size_t generateRstStream(folly::IOBufQueue &writeBuf, StreamID txn, ErrorCode statusCode) override
KeepaliveRequested keepaliveRequested_
Definition: HTTP1xCodec.h:172
static int onHeaderFieldCB(http_parser *parser, const char *buf, size_t len)
bool isWaitingToDrain() const override
Definition: HTTP1xCodec.h:46
std::string generateWebsocketAccept(const std::string &acceptKey) const
StreamID createStream() override
bool supportsPushTransactions() const override
Definition: HTTP1xCodec.h:51
size_t generateEOM(folly::IOBufQueue &writeBuf, StreamID txn) override
int onURL(const char *buf, size_t len)
std::unique_ptr< HTTPHeaders > trailers_
Definition: HTTP1xCodec.h:160
static int onPathCB(http_parser *parser, const char *buf, size_t len)
const folly::IOBuf * currentIngressBuf_
Definition: HTTP1xCodec.h:157
size_t generateGoaway(folly::IOBufQueue &writeBuf, StreamID lastStream, ErrorCode statusCode, std::unique_ptr< folly::IOBuf > debugData=nullptr) override
int onChunkHeader(size_t len)
HTTP1xCodec(TransportDirection direction, bool forceUpstream1_1=false)
Definition: HTTP1xCodec.cpp:81
void onIngressEOF() override
static const http_parser_settings * getParserSettings()
size_t generateChunkHeader(folly::IOBufQueue &writeBuf, StreamID txn, size_t length) override
size_t generateBody(folly::IOBufQueue &writeBuf, StreamID txn, std::unique_ptr< folly::IOBuf > chain, folly::Optional< uint8_t > padding, bool eom) override
size_t generateChunkTerminator(folly::IOBufQueue &writeBuf, StreamID txn) override