Skip to content

Commit

Permalink
Merge pull request #24 from gilmaimon/feature_custom_headers
Browse files Browse the repository at this point in the history
Added addHeader method to WebsocketsClient as suggested in issue #22
  • Loading branch information
gilmaimon authored Jul 25, 2019
2 parents 1776b82 + 7bfb531 commit 66b25ca
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)!
- **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)!
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ArduinoWebsockets
version=0.4.7
version=0.4.8
author=Gil Maimon <[email protected]>
maintainer=Gil Maimon <[email protected]>
sentence=A library for writing modern Websockets applications with Arduino.
Expand Down
4 changes: 4 additions & 0 deletions src/tiny_websockets/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <tiny_websockets/message.hpp>
#include <memory>
#include <functional>
#include <vector>

namespace websockets {
enum class WebsocketsEvent {
Expand All @@ -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);

Expand Down Expand Up @@ -84,6 +87,7 @@ namespace websockets {

private:
std::shared_ptr<network::TcpClient> _client;
std::vector<std::pair<WSString, WSString>> _customHeaders;
internals::WebsocketsEndpoint _endpoint;
bool _connectionOpen;
MessageCallback _messagesCallback;
Expand Down
16 changes: 13 additions & 3 deletions src/websockets_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <tiny_websockets/message.hpp>
#include <tiny_websockets/client.hpp>
#include <tiny_websockets/internals/wscrypto/crypto.hpp>
#include <vector>

namespace websockets {
WebsocketsClient::WebsocketsClient() : WebsocketsClient(std::make_shared<WSDefaultTcpClient>()) {
Expand Down Expand Up @@ -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<std::pair<WSString, WSString>>& customHeaders) {

WSString key = crypto::base64Encode(crypto::randomBytes(16));

WSString handshake = "GET " + uri + " HTTP/1.1\r\n";
Expand All @@ -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;
Expand Down Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 66b25ca

Please sign in to comment.