diff options
author | Jaeseung Ha <ipknhama@gmail.com> | 2017-09-18 00:43:42 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-18 00:43:42 +0900 |
commit | 08acc7c0bedc24bf5b8c6cc01f1701b0d45e141e (patch) | |
tree | 77aa4aa0f2cbfd139dc3194a3cf113ed2100454f /include/crow/websocket.h | |
parent | e40605da59e368be524dee0e9e9d41ef05149473 (diff) | |
parent | 194a9ef6812e3d5476419e33be2756ab69fc0696 (diff) | |
download | crow-08acc7c0bedc24bf5b8c6cc01f1701b0d45e141e.tar.gz crow-08acc7c0bedc24bf5b8c6cc01f1701b0d45e141e.zip |
Merge branch 'master' into master
Diffstat (limited to 'include/crow/websocket.h')
-rw-r--r-- | include/crow/websocket.h | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/include/crow/websocket.h b/include/crow/websocket.h index c3ec331..c0e8c3f 100644 --- a/include/crow/websocket.h +++ b/include/crow/websocket.h @@ -1,5 +1,6 @@ #pragma once #include <boost/algorithm/string/predicate.hpp> +#include <boost/array.hpp> #include "crow/socket_adaptors.h" #include "crow/http_request.h" #include "crow/TinySHA1.hpp" @@ -39,8 +40,10 @@ namespace crow std::function<void(crow::websocket::connection&)> open_handler, std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler, std::function<void(crow::websocket::connection&, const std::string&)> close_handler, - std::function<void(crow::websocket::connection&)> error_handler) + std::function<void(crow::websocket::connection&)> error_handler, + std::function<bool(const crow::request&)> accept_handler) : adaptor_(std::move(adaptor)), open_handler_(std::move(open_handler)), message_handler_(std::move(message_handler)), close_handler_(std::move(close_handler)), error_handler_(std::move(error_handler)) + , accept_handler_(std::move(accept_handler)) { if (!boost::iequals(req.get_header_value("upgrade"), "websocket")) { @@ -48,6 +51,17 @@ namespace crow delete this; return; } + + if (accept_handler_) + { + if (!accept_handler_(req)) + { + adaptor.close(); + delete this; + return; + } + } + // Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== // Sec-WebSocket-Version: 13 std::string magic = req.get_header_value("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; @@ -212,11 +226,13 @@ namespace crow case WebSocketReadState::Len16: { remaining_length_ = 0; - boost::asio::async_read(adaptor_.socket(), boost::asio::buffer(&remaining_length_, 2), - [this](const boost::system::error_code& ec, std::size_t bytes_transferred) + uint16_t remaining_length16_ = 0; + boost::asio::async_read(adaptor_.socket(), boost::asio::buffer(&remaining_length16_, 2), + [this,&remaining_length16_](const boost::system::error_code& ec, std::size_t bytes_transferred) { is_reading = false; - remaining_length_ = ntohs((uint16_t)remaining_length_); + remaining_length16_ = ntohs(remaining_length16_); + remaining_length_ = remaining_length16_; #ifdef CROW_ENABLE_DEBUG if (!ec && bytes_transferred != 2) { @@ -484,6 +500,7 @@ namespace crow std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler_; std::function<void(crow::websocket::connection&, const std::string&)> close_handler_; std::function<void(crow::websocket::connection&)> error_handler_; + std::function<bool(const crow::request&)> accept_handler_; }; } } |