WebSocket++  0.8.3-dev
C++ websocket client/server library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
parser.hpp
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) {
42  m_version = version;
43 }
44 
45 inline std::string const & parser::get_header(std::string const & key) const {
46  // This find is case insensitive due to the case insensitive comparator
47  // templated into header_list.
48  header_list::const_iterator h = m_headers.find(key);
49 
50  if (h == m_headers.end()) {
51  return empty_header;
52  } else {
53  return h->second;
54  }
55 }
56 
57 inline bool parser::get_header_as_plist(std::string const & key,
58  parameter_list & out) const
59 {
60  header_list::const_iterator it = m_headers.find(key);
61 
62  if (it == m_headers.end() || it->second.size() == 0) {
63  return false;
64  }
65 
66  return this->parse_parameter_list(it->second,out);
67 }
68 
69 inline void parser::append_header(std::string const & key, std::string const &
70  val, lib::error_code & ec)
71 {
72  if (std::find_if(key.begin(),key.end(),is_not_token_char) != key.end()) {
73  ec = error::make_error_code(error::invalid_header_name);
74  return;
75  }
76 
77  if (this->get_header(key).empty()) {
78  m_headers[key] = val;
79  } else {
80  m_headers[key] += ", " + val;
81  }
82  ec = lib::error_code();
83 }
84 
85 inline void parser::replace_header(std::string const & key, std::string const &
86  val, lib::error_code & ec)
87 {
88  if (std::find_if(key.begin(),key.end(),is_not_token_char) != key.end()) {
89  ec = error::make_error_code(error::invalid_header_name);
90  return;
91  }
92 
93  m_headers[key] = val;
94  ec = lib::error_code();
95 }
96 
97 inline void parser::remove_header(std::string const & key,
98  lib::error_code & ec)
99 {
100  if (std::find_if(key.begin(),key.end(),is_not_token_char) != key.end()) {
101  ec = error::make_error_code(error::invalid_header_name);
102  return;
103  }
104 
105  m_headers.erase(key);
106  ec = lib::error_code();
107 }
108 
109 inline void parser::set_body(std::string const & value, lib::error_code & ec) {
110  if (value.size() == 0) {
111  remove_header("Content-Length", ec);
112  if (ec) { return; }
113 
114  m_body.clear();
115  ec = lib::error_code();
116  return;
117  }
118 
119  if (value.size() > m_body_bytes_max) {
120  ec = error::make_error_code(error::body_too_large);
121  return;
122  }
123 
124  std::stringstream len;
125  len << value.size();
126  replace_header("Content-Length", len.str(), ec);
127  if (ec) { return; }
128  m_body = value;
129  ec = lib::error_code();
130 }
131 
132 inline bool parser::parse_parameter_list(std::string const & in,
133  parameter_list & out) const
134 {
135  if (in.size() == 0) {
136  return false;
137  }
138 
139  std::string::const_iterator it;
140  it = extract_parameters(in.begin(),in.end(),out);
141  return (it == in.begin());
142 }
143 
144 inline bool parser::prepare_body() {
145  if (!get_header("Content-Length").empty()) {
146  std::string const & cl_header = get_header("Content-Length");
147  char * end;
148 
149  // TODO: not 100% sure what the compatibility of this method is. Also,
150  // I believe this will only work up to 32bit sizes. Is there a need for
151  // > 4GiB HTTP payloads?
152  m_body_bytes_needed = std::strtoul(cl_header.c_str(),&end,10);
153 
154  if (m_body_bytes_needed > m_body_bytes_max) {
155  ec = error::make_error_code(error::body_too_large);
156  return false;
157  }
158 
159  m_body_encoding = body_encoding::plain;
160  return true;
161  } else if (get_header("Transfer-Encoding") == "chunked") {
162  // TODO
163  //m_body_encoding = body_encoding::chunked;
164  return false;
165  } else {
166  return false;
167  }
168 }
169 
170 inline size_t parser::process_body(char const * buf, size_t len) {
171  if (m_body_encoding == body_encoding::plain) {
172  size_t processed = (std::min)(m_body_bytes_needed,len);
173  m_body.append(buf,processed);
174  m_body_bytes_needed -= processed;
175  return processed;
176  } else if (m_body_encoding == body_encoding::chunked) {
177  // TODO:
178  throw exception("Unexpected body encoding",
179  status_code::internal_server_error);
180  } else {
181  throw exception("Unexpected body encoding",
182  status_code::internal_server_error);
183  }
184 }
185 
186 inline void parser::process_header(std::string::iterator begin,
187  std::string::iterator end)
188 {
189  std::string::iterator cursor = std::search(
190  begin,
191  end,
192  header_separator,
193  header_separator + sizeof(header_separator) - 1
194  );
195 
196  if (cursor == end) {
197  throw exception("Invalid header line",status_code::bad_request);
198  }
199 
200  append_header(strip_lws(std::string(begin,cursor)),
201  strip_lws(std::string(cursor+sizeof(header_separator)-1,end)));
202 }
203 
204 inline header_list const & parser::get_headers() const {
205  return m_headers;
206 }
207 
208 inline std::string parser::raw_headers() const {
209  std::stringstream raw;
210 
211  header_list::const_iterator it;
212  for (it = m_headers.begin(); it != m_headers.end(); it++) {
213  raw << it->first << ": " << it->second << "\r\n";
214  }
215 
216  return raw.str();
217 }
218 
219 
220 
221 } // namespace parser
222 } // namespace http
223 } // namespace websocketpp
224 
225 #endif // HTTP_PARSER_IMPL_HPP
websocketpp::versions_supported
static std::vector< int > const versions_supported(helper, helper+4)
Container that stores the list of protocol versions supported.
websocketpp::http
HTTP handling support.
Definition: constants.hpp:39