response.hpp 8.1 KB

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