28 #ifndef HTTP_PARSER_HPP
29 #define HTTP_PARSER_HPP
35 #include <websocketpp/utilities.hpp>
36 #include <websocketpp/http/constants.hpp>
51 typedef std::map<std::string, std::string, utility::ci_less > header_list;
63 template <
typename InputIterator>
64 std::pair<std::string,InputIterator> extract_token(InputIterator begin,
68 return std::make_pair(std::string(begin,it),it);
82 template <
typename InputIterator>
83 std::pair<std::string,InputIterator> extract_quoted_string(InputIterator begin,
89 return std::make_pair(s,begin);
93 return std::make_pair(s,begin);
96 InputIterator cursor = begin+1;
97 InputIterator marker = cursor;
99 cursor = std::find(cursor,end,
'"');
101 while (cursor != end) {
103 if (*(cursor-1) ==
'\\') {
104 s.append(marker,cursor-1);
109 s.append(marker,cursor);
111 return std::make_pair(s,cursor);
114 cursor = std::find(cursor,end,
'"');
117 return std::make_pair(
"",begin);
129 template <
typename InputIterator>
130 InputIterator extract_lws(InputIterator begin, InputIterator end) {
131 InputIterator it = begin;
134 if (end-begin > 2 && *begin ==
'\r' && *(begin+1) ==
'\n' &&
154 template <
typename InputIterator>
155 InputIterator extract_all_lws(InputIterator begin, InputIterator end) {
156 InputIterator old_it;
157 InputIterator new_it = begin;
164 new_it = extract_lws(old_it,end);
165 }
while (new_it != end && old_it != new_it);
185 template <
typename InputIterator>
186 InputIterator extract_attributes(InputIterator begin, InputIterator end,
189 InputIterator cursor;
197 std::pair<std::string,InputIterator> ret;
199 while (cursor != end) {
202 cursor = http::parser::extract_all_lws(cursor,end);
211 if (*cursor ==
';') {
221 cursor = http::parser::extract_all_lws(cursor,end);
222 ret = http::parser::extract_token(cursor,end);
224 if (ret.first ==
"") {
232 cursor = http::parser::extract_all_lws(cursor,end);
233 if (cursor == end || *cursor !=
'=') {
236 attributes[name] =
"";
243 cursor = http::parser::extract_all_lws(cursor,end);
249 ret = http::parser::extract_quoted_string(cursor,end);
250 if (ret.second != cursor) {
251 attributes[name] = ret.first;
256 ret = http::parser::extract_token(cursor,end);
257 if (ret.first ==
"") {
261 attributes[name] = ret.first;
283 template <
typename InputIterator>
284 InputIterator extract_parameters(InputIterator begin, InputIterator end,
287 InputIterator cursor;
295 std::pair<std::string,InputIterator> ret;
305 while (cursor != end) {
306 std::string parameter_name;
310 cursor = http::parser::extract_all_lws(cursor,end);
311 if (cursor == end) {
break;}
313 ret = http::parser::extract_token(cursor,end);
315 if (ret.first ==
"") {
319 parameter_name = ret.first;
324 cursor = http::parser::extract_all_lws(cursor,end);
327 parameters.push_back(std::make_pair(parameter_name,attributes));
332 if (*cursor ==
';') {
333 InputIterator acursor;
336 acursor = http::parser::extract_attributes(cursor,end,attributes);
338 if (acursor == cursor) {
348 parameters.push_back(std::make_pair(parameter_name,attributes));
350 cursor = http::parser::extract_all_lws(cursor,end);
351 if (cursor == end) {
break;}
354 if (*cursor !=
',') {
370 inline std::string strip_lws(std::string
const & input) {
371 std::string::const_iterator begin = extract_all_lws(input.begin(),input.end());
372 if (begin == input.end()) {
373 return std::string();
375 std::string::const_reverse_iterator end = extract_all_lws(input.rbegin(),input.rend());
377 return std::string(begin,end.base());
411 std::string
const &
get_header(std::string
const & key)
const;
440 void append_header(std::string
const & key, std::string
const & val);
457 void replace_header(std::string
const & key, std::string
const & val);
516 void process_header(std::string::iterator begin, std::string::iterator end);
527 std::string m_version;
528 header_list m_headers;
536 #include <websocketpp/http/impl/parser.hpp>
538 #endif // HTTP_PARSER_HPP
uint16_t value
The type of a close code value.
bool get_header_as_plist(std::string const &key, parameter_list &out) const
Extract an HTTP parameter list from a parser header.
bool is_not_whitespace_char(unsigned char c)
Is the character non-whitespace.
std::string const & get_body() const
Set HTTP body.
void set_body(std::string const &value)
Set body content.
bool parse_parameter_list(std::string const &in, parameter_list &out) const
Extract an HTTP parameter list from a string.
std::vector< std::pair< std::string, attribute_list > > parameter_list
The type of an HTTP parameter list.
bool is_not_token_char(unsigned char c)
Is the character a non-token.
void process_header(std::string::iterator begin, std::string::iterator end)
Process a header line.
bool parse_headers(std::istream &s)
Parse headers from an istream.
std::string const & get_header(std::string const &key) const
Get the value of an HTTP header.
std::string const & get_version() const
Get the HTTP version string.
Namespace for the WebSocket++ project.
void append_header(std::string const &key, std::string const &val)
Append a value to an existing HTTP header.
std::map< std::string, std::string > attribute_list
The type of an HTTP attribute list.
void replace_header(std::string const &key, std::string const &val)
Set a value for an HTTP header, replacing an existing value.
std::string raw_headers() const
Generate and return the HTTP headers as a string.
void set_version(std::string const &version)
Set HTTP parser Version.
void remove_header(std::string const &key)
Remove a header from the parser.
bool is_whitespace_char(unsigned char c)
Is the character whitespace.