28 #ifndef WEBSOCKETPP_PROCESSOR_HYBI00_HPP
29 #define WEBSOCKETPP_PROCESSOR_HYBI00_HPP
31 #include <websocketpp/frame.hpp>
32 #include <websocketpp/utf8_validator.hpp>
33 #include <websocketpp/common/network.hpp>
34 #include <websocketpp/common/md5.hpp>
35 #include <websocketpp/common/platforms.hpp>
37 #include <websocketpp/processors/processor.hpp>
51 template <
typename config>
56 typedef typename config::request_type request_type;
57 typedef typename config::response_type response_type;
59 typedef typename config::message_type message_type;
60 typedef typename message_type::ptr message_ptr;
62 typedef typename config::con_msg_manager_type::ptr msg_manager_ptr;
64 explicit hybi00(
bool secure,
bool p_is_server, msg_manager_ptr manager)
69 , m_msg_manager(manager) {}
76 if (r.get_method() !=
"GET") {
80 if (r.get_version() !=
"HTTP/1.1") {
88 if (r.get_header(
"Sec-WebSocket-Key1") ==
"" ||
89 r.get_header(
"Sec-WebSocket-Key2") ==
"" ||
90 r.get_header(
"Sec-WebSocket-Key3") ==
"")
95 return lib::error_code();
99 std::string
const & subprotocol, response_type & res)
const
104 decode_client_key(req.get_header(
"Sec-WebSocket-Key1"), &key_final[0]);
107 decode_client_key(req.get_header(
"Sec-WebSocket-Key2"), &key_final[4]);
114 std::string
const & key3 = req.get_header(
"Sec-WebSocket-Key3");
115 std::copy(key3.c_str(),
116 key3.c_str()+(std::min)(static_cast<size_t>(8), key3.size()),
120 "Sec-WebSocket-Key3",
121 md5::md5_hash_string(std::string(key_final,16))
124 res.append_header(
"Upgrade",
"WebSocket");
125 res.append_header(
"Connection",
"Upgrade");
129 if (res.get_header(
"Sec-WebSocket-Origin") ==
"") {
130 res.append_header(
"Sec-WebSocket-Origin",req.get_header(
"Origin"));
135 if (res.get_header(
"Sec-WebSocket-Location") ==
"") {
137 res.append_header(
"Sec-WebSocket-Location",uri->str());
140 if (subprotocol !=
"") {
141 res.replace_header(
"Sec-WebSocket-Protocol",subprotocol);
144 return lib::error_code();
157 std::vector<std::string>
const &)
const
172 response_type &)
const
177 std::string
get_raw(response_type
const & res)
const {
178 response_type temp = res;
179 temp.remove_header(
"Sec-WebSocket-Key3");
180 return temp.raw() + res.get_header(
"Sec-WebSocket-Key3");
183 std::string
const &
get_origin(request_type
const & r)
const {
184 return r.get_header(
"Origin");
196 std::vector<std::string> &)
198 return lib::error_code();
202 std::string h = request.get_header(
"Host");
204 size_t last_colon = h.rfind(
":");
205 size_t last_sbrace = h.rfind(
"]");
212 if (last_colon == std::string::npos ||
213 (last_sbrace != std::string::npos && last_sbrace > last_colon))
215 return lib::make_shared<uri>(base::m_secure, h, request.get_uri());
217 return lib::make_shared<uri>(base::m_secure,
218 h.substr(0,last_colon),
219 h.substr(last_colon+1),
236 size_t consume(uint8_t * buf,
size_t len, lib::error_code & ec) {
242 ec = lib::error_code();
245 if (m_state == HEADER) {
246 if (buf[p] == msg_hdr) {
248 m_msg_ptr = m_msg_manager->get_message(frame::opcode::text,1);
252 m_state = FATAL_ERROR;
258 m_state = FATAL_ERROR;
260 }
else if (m_state == PAYLOAD) {
261 uint8_t *it = std::find(buf+p,buf+len,msg_ftr);
267 l =
static_cast<size_t>(it-(buf+p));
268 m_msg_ptr->append_payload(buf+p,l);
294 return (m_state == READY);
302 message_ptr ret = m_msg_ptr;
303 m_msg_ptr = message_ptr();
322 if (in->get_opcode() != frame::opcode::text) {
326 std::string& i = in->get_raw_payload();
330 if (!utf8_validator::validate(i)) {
335 out->set_header(std::string(reinterpret_cast<char const *>(&msg_hdr),1));
339 out->append_payload(std::string(reinterpret_cast<char const *>(&msg_ftr),1));
344 out->set_prepared(
true);
346 return lib::error_code();
386 message_ptr out)
const
393 val.append(1,
'\xff');
394 val.append(1,
'\x00');
395 out->set_payload(val);
396 out->set_prepared(
true);
398 return lib::error_code();
401 void decode_client_key(std::string
const & key,
char * result)
const {
402 unsigned int spaces = 0;
403 std::string digits =
"";
407 for (
size_t i = 0; i < key.size(); i++) {
410 }
else if (key[i] >=
'0' && key[i] <=
'9') {
415 num =
static_cast<uint32_t
>(strtoul(digits.c_str(), NULL, 10));
416 if (spaces > 0 && num > 0) {
417 num = htonl(num/spaces);
418 std::copy(reinterpret_cast<char*>(&num),
419 reinterpret_cast<char*>(&num)+4,
422 std::fill(result,result+4,0);
433 uint8_t
const msg_hdr;
434 uint8_t
const msg_ftr;
438 msg_manager_ptr m_msg_manager;
439 message_ptr m_msg_ptr;
440 utf8_validator::validator m_validator;
446 #endif //WEBSOCKETPP_PROCESSOR_HYBI00_HPP
bool ready() const
Checks if there is a message ready.
int get_version() const
Get the protocol version of this processor.
uint16_t value
The type of a close code value.
lib::error_code validate_handshake(request_type const &r) const
validate a WebSocket handshake request for this version
lib::error_code extract_subprotocols(request_type const &, std::vector< std::string > &)
Extracts requested subprotocols from a handshake request.
lib::error_code process_handshake(request_type const &req, std::string const &subprotocol, response_type &res) const
Calculate the appropriate response for this websocket request.
WebSocket protocol processor abstract base class.
No support for this feature in this protocol version.
Processor encountered invalid payload data.
std::string const & get_origin(request_type const &r) const
Return the value of the header containing the CORS origin.
Processor encountered a protocol violation in an incoming message.
uri_ptr get_uri(request_type const &request) const
Extracts client uri from a handshake request.
lib::error_code prepare_close(close::status::value, std::string const &, message_ptr out) const
Prepare a close frame.
message_ptr get_message()
Retrieves the most recently processed message.
lib::error_code make_error_code(error::processor_errors e)
Create an error code with the given value and the processor category.
size_t consume(uint8_t *buf, size_t len, lib::error_code &ec)
Process new websocket connection bytes.
std::string get_key3() const
Get hybi00 handshake key3.
Namespace for the WebSocket++ project.
bool get_error() const
Tests whether the processor is in a fatal error state.
The endpoint is out of incoming message buffers.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
std::string get_raw(response_type const &res) const
Given a completed response, get the raw bytes to put on the wire.
virtual lib::error_code prepare_data_frame(message_ptr in, message_ptr out)
Prepare a message for writing.
Opcode was invalid for requested operation.
lib::error_code client_handshake_request(request_type &, uri_ptr, std::vector< std::string > const &) const
Fill in a set of request headers for a client connection request.
lib::error_code validate_server_handshake_response(request_type const &, response_type &) const
Validate the server's response to an outgoing handshake request.
lib::error_code prepare_pong(std::string const &, message_ptr) const
Prepare a pong frame.
lib::error_code prepare_ping(std::string const &, message_ptr) const
Prepare a ping frame.
The processor method was called with invalid arguments.
Processor for Hybi Draft version 00.