From cb6c396a6bb9ffc1c9e9276a8d67a75403201f47 Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sat, 30 Mar 2013 19:55:04 -0500 Subject: [PATCH] adds handshake request processing to hybi13 and 00 processors --- websocketpp/processors/hybi00.hpp | 7 ++++++- websocketpp/processors/hybi13.hpp | 24 ++++++++++++++++++++++++ websocketpp/processors/processor.hpp | 13 +++++++++++-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/websocketpp/processors/hybi00.hpp b/websocketpp/processors/hybi00.hpp index 1bd996a93d..cfd0c335e5 100644 --- a/websocketpp/processors/hybi00.hpp +++ b/websocketpp/processors/hybi00.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, Peter Thorson. All rights reserved. + * Copyright (c) 2013, Peter Thorson. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -136,6 +136,11 @@ public: return lib::error_code(); } + // outgoing client connection processing is not supported for this version + lib::error_code handshake_request(request_type& req, uri_ptr uri) const { + return error::make_error_code(error::no_protocol_support); + } + std::string get_raw(const response_type& res) const { return res.raw() + res.get_header("Sec-WebSocket-Key3"); } diff --git a/websocketpp/processors/hybi13.hpp b/websocketpp/processors/hybi13.hpp index dabc192edd..420e78b80d 100644 --- a/websocketpp/processors/hybi13.hpp +++ b/websocketpp/processors/hybi13.hpp @@ -195,6 +195,30 @@ public: return lib::error_code(); } + lib::error_code handshake_request(request_type& req, uri_ptr uri) const { + req.set_method("GET"); + req.set_uri(uri->get_resource()); + req.set_version("HTTP/1.1"); + + req.append_header("Upgrade","websocket"); + req.append_header("Connection","Upgrade"); + req.replace_header("Sec-WebSocket-Version","13"); + req.replace_header("Host",uri->get_host_port()); + + // Generate handshake key + frame::uint32_converter conv; + unsigned char raw_key[16]; + + for (int i = 0; i < 4; i++) { + conv.i = m_rng(); + std::copy(conv.c,conv.c+4,&raw_key[i*4]); + } + + req.replace_header("Sec-WebSocket-Key",base64_encode(raw_key, 16)); + + return lib::error_code(); + } + std::string get_raw(const response_type& res) const { return res.raw(); } diff --git a/websocketpp/processors/processor.hpp b/websocketpp/processors/processor.hpp index 4714a77f97..1e4b9b1f36 100644 --- a/websocketpp/processors/processor.hpp +++ b/websocketpp/processors/processor.hpp @@ -193,6 +193,15 @@ public: virtual lib::error_code process_handshake(const request_type& req, response_type& res) const = 0; + /// Fill in an HTTP request for an outgoing connection handshake + /** + * @param req The request to process. + * + * @return An error code, 0 on success, non-zero for other errors + */ + virtual lib::error_code handshake_request(request_type& req, uri_ptr uri) + const = 0; + /// Given a completed response, get the raw bytes to put on the wire virtual std::string get_raw(const response_type& request) const = 0; @@ -307,8 +316,8 @@ public: virtual lib::error_code prepare_close(close::status::value code, const std::string & reason, message_ptr out) const = 0; protected: - const bool m_secure; - const bool m_server; + const bool m_secure; + const bool m_server; };