proxygen
proxygen::StructuredHeadersEncoder Class Reference

#include <StructuredHeadersEncoder.h>

Public Member Functions

 StructuredHeadersEncoder ()
 
EncodeError encodeParameterisedList (const ParameterisedList &input)
 
EncodeError encodeDictionary (const Dictionary &input)
 
EncodeError encodeList (const std::vector< StructuredHeaderItem > &input)
 
EncodeError encodeItem (const StructuredHeaderItem &input)
 
EncodeError encodeIdentifier (const std::string &input)
 
std::string get ()
 

Private Member Functions

EncodeError encodeBinaryContent (const std::string &input)
 
EncodeError encodeString (const std::string &input)
 
EncodeError encodeInteger (int64_t input)
 
EncodeError encodeFloat (double input)
 
EncodeError handleEncodeError (EncodeError err, const std::string &badContent)
 
EncodeError handleEncodeError (EncodeError err)
 

Private Attributes

std::string output_
 
string_buf buf_
 
std::ostream outputStream_
 

Detailed Description

Definition at line 28 of file StructuredHeadersEncoder.h.

Constructor & Destructor Documentation

proxygen::StructuredHeadersEncoder::StructuredHeadersEncoder ( )

Member Function Documentation

EncodeError proxygen::StructuredHeadersEncoder::encodeBinaryContent ( const std::string input)
private

Definition at line 142 of file StructuredHeadersEncoder.cpp.

References proxygen::StructuredHeaders::encodeBase64(), and outputStream_.

Referenced by encodeItem().

143  {
144 
145  outputStream_ << "*";
146  outputStream_ << encodeBase64(input);
147  outputStream_ << "*";
148 
149  return EncodeError::OK;
150 }
std::string encodeBase64(const std::string &input)
EncodeError proxygen::StructuredHeadersEncoder::encodeDictionary ( const Dictionary input)

Definition at line 43 of file StructuredHeadersEncoder.cpp.

References proxygen::StructuredHeaders::EMPTY_DATA_STRUCTURE, cpp.ast::next(), and proxygen::StructuredHeaders::OK.

Referenced by proxygen::TEST_F().

44  {
45 
46  if (input.empty()) {
47  return handleEncodeError(EncodeError::EMPTY_DATA_STRUCTURE);
48  }
49 
50  for (auto it = input.begin(); it != input.end(); it++) {
51  auto err = encodeIdentifier(it->first);
52  if (err != EncodeError::OK) {
53  return err;
54  }
55 
56  outputStream_ << "=";
57 
58  err = encodeItem(it->second);
59  if (err != EncodeError::OK) {
60  return err;
61  }
62 
63  if (std::next(it, 1) != input.end()) {
64  outputStream_ << ", ";
65  }
66  }
67 
68  return EncodeError::OK;
69 }
EncodeError encodeItem(const StructuredHeaderItem &input)
EncodeError encodeIdentifier(const std::string &input)
EncodeError handleEncodeError(EncodeError err, const std::string &badContent)
def next(obj)
Definition: ast.py:58
EncodeError proxygen::StructuredHeadersEncoder::encodeFloat ( double  input)
private

Definition at line 178 of file StructuredHeadersEncoder.cpp.

References outputStream_.

Referenced by encodeItem().

178  {
179 
180  outputStream_ << input;
181 
182  return EncodeError::OK;
183 }
EncodeError proxygen::StructuredHeadersEncoder::encodeIdentifier ( const std::string input)

Definition at line 185 of file StructuredHeadersEncoder.cpp.

References handleEncodeError(), proxygen::StructuredHeaders::isValidIdentifier(), and outputStream_.

Referenced by proxygen::TEST_F().

186  {
187 
188  if (!isValidIdentifier(input)) {
189  return handleEncodeError(EncodeError::BAD_IDENTIFIER, input);
190  }
191  outputStream_ << input;
192  return EncodeError::OK;
193 }
bool isValidIdentifier(const std::string &s)
EncodeError handleEncodeError(EncodeError err, const std::string &badContent)
EncodeError proxygen::StructuredHeadersEncoder::encodeInteger ( int64_t  input)
private

Definition at line 171 of file StructuredHeadersEncoder.cpp.

References outputStream_.

Referenced by encodeItem().

171  {
172 
173  outputStream_ << input;
174 
175  return EncodeError::OK;
176 }
EncodeError proxygen::StructuredHeadersEncoder::encodeItem ( const StructuredHeaderItem input)

Definition at line 118 of file StructuredHeadersEncoder.cpp.

References encodeBinaryContent(), encodeFloat(), encodeInteger(), encodeString(), handleEncodeError(), proxygen::StructuredHeaders::itemTypeMatchesContent(), STRING, proxygen::StructuredHeaders::StructuredHeaderItem::tag, and proxygen::StructuredHeaders::StructuredHeaderItem::value.

Referenced by proxygen::TEST_F().

119  {
120 
121  if (!itemTypeMatchesContent(input)) {
122  return handleEncodeError(EncodeError::ITEM_TYPE_MISMATCH);
123  }
124 
125  switch (input.tag) {
127  return encodeString(boost::get<std::string>(input.value));
128  case StructuredHeaderItem::Type::INT64:
129  return encodeInteger(boost::get<int64_t>(input.value));
130  case StructuredHeaderItem::Type::DOUBLE:
131  return encodeFloat(boost::get<double>(input.value));
132  case StructuredHeaderItem::Type::BINARYCONTENT:
133  return encodeBinaryContent(
134  boost::get<std::string>(input.value));
135  default:
136  return handleEncodeError(EncodeError::ENCODING_NULL_ITEM);
137  }
138 
139  return EncodeError::OK;
140 }
bool itemTypeMatchesContent(const StructuredHeaderItem &input)
#define STRING
EncodeError handleEncodeError(EncodeError err, const std::string &badContent)
EncodeError encodeString(const std::string &input)
EncodeError encodeBinaryContent(const std::string &input)
EncodeError proxygen::StructuredHeadersEncoder::encodeList ( const std::vector< StructuredHeaderItem > &  input)

Definition at line 22 of file StructuredHeadersEncoder.cpp.

References proxygen::StructuredHeaders::EMPTY_DATA_STRUCTURE, cpp.ast::next(), and proxygen::StructuredHeaders::OK.

Referenced by proxygen::TEST_F().

23  {
24 
25  if (input.empty()) {
26  return handleEncodeError(EncodeError::EMPTY_DATA_STRUCTURE);
27  }
28 
29  for (auto it = input.begin(); it != input.end(); it++) {
30  auto err = encodeItem(*it);
31  if (err != EncodeError::OK) {
32  return err;
33  }
34 
35  if (std::next(it, 1)!= input.end()) {
36  outputStream_ << ", ";
37  }
38  }
39 
40  return EncodeError::OK;
41 }
EncodeError encodeItem(const StructuredHeaderItem &input)
EncodeError handleEncodeError(EncodeError err, const std::string &badContent)
def next(obj)
Definition: ast.py:58
EncodeError proxygen::StructuredHeadersEncoder::encodeParameterisedList ( const ParameterisedList input)

Definition at line 71 of file StructuredHeadersEncoder.cpp.

References proxygen::StructuredHeaders::EMPTY_DATA_STRUCTURE, cpp.ast::next(), proxygen::StructuredHeaders::StructuredHeaderItem::NONE, and proxygen::StructuredHeaders::OK.

Referenced by proxygen::TEST_F().

72  {
73 
74  if (input.empty()) {
75  return handleEncodeError(EncodeError::EMPTY_DATA_STRUCTURE);
76  }
77 
78  for (auto it1 = input.begin(); it1 != input.end(); it1++) {
79  auto err = encodeIdentifier(it1->identifier);
80  if (err != EncodeError::OK) {
81  return err;
82  }
83 
84  for (auto it2 = it1->parameterMap.begin();
85  it2 != it1->parameterMap.end(); it2++) {
86 
87  outputStream_ << "; ";
88 
89  err = encodeIdentifier(it2->first);
90  if (err != EncodeError::OK) {
91  return err;
92  }
93 
94  if (it2->second.tag != StructuredHeaderItem::Type::NONE) {
95  outputStream_ << "=";
96  err = encodeItem(it2->second);
97  if (err != EncodeError::OK) {
98  return err;
99  }
100  }
101  }
102 
103  if (std::next(it1, 1) != input.end()) {
104  outputStream_ << ", ";
105  }
106  }
107 
108  return EncodeError::OK;
109 }
EncodeError encodeItem(const StructuredHeaderItem &input)
EncodeError encodeIdentifier(const std::string &input)
EncodeError handleEncodeError(EncodeError err, const std::string &badContent)
def next(obj)
Definition: ast.py:58
EncodeError proxygen::StructuredHeadersEncoder::encodeString ( const std::string input)
private

Definition at line 152 of file StructuredHeadersEncoder.cpp.

References c, handleEncodeError(), proxygen::StructuredHeaders::isValidString(), and outputStream_.

Referenced by encodeItem().

152  {
153 
154  if (!isValidString(input)) {
155  return handleEncodeError(EncodeError::BAD_STRING, input);
156  }
157 
158  outputStream_ << "\"";
159  for (char c : input) {
160  if (c == '"' || c == '\\') {
161  outputStream_ << "\\";
162  }
163  outputStream_ << c;
164  }
165 
166  outputStream_ << "\"";
167 
168  return EncodeError::OK;
169 }
EncodeError handleEncodeError(EncodeError err, const std::string &badContent)
bool isValidString(const std::string &s)
char c
std::string proxygen::StructuredHeadersEncoder::get ( )

Definition at line 212 of file StructuredHeadersEncoder.cpp.

References folly::gen::move, output_, and outputStream_.

Referenced by proxygen::TEST_F().

212  {
213  outputStream_.flush();
214  return std::move(output_);
215 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
EncodeError proxygen::StructuredHeadersEncoder::handleEncodeError ( EncodeError  err,
const std::string badContent 
)
private

Definition at line 197 of file StructuredHeadersEncoder.cpp.

References proxygen::StructuredHeaders::encodeErrorDescription, and proxygen::ERROR.

Referenced by encodeIdentifier(), encodeItem(), and encodeString().

198  {
199 
200  LOG_EVERY_N(ERROR, 1000) << "Error message: " <<
201  encodeErrorDescription.at(err) << " .The culprit was: " << culprit;
202  return err;
203 }
static const std::map< EncodeError, std::string > encodeErrorDescription
EncodeError proxygen::StructuredHeadersEncoder::handleEncodeError ( EncodeError  err)
private

Definition at line 206 of file StructuredHeadersEncoder.cpp.

References proxygen::StructuredHeaders::encodeErrorDescription, and proxygen::ERROR.

206  {
207  LOG_EVERY_N(ERROR, 1000) << "Error message: " <<
208  encodeErrorDescription.at(err);
209  return err;
210 }
static const std::map< EncodeError, std::string > encodeErrorDescription

Member Data Documentation

string_buf proxygen::StructuredHeadersEncoder::buf_
private

Definition at line 62 of file StructuredHeadersEncoder.h.

std::string proxygen::StructuredHeadersEncoder::output_
private

Definition at line 61 of file StructuredHeadersEncoder.h.

Referenced by get().

std::ostream proxygen::StructuredHeadersEncoder::outputStream_
private

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