proxygen
proxygen::HeaderDecodeInfo Class Reference

#include <HeaderDecodeInfo.h>

Public Member Functions

void init (bool isRequestIn, bool isRequestTrailers)
 
bool onHeader (const folly::fbstring &name, const folly::fbstring &value)
 
void onHeadersComplete (HTTPHeaderSize decodedSize)
 
bool hasStatus () const
 

Public Attributes

std::unique_ptr< HTTPMessagemsg
 
HTTPRequestVerifier verifier
 
std::string parsingError
 
HPACK::DecodeError decodeError {HPACK::DecodeError::NONE}
 

Private Attributes

bool isRequest_ {false}
 
bool isRequestTrailers_ {false}
 
bool hasStatus_ {false}
 
bool regularHeaderSeen_ {false}
 
bool pseudoHeaderSeen_ {false}
 
folly::Optional< uint32_tcontentLength_
 

Detailed Description

Definition at line 19 of file HeaderDecodeInfo.h.

Member Function Documentation

bool proxygen::HeaderDecodeInfo::hasStatus ( ) const

Definition at line 135 of file HeaderDecodeInfo.cpp.

References hasStatus_.

Referenced by init(), and proxygen::HTTP2Codec::parsingTrailers().

135  {
136  return hasStatus_;
137 }
void proxygen::HeaderDecodeInfo::init ( bool  isRequestIn,
bool  isRequestTrailers 
)
inline

Definition at line 21 of file HeaderDecodeInfo.h.

References contentLength_, decodeError, hasStatus(), hasStatus_, isRequest_, isRequestTrailers_, msg, name, proxygen::HPACK::NONE, folly::none, onHeader(), onHeadersComplete(), parsingError, pseudoHeaderSeen_, regularHeaderSeen_, proxygen::HTTPRequestVerifier::reset(), value, and verifier.

Referenced by proxygen::HTTP2Codec::parseHeadersDecodeFrames().

21  {
22  CHECK(!msg);
23  msg.reset(new HTTPMessage());
24  isRequest_ = isRequestIn;
25  isRequestTrailers_ = isRequestTrailers;
26  hasStatus_ = false;
28  regularHeaderSeen_ = false;
29  pseudoHeaderSeen_ = false;
30  parsingError = "";
32  verifier.reset(msg.get());
33  }
std::unique_ptr< HTTPMessage > msg
void reset(HTTPMessage *msg)
HPACK::DecodeError decodeError
folly::Optional< uint32_t > contentLength_
constexpr None none
Definition: Optional.h:87
HTTPRequestVerifier verifier
bool proxygen::HeaderDecodeInfo::onHeader ( const folly::fbstring name,
const folly::fbstring value 
)

Definition at line 16 of file HeaderDecodeInfo.cpp.

References contentLength_, decodeError, proxygen::HTTPMessage::getDefaultReason(), hasStatus_, int32_t, isRequest_, proxygen::headers::kAuthority, proxygen::headers::kMethod, proxygen::headers::kPath, proxygen::headers::kProtocol, proxygen::headers::kScheme, proxygen::headers::kStatus, msg, proxygen::HPACK::NONE, parsingError, pseudoHeaderSeen_, regularHeaderSeen_, proxygen::HTTPRequestVerifier::setAuthority(), proxygen::HTTPRequestVerifier::setMethod(), proxygen::HTTPRequestVerifier::setPath(), proxygen::HTTPRequestVerifier::setScheme(), proxygen::HTTPRequestVerifier::setUpgradeProtocol(), folly::Range< Iter >::startsWith(), proxygen::CodecUtil::STRICT, string, uint32_t, proxygen::CodecUtil::validateHeaderName(), proxygen::CodecUtil::validateHeaderValue(), value, and verifier.

Referenced by init(), and proxygen::HTTP2Codec::onHeader().

17  {
18  // Refuse decoding other headers if an error is already found
20  || parsingError != "") {
21  VLOG(4) << "Ignoring header=" << name << " value=" << value <<
22  " due to parser error=" << parsingError;
23  return true;
24  }
25  VLOG(5) << "Processing header=" << name << " value=" << value;
26  folly::StringPiece nameSp(name);
27  folly::StringPiece valueSp(value);
28 
29  if (nameSp.startsWith(':')) {
30  pseudoHeaderSeen_ = true;
31  if (regularHeaderSeen_) {
32  parsingError = folly::to<string>("Illegal pseudo header name=", nameSp);
33  return false;
34  }
35  if (isRequest_) {
36  if (nameSp == headers::kMethod) {
37  if (!verifier.setMethod(valueSp)) {
38  return false;
39  }
40  } else if (nameSp == headers::kScheme) {
41  if (!verifier.setScheme(valueSp)) {
42  return false;
43  }
44  } else if (nameSp == headers::kAuthority) {
45  if (!verifier.setAuthority(valueSp)) {
46  return false;
47  }
48  } else if (nameSp == headers::kPath) {
49  if (!verifier.setPath(valueSp)) {
50  return false;
51  }
52  } else if (nameSp == headers::kProtocol) {
53  if (!verifier.setUpgradeProtocol(valueSp)) {
54  return false;
55  }
56  } else {
57  parsingError = folly::to<string>("Invalid req header name=", nameSp);
58  return false;
59  }
60  } else {
61  if (nameSp == headers::kStatus) {
62  if (hasStatus_) {
63  parsingError = string("Duplicate status");
64  return false;
65  }
66  hasStatus_ = true;
67  int32_t code = -1;
68  folly::tryTo<int32_t>(valueSp).then(
69  [&code](int32_t num) { code = num; });
70  if (code >= 100 && code <= 999) {
71  msg->setStatusCode(code);
72  msg->setStatusMessage(HTTPMessage::getDefaultReason(code));
73  } else {
74  parsingError = folly::to<string>("Malformed status code=", valueSp);
75  return false;
76  }
77  } else {
78  parsingError = folly::to<string>("Invalid resp header name=", nameSp);
79  return false;
80  }
81  }
82  } else {
83  regularHeaderSeen_ = true;
84  if (nameSp == "connection") {
85  parsingError = string("HTTP/2 Message with Connection header");
86  return false;
87  }
88  if (nameSp == "content-length") {
89  uint32_t cl = 0;
90  folly::tryTo<uint32_t>(valueSp).then(
91  [&cl](uint32_t num) { cl = num; });
92  if (contentLength_ && *contentLength_ != cl) {
93  parsingError = string("Multiple content-length headers");
94  return false;
95  }
96  contentLength_ = cl;
97  }
98  bool nameOk = CodecUtil::validateHeaderName(nameSp);
99  bool valueOk = CodecUtil::validateHeaderValue(valueSp, CodecUtil::STRICT);
100  if (!nameOk || !valueOk) {
101  parsingError = folly::to<string>("Bad header value: name=",
102  nameSp, " value=", valueSp);
103  return false;
104  }
105  // Add the (name, value) pair to headers
106  msg->getHeaders().add(nameSp, valueSp);
107  }
108  return true;
109 }
const std::string kProtocol
bool setUpgradeProtocol(folly::StringPiece protocol)
static bool validateHeaderName(folly::ByteRange name)
Definition: CodecUtil.h:43
std::unique_ptr< HTTPMessage > msg
bool setScheme(folly::StringPiece scheme)
HPACK::DecodeError decodeError
folly::Optional< uint32_t > contentLength_
const std::string kScheme
bool setMethod(folly::StringPiece method)
static const char *const value
Definition: Conv.cpp:50
const char * string
Definition: Conv.cpp:212
const std::string kStatus
const std::string kPath
bool setAuthority(folly::StringPiece authority)
static bool validateHeaderValue(folly::ByteRange value, CtlEscapeMode mode)
Definition: CodecUtil.h:66
static const char * getDefaultReason(uint16_t status)
const std::string kAuthority
bool setPath(folly::StringPiece path)
const std::string kMethod
HTTPRequestVerifier verifier
void proxygen::HeaderDecodeInfo::onHeadersComplete ( HTTPHeaderSize  decodedSize)

Definition at line 111 of file HeaderDecodeInfo.cpp.

References proxygen::HTTPHeaders::combine(), proxygen::HTTPRequestVerifier::error, hasStatus_, proxygen::HTTP_HEADER_COOKIE, isRequest_, isRequestTrailers_, msg, parsingError, pseudoHeaderSeen_, proxygen::HTTPHeaders::set(), proxygen::HTTPRequestVerifier::validate(), and verifier.

Referenced by init(), and proxygen::HTTP2Codec::onHeadersComplete().

111  {
112  HTTPHeaders& headers = msg->getHeaders();
113 
114  if (isRequest_ && !isRequestTrailers_) {
115  auto combinedCookie = headers.combine(HTTP_HEADER_COOKIE, "; ");
116  if (!combinedCookie.empty()) {
117  headers.set(HTTP_HEADER_COOKIE, combinedCookie);
118  }
119  if (!verifier.validate()) {
121  return;
122  }
123  }
124 
125  bool isResponseTrailers = (!isRequest_ && !hasStatus_);
126  if ((isRequestTrailers_ || isResponseTrailers) && pseudoHeaderSeen_) {
127  parsingError = "Pseudo headers forbidden in trailers.";
128  return;
129  }
130 
131  msg->setHTTPVersion(1, 1);
132  msg->setIngressHeaderSize(decodedSize);
133 }
std::unique_ptr< HTTPMessage > msg
HTTPRequestVerifier verifier

Member Data Documentation

folly::Optional<uint32_t> proxygen::HeaderDecodeInfo::contentLength_
private

Definition at line 54 of file HeaderDecodeInfo.h.

Referenced by init(), and onHeader().

bool proxygen::HeaderDecodeInfo::hasStatus_ {false}
private

Definition at line 51 of file HeaderDecodeInfo.h.

Referenced by hasStatus(), init(), onHeader(), and onHeadersComplete().

bool proxygen::HeaderDecodeInfo::isRequest_ {false}
private

Definition at line 49 of file HeaderDecodeInfo.h.

Referenced by init(), onHeader(), and onHeadersComplete().

bool proxygen::HeaderDecodeInfo::isRequestTrailers_ {false}
private

Definition at line 50 of file HeaderDecodeInfo.h.

Referenced by init(), and onHeadersComplete().

std::unique_ptr<HTTPMessage> proxygen::HeaderDecodeInfo::msg
bool proxygen::HeaderDecodeInfo::pseudoHeaderSeen_ {false}
private

Definition at line 53 of file HeaderDecodeInfo.h.

Referenced by init(), onHeader(), and onHeadersComplete().

bool proxygen::HeaderDecodeInfo::regularHeaderSeen_ {false}
private

Definition at line 52 of file HeaderDecodeInfo.h.

Referenced by init(), and onHeader().

HTTPRequestVerifier proxygen::HeaderDecodeInfo::verifier

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