parser.hpp 6.0 KB

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