2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
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.
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.
28 #ifndef HTTP_PARSER_RESPONSE_HPP
29 #define HTTP_PARSER_RESPONSE_HPP
34 #include <websocketpp/http/parser.hpp>
36 namespace websocketpp {
40 /// Stores, parses, and manipulates HTTP responses
42 * http::response provides the following functionality for working with HTTP
45 * - Initialize response via manually setting each element
46 * - Initialize response via reading raw bytes and parsing
47 * - Once initialized, access individual parsed elements
48 * - Once initialized, read entire response as raw bytes
50 * http::response checks for header completeness separately from the full
51 * response. Once the header is complete, the Content-Length header is read to
52 * determine when to stop reading body bytes. If no Content-Length is present
53 * ready() will never return true. It is the responsibility of the caller to
54 * consume to determine when the response is complete (ie when the connection
55 * terminates, or some other metric).
57 class response : public parser {
59 typedef response type;
60 typedef lib::shared_ptr<type> ptr;
64 , m_buf(lib::make_shared<std::string>())
65 , m_status_code(status_code::uninitialized)
66 , m_state(RESPONSE_LINE) {}
68 /// Process bytes in the input buffer
70 * Process up to len bytes from input buffer buf. Returns the number of
71 * bytes processed. Bytes left unprocessed means bytes left over after the
72 * final header delimiters.
74 * Consume is a streaming processor. It may be called multiple times on one
75 * response and the full headers need not be available before processing can
76 * begin. If the end of the response was reached during this call to consume
77 * the ready flag will be set. Further calls to consume once ready will be
80 * Consume will throw an http::exception in the case of an error. Typical
81 * error reasons include malformed responses, incomplete responses, and max
82 * header size being reached.
84 * @param buf Pointer to byte buffer
85 * @param len Size of byte buffer
86 * @return Number of bytes processed.
88 size_t consume(char const * buf, size_t len);
90 /// Process bytes in the input buffer (istream version)
92 * Process bytes from istream s. Returns the number of bytes processed.
93 * Bytes left unprocessed means bytes left over after the final header
96 * Consume is a streaming processor. It may be called multiple times on one
97 * response and the full headers need not be available before processing can
98 * begin. If the end of the response was reached during this call to consume
99 * the ready flag will be set. Further calls to consume once ready will be
102 * Consume will throw an http::exception in the case of an error. Typical
103 * error reasons include malformed responses, incomplete responses, and max
104 * header size being reached.
106 * @param buf Pointer to byte buffer
107 * @param len Size of byte buffer
108 * @return Number of bytes processed.
110 size_t consume(std::istream & s);
112 /// Returns true if the response is ready.
114 * @note will never return true if the content length header is not present
117 return m_state == DONE;
120 /// Returns true if the response headers are fully parsed.
121 bool headers_ready() const {
122 return (m_state == BODY || m_state == DONE);
125 /// Returns the full raw response
126 std::string raw() const;
128 /// Set response status code and message
130 * Sets the response status code to `code` and looks up the corresponding
131 * message for standard codes. Non-standard codes will be entered as Unknown
132 * use set_status(status_code::value,std::string) overload to set both
135 * @param code Code to set
136 * @param msg Message to set
138 void set_status(status_code::value code);
140 /// Set response status code and message
142 * Sets the response status code and message to independent custom values.
143 * use set_status(status_code::value) to set the code and have the standard
144 * message be automatically set.
146 * @param code Code to set
147 * @param msg Message to set
149 void set_status(status_code::value code, std::string const & msg);
151 /// Return the response status code
152 status_code::value get_status_code() const {
153 return m_status_code;
156 /// Return the response status message
157 const std::string& get_status_msg() const {
161 /// Helper function for consume. Process response line
162 void process(std::string::iterator begin, std::string::iterator end);
164 /// Helper function for processing body bytes
165 size_t process_body(char const * buf, size_t len);
174 std::string m_status_msg;
176 lib::shared_ptr<std::string> m_buf;
177 status_code::value m_status_code;
182 } // namespace parser
184 } // namespace websocketpp
186 #include <websocketpp/http/impl/response.hpp>
188 #endif // HTTP_PARSER_RESPONSE_HPP