proxygen
proxygen::StructuredHeaders Namespace Reference

Classes

struct  ParameterisedIdentifier
 
class  StructuredHeaderItem
 
class  StructuredHeadersUtilitiesTest
 

Typedefs

using ParameterisedList = std::vector< StructuredHeaders::ParameterisedIdentifier >
 
using Dictionary = std::unordered_map< std::string, StructuredHeaderItem >
 

Enumerations

enum  DecodeError : uint8_t {
  DecodeError::OK = 0, DecodeError::VALUE_TOO_LONG = 1, DecodeError::INVALID_CHARACTER = 2, DecodeError::UNDECODEABLE_BINARY_CONTENT = 3,
  DecodeError::UNEXPECTED_END_OF_BUFFER = 4, DecodeError::UNPARSEABLE_NUMERIC_TYPE = 5, DecodeError::DUPLICATE_KEY = 6
}
 
enum  EncodeError : uint8_t {
  EncodeError::OK = 0, EncodeError::EMPTY_DATA_STRUCTURE = 1, EncodeError::BAD_IDENTIFIER = 2, EncodeError::BAD_STRING = 3,
  EncodeError::ITEM_TYPE_MISMATCH = 4, EncodeError::ENCODING_NULL_ITEM = 5
}
 

Functions

bool isLcAlpha (char c)
 
bool isValidIdentifierChar (char c)
 
bool isValidEncodedBinaryContentChar (char c)
 
bool isValidStringChar (char c)
 
bool isValidIdentifier (const std::string &s)
 
bool isValidString (const std::string &s)
 
bool isValidEncodedBinaryContent (const std::string &s)
 
bool itemTypeMatchesContent (const StructuredHeaderItem &input)
 
std::string decodeBase64 (const std::string &encoded)
 
std::string encodeBase64 (const std::string &input)
 
 TEST_F (StructuredHeadersUtilitiesTest, TestLcalpha)
 
 TEST_F (StructuredHeadersUtilitiesTest, TestIsValidIdentifierChar)
 
 TEST_F (StructuredHeadersUtilitiesTest, test_isValidEncodedBinaryContentChar_alphanumeric)
 
 TEST_F (StructuredHeadersUtilitiesTest, test_isValidEncodedBinaryContentChar_allowed_symbols)
 
 TEST_F (StructuredHeadersUtilitiesTest, test_isValidEncodedBinaryContentChar_disallowed_symbols)
 
 TEST_F (StructuredHeadersUtilitiesTest, TestIsValidStringCharAllowed)
 
 TEST_F (StructuredHeadersUtilitiesTest, TestIsValidStringCharDisallowed)
 
 TEST_F (StructuredHeadersUtilitiesTest, TestIsValidIdentifierAllowed)
 
 TEST_F (StructuredHeadersUtilitiesTest, TestIsValidIdentifierDisallowed)
 
 TEST_F (StructuredHeadersUtilitiesTest, TestIsValidStringAllowed)
 
 TEST_F (StructuredHeadersUtilitiesTest, TestIsValidStringDisallowed)
 
 TEST_F (StructuredHeadersUtilitiesTest, TestGoodBinaryContent)
 
 TEST_F (StructuredHeadersUtilitiesTest, TestBadBinaryContent)
 
 TEST_F (StructuredHeadersUtilitiesTest, Test_DecodeBinaryContent)
 
 TEST_F (StructuredHeadersUtilitiesTest, Test_EncodeBinaryContent)
 
 TEST_F (StructuredHeadersUtilitiesTest, Test_BinaryContentEmpty)
 
 TEST_F (StructuredHeadersUtilitiesTest, TestItemTypeMatchesContentGood)
 
 TEST_F (StructuredHeadersUtilitiesTest, TestItemTypeMatchesContentBad)
 

Variables

static const int kMaxValidIntegerLength = 19
 
static const int kMaxValidFloatLength = 16
 
static const std::map< DecodeError, std::stringdecodeErrorDescription
 
static const std::map< EncodeError, std::stringencodeErrorDescription
 

Typedef Documentation

Definition at line 61 of file StructuredHeadersConstants.h.

Enumeration Type Documentation

Function Documentation

std::string proxygen::StructuredHeaders::decodeBase64 ( const std::string encoded)

Definition at line 105 of file StructuredHeadersUtilities.cpp.

References folly::test::begin(), count, folly::test::end(), string, and uint32_t.

Referenced by proxygen::StructuredHeadersBuffer::parseBinaryContent(), and TEST_F().

106  {
107 
108  if (encoded.size() == 0) {
109  // special case, to prevent an integer overflow down below.
110  return "";
111  }
112 
113  using namespace boost::archive::iterators;
114  using b64it =
115  transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
116 
117  std::string decoded = std::string(b64it(std::begin(encoded)),
118  b64it(std::end(encoded)));
119 
120  uint32_t numPadding = std::count(encoded.begin(), encoded.end(), '=');
121  decoded.erase(decoded.end() - numPadding, decoded.end());
122 
123  return decoded;
124 }
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
int * count
const char * string
Definition: Conv.cpp:212
std::string proxygen::StructuredHeaders::encodeBase64 ( const std::string input)

Definition at line 126 of file StructuredHeadersUtilities.cpp.

References data, and string.

Referenced by proxygen::StructuredHeadersEncoder::encodeBinaryContent(), proxygen::StructuredHeadersBuffer::parseBinaryContent(), and TEST_F().

126  {
127  using namespace boost::archive::iterators;
128  using b64it = base64_from_binary<transform_width<const char*, 6, 8>>;
129 
130  auto data = input.data();
131  std::string encoded(b64it(data), b64it(data + (input.length())));
132  encoded.append((3 - (input.length() % 3)) % 3, '=');
133 
134  return encoded;
135 }
const char * string
Definition: Conv.cpp:212
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
bool proxygen::StructuredHeaders::isLcAlpha ( char  c)

Definition at line 20 of file StructuredHeadersUtilities.cpp.

Referenced by isValidIdentifier(), isValidIdentifierChar(), proxygen::StructuredHeadersBuffer::parseIdentifier(), and TEST_F().

20  {
21  return c >= 0x61 && c <= 0x7A;
22 }
char c
bool proxygen::StructuredHeaders::isValidEncodedBinaryContent ( const std::string s)

Definition at line 68 of file StructuredHeadersUtilities.cpp.

References isValidEncodedBinaryContentChar().

Referenced by proxygen::StructuredHeadersBuffer::parseBinaryContent(), and TEST_F().

69  {
70 
71  if (s.size() % 4 != 0) {
72  return false;
73  }
74 
75  bool equalSeen = false;
76  for (auto it = s.begin(); it != s.end(); it++) {
77  if (*it == '=') {
78  equalSeen = true;
79  } else if (equalSeen || !isValidEncodedBinaryContentChar(*it)) {
80  return false;
81  }
82  }
83 
84  return true;
85 }
static set< string > s
bool proxygen::StructuredHeaders::isValidEncodedBinaryContentChar ( char  c)

Definition at line 29 of file StructuredHeadersUtilities.cpp.

Referenced by isValidEncodedBinaryContent(), proxygen::StructuredHeadersBuffer::parseBinaryContent(), and TEST_F().

30  {
31  return std::isalpha(c) || std::isdigit(c) || c == '+' || c == '/' || c == '=';
32 }
char c
bool proxygen::StructuredHeaders::isValidIdentifier ( const std::string s)

Definition at line 45 of file StructuredHeadersUtilities.cpp.

References c, isLcAlpha(), and isValidIdentifierChar().

Referenced by proxygen::StructuredHeadersEncoder::encodeIdentifier(), and TEST_F().

45  {
46  if (s.size() == 0 || !isLcAlpha(s[0])) {
47  return false;
48  }
49 
50  for (char c : s) {
51  if (!isValidIdentifierChar(c)) {
52  return false;
53  }
54  }
55 
56  return true;
57 }
static set< string > s
char c
bool proxygen::StructuredHeaders::isValidIdentifierChar ( char  c)

Definition at line 24 of file StructuredHeadersUtilities.cpp.

References isLcAlpha().

Referenced by isValidIdentifier(), proxygen::StructuredHeadersBuffer::parseIdentifier(), and TEST_F().

24  {
25  return isLcAlpha(c) || std::isdigit(c) || c == '_' || c == '-' || c == '*' ||
26  c == '/';
27 }
char c
bool proxygen::StructuredHeaders::isValidString ( const std::string s)

Definition at line 59 of file StructuredHeadersUtilities.cpp.

References c, and isValidStringChar().

Referenced by proxygen::StructuredHeadersEncoder::encodeString(), and TEST_F().

59  {
60  for (char c : s) {
61  if (!isValidStringChar(c)) {
62  return false;
63  }
64  }
65  return true;
66 }
static set< string > s
char c
bool proxygen::StructuredHeaders::isValidStringChar ( char  c)

Definition at line 34 of file StructuredHeadersUtilities.cpp.

Referenced by isValidString(), proxygen::StructuredHeadersBuffer::parseString(), and TEST_F().

34  {
35  /*
36  * The difference between the character restriction here and that mentioned
37  * in section 3.7 of version 6 of the Structured Headers draft is that this
38  * function accepts \ and DQUOTE characters. These characters are allowed
39  * as long as they are present as a part of an escape sequence, which is
40  * checked for in the parseString() function in the StructuredHeadersBuffer.
41  */
42  return c >= 0x20 && c <= 0x7E;
43 }
char c
bool proxygen::StructuredHeaders::itemTypeMatchesContent ( const StructuredHeaderItem input)

Definition at line 87 of file StructuredHeadersUtilities.cpp.

References proxygen::StructuredHeaders::StructuredHeaderItem::BINARYCONTENT, proxygen::StructuredHeaders::StructuredHeaderItem::DOUBLE, proxygen::StructuredHeaders::StructuredHeaderItem::IDENTIFIER, proxygen::StructuredHeaders::StructuredHeaderItem::INT64, int64_t, proxygen::StructuredHeaders::StructuredHeaderItem::NONE, proxygen::StructuredHeaders::StructuredHeaderItem::STRING, string, proxygen::StructuredHeaders::StructuredHeaderItem::tag, and proxygen::StructuredHeaders::StructuredHeaderItem::value.

Referenced by proxygen::StructuredHeadersEncoder::encodeItem(), and TEST_F().

88  {
89  switch (input.tag) {
90  case StructuredHeaderItem::Type::BINARYCONTENT:
91  case StructuredHeaderItem::Type::IDENTIFIER:
93  return input.value.type() == typeid(std::string);
94  case StructuredHeaderItem::Type::INT64:
95  return input.value.type() == typeid(int64_t);
96  case StructuredHeaderItem::Type::DOUBLE:
97  return input.value.type() == typeid(double);
98  case StructuredHeaderItem::Type::NONE:
99  return true;
100  }
101 
102  return false;
103 }
#define STRING
const char * string
Definition: Conv.cpp:212
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
TestLcalpha   
)

Definition at line 20 of file StructuredHeadersUtilitiesTest.cpp.

References c, EXPECT_FALSE, EXPECT_TRUE, i, isLcAlpha(), uint32_t, and uint8_t.

20  {
21  for (uint32_t i = 0; i < 256; i++) {
22  uint8_t c = (uint8_t) i;
23  if (c >= 'a' && c <= 'z') {
25  } else {
27  }
28  }
29 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
char c
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
TestIsValidIdentifierChar   
)

Definition at line 31 of file StructuredHeadersUtilitiesTest.cpp.

References c, EXPECT_FALSE, EXPECT_TRUE, i, isValidIdentifierChar(), uint32_t, and uint8_t.

31  {
32  for (uint32_t i = 0; i < 256; i++) {
33  uint8_t c = (uint8_t) i;
34  if ((c >= 'a' && c <= 'z') ||
35  (c >= '0' && c <= '9') ||
36  (c == '_' || c == '-' || c == '*' || c == '/')) {
38  } else {
40  }
41  }
42 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
char c
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
test_isValidEncodedBinaryContentChar_alphanumeric   
)
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
test_isValidEncodedBinaryContentChar_allowed_symbols   
)
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
test_isValidEncodedBinaryContentChar_disallowed_symbols   
)
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
TestIsValidStringCharAllowed   
)

Definition at line 67 of file StructuredHeadersUtilitiesTest.cpp.

References EXPECT_TRUE, and isValidStringChar().

67  {
75 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
TestIsValidStringCharDisallowed   
)

Definition at line 77 of file StructuredHeadersUtilitiesTest.cpp.

References EXPECT_FALSE, and isValidStringChar().

77  {
82 }
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
TestIsValidIdentifierAllowed   
)

Definition at line 84 of file StructuredHeadersUtilitiesTest.cpp.

References EXPECT_TRUE, and isValidIdentifier().

84  {
86  EXPECT_TRUE(isValidIdentifier("a_0-*/"));
87  EXPECT_TRUE(isValidIdentifier("abc___xyz"));
88 }
bool isValidIdentifier(const std::string &s)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
TestIsValidIdentifierDisallowed   
)

Definition at line 90 of file StructuredHeadersUtilitiesTest.cpp.

References EXPECT_FALSE, and isValidIdentifier().

90  {
95 }
bool isValidIdentifier(const std::string &s)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
TestIsValidStringAllowed   
)

Definition at line 97 of file StructuredHeadersUtilitiesTest.cpp.

References EXPECT_TRUE, and isValidString().

97  {
98  EXPECT_TRUE(isValidString("a cat."));
99  EXPECT_TRUE(isValidString("!~)($@^^) g"));
100  EXPECT_TRUE(isValidString("\\\"\"\\"));
102 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool isValidString(const std::string &s)
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
TestIsValidStringDisallowed   
)

Definition at line 104 of file StructuredHeadersUtilitiesTest.cpp.

References EXPECT_FALSE, and isValidString().

104  {
105  EXPECT_FALSE(isValidString("a\tcat."));
106  EXPECT_FALSE(isValidString("\x10 aaaaaaa"));
107  EXPECT_FALSE(isValidString("chocolate\x11"));
108  EXPECT_FALSE(isValidString("pota\nto"));
109 }
bool isValidString(const std::string &s)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
TestGoodBinaryContent   
)

Definition at line 111 of file StructuredHeadersUtilitiesTest.cpp.

References EXPECT_TRUE, and isValidEncodedBinaryContent().

111  {
113  EXPECT_TRUE(isValidEncodedBinaryContent("ZGZzZGZmc2Rm"));
115 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool isValidEncodedBinaryContent(const std::string &s)
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
TestBadBinaryContent   
)

Definition at line 117 of file StructuredHeadersUtilitiesTest.cpp.

References EXPECT_FALSE, and isValidEncodedBinaryContent().

117  {
122 }
bool isValidEncodedBinaryContent(const std::string &s)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
Test_DecodeBinaryContent   
)

Definition at line 124 of file StructuredHeadersUtilitiesTest.cpp.

References decodeBase64(), EXPECT_EQ, and string.

124  {
125  std::string input1 = "ZnJ1aXQ=";
126  std::string input2 = "dG9tYXRv";
127  std::string input3 = "ZWdncw==";
128  EXPECT_EQ(decodeBase64(input1), "fruit");
129  EXPECT_EQ(decodeBase64(input2), "tomato");
130  EXPECT_EQ(decodeBase64(input3), "eggs");
131 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string decodeBase64(const std::string &encoded)
const char * string
Definition: Conv.cpp:212
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
Test_EncodeBinaryContent   
)

Definition at line 133 of file StructuredHeadersUtilitiesTest.cpp.

References encodeBase64(), EXPECT_EQ, and string.

133  {
134  std::string input1 = "fruit";
135  std::string input2 = "tomato";
136  std::string input3 = "eggs";
137  EXPECT_EQ(encodeBase64(input1), "ZnJ1aXQ=");
138  EXPECT_EQ(encodeBase64(input2), "dG9tYXRv");
139  EXPECT_EQ(encodeBase64(input3), "ZWdncw==");
140 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string encodeBase64(const std::string &input)
const char * string
Definition: Conv.cpp:212
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
Test_BinaryContentEmpty   
)

Definition at line 142 of file StructuredHeadersUtilitiesTest.cpp.

References decodeBase64(), encodeBase64(), EXPECT_EQ, and string.

142  {
143  std::string input1 = "";
144  std::string input2 = "";
145  EXPECT_EQ(encodeBase64(input1), "");
146  EXPECT_EQ(decodeBase64(input2), "");
147 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string encodeBase64(const std::string &input)
std::string decodeBase64(const std::string &encoded)
const char * string
Definition: Conv.cpp:212
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
TestItemTypeMatchesContentGood   
)

Definition at line 149 of file StructuredHeadersUtilitiesTest.cpp.

References proxygen::StructuredHeaders::StructuredHeaderItem::BINARYCONTENT, proxygen::StructuredHeaders::StructuredHeaderItem::DOUBLE, EXPECT_TRUE, proxygen::StructuredHeaders::StructuredHeaderItem::IDENTIFIER, proxygen::StructuredHeaders::StructuredHeaderItem::INT64, int64_t, itemTypeMatchesContent(), proxygen::StructuredHeaders::StructuredHeaderItem::NONE, proxygen::StructuredHeaders::StructuredHeaderItem::STRING, proxygen::StructuredHeaders::StructuredHeaderItem::tag, and proxygen::StructuredHeaders::StructuredHeaderItem::value.

149  {
150  StructuredHeaderItem item;
151  item.value = "\"potato\"";
154 
155  item.value = "a_800";
156  item.tag = StructuredHeaderItem::Type::IDENTIFIER;
158 
159  item.tag = StructuredHeaderItem::Type::NONE;
161 
162  item.value = "hello";
163  item.tag = StructuredHeaderItem::Type::BINARYCONTENT;
165 
166  item.value = int64_t(88);
167  item.tag = StructuredHeaderItem::Type::INT64;
169 
170  item.value = double(88.8);
171  item.tag = StructuredHeaderItem::Type::DOUBLE;
173 }
bool itemTypeMatchesContent(const StructuredHeaderItem &input)
#define STRING
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
proxygen::StructuredHeaders::TEST_F ( StructuredHeadersUtilitiesTest  ,
TestItemTypeMatchesContentBad   
)

Definition at line 175 of file StructuredHeadersUtilitiesTest.cpp.

References proxygen::StructuredHeaders::StructuredHeaderItem::BINARYCONTENT, proxygen::StructuredHeaders::StructuredHeaderItem::DOUBLE, EXPECT_FALSE, proxygen::StructuredHeaders::StructuredHeaderItem::IDENTIFIER, proxygen::StructuredHeaders::StructuredHeaderItem::INT64, int64_t, itemTypeMatchesContent(), proxygen::StructuredHeaders::StructuredHeaderItem::STRING, proxygen::StructuredHeaders::StructuredHeaderItem::tag, and proxygen::StructuredHeaders::StructuredHeaderItem::value.

175  {
176  StructuredHeaderItem item;
177 
178  item.value = "hello";
179  item.tag = StructuredHeaderItem::Type::DOUBLE;
181  item.tag = StructuredHeaderItem::Type::INT64;
183 
184  item.value = int64_t(68);
185  item.tag = StructuredHeaderItem::Type::DOUBLE;
189  item.tag = StructuredHeaderItem::Type::BINARYCONTENT;
191  item.tag = StructuredHeaderItem::Type::IDENTIFIER;
193 
194  item.value = double(68.8);
195  item.tag = StructuredHeaderItem::Type::INT64;
197  item.tag = StructuredHeaderItem::Type::IDENTIFIER;
201  item.tag = StructuredHeaderItem::Type::BINARYCONTENT;
203 }
bool itemTypeMatchesContent(const StructuredHeaderItem &input)
#define STRING
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862

Variable Documentation

const std::map<DecodeError, std::string> proxygen::StructuredHeaders::decodeErrorDescription
static
Initial value:
{
{DecodeError::OK, "No error"},
{DecodeError::VALUE_TOO_LONG, "Numeric value is too long"},
{DecodeError::INVALID_CHARACTER, "Invalid character"},
{DecodeError::UNDECODEABLE_BINARY_CONTENT, "Undecodable binary content"},
{DecodeError::UNEXPECTED_END_OF_BUFFER, "Unexpected end of buffer"},
{DecodeError::UNPARSEABLE_NUMERIC_TYPE, "Unparseable numeric type"},
{DecodeError::DUPLICATE_KEY, "Duplicate key found"}
}

Definition at line 83 of file StructuredHeadersConstants.h.

Referenced by proxygen::StructuredHeadersBuffer::handleDecodeError().

const std::map<EncodeError, std::string> proxygen::StructuredHeaders::encodeErrorDescription
static
Initial value:
{
{EncodeError::OK, "No error"},
{EncodeError::EMPTY_DATA_STRUCTURE, "Empty data structure"},
{EncodeError::BAD_IDENTIFIER, "Bad identifier"},
{EncodeError::BAD_STRING, "Bad string"},
{EncodeError::ITEM_TYPE_MISMATCH, "Item type mismatch"},
{EncodeError::ENCODING_NULL_ITEM, "Tried to encode null item"}
}

Definition at line 94 of file StructuredHeadersConstants.h.

Referenced by proxygen::StructuredHeadersEncoder::handleEncodeError().

const int proxygen::StructuredHeaders::kMaxValidFloatLength = 16
static
const int proxygen::StructuredHeaders::kMaxValidIntegerLength = 19
static