proxygen
StructuredHeadersDecoder.cpp
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 
12 
13 namespace proxygen {
14 
15 using namespace StructuredHeaders;
16 
18  StructuredHeaderItem& result) {
19  auto err = buf_.parseItem(result);
20  if (err != DecodeError::OK) {
21  return err;
22  }
23  return buf_.isEmpty() ?
24  DecodeError::OK : buf_.handleDecodeError(DecodeError::INVALID_CHARACTER);
25 }
26 
28  std::vector<StructuredHeaderItem>& result) {
29 
30  while (!buf_.isEmpty()) {
31 
33  auto err = buf_.parseItem(item);
34  if (err != DecodeError::OK) {
35  return err;
36  }
37 
38  result.push_back(item);
39 
40  buf_.removeOptionalWhitespace();
41 
42  if (buf_.isEmpty()) {
43  return DecodeError::OK;
44  }
45 
46  err = buf_.removeSymbol(",", true);
47  if (err != DecodeError::OK) {
48  return err;
49  }
50 
51  buf_.removeOptionalWhitespace();
52 
53  if (buf_.isEmpty()) {
54  return buf_.handleDecodeError(DecodeError::UNEXPECTED_END_OF_BUFFER);
55  }
56  }
57 
58  return buf_.handleDecodeError(DecodeError::UNEXPECTED_END_OF_BUFFER);
59 }
60 
62  return decodeMap(result, MapType::DICTIONARY);
63 }
64 
66  ParameterisedList& result) {
67 
68  while (!buf_.isEmpty()) {
69 
70  ParameterisedIdentifier primaryIdentifier;
71 
72  auto err = buf_.parseIdentifier(primaryIdentifier.identifier);
73  if (err != DecodeError::OK) {
74  return err;
75  }
76 
77  buf_.removeOptionalWhitespace();
78 
79  err = decodeMap(primaryIdentifier.parameterMap, MapType::PARAMETERISED_MAP);
80  if (err != DecodeError::OK) {
81  return err;
82  }
83 
84  result.emplace_back(primaryIdentifier);
85 
86  buf_.removeOptionalWhitespace();
87 
88  if (buf_.isEmpty()) {
89  return DecodeError::OK;
90  }
91 
92  if (buf_.removeSymbol(",", true) != DecodeError::OK) {
93  return err;
94  }
95 
96  buf_.removeOptionalWhitespace();
97 
98  }
99 
100  return buf_.handleDecodeError(DecodeError::UNEXPECTED_END_OF_BUFFER);
101 }
102 
104  std::unordered_map<std::string, StructuredHeaderItem>& result,
105  MapType mapType) {
106 
107  std::string delimiter = (mapType == MapType::PARAMETERISED_MAP) ? ";" : ",";
108 
109  buf_.removeOptionalWhitespace();
110 
111  if ((mapType == MapType::PARAMETERISED_MAP) &&
112  (buf_.removeSymbol(delimiter, false) != DecodeError::OK)) {
113  return DecodeError::OK;
114  }
115 
116  while (!buf_.isEmpty()) {
117 
118  buf_.removeOptionalWhitespace();
119 
120  std::string thisKey;
121  auto err = buf_.parseIdentifier(thisKey);
122  if (err != DecodeError::OK) {
123  return err;
124  }
125 
126  if (result.find(thisKey) != result.end()) {
127  return buf_.handleDecodeError(DecodeError::DUPLICATE_KEY);
128  }
129 
130  err = buf_.removeSymbol("=", mapType == MapType::DICTIONARY);
131  if (err != DecodeError::OK) {
132  if (mapType == MapType::DICTIONARY) {
133  return err;
134  } else {
137  result[thisKey] = value;
138  }
139  } else {
141  err = buf_.parseItem(value);
142  if (err != DecodeError::OK) {
143  return err;
144  }
145 
146  result[thisKey] = value;
147  }
148 
149  buf_.removeOptionalWhitespace();
150 
151  if (buf_.isEmpty()) {
152  return DecodeError::OK;
153  }
154 
155  err = buf_.removeSymbol(delimiter, mapType == MapType::DICTIONARY);
156  if (err != DecodeError::OK) {
157  if (mapType == MapType::PARAMETERISED_MAP) {
158  return DecodeError::OK;
159  } else {
160  return err;
161  }
162  }
163  }
164 
165  return buf_.handleDecodeError(DecodeError::UNEXPECTED_END_OF_BUFFER);
166 
167 }
168 
169 }
StructuredHeaders::DecodeError decodeMap(std::unordered_map< std::string, StructuredHeaderItem > &result, MapType mapType)
std::vector< StructuredHeaders::ParameterisedIdentifier > ParameterisedList
std::unordered_map< std::string, StructuredHeaderItem > parameterMap
StructuredHeaders::DecodeError decodeParameterisedList(ParameterisedList &result)
StructuredHeaders::DecodeError decodeDictionary(Dictionary &result)
std::unordered_map< std::string, StructuredHeaderItem > Dictionary
static const char *const value
Definition: Conv.cpp:50
StructuredHeaders::DecodeError decodeList(std::vector< StructuredHeaderItem > &result)
const char * string
Definition: Conv.cpp:212
StructuredHeaders::DecodeError decodeItem(StructuredHeaderItem &result)