2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
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.
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.
28 #ifndef HTTP_PARSER_REQUEST_IMPL_HPP
29 #define HTTP_PARSER_REQUEST_IMPL_HPP
35 #include <websocketpp/http/parser.hpp>
37 namespace websocketpp {
41 inline size_t request::consume(char const * buf, size_t len) {
42 size_t bytes_processed;
44 if (m_ready) {return 0;}
46 if (m_body_bytes_needed > 0) {
47 bytes_processed = process_body(buf,len);
51 return bytes_processed;
54 // copy new header bytes into buffer
55 m_buf->append(buf,len);
57 // Search for delimiter in buf. If found read until then. If not read all
58 std::string::iterator begin = m_buf->begin();
59 std::string::iterator end;
62 // search for line delimiter
67 header_delimiter+sizeof(header_delimiter)-1
70 m_header_bytes += (end-begin+sizeof(header_delimiter));
72 if (m_header_bytes > max_header_size) {
73 // exceeded max header size
74 throw exception("Maximum header size exceeded.",
75 status_code::request_header_fields_too_large);
78 if (end == m_buf->end()) {
79 // we are out of bytes. Discard the processed bytes and copy the
80 // remaining unprecessed bytes to the beginning of the buffer
81 std::copy(begin,end,m_buf->begin());
82 m_buf->resize(static_cast<std::string::size_type>(end-begin));
83 m_header_bytes -= m_buf->size();
88 //the range [begin,end) now represents a line to be processed.
90 // we got a blank line
91 if (m_method.empty() || get_header("Host").empty()) {
92 throw exception("Incomplete Request",status_code::bad_request);
96 len - static_cast<std::string::size_type>(m_buf->end()-end)
97 + sizeof(header_delimiter) - 1
100 // frees memory used temporarily during request parsing
103 // if this was not an upgrade request and has a content length
104 // continue capturing content-length bytes and expose them as a
107 if (prepare_body()) {
108 bytes_processed += process_body(buf+bytes_processed,len-bytes_processed);
112 return bytes_processed;
116 // return number of bytes processed (starting bytes - bytes left)
117 return bytes_processed;
120 if (m_method.empty()) {
121 this->process(begin,end);
123 this->process_header(begin,end);
127 begin = end+(sizeof(header_delimiter)-1);
131 inline std::string request::raw() const {
132 // TODO: validation. Make sure all required fields have been set?
133 std::stringstream ret;
135 ret << m_method << " " << m_uri << " " << get_version() << "\r\n";
136 ret << raw_headers() << "\r\n" << m_body;
141 inline std::string request::raw_head() const {
142 // TODO: validation. Make sure all required fields have been set?
143 std::stringstream ret;
145 ret << m_method << " " << m_uri << " " << get_version() << "\r\n";
146 ret << raw_headers() << "\r\n";
151 inline void request::set_method(std::string const & method) {
152 if (std::find_if(method.begin(),method.end(),is_not_token_char) != method.end()) {
153 throw exception("Invalid method token.",status_code::bad_request);
159 inline void request::set_uri(std::string const & uri) {
164 inline void request::process(std::string::iterator begin, std::string::iterator
167 std::string::iterator cursor_start = begin;
168 std::string::iterator cursor_end = std::find(begin,end,' ');
170 if (cursor_end == end) {
171 throw exception("Invalid request line1",status_code::bad_request);
174 set_method(std::string(cursor_start,cursor_end));
176 cursor_start = cursor_end+1;
177 cursor_end = std::find(cursor_start,end,' ');
179 if (cursor_end == end) {
180 throw exception("Invalid request line2",status_code::bad_request);
183 set_uri(std::string(cursor_start,cursor_end));
184 set_version(std::string(cursor_end+1,end));
187 } // namespace parser
189 } // namespace websocketpp
191 #endif // HTTP_PARSER_REQUEST_IMPL_HPP