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.

200 lines
7.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_RESPONSE_HPP
  28. #define HTTP_PARSER_RESPONSE_HPP
  29. #include <iostream>
  30. #include <string>
  31. #include <websocketpp/http/parser.hpp>
  32. namespace websocketpp
  33. {
  34. namespace http
  35. {
  36. namespace parser
  37. {
  38. /// Stores, parses, and manipulates HTTP responses
  39. /**
  40. * http::response provides the following functionality for working with HTTP
  41. * responses.
  42. *
  43. * - Initialize response via manually setting each element
  44. * - Initialize response via reading raw bytes and parsing
  45. * - Once initialized, access individual parsed elements
  46. * - Once initialized, read entire response as raw bytes
  47. *
  48. * http::response checks for header completeness separately from the full
  49. * response. Once the header is complete, the Content-Length header is read to
  50. * determine when to stop reading body bytes. If no Content-Length is present
  51. * ready() will never return true. It is the responsibility of the caller to
  52. * consume to determine when the response is complete (ie when the connection
  53. * terminates, or some other metric).
  54. */
  55. class response : public parser
  56. {
  57. public:
  58. typedef response type;
  59. typedef lib::shared_ptr<type> ptr;
  60. response() :
  61. m_read(0),
  62. m_buf(lib::make_shared<std::string>()),
  63. m_status_code(status_code::uninitialized),
  64. m_state(RESPONSE_LINE)
  65. {
  66. }
  67. /// Process bytes in the input buffer
  68. /**
  69. * Process up to len bytes from input buffer buf. Returns the number of
  70. * bytes processed. Bytes left unprocessed means bytes left over after the
  71. * final header delimiters.
  72. *
  73. * Consume is a streaming processor. It may be called multiple times on one
  74. * response and the full headers need not be available before processing can
  75. * begin. If the end of the response was reached during this call to consume
  76. * the ready flag will be set. Further calls to consume once ready will be
  77. * ignored.
  78. *
  79. * Consume will throw an http::exception in the case of an error. Typical
  80. * error reasons include malformed responses, incomplete responses, and max
  81. * header size being reached.
  82. *
  83. * @param buf Pointer to byte buffer
  84. * @param len Size of byte buffer
  85. * @return Number of bytes processed.
  86. */
  87. size_t consume(const char * buf, size_t len);
  88. /// Process bytes in the input buffer (istream version)
  89. /**
  90. * Process bytes from istream s. Returns the number of bytes processed.
  91. * Bytes left unprocessed means bytes left over after the final header
  92. * delimiters.
  93. *
  94. * Consume is a streaming processor. It may be called multiple times on one
  95. * response and the full headers need not be available before processing can
  96. * begin. If the end of the response was reached during this call to consume
  97. * the ready flag will be set. Further calls to consume once ready will be
  98. * ignored.
  99. *
  100. * Consume will throw an http::exception in the case of an error. Typical
  101. * error reasons include malformed responses, incomplete responses, and max
  102. * header size being reached.
  103. *
  104. * @param buf Pointer to byte buffer
  105. * @param len Size of byte buffer
  106. * @return Number of bytes processed.
  107. */
  108. size_t consume(std::istream & s);
  109. /// Returns true if the response is ready.
  110. /**
  111. * @note will never return true if the content length header is not present
  112. */
  113. bool ready() const
  114. {
  115. return m_state == DONE;
  116. }
  117. /// Returns true if the response headers are fully parsed.
  118. bool headers_ready() const
  119. {
  120. return (m_state == BODY || m_state == DONE);
  121. }
  122. /// Returns the full raw response
  123. std::string raw() const;
  124. /// Set response status code and message
  125. /**
  126. * Sets the response status code to `code` and looks up the corresponding
  127. * message for standard codes. Non-standard codes will be entered as Unknown
  128. * use set_status(status_code::value,std::string) overload to set both
  129. * values explicitly.
  130. *
  131. * @param code Code to set
  132. * @param msg Message to set
  133. */
  134. void set_status(status_code::value code);
  135. /// Set response status code and message
  136. /**
  137. * Sets the response status code and message to independent custom values.
  138. * use set_status(status_code::value) to set the code and have the standard
  139. * message be automatically set.
  140. *
  141. * @param code Code to set
  142. * @param msg Message to set
  143. */
  144. void set_status(status_code::value code, const std::string & msg);
  145. /// Return the response status code
  146. status_code::value get_status_code() const
  147. {
  148. return m_status_code;
  149. }
  150. /// Return the response status message
  151. const std::string & get_status_msg() const
  152. {
  153. return m_status_msg;
  154. }
  155. private:
  156. /// Helper function for consume. Process response line
  157. void process(std::string::iterator begin, std::string::iterator end);
  158. /// Helper function for processing body bytes
  159. size_t process_body(const char * buf, size_t len);
  160. enum state
  161. {
  162. RESPONSE_LINE = 0,
  163. HEADERS = 1,
  164. BODY = 2,
  165. DONE = 3
  166. };
  167. std::string m_status_msg;
  168. size_t m_read;
  169. lib::shared_ptr<std::string> m_buf;
  170. status_code::value m_status_code;
  171. state m_state;
  172. };
  173. } // namespace parser
  174. } // namespace http
  175. } // namespace websocketpp
  176. #include <websocketpp/http/impl/response.hpp>
  177. #endif // HTTP_PARSER_RESPONSE_HPP