websocketpp  0.3.0
C++/Boost Asio based websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
response.hpp
1 /*
2  * Copyright (c) 2013, 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 
28 #ifndef HTTP_PARSER_RESPONSE_HPP
29 #define HTTP_PARSER_RESPONSE_HPP
30 
31 #include <websocketpp/http/parser.hpp>
32 
33 namespace websocketpp {
34 namespace http {
35 namespace parser {
36 
38 
54 class response : public parser {
55 public:
56  typedef response type;
57  typedef lib::shared_ptr<type> ptr;
58 
59  response()
60  : m_read(0)
61  , m_buf(new std::string())
62  , m_status_code(status_code::uninitialized)
63  , m_state(RESPONSE_LINE) {}
64 
66 
85  size_t consume(const char *buf, size_t len);
86 
87  size_t consume(std::istream & s);
88 
90 
93  bool ready() const {
94  return m_state == DONE;
95  }
96 
98  bool headers_ready() const {
99  return (m_state == BODY || m_state == DONE);
100  }
101 
103  bool parse_complete(std::istream& s);
104 
106  std::string raw() const;
107 
109 
118  void set_status(status_code::value code);
119 
121 
129  void set_status(status_code::value code, const std::string& msg);
130 
132  status_code::value get_status_code() const {
133  return m_status_code;
134  }
135 
137  const std::string& get_status_msg() const {
138  return m_status_msg;
139  }
140 private:
142  void process(std::string::iterator begin, std::string::iterator end);
143 
145  size_t process_body(const char *buf, size_t len);
146 
147  enum state {
148  RESPONSE_LINE = 0,
149  HEADERS = 1,
150  BODY = 2,
151  DONE = 3
152  };
153 
154  std::string m_status_msg;
155  size_t m_read;
156  lib::shared_ptr<std::string> m_buf;
157  status_code::value m_status_code;
158  state m_state;
159 
160 };
161 
162 } // namespace parser
163 } // namespace http
164 } // namespace websocketpp
165 
166 #include <websocketpp/http/impl/response.hpp>
167 
168 #endif // HTTP_PARSER_RESPONSE_HPP
bool parse_complete(std::istream &s)
DEPRECATED parse a complete response from a pre-delimited istream.
Definition: response.hpp:173
status_code::value get_status_code() const
Return the response status code.
Definition: response.hpp:132
Stores, parses, and manipulates HTTP responses.
Definition: response.hpp:54
bool ready() const
Returns true if the response is ready.
Definition: response.hpp:93
size_t consume(const char *buf, size_t len)
Process bytes in the input buffer.
Definition: response.hpp:40
bool headers_ready() const
Returns true if the response headers are fully parsed.
Definition: response.hpp:98
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
void set_status(status_code::value code)
Set response status code and message.
Definition: response.hpp:214
const std::string & get_status_msg() const
Return the response status message.
Definition: response.hpp:137
std::string raw() const
Returns the full raw response.
Definition: response.hpp:201