NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
parser.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Peter Thorson. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the WebSocket++ Project nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #ifndef HTTP_PARSER_IMPL_HPP
29 #define HTTP_PARSER_IMPL_HPP
30 
31 #include <algorithm>
32 #include <cstdlib>
33 #include <istream>
34 #include <sstream>
35 #include <string>
36 
37 namespace websocketpp {
38 namespace http {
39 namespace parser {
40 
41 inline void parser::set_version(std::string const & version) {
43 }
44 
45 inline std::string const & parser::get_header(std::string const & key) const {
46  header_list::const_iterator h = m_headers.find(key);
47 
48  if (h == m_headers.end()) {
49  return empty_header;
50  } else {
51  return h->second;
52  }
53 }
54 
55 inline bool parser::get_header_as_plist(std::string const & key,
56  parameter_list & out) const
57 {
58  header_list::const_iterator it = m_headers.find(key);
59 
60  if (it == m_headers.end() || it->second.size() == 0) {
61  return false;
62  }
63 
64  return this->parse_parameter_list(it->second,out);
65 }
66 
67 inline void parser::append_header(std::string const & key, std::string const &
68  val)
69 {
70  if (std::find_if(key.begin(),key.end(),is_not_token_char) != key.end()) {
71  throw exception("Invalid header name",status_code::bad_request);
72  }
73 
74  if (this->get_header(key).empty()) {
75  m_headers[key] = val;
76  } else {
77  m_headers[key] += ", " + val;
78  }
79 }
80 
81 inline void parser::replace_header(std::string const & key, std::string const &
82  val)
83 {
84  m_headers[key] = val;
85 }
86 
87 inline void parser::remove_header(std::string const & key) {
88  m_headers.erase(key);
89 }
90 
91 inline void parser::set_body(std::string const & value) {
92  if (value.size() == 0) {
93  remove_header("Content-Length");
94  m_body.clear();
95  return;
96  }
97 
98  // TODO: should this method respect the max size? If so how should errors
99  // be indicated?
100 
101  std::stringstream len;
102  len << value.size();
103  replace_header("Content-Length", len.str());
104  m_body = value;
105 }
106 
107 inline bool parser::parse_parameter_list(std::string const & in,
108  parameter_list & out) const
109 {
110  if (in.size() == 0) {
111  return false;
112  }
113 
114  std::string::const_iterator it;
115  it = extract_parameters(in.begin(),in.end(),out);
116  return (it == in.begin());
117 }
118 
119 inline bool parser::prepare_body() {
120  if (!get_header("Content-Length").empty()) {
121  std::string const & cl_header = get_header("Content-Length");
122  char * end;
123 
124  // TODO: not 100% sure what the compatibility of this method is. Also,
125  // I believe this will only work up to 32bit sizes. Is there a need for
126  // > 4GiB HTTP payloads?
127  m_body_bytes_needed = std::strtoul(cl_header.c_str(),&end,10);
128 
130  throw exception("HTTP message body too large",
132  }
133 
135  return true;
136  } else if (get_header("Transfer-Encoding") == "chunked") {
137  // TODO
138  //m_body_encoding = body_encoding::chunked;
139  return false;
140  } else {
141  return false;
142  }
143 }
144 
145 inline size_t parser::process_body(char const * buf, size_t len) {
147  size_t processed = (std::min)(m_body_bytes_needed,len);
148  m_body.append(buf,processed);
149  m_body_bytes_needed -= processed;
150  return processed;
151  } else if (m_body_encoding == body_encoding::chunked) {
152  // TODO:
153  throw exception("Unexpected body encoding",
155  } else {
156  throw exception("Unexpected body encoding",
158  }
159 }
160 
161 inline void parser::process_header(std::string::iterator begin,
162  std::string::iterator end)
163 {
164  std::string::iterator cursor = std::search(
165  begin,
166  end,
168  header_separator + sizeof(header_separator) - 1
169  );
170 
171  if (cursor == end) {
172  throw exception("Invalid header line",status_code::bad_request);
173  }
174 
175  append_header(strip_lws(std::string(begin,cursor)),
176  strip_lws(std::string(cursor+sizeof(header_separator)-1,end)));
177 }
178 
179 inline header_list const & parser::get_headers() const {
180  return m_headers;
181 }
182 
183 inline std::string parser::raw_headers() const {
184  std::stringstream raw;
185 
186  header_list::const_iterator it;
187  for (it = m_headers.begin(); it != m_headers.end(); it++) {
188  raw << it->first << ": " << it->second << "\r\n";
189  }
190 
191  return raw.str();
192 }
193 
194 
195 
196 } // namespace parser
197 } // namespace http
198 } // namespace websocketpp
199 
200 #endif // HTTP_PARSER_IMPL_HPP
std::string const & get_header(std::string const &key) const
Get the value of an HTTP header.
Definition: parser.hpp:45
static std::string const empty_header
Literal value of an empty header.
Definition: constants.hpp:62
bool get_header_as_plist(std::string const &key, parameter_list &out) const
Extract an HTTP parameter list from a parser header.
Definition: parser.hpp:55
header_list const & get_headers() const
Return a list of all HTTP headers.
Definition: parser.hpp:179
void set_body(std::string const &value)
Set body content.
Definition: parser.hpp:91
std::vector< std::pair< std::string, attribute_list > > parameter_list
The type of an HTTP parameter list.
Definition: constants.hpp:53
size_t process_body(char const *buf, size_t len)
Process body data.
Definition: parser.hpp:145
bool is_not_token_char(unsigned char c)
Is the character a non-token.
Definition: constants.hpp:103
bool prepare_body()
Prepare the parser to begin parsing body data.
Definition: parser.hpp:119
std::map< std::string, std::string, utility::ci_less > header_list
Definition: parser.hpp:60
void process_header(std::string::iterator begin, std::string::iterator end)
Process a header line.
Definition: parser.hpp:161
std::string raw_headers() const
Generate and return the HTTP headers as a string.
Definition: parser.hpp:183
static char const header_separator[]
Literal value of the HTTP header separator.
Definition: constants.hpp:59
InputIterator extract_parameters(InputIterator begin, InputIterator end, parameter_list &parameters)
Extract HTTP parameters.
Definition: parser.hpp:293
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
void append_header(std::string const &key, std::string const &val)
Append a value to an existing HTTP header.
Definition: parser.hpp:67
std::string strip_lws(std::string const &input)
Definition: parser.hpp:379
void replace_header(std::string const &key, std::string const &val)
Set a value for an HTTP header, replacing an existing value.
Definition: parser.hpp:81
body_encoding::value m_body_encoding
Definition: parser.hpp:620
bool parse_parameter_list(std::string const &in, parameter_list &out) const
Extract an HTTP parameter list from a string.
Definition: parser.hpp:107
void remove_header(std::string const &key)
Remove a header from the parser.
Definition: parser.hpp:87