request.hpp 6.3 KB

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