Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to mirror Sec-WebSocket-Protocols #980

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions include/crow/routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,12 @@ namespace crow // NOTE: Already documented in "crow/app.h"
void handle_upgrade(const request& req, response&, SocketAdaptor&& adaptor) override
{
max_payload_ = max_payload_override_ ? max_payload_ : app_->websocket_max_payload();
new crow::websocket::Connection<SocketAdaptor, App>(req, std::move(adaptor), app_, max_payload_, subprotocols_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_);
new crow::websocket::Connection<SocketAdaptor, App>(req, std::move(adaptor), app_, max_payload_, subprotocols_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_, mirror_protocols_);
}
#ifdef CROW_ENABLE_SSL
void handle_upgrade(const request& req, response&, SSLAdaptor&& adaptor) override
{
new crow::websocket::Connection<SSLAdaptor, App>(req, std::move(adaptor), app_, max_payload_, subprotocols_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_);
new crow::websocket::Connection<SSLAdaptor, App>(req, std::move(adaptor), app_, max_payload_, subprotocols_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_, mirror_protocols_);
}
#endif

Expand Down Expand Up @@ -498,13 +498,20 @@ namespace crow // NOTE: Already documented in "crow/app.h"
return *this;
}

self_t& mirrorprotocols()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have expected:

         self_t& mirrorprotocols(bool mirror_protocols=true)
         {
             mirror_protocols_ = mirror_protocols;
             return *this;
         }

to make clear that something will be set?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change this, for sure. But not clearly understand, how this method can be called somewhere to disable mirror functionality, because we're usually doing like this:

    CROW_WEBSOCKET_ROUTE(app, "/PATH")
      .mirrorprotocols()
      .onaccept(...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

{
mirror_protocols_ = true;
return *this;
}

protected:
App* app_;
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&, uint16_t)> close_handler_;
std::function<void(crow::websocket::connection&, const std::string&)> error_handler_;
std::function<bool(const crow::request&, void**)> accept_handler_;
bool mirror_protocols_ = false;
uint64_t max_payload_;
bool max_payload_override_ = false;
std::vector<std::string> subprotocols_;
Expand Down
8 changes: 7 additions & 1 deletion include/crow/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler,
std::function<void(crow::websocket::connection&, const std::string&, uint16_t)> close_handler,
std::function<void(crow::websocket::connection&, const std::string&)> error_handler,
std::function<bool(const crow::request&, void**)> accept_handler):
std::function<bool(const crow::request&, void**)> accept_handler,
bool mirror_protocols):
adaptor_(std::move(adaptor)),
handler_(handler),
max_payload_bytes_(max_payload),
Expand Down Expand Up @@ -145,6 +146,11 @@ namespace crow // NOTE: Already documented in "crow/app.h"
}
}

if (mirror_protocols & !requested_subprotocols_header.empty())
{
subprotocol_ = requested_subprotocols_header;
}

if (accept_handler_)
{
void* ud = nullptr;
Expand Down
51 changes: 51 additions & 0 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3200,6 +3200,57 @@ TEST_CASE("websocket_subprotocols")
app.stop();
}

TEST_CASE("mirror_websocket_subprotocols")
{
static std::string http_message = "GET /ws HTTP/1.1\r\nConnection: keep-alive, Upgrade\r\nupgrade: websocket\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Protocol: protocol1, protocol2\r\nSec-WebSocket-Version: 13\r\nHost: localhost\r\n\r\n";

websocket::connection* connection = nullptr;
bool connected{false};

SimpleApp app;

CROW_WEBSOCKET_ROUTE(app, "/ws")
.mirrorprotocols()
.onaccept([&](const crow::request& req, void**) {
CROW_LOG_INFO << "Accepted websocket with URL " << req.url;
return true;
})
.onopen([&](websocket::connection& con) {
connected = true;
connection = &con;
CROW_LOG_INFO << "Connected websocket and subprotocol is " << con.get_subprotocol();
})
.onclose([&](websocket::connection&, const std::string&, uint16_t) {
CROW_LOG_INFO << "Closing websocket";
});

app.validate();

auto _ = app.bindaddr(LOCALHOST_ADDRESS).port(45451).run_async();
app.wait_for_server_start();
asio::io_context ic;

asio::ip::tcp::socket c(ic);
c.connect(asio::ip::tcp::endpoint(
asio::ip::make_address(LOCALHOST_ADDRESS), 45451));


char buf[2048];

//----------Handshake----------
{
std::fill_n(buf, 2048, 0);
c.send(asio::buffer(http_message));

c.receive(asio::buffer(buf, 2048));
std::this_thread::sleep_for(std::chrono::milliseconds(5));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this there only to keep the connection open?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in many tests before...

CHECK(connected);
CHECK(connection->get_subprotocol() == "protocol1, protocol2");
}

app.stop();
}

#ifdef CROW_ENABLE_COMPRESSION
TEST_CASE("zlib_compression")
{
Expand Down
Loading