From 7dbfe42a36f71335a8ffab72289aca8b6153716f Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Mon, 24 Jul 2023 09:05:03 +0000 Subject: [PATCH] Support passing custom headers through WebSockets. --- p2p/source/duplex.cpp | 13 +++++++++---- p2p/source/duplex.hpp | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/p2p/source/duplex.cpp b/p2p/source/duplex.cpp index fb7844921..1026e8647 100644 --- a/p2p/source/duplex.cpp +++ b/p2p/source/duplex.cpp @@ -43,7 +43,7 @@ class Duplex__ : public Stream { public: - virtual task Open(const Locator &locator) = 0; + virtual task Open(const Locator &locator, const std::map &headers) = 0; }; template @@ -67,7 +67,12 @@ class Duplex_ final : return &inner_; } - task Open(const Locator &locator) override { + task Open(const Locator &locator, const std::map &headers) override { + inner_.set_option(boost::beast::websocket::stream_base::decorator([&](auto &request) { + for (const auto &[name, value] : headers) + request.insert(name, value); + })); + co_await inner_.async_handshake(locator.origin_.host_, locator.path_, Adapt()); // XXX: this needs to be configurable :/ inner_.text(true); @@ -106,9 +111,9 @@ class Duplex_ final : } }; -task> Duplex(const S &base, const Locator &locator) { orc_block({ +task> Duplex(const S &base, const Locator &locator, const std::map &headers) { orc_block({ co_return co_await Layer(*base, locator, {}, [&](U stream) -> task> { - co_await stream->Open(locator); + co_await stream->Open(locator, headers); co_return stream; }); }, "opening " << locator); } diff --git a/p2p/source/duplex.hpp b/p2p/source/duplex.hpp index 32401f63f..9ae216dc8 100644 --- a/p2p/source/duplex.hpp +++ b/p2p/source/duplex.hpp @@ -29,7 +29,7 @@ namespace orc { -task> Duplex(const S &base, const Locator &locator); +task> Duplex(const S &base, const Locator &locator, const std::map &headers = {}); }