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.

294 lines
7.7 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_RESPONSE_IMPL_HPP
  28. #define HTTP_PARSER_RESPONSE_IMPL_HPP
  29. #include <algorithm>
  30. #include <istream>
  31. #include <sstream>
  32. #include <string>
  33. #include <websocketpp/http/parser.hpp>
  34. namespace websocketpp
  35. {
  36. namespace http
  37. {
  38. namespace parser
  39. {
  40. inline size_t response::consume(const char * buf, size_t len)
  41. {
  42. if (m_state == DONE)
  43. {
  44. return 0;
  45. }
  46. if (m_state == BODY)
  47. {
  48. return this->process_body(buf, len);
  49. }
  50. // copy new header bytes into buffer
  51. m_buf->append(buf, len);
  52. // Search for delimiter in buf. If found read until then. If not read all
  53. std::string::iterator begin = m_buf->begin();
  54. std::string::iterator end = begin;
  55. for (;;)
  56. {
  57. // search for delimiter
  58. end = std::search(begin, m_buf->end(), header_delimiter, header_delimiter + sizeof(header_delimiter) - 1);
  59. m_header_bytes += (end - begin + sizeof(header_delimiter));
  60. if (m_header_bytes > max_header_size)
  61. {
  62. // exceeded max header size
  63. throw exception("Maximum header size exceeded.", status_code::request_header_fields_too_large);
  64. }
  65. if (end == m_buf->end())
  66. {
  67. // we are out of bytes. Discard the processed bytes and copy the
  68. // remaining unprecessed bytes to the beginning of the buffer
  69. std::copy(begin, end, m_buf->begin());
  70. m_buf->resize(static_cast<std::string::size_type>(end - begin));
  71. m_read += len;
  72. m_header_bytes -= m_buf->size();
  73. return len;
  74. }
  75. // the range [begin,end) now represents a line to be processed.
  76. if (end - begin == 0)
  77. {
  78. // we got a blank line
  79. if (m_state == RESPONSE_LINE)
  80. {
  81. throw exception("Incomplete Request", status_code::bad_request);
  82. }
  83. // TODO: grab content-length
  84. std::string length = get_header("Content-Length");
  85. if (length.empty())
  86. {
  87. // no content length found, read indefinitely
  88. m_read = 0;
  89. }
  90. else
  91. {
  92. std::istringstream ss(length);
  93. if ((ss >> m_read).fail())
  94. {
  95. throw exception("Unable to parse Content-Length header", status_code::bad_request);
  96. }
  97. }
  98. m_state = BODY;
  99. // calc header bytes processed (starting bytes - bytes left)
  100. size_t read = (len - static_cast<std::string::size_type>(m_buf->end() - end) + sizeof(header_delimiter) - 1);
  101. // if there were bytes left process them as body bytes
  102. if (read < len)
  103. {
  104. read += this->process_body(buf + read, len - read);
  105. }
  106. // frees memory used temporarily during header parsing
  107. m_buf.reset();
  108. return read;
  109. }
  110. else
  111. {
  112. if (m_state == RESPONSE_LINE)
  113. {
  114. this->process(begin, end);
  115. m_state = HEADERS;
  116. }
  117. else
  118. {
  119. this->process_header(begin, end);
  120. }
  121. }
  122. begin = end + (sizeof(header_delimiter) - 1);
  123. }
  124. }
  125. inline size_t response::consume(std::istream & s)
  126. {
  127. char buf[istream_buffer];
  128. size_t bytes_read;
  129. size_t bytes_processed;
  130. size_t total = 0;
  131. while (s.good())
  132. {
  133. s.getline(buf, istream_buffer);
  134. bytes_read = static_cast<size_t>(s.gcount());
  135. if (s.fail() || s.eof())
  136. {
  137. bytes_processed = this->consume(buf, bytes_read);
  138. total += bytes_processed;
  139. if (bytes_processed != bytes_read)
  140. {
  141. // problem
  142. break;
  143. }
  144. }
  145. else if (s.bad())
  146. {
  147. // problem
  148. break;
  149. }
  150. else
  151. {
  152. // the delimiting newline was found. Replace the trailing null with
  153. // the newline that was discarded, since our raw consume function
  154. // expects the newline to be be there.
  155. buf[bytes_read - 1] = '\n';
  156. bytes_processed = this->consume(buf, bytes_read);
  157. total += bytes_processed;
  158. if (bytes_processed != bytes_read)
  159. {
  160. // problem
  161. break;
  162. }
  163. }
  164. }
  165. return total;
  166. }
  167. inline std::string response::raw() const
  168. {
  169. // TODO: validation. Make sure all required fields have been set?
  170. std::stringstream ret;
  171. ret << get_version() << " " << m_status_code << " " << m_status_msg;
  172. ret << "\r\n" << raw_headers() << "\r\n";
  173. ret << m_body;
  174. return ret.str();
  175. }
  176. inline void response::set_status(status_code::value code)
  177. {
  178. // TODO: validation?
  179. m_status_code = code;
  180. m_status_msg = get_string(code);
  181. }
  182. inline void response::set_status(status_code::value code, const std::string & msg)
  183. {
  184. // TODO: validation?
  185. m_status_code = code;
  186. m_status_msg = msg;
  187. }
  188. inline void response::process(std::string::iterator begin, std::string::iterator end)
  189. {
  190. std::string::iterator cursor_start = begin;
  191. std::string::iterator cursor_end = std::find(begin, end, ' ');
  192. if (cursor_end == end)
  193. {
  194. throw exception("Invalid response line", status_code::bad_request);
  195. }
  196. set_version(std::string(cursor_start, cursor_end));
  197. cursor_start = cursor_end + 1;
  198. cursor_end = std::find(cursor_start, end, ' ');
  199. if (cursor_end == end)
  200. {
  201. throw exception("Invalid request line", status_code::bad_request);
  202. }
  203. int code;
  204. std::istringstream ss(std::string(cursor_start, cursor_end));
  205. if ((ss >> code).fail())
  206. {
  207. throw exception("Unable to parse response code", status_code::bad_request);
  208. }
  209. set_status(status_code::value(code), std::string(cursor_end + 1, end));
  210. }
  211. inline size_t response::process_body(const char * buf, size_t len)
  212. {
  213. // If no content length was set then we read forever and never set m_ready
  214. if (m_read == 0)
  215. {
  216. // m_body.append(buf,len);
  217. // return len;
  218. m_state = DONE;
  219. return 0;
  220. }
  221. // Otherwise m_read is the number of bytes left.
  222. size_t to_read;
  223. if (len >= m_read)
  224. {
  225. // if we have more bytes than we need read, read only the amount needed
  226. // then set done state
  227. to_read = m_read;
  228. m_state = DONE;
  229. }
  230. else
  231. {
  232. // we need more bytes than are available, read them all
  233. to_read = len;
  234. }
  235. m_body.append(buf, to_read);
  236. m_read -= to_read;
  237. return to_read;
  238. }
  239. } // namespace parser
  240. } // namespace http
  241. } // namespace websocketpp
  242. #endif // HTTP_PARSER_RESPONSE_IMPL_HPP