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.

210 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_REQUEST_IMPL_HPP
  28. #define HTTP_PARSER_REQUEST_IMPL_HPP
  29. #include <algorithm>
  30. #include <sstream>
  31. #include <string>
  32. #include <websocketpp/http/parser.hpp>
  33. namespace websocketpp
  34. {
  35. namespace http
  36. {
  37. namespace parser
  38. {
  39. inline size_t request::consume(const char * buf, size_t len)
  40. {
  41. size_t bytes_processed;
  42. if (m_ready)
  43. {
  44. return 0;
  45. }
  46. if (m_body_bytes_needed > 0)
  47. {
  48. bytes_processed = process_body(buf, len);
  49. if (body_ready())
  50. {
  51. m_ready = true;
  52. }
  53. return bytes_processed;
  54. }
  55. // copy new header bytes into buffer
  56. 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;
  60. for (;;)
  61. {
  62. // search for line delimiter
  63. end = std::search(begin, m_buf->end(), header_delimiter, header_delimiter + sizeof(header_delimiter) - 1);
  64. m_header_bytes += (end - begin + sizeof(header_delimiter));
  65. if (m_header_bytes > max_header_size)
  66. {
  67. // exceeded max header size
  68. throw exception("Maximum header size exceeded.", status_code::request_header_fields_too_large);
  69. }
  70. if (end == m_buf->end())
  71. {
  72. // we are out of bytes. Discard the processed bytes and copy the
  73. // remaining unprecessed bytes to the beginning of the buffer
  74. std::copy(begin, end, m_buf->begin());
  75. m_buf->resize(static_cast<std::string::size_type>(end - begin));
  76. m_header_bytes -= m_buf->size();
  77. return len;
  78. }
  79. // the range [begin,end) now represents a line to be processed.
  80. if (end - begin == 0)
  81. {
  82. // we got a blank line
  83. if (m_method.empty() || get_header("Host").empty())
  84. {
  85. throw exception("Incomplete Request", status_code::bad_request);
  86. }
  87. bytes_processed = (len - static_cast<std::string::size_type>(m_buf->end() - end) + sizeof(header_delimiter) - 1);
  88. // frees memory used temporarily during request parsing
  89. m_buf.reset();
  90. // if this was not an upgrade request and has a content length
  91. // continue capturing content-length bytes and expose them as a
  92. // request body.
  93. if (prepare_body())
  94. {
  95. bytes_processed += process_body(buf + bytes_processed, len - bytes_processed);
  96. if (body_ready())
  97. {
  98. m_ready = true;
  99. }
  100. return bytes_processed;
  101. }
  102. else
  103. {
  104. m_ready = true;
  105. // return number of bytes processed (starting bytes - bytes left)
  106. return bytes_processed;
  107. }
  108. }
  109. else
  110. {
  111. if (m_method.empty())
  112. {
  113. this->process(begin, end);
  114. }
  115. else
  116. {
  117. this->process_header(begin, end);
  118. }
  119. }
  120. begin = end + (sizeof(header_delimiter) - 1);
  121. }
  122. }
  123. inline std::string request::raw() const
  124. {
  125. // TODO: validation. Make sure all required fields have been set?
  126. std::stringstream ret;
  127. ret << m_method << " " << m_uri << " " << get_version() << "\r\n";
  128. ret << raw_headers() << "\r\n" << m_body;
  129. return ret.str();
  130. }
  131. inline std::string request::raw_head() const
  132. {
  133. // TODO: validation. Make sure all required fields have been set?
  134. std::stringstream ret;
  135. ret << m_method << " " << m_uri << " " << get_version() << "\r\n";
  136. ret << raw_headers() << "\r\n";
  137. return ret.str();
  138. }
  139. inline void request::set_method(const std::string & method)
  140. {
  141. if (std::find_if(method.begin(), method.end(), is_not_token_char) != method.end())
  142. {
  143. throw exception("Invalid method token.", status_code::bad_request);
  144. }
  145. m_method = method;
  146. }
  147. inline void request::set_uri(const std::string & uri)
  148. {
  149. // TODO: validation?
  150. m_uri = uri;
  151. }
  152. inline void request::process(std::string::iterator begin, std::string::iterator end)
  153. {
  154. std::string::iterator cursor_start = begin;
  155. std::string::iterator cursor_end = std::find(begin, end, ' ');
  156. if (cursor_end == end)
  157. {
  158. throw exception("Invalid request line1", status_code::bad_request);
  159. }
  160. set_method(std::string(cursor_start, cursor_end));
  161. cursor_start = cursor_end + 1;
  162. cursor_end = std::find(cursor_start, end, ' ');
  163. if (cursor_end == end)
  164. {
  165. throw exception("Invalid request line2", status_code::bad_request);
  166. }
  167. set_uri(std::string(cursor_start, cursor_end));
  168. set_version(std::string(cursor_end + 1, end));
  169. }
  170. } // namespace parser
  171. } // namespace http
  172. } // namespace websocketpp
  173. #endif // HTTP_PARSER_REQUEST_IMPL_HPP