diff --git a/README.md b/README.md index 59acb04..5438d06 100644 --- a/README.md +++ b/README.md @@ -254,4 +254,5 @@ Contributions are welcomed! Please open issues if you have troubles while using - **09/06/2019 (v0.4.4)** - Patch! Fixed an issue with `close` event callback not called in some cases (sudden disconnect, for example). Thank you @adelin-mcbsoft for pointing out the issue ([related issue](https://github.com/gilmaimon/ArduinoWebsockets/issues/14)) - **14/06/2019 (v0.4.5)** - Patch! Fixed a memory leak and an unnecessary use of heap memory in case of masking messages. This was discoverd thanks to [issue #16](https://github.com/gilmaimon/ArduinoWebsockets/issues/16). Thank you [xgarb](https://github.com/xgarb)! - **13/07/2019 (v0.4.6)** - Very small update. Changed readme to document esp32's secured client behvior ([As discussed in issue #18](https://github.com/gilmaimon/ArduinoWebsockets/issues/18)). Also esp32's version of WebsocketsClient now has a `setInsecure` method. Thank you [adelin-mcbsoft](https://github.com/adelin-mcbsoft)! -- **25/07/2019 (v0.4.7)** - Bugfix. Fixed issues with receving large messages (unchecked reads) which was pointed out in [in issue #21](https://github.com/gilmaimon/ArduinoWebsockets/issues/21)). Thank you [Jonty](https://github.com/Jonty)! \ No newline at end of file +- **25/07/2019 (v0.4.7)** - Bugfix. Fixed issues with receving large messages (unchecked reads) which was pointed out in [in issue #21](https://github.com/gilmaimon/ArduinoWebsockets/issues/21)). Thank you [Jonty](https://github.com/Jonty)! +- **26/07/2019 (v0.4.8)** - Feature. Added an `addHeader` method as suggested [in issue #22](https://github.com/gilmaimon/ArduinoWebsockets/issues/21)). Thank you [mmcArg](https://github.com/mmcArg)! \ No newline at end of file diff --git a/library.properties b/library.properties index ad0daf2..4a96449 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ArduinoWebsockets -version=0.4.7 +version=0.4.8 author=Gil Maimon maintainer=Gil Maimon sentence=A library for writing modern Websockets applications with Arduino. diff --git a/src/tiny_websockets/client.hpp b/src/tiny_websockets/client.hpp index 0b3b0ba..c1272de 100644 --- a/src/tiny_websockets/client.hpp +++ b/src/tiny_websockets/client.hpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace websockets { enum class WebsocketsEvent { @@ -33,6 +34,8 @@ namespace websockets { WebsocketsClient& operator=(const WebsocketsClient& other); WebsocketsClient& operator=(const WebsocketsClient&& other); + void addHeader(const WSInterfaceString key, const WSInterfaceString value); + bool connect(const WSInterfaceString url); bool connect(const WSInterfaceString host, const int port, const WSInterfaceString path); @@ -84,6 +87,7 @@ namespace websockets { private: std::shared_ptr _client; + std::vector> _customHeaders; internals::WebsocketsEndpoint _endpoint; bool _connectionOpen; MessageCallback _messagesCallback; diff --git a/src/websockets_client.cpp b/src/websockets_client.cpp index a945c7e..6929feb 100644 --- a/src/websockets_client.cpp +++ b/src/websockets_client.cpp @@ -3,7 +3,6 @@ #include #include #include -#include namespace websockets { WebsocketsClient::WebsocketsClient() : WebsocketsClient(std::make_shared()) { @@ -84,7 +83,9 @@ namespace websockets { WSString requestStr; WSString expectedAcceptKey; }; - HandshakeRequestResult generateHandshake(WSString host, WSString uri) { + HandshakeRequestResult generateHandshake(const WSString& host, const WSString& uri, + const std::vector>& customHeaders) { + WSString key = crypto::base64Encode(crypto::randomBytes(16)); WSString handshake = "GET " + uri + " HTTP/1.1\r\n"; @@ -93,6 +94,11 @@ namespace websockets { handshake += "Connection: Upgrade\r\n"; handshake += "Sec-WebSocket-Key: " + key + "\r\n"; handshake += "Sec-WebSocket-Version: 13\r\n"; + + for (const auto& header: customHeaders) { + handshake += header.first + ": " + header.second + "\r\n"; + } + handshake += "\r\n"; HandshakeRequestResult result; @@ -166,6 +172,10 @@ namespace websockets { #endif //_WS_CONFIG_NO_SSL } + void WebsocketsClient::addHeader(const WSInterfaceString key, const WSInterfaceString value) { + _customHeaders.push_back({internals::fromInterfaceString(key), internals::fromInterfaceString(value)}); + } + bool WebsocketsClient::connect(WSInterfaceString _url) { WSString url = internals::fromInterfaceString(_url); WSString protocol = ""; @@ -235,7 +245,7 @@ namespace websockets { this->_connectionOpen = this->_client->connect(internals::fromInterfaceString(host), port); if (!this->_connectionOpen) return false; - auto handshake = generateHandshake(internals::fromInterfaceString(host), internals::fromInterfaceString(path)); + auto handshake = generateHandshake(internals::fromInterfaceString(host), internals::fromInterfaceString(path), _customHeaders); this->_client->send(handshake.requestStr); auto head = this->_client->readLine();