Skip to content

Commit

Permalink
Support passing custom headers through WebSockets.
Browse files Browse the repository at this point in the history
  • Loading branch information
saurik committed Jul 24, 2023
1 parent 0196f74 commit 7dbfe42
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions p2p/source/duplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Duplex__ :
public Stream
{
public:
virtual task<void> Open(const Locator &locator) = 0;
virtual task<void> Open(const Locator &locator, const std::map<std::string, std::string> &headers) = 0;
};

template <typename Inner_>
Expand All @@ -67,7 +67,12 @@ class Duplex_ final :
return &inner_;
}

task<void> Open(const Locator &locator) override {
task<void> Open(const Locator &locator, const std::map<std::string, std::string> &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);
Expand Down Expand Up @@ -106,9 +111,9 @@ class Duplex_ final :
}
};

task<U<Stream>> Duplex(const S<Base> &base, const Locator &locator) { orc_block({
task<U<Stream>> Duplex(const S<Base> &base, const Locator &locator, const std::map<std::string, std::string> &headers) { orc_block({
co_return co_await Layer<Duplex_>(*base, locator, {}, [&](U<Duplex__> stream) -> task<U<Stream>> {
co_await stream->Open(locator);
co_await stream->Open(locator, headers);
co_return stream;
});
}, "opening " << locator); }
Expand Down
2 changes: 1 addition & 1 deletion p2p/source/duplex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace orc {

task<U<Stream>> Duplex(const S<Base> &base, const Locator &locator);
task<U<Stream>> Duplex(const S<Base> &base, const Locator &locator, const std::map<std::string, std::string> &headers = {});

}

Expand Down

0 comments on commit 7dbfe42

Please sign in to comment.