-
-
Notifications
You must be signed in to change notification settings - Fork 482
Feature: Allow custom headers to be passed via WebSocket #1424
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
|
|
||
| #include <atomic> | ||
| #include <functional> | ||
| #include <map> | ||
|
|
||
| namespace rtc { | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,8 +30,8 @@ using std::chrono::system_clock; | |||||||||||||||||
|
|
||||||||||||||||||
| WsHandshake::WsHandshake() {} | ||||||||||||||||||
|
|
||||||||||||||||||
| WsHandshake::WsHandshake(string host, string path, std::vector<string> protocols) | ||||||||||||||||||
| : mHost(std::move(host)), mPath(std::move(path)), mProtocols(std::move(protocols)) { | ||||||||||||||||||
| WsHandshake::WsHandshake(string host, string path, std::vector<string> protocols, std::map<string, string> headers) | ||||||||||||||||||
| : mHost(std::move(host)), mPath(std::move(path)), mProtocols(std::move(protocols)), mCustomHeaders(std::move(headers)) { | ||||||||||||||||||
|
|
||||||||||||||||||
| if (mHost.empty()) | ||||||||||||||||||
| throw std::invalid_argument("WebSocket HTTP host cannot be empty"); | ||||||||||||||||||
|
|
@@ -55,6 +55,11 @@ std::vector<string> WsHandshake::protocols() const { | |||||||||||||||||
| return mProtocols; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| std::multimap<string, string> WsHandshake::requestHeaders() const { | ||||||||||||||||||
| std::unique_lock lock(mMutex); | ||||||||||||||||||
| return mRequestHeaders; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| string WsHandshake::generateHttpRequest() { | ||||||||||||||||||
| std::unique_lock lock(mMutex); | ||||||||||||||||||
| mKey = generateKey(); | ||||||||||||||||||
|
|
@@ -73,6 +78,11 @@ string WsHandshake::generateHttpRequest() { | |||||||||||||||||
| if (!mProtocols.empty()) | ||||||||||||||||||
| out += "Sec-WebSocket-Protocol: " + utils::implode(mProtocols, ',') + "\r\n"; | ||||||||||||||||||
|
|
||||||||||||||||||
| // Add custom headers | ||||||||||||||||||
| for (const auto& [headerName, headerValue] : mCustomHeaders) { | ||||||||||||||||||
| out += headerName + ": " + headerValue + "\r\n"; | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+82
to
+84
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should at least sanitize
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| out += "\r\n"; | ||||||||||||||||||
|
|
||||||||||||||||||
| return out; | ||||||||||||||||||
|
|
@@ -161,6 +171,9 @@ size_t WsHandshake::parseHttpRequest(const byte *buffer, size_t size) { | |||||||||||||||||
|
|
||||||||||||||||||
| auto headers = parseHttpHeaders(lines); | ||||||||||||||||||
|
|
||||||||||||||||||
| // Store all request headers for later access | ||||||||||||||||||
| mRequestHeaders = headers; | ||||||||||||||||||
|
|
||||||||||||||||||
| auto h = headers.find("host"); | ||||||||||||||||||
| if (h == headers.end()) | ||||||||||||||||||
| throw RequestError("WebSocket host header missing in request", 400); | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,11 +23,12 @@ namespace rtc::impl { | |
| class WsHandshake final { | ||
| public: | ||
| WsHandshake(); | ||
| WsHandshake(string host, string path = "/", std::vector<string> protocols = {}); | ||
| WsHandshake(string host, string path = "/", std::vector<string> protocols = {}, std::map<string, string> headers = {}); | ||
|
|
||
| string host() const; | ||
| string path() const; | ||
| std::vector<string> protocols() const; | ||
| std::multimap<string, string> requestHeaders() const; | ||
|
|
||
| string generateHttpRequest(); | ||
| string generateHttpResponse(); | ||
|
|
@@ -57,6 +58,8 @@ class WsHandshake final { | |
| string mHost; | ||
| string mPath; | ||
| std::vector<string> mProtocols; | ||
| std::map<string, string> mCustomHeaders; | ||
| std::multimap<string, string> mRequestHeaders; | ||
|
Comment on lines
+61
to
+62
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to use two different maps here? You could use only one multimap.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey ! It's to add support for the websocket server. But good suggestion. Rather than custom and request we can just rename them to request headers or just headers up to you. |
||
| string mKey; | ||
| mutable std::mutex mMutex; | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick, but since you plan to store the map, you should pass it by value and move it into the
WsHandshake()constructor.