You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
6.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef HTTP_PARSER_IMPL_HPP
  28. #define HTTP_PARSER_IMPL_HPP
  29. #include <algorithm>
  30. #include <cstdlib>
  31. #include <istream>
  32. #include <sstream>
  33. #include <string>
  34. namespace websocketpp
  35. {
  36. namespace http
  37. {
  38. namespace parser
  39. {
  40. inline void parser::set_version(const std::string & version)
  41. {
  42. m_version = version;
  43. }
  44. inline const std::string & parser::get_header(const std::string & key) const
  45. {
  46. header_list::const_iterator h = m_headers.find(key);
  47. if (h == m_headers.end())
  48. {
  49. return empty_header;
  50. }
  51. else
  52. {
  53. return h->second;
  54. }
  55. }
  56. inline bool parser::get_header_as_plist(const std::string & key, parameter_list & out) const
  57. {
  58. header_list::const_iterator it = m_headers.find(key);
  59. if (it == m_headers.end() || it->second.size() == 0)
  60. {
  61. return false;
  62. }
  63. return this->parse_parameter_list(it->second, out);
  64. }
  65. inline void parser::append_header(const std::string & key, const std::string & val)
  66. {
  67. if (std::find_if(key.begin(), key.end(), is_not_token_char) != key.end())
  68. {
  69. throw exception("Invalid header name", status_code::bad_request);
  70. }
  71. if (this->get_header(key).empty())
  72. {
  73. m_headers[key] = val;
  74. }
  75. else
  76. {
  77. m_headers[key] += ", " + val;
  78. }
  79. }
  80. inline void parser::replace_header(const std::string & key, const std::string & val)
  81. {
  82. m_headers[key] = val;
  83. }
  84. inline void parser::remove_header(const std::string & key)
  85. {
  86. m_headers.erase(key);
  87. }
  88. inline void parser::set_body(const std::string & value)
  89. {
  90. if (value.size() == 0)
  91. {
  92. remove_header("Content-Length");
  93. m_body.clear();
  94. return;
  95. }
  96. // TODO: should this method respect the max size? If so how should errors
  97. // be indicated?
  98. std::stringstream len;
  99. len << value.size();
  100. replace_header("Content-Length", len.str());
  101. m_body = value;
  102. }
  103. inline bool parser::parse_parameter_list(const std::string & in, parameter_list & out) const
  104. {
  105. if (in.size() == 0)
  106. {
  107. return false;
  108. }
  109. std::string::const_iterator it;
  110. it = extract_parameters(in.begin(), in.end(), out);
  111. return (it == in.begin());
  112. }
  113. inline bool parser::prepare_body()
  114. {
  115. if (! get_header("Content-Length").empty())
  116. {
  117. const std::string & cl_header = get_header("Content-Length");
  118. char * end;
  119. // TODO: not 100% sure what the compatibility of this method is. Also,
  120. // I believe this will only work up to 32bit sizes. Is there a need for
  121. // > 4GiB HTTP payloads?
  122. m_body_bytes_needed = std::strtoul(cl_header.c_str(), &end, 10);
  123. if (m_body_bytes_needed > m_body_bytes_max)
  124. {
  125. throw exception("HTTP message body too large", status_code::request_entity_too_large);
  126. }
  127. m_body_encoding = body_encoding::plain;
  128. return true;
  129. }
  130. else if (get_header("Transfer-Encoding") == "chunked")
  131. {
  132. // TODO
  133. // m_body_encoding = body_encoding::chunked;
  134. return false;
  135. }
  136. else
  137. {
  138. return false;
  139. }
  140. }
  141. inline size_t parser::process_body(const char * buf, size_t len)
  142. {
  143. if (m_body_encoding == body_encoding::plain)
  144. {
  145. size_t processed = (std::min) (m_body_bytes_needed, len);
  146. m_body.append(buf, processed);
  147. m_body_bytes_needed -= processed;
  148. return processed;
  149. }
  150. else if (m_body_encoding == body_encoding::chunked)
  151. {
  152. // TODO:
  153. throw exception("Unexpected body encoding", status_code::internal_server_error);
  154. }
  155. else
  156. {
  157. throw exception("Unexpected body encoding", status_code::internal_server_error);
  158. }
  159. }
  160. inline void parser::process_header(std::string::iterator begin, std::string::iterator end)
  161. {
  162. std::string::iterator cursor = std::search(begin, end, header_separator, header_separator + sizeof(header_separator) - 1);
  163. if (cursor == end)
  164. {
  165. throw exception("Invalid header line", status_code::bad_request);
  166. }
  167. append_header(strip_lws(std::string(begin, cursor)), strip_lws(std::string(cursor + sizeof(header_separator) - 1, end)));
  168. }
  169. inline const header_list & parser::get_headers() const
  170. {
  171. return m_headers;
  172. }
  173. inline std::string parser::raw_headers() const
  174. {
  175. std::stringstream raw;
  176. header_list::const_iterator it;
  177. for (it = m_headers.begin(); it != m_headers.end(); it++)
  178. {
  179. raw << it->first << ": " << it->second << "\r\n";
  180. }
  181. return raw.str();
  182. }
  183. } // namespace parser
  184. } // namespace http
  185. } // namespace websocketpp
  186. #endif // HTTP_PARSER_IMPL_HPP