websocketpp  0.3.0
C++/Boost Asio based websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
request.hpp
1 /*
2  * Copyright (c) 2013, 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_REQUEST_IMPL_HPP
29 #define HTTP_PARSER_REQUEST_IMPL_HPP
30 
31 #include <algorithm>
32 #include <sstream>
33 
34 #include <websocketpp/http/parser.hpp>
35 
36 namespace websocketpp {
37 namespace http {
38 namespace parser {
39 
40 inline bool request::parse_complete(std::istream& s) {
41  std::string req;
42 
43  // get status line
44  std::getline(s, req);
45 
46  if (req[req.size()-1] == '\r') {
47  req.erase(req.end()-1);
48 
49  std::stringstream ss(req);
50  std::string val;
51 
52  ss >> val;
53  set_method(val);
54 
55  ss >> val;
56  set_uri(val);
57 
58  ss >> val;
59  set_version(val);
60  } else {
61  return false;
62  }
63 
64  return parse_headers(s);
65 }
66 
67 inline size_t request::consume(const char *buf, size_t len) {
68  if (m_ready) {return 0;}
69 
70  if (m_buf->size() + len > max_header_size) {
71  // exceeded max header size
72  throw exception("Maximum header size exceeded.",
73  status_code::request_header_fields_too_large);
74  }
75 
76  // copy new header bytes into buffer
77  m_buf->append(buf,len);
78 
79  // Search for delimiter in buf. If found read until then. If not read all
80  std::string::iterator begin = m_buf->begin();
81  std::string::iterator end;
82 
83  for (;;) {
84  // search for line delimiter
85  end = std::search(
86  begin,
87  m_buf->end(),
90  );
91 
92  //std::cout << "mark5: " << end-begin << std::endl;
93  //std::cout << "mark6: " << sizeof(header_delimiter) << std::endl;
94 
95  if (end == m_buf->end()) {
96  // we are out of bytes. Discard the processed bytes and copy the
97  // remaining unprecessed bytes to the beginning of the buffer
98  std::copy(begin,end,m_buf->begin());
99  m_buf->resize(static_cast<std::string::size_type>(end-begin));
100 
101  return len;
102  }
103 
104  //the range [begin,end) now represents a line to be processed.
105  if (end-begin == 0) {
106  // we got a blank line
107  if (m_method.empty() || get_header("Host") == "") {
108  throw exception("Incomplete Request",status_code::bad_request);
109  }
110  m_ready = true;
111 
112  size_t bytes_processed = (
113  len - static_cast<std::string::size_type>(m_buf->end()-end)
114  + sizeof(header_delimiter) - 1
115  );
116 
117  // frees memory used temporarily during request parsing
118  m_buf.reset();
119 
120  // return number of bytes processed (starting bytes - bytes left)
121  return bytes_processed;
122  } else {
123  if (m_method.empty()) {
124  this->process(begin,end);
125  } else {
126  this->process_header(begin,end);
127  }
128  }
129 
130  begin = end+(sizeof(header_delimiter)-1);
131  }
132 }
133 
134 inline std::string request::raw() {
135  // TODO: validation. Make sure all required fields have been set?
136  std::stringstream ret;
137 
138  ret << m_method << " " << m_uri << " " << get_version() << "\r\n";
139  ret << raw_headers() << "\r\n" << m_body;
140 
141  return ret.str();
142 }
143 
144 inline void request::set_method(const std::string& method) {
145  if (std::find_if(method.begin(),method.end(),is_not_token_char) != method.end()) {
146  throw exception("Invalid method token.",status_code::bad_request);
147  }
148 
149  m_method = method;
150 }
151 
153 
159 inline void request::set_uri(const std::string& uri) {
160  // TODO: validation?
161  m_uri = uri;
162 }
163 
164 inline void request::process(std::string::iterator begin, std::string::iterator
165  end)
166 {
167  std::string::iterator cursor_start = begin;
168  std::string::iterator cursor_end = std::find(begin,end,' ');
169 
170  if (cursor_end == end) {
171  throw exception("Invalid request line1",status_code::bad_request);
172  }
173 
174  set_method(std::string(cursor_start,cursor_end));
175 
176  cursor_start = cursor_end+1;
177  cursor_end = std::find(cursor_start,end,' ');
178 
179  if (cursor_end == end) {
180  throw exception("Invalid request line2",status_code::bad_request);
181  }
182 
183  set_uri(std::string(cursor_start,cursor_end));
184  set_version(std::string(cursor_end+1,end));
185 }
186 
187 } // namespace parser
188 } // namespace http
189 } // namespace websocketpp
190 
191 #endif // HTTP_PARSER_REQUEST_IMPL_HPP
void set_uri(const std::string &uri)
Set the HTTP uri. Must be a valid HTTP uri.
Definition: request.hpp:159
static char const header_delimiter[]
Literal value of the HTTP header delimiter.
Definition: constants.hpp:54
std::string raw()
Returns the full raw request.
Definition: request.hpp:134
bool is_not_token_char(unsigned char c)
Is the character a non-token.
Definition: constants.hpp:98
bool parse_complete(std::istream &s)
DEPRECATED parse a complete header ( MUST be in the istream)
Definition: request.hpp:40
void process_header(std::string::iterator begin, std::string::iterator end)
Process a header line.
Definition: parser.hpp:136
bool parse_headers(std::istream &s)
Parse headers from an istream.
Definition: parser.hpp:114
std::string const & get_header(std::string const &key) const
Get the value of an HTTP header.
Definition: parser.hpp:43
std::string const & get_version() const
Get the HTTP version string.
Definition: parser.hpp:391
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
size_t const max_header_size
Maximum size in bytes before rejecting an HTTP header as too big.
Definition: constants.hpp:63
void set_method(const std::string &method)
Set the HTTP method. Must be a valid HTTP token.
Definition: request.hpp:144
std::string raw_headers() const
Generate and return the HTTP headers as a string.
Definition: parser.hpp:154
void set_version(std::string const &version)
Set HTTP parser Version.
Definition: parser.hpp:39
size_t consume(const char *buf, size_t len)
Process bytes in the input buffer.
Definition: request.hpp:67