proxygen
HTTPRequestVerifier.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 
16 namespace proxygen {
17 
19  public:
20  explicit HTTPRequestVerifier() {}
21 
22  void reset(HTTPMessage* msg) {
23  msg_ = msg;
24  error = "";
25  hasMethod_ = false;
26  hasPath_ = false;
27  hasScheme_ = false;
28  hasAuthority_ = false;
29  hasUpgradeProtocol_ = false;
30  }
31 
33  if (hasMethod_) {
34  error = "Duplicate method";
35  return false;
36  }
37  if (!CodecUtil::validateMethod(method)) {
38  error = "Invalid method";
39  return false;
40  }
41  hasMethod_ = true;
42  assert(msg_ != nullptr);
43  msg_->setMethod(method);
44  return true;
45  }
46 
48  if (hasPath_) {
49  error = "Duplicate path";
50  return false;
51  }
52  if (!CodecUtil::validateURL(path)) {
53  error = "Invalid url";
54  return false;
55  }
56  hasPath_ = true;
57  assert(msg_ != nullptr);
58  msg_->setURL(path.str());
59  return true;
60  }
61 
63  if (hasScheme_) {
64  error = "Duplicate scheme";
65  return false;
66  }
67  // This just checks for alpha chars
68  if (!CodecUtil::validateMethod(scheme)) {
69  error = "Invalid scheme";
70  return false;
71  }
72  hasScheme_ = true;
73  // TODO support non http/https schemes
74  if (scheme == headers::kHttps) {
75  assert(msg_ != nullptr);
76  msg_->setSecure(true);
77  }
78  return true;
79  }
80 
81  bool setAuthority(folly::StringPiece authority) {
82  if (hasAuthority_) {
83  error = "Duplicate authority";
84  return false;
85  }
87  error = "Invalid authority";
88  return false;
89  }
90  hasAuthority_ = true;
91  assert(msg_ != nullptr);
92  msg_->getHeaders().add(HTTP_HEADER_HOST, authority.str());
93  return true;
94  }
95 
97  if (hasUpgradeProtocol_) {
98  error = "Duplicate protocol";
99  return false;
100  }
101  setHasUpgradeProtocol(true);
102  msg_->setUpgradeProtocol(folly::to<std::string>(protocol));
103  return true;
104  }
105 
106  bool validate() {
107  if (error.size()) {
108  return false;
109  }
110  if (msg_->getMethod() == HTTPMethod::CONNECT) {
111  if ((!hasUpgradeProtocol_ &&
114  error = folly::to<std::string>("Malformed CONNECT request m/a/s/pa/pr=",
117  }
118  } else if (hasUpgradeProtocol_ || !hasMethod_ || !hasScheme_ || !hasPath_) {
119  error = folly::to<std::string>("Malformed request m/a/s/pa/pr=",
122  }
123  return error.empty();
124  }
125 
126  void setMessage(HTTPMessage* msg) {
127  msg_ = msg;
128  }
129 
130  void setHasMethod(bool hasMethod) {
131  hasMethod_ = hasMethod;
132  }
133 
134  void setHasPath(bool hasPath) {
135  hasPath_ = hasPath;
136  }
137 
138  void setHasScheme(bool hasScheme) {
139  hasScheme_ = hasScheme;
140  }
141 
142  void setHasAuthority(bool hasAuthority) {
143  hasAuthority_ = hasAuthority;
144  }
145 
148  }
149 
151  return hasUpgradeProtocol_;
152  }
153 
155 
156  private:
157  HTTPMessage* msg_{nullptr};
158  bool hasMethod_{false};
159  bool hasPath_{false};
160  bool hasScheme_{false};
161  bool hasAuthority_{false};
162  bool hasUpgradeProtocol_{false};
163 };
164 
165 } // proxygen
void setMessage(HTTPMessage *msg)
std::string str() const
Definition: Range.h:591
bool setUpgradeProtocol(folly::StringPiece protocol)
double val
Definition: String.cpp:273
bool setScheme(folly::StringPiece scheme)
void reset(HTTPMessage *msg)
static bool validateMethod(folly::ByteRange method)
Definition: CodecUtil.h:33
void setSecure(bool secure)
Definition: HTTPMessage.h:534
bool setMethod(folly::StringPiece method)
ParseURL setURL(T &&url)
Definition: HTTPMessage.h:183
static bool validateURL(folly::ByteRange url)
Definition: CodecUtil.h:29
void setUpgradeProtocol(std::string protocol)
Definition: HTTPMessage.h:375
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void setMethod(HTTPMethod method)
const char * string
Definition: Conv.cpp:212
bool setAuthority(folly::StringPiece authority)
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
static bool validateHeaderValue(folly::ByteRange value, CtlEscapeMode mode)
Definition: CodecUtil.h:66
void setHasAuthority(bool hasAuthority)
folly::Optional< HTTPMethod > getMethod() const
bool setPath(folly::StringPiece path)
const std::string kHttps