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_IMPL_HPP
29 #define HTTP_PARSER_IMPL_HPP
37 namespace websocketpp {
41 inline void parser::set_version(std::string const & version) {
45 inline std::string const & parser::get_header(std::string const & key) const {
46 header_list::const_iterator h = m_headers.find(key);
48 if (h == m_headers.end()) {
55 inline bool parser::get_header_as_plist(std::string const & key,
56 parameter_list & out) const
58 header_list::const_iterator it = m_headers.find(key);
60 if (it == m_headers.end() || it->second.size() == 0) {
64 return this->parse_parameter_list(it->second,out);
67 inline void parser::append_header(std::string const & key, std::string const &
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);
74 if (this->get_header(key).empty()) {
77 m_headers[key] += ", " + val;
81 inline void parser::replace_header(std::string const & key, std::string const &
87 inline void parser::remove_header(std::string const & key) {
91 inline void parser::set_body(std::string const & value) {
92 if (value.size() == 0) {
93 remove_header("Content-Length");
98 // TODO: should this method respect the max size? If so how should errors
101 std::stringstream len;
103 replace_header("Content-Length", len.str());
107 inline bool parser::parse_parameter_list(std::string const & in,
108 parameter_list & out) const
110 if (in.size() == 0) {
114 std::string::const_iterator it;
115 it = extract_parameters(in.begin(),in.end(),out);
116 return (it == in.begin());
119 inline bool parser::prepare_body() {
120 if (!get_header("Content-Length").empty()) {
121 std::string const & cl_header = get_header("Content-Length");
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);
129 if (m_body_bytes_needed > m_body_bytes_max) {
130 throw exception("HTTP message body too large",
131 status_code::request_entity_too_large);
134 m_body_encoding = body_encoding::plain;
136 } else if (get_header("Transfer-Encoding") == "chunked") {
138 //m_body_encoding = body_encoding::chunked;
145 inline size_t parser::process_body(char const * buf, size_t len) {
146 if (m_body_encoding == body_encoding::plain) {
147 size_t processed = (std::min)(m_body_bytes_needed,len);
148 m_body.append(buf,processed);
149 m_body_bytes_needed -= processed;
151 } else if (m_body_encoding == body_encoding::chunked) {
153 throw exception("Unexpected body encoding",
154 status_code::internal_server_error);
156 throw exception("Unexpected body encoding",
157 status_code::internal_server_error);
161 inline void parser::process_header(std::string::iterator begin,
162 std::string::iterator end)
164 std::string::iterator cursor = std::search(
168 header_separator + sizeof(header_separator) - 1
172 throw exception("Invalid header line",status_code::bad_request);
175 append_header(strip_lws(std::string(begin,cursor)),
176 strip_lws(std::string(cursor+sizeof(header_separator)-1,end)));
179 inline std::string parser::raw_headers() const {
180 std::stringstream raw;
182 header_list::const_iterator it;
183 for (it = m_headers.begin(); it != m_headers.end(); it++) {
184 raw << it->first << ": " << it->second << "\r\n";
192 } // namespace parser
194 } // namespace websocketpp
196 #endif // HTTP_PARSER_IMPL_HPP