proxygen
CodecUtil.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 <assert.h>
13 #include <cctype>
14 #include <folly/Range.h>
15 #include <stdint.h>
16 #include <string>
20 
21 namespace proxygen {
22 
23 class CodecUtil {
24  public:
25  // If these are needed elsewhere, we can move them to a more generic
26  // namespace/class later
27  static const char http_tokens[256];
28 
29  static bool validateURL(folly::ByteRange url) {
30  return proxygen::validateURL(url);
31  }
32 
33  static bool validateMethod(folly::ByteRange method) {
34  for (auto p: method) {
35  if (!isalpha(p)) {
36  // methods are all characters
37  return false;
38  }
39  }
40  return true;
41  }
42 
44  if (name.size() == 0) {
45  return false;
46  }
47  for (auto p: name) {
48  if (p < 0x80 && http_tokens[(uint8_t)p] != p) {
49  return false;
50  }
51  }
52  return true;
53  }
54 
64  };
65 
68  bool escape = false;
69  bool quote = false;
70  enum { lws_none,
71  lws_expect_nl,
72  lws_expect_ws1,
73  lws_expect_ws2 } state = lws_none;
74 
75  for (auto p = std::begin(value); p != std::end(value); ++p) {
76  if (escape) {
77  escape = false;
78  if (mode == COMPLIANT) {
79  // prev char escaped. Turn off escape and go to next char
80  // COMPLIANT mode only
81  assert(quote);
82  continue;
83  }
84  }
85  switch (state) {
86  case lws_none:
87  switch (*p) {
88  case '\\':
89  if (quote) {
90  escape = true;
91  }
92  break;
93  case '\"':
94  quote = !quote;
95  break;
96  case '\r':
97  state = lws_expect_nl;
98  break;
99  default:
100  if ((*p < 0x20 || *p == 0x7f) && *p != '\t') {
101  // unexpected ctl per rfc2616, HT OK
102  return false;
103  }
104  break;
105  }
106  break;
107  case lws_expect_nl:
108  if (*p != '\n') {
109  // unescaped \r must be LWS
110  return false;
111  }
112  state = lws_expect_ws1;
113  break;
114  case lws_expect_ws1:
115  if (*p != ' ' && *p != '\t') {
116  // unescaped \r\n must be LWS
117  return false;
118  }
119  state = lws_expect_ws2;
120  break;
121  case lws_expect_ws2:
122  if (*p != ' ' && *p != '\t') {
123  // terminated LWS
124  state = lws_none;
125  // check this char again
126  p--;
127  }
128  break;
129  }
130  }
131  // Unterminated quotes are OK, since the value can be* TEXT which treats
132  // the " like any other char.
133  // Unterminated escapes are bad because it will escape the next character
134  // when converting to HTTP
135  // Unterminated LWS (dangling \r or \r\n) is bad because it could
136  // prematurely terminate the headers when converting to HTTP
137  return !escape && (state == lws_none || state == lws_expect_ws2);
138  }
139 
140  static bool hasGzipAndDeflate(const std::string& value, bool& hasGzip,
141  bool& hasDeflate);
142 
143  static std::vector<compress::Header> prepareMessageForCompression(
144  const HTTPMessage& msg,
145  std::vector<std::string>& temps);
146 
147  static bool appendHeaders(const HTTPHeaders& inputHeaders,
148  std::vector<compress::Header>& headers,
149  HTTPHeaderCode headerToCheck);
150 };
151 }
static std::vector< compress::Header > prepareMessageForCompression(const HTTPMessage &msg, std::vector< std::string > &temps)
Definition: CodecUtil.cpp:82
static bool validateHeaderName(folly::ByteRange name)
Definition: CodecUtil.h:43
bool quote
Definition: Conv.cpp:213
constexpr size_type size() const
Definition: Range.h:431
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
bool validateURL(folly::ByteRange url)
Definition: UtilInl.h:25
static bool validateMethod(folly::ByteRange method)
Definition: CodecUtil.h:33
folly::Optional< PskKeyExchangeMode > mode
const char * name
Definition: http_parser.c:437
static bool validateURL(folly::ByteRange url)
Definition: CodecUtil.h:29
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
static const char http_tokens[256]
Definition: CodecUtil.h:27
static const char *const value
Definition: Conv.cpp:50
const char * string
Definition: Conv.cpp:212
static bool appendHeaders(const HTTPHeaders &inputHeaders, std::vector< compress::Header > &headers, HTTPHeaderCode headerToCheck)
Definition: CodecUtil.cpp:131
static bool validateHeaderValue(folly::ByteRange value, CtlEscapeMode mode)
Definition: CodecUtil.h:66
state
Definition: http_parser.c:272
static bool hasGzipAndDeflate(const std::string &value, bool &hasGzip, bool &hasDeflate)
Definition: CodecUtil.cpp:61