Compare commits

..

1 Commits

Author SHA1 Message Date
f6e730c2b4 fix _fingerprint is set checks for ESP32
see #633 and #632
2021-03-06 09:24:07 +01:00
13 changed files with 105 additions and 173 deletions

View File

@ -1,47 +0,0 @@
set(headers
src/SocketIOclient.h
src/WebSockets.h
src/WebSockets4WebServer.h
src/WebSocketsClient.h
src/WebSocketsServer.h
src/WebSocketsVersion.h
src/libb64/cdecode_inc.h
src/libb64/cencode_inc.h
src/libsha1/libsha1.h
)
set(sources
src/SocketIOclient.cpp
src/WebSockets.cpp
src/WebSocketsClient.cpp
src/WebSocketsServer.cpp
src/libb64/cdecode.c
src/libb64/cencode.c
src/libsha1/libsha1.c
)
set(dependencies
)
idf_component_register(
INCLUDE_DIRS
src
SRCS
${headers}
${sources}
REQUIRES
${dependencies}
)
target_compile_options(${COMPONENT_TARGET}
PUBLIC
-DESP32
-DWEBSOCKETS_NETWORK_TYPE=NETWORK_ESP32
PRIVATE
-fstack-reuse=all
-fstack-protector-all
-Wno-unused-function
-Wno-deprecated-declarations
-Wno-missing-field-initializers
-Wno-parentheses
)

View File

@ -21,5 +21,5 @@
"type": "git",
"url": "https://github.com/Links2004/arduinoWebSockets.git"
},
"version": "2.3.6"
"version": "2.3.5"
}

View File

@ -1,5 +1,5 @@
name=WebSockets
version=2.3.6
version=2.3.5
author=Markus Sattler
maintainer=Markus Sattler
sentence=WebSockets for Arduino (Server + Client)

View File

@ -5,12 +5,9 @@
* Author: links
*/
#include "SocketIOclient.h"
#include <string>
#include "WebSockets.h"
#include "WebSocketsClient.h"
#include "SocketIOclient.h"
SocketIOclient::SocketIOclient() {
}
@ -24,7 +21,7 @@ void SocketIOclient::begin(const char * host, uint16_t port, const char * url, c
initClient();
}
void SocketIOclient::begin(std::string host, uint16_t port, std::string url, std::string protocol) {
void SocketIOclient::begin(String host, uint16_t port, String url, String protocol) {
WebSocketsClient::beginSocketIO(host, port, url, protocol);
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
initClient();
@ -36,7 +33,7 @@ void SocketIOclient::beginSSL(const char * host, uint16_t port, const char * url
initClient();
}
void SocketIOclient::beginSSL(std::string host, uint16_t port, std::string url, std::string protocol) {
void SocketIOclient::beginSSL(String host, uint16_t port, String url, String protocol) {
WebSocketsClient::beginSocketIOSSL(host, port, url, protocol);
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
initClient();
@ -134,7 +131,7 @@ bool SocketIOclient::send(socketIOmessageType_t type, const char * payload, size
return send(type, (uint8_t *)payload, length);
}
bool SocketIOclient::send(socketIOmessageType_t type, std::string & payload) {
bool SocketIOclient::send(socketIOmessageType_t type, String & payload) {
return send(type, (uint8_t *)payload.c_str(), payload.length());
}
@ -162,7 +159,7 @@ bool SocketIOclient::sendEVENT(const char * payload, size_t length) {
return sendEVENT((uint8_t *)payload, length);
}
bool SocketIOclient::sendEVENT(std::string & payload) {
bool SocketIOclient::sendEVENT(String & payload) {
return sendEVENT((uint8_t *)payload.c_str(), payload.length());
}

View File

@ -8,8 +8,6 @@
#ifndef SOCKETIOCLIENT_H_
#define SOCKETIOCLIENT_H_
#include <string>
#include "WebSockets.h"
#define EIO_HEARTBEAT_INTERVAL 20000
@ -49,11 +47,11 @@ class SocketIOclient : protected WebSocketsClient {
virtual ~SocketIOclient(void);
void begin(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
void begin(std::string host, uint16_t port, std::string url = "/socket.io/?EIO=3", std::string protocol = "arduino");
void begin(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
#ifdef HAS_SSL
void beginSSL(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
void beginSSL(std::string host, uint16_t port, std::string url = "/socket.io/?EIO=3", std::string protocol = "arduino");
void beginSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
#ifndef SSL_AXTLS
void beginSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * CA_cert = NULL, const char * protocol = "arduino");
void beginSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", BearSSL::X509List * CA_cert = NULL, const char * protocol = "arduino");
@ -69,13 +67,13 @@ class SocketIOclient : protected WebSocketsClient {
bool sendEVENT(const uint8_t * payload, size_t length = 0);
bool sendEVENT(char * payload, size_t length = 0, bool headerToPayload = false);
bool sendEVENT(const char * payload, size_t length = 0);
bool sendEVENT(std::string & payload);
bool sendEVENT(String & payload);
bool send(socketIOmessageType_t type, uint8_t * payload, size_t length = 0, bool headerToPayload = false);
bool send(socketIOmessageType_t type, const uint8_t * payload, size_t length = 0);
bool send(socketIOmessageType_t type, char * payload, size_t length = 0, bool headerToPayload = false);
bool send(socketIOmessageType_t type, const char * payload, size_t length = 0);
bool send(socketIOmessageType_t type, std::string & payload);
bool send(socketIOmessageType_t type, String & payload);
void loop(void);

View File

@ -24,8 +24,6 @@
#include "WebSockets.h"
#include <string>
#ifdef ESP8266
#include <core_esp8266_features.h>
#endif
@ -537,15 +535,15 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
/**
* generate the key for Sec-WebSocket-Accept
* @param clientKey std::string
* @return std::string Accept Key
* @param clientKey String
* @return String Accept Key
*/
std::string WebSockets::acceptKey(std::string & clientKey) {
String WebSockets::acceptKey(String & clientKey) {
uint8_t sha1HashBin[20] = { 0 };
#ifdef ESP8266
sha1(clientKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", &sha1HashBin[0]);
#elif defined(ESP32)
std::string data = clientKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
String data = clientKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
esp_sha(SHA1, (unsigned char *)data.c_str(), data.length(), &sha1HashBin[0]);
#else
clientKey += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
@ -555,7 +553,7 @@ std::string WebSockets::acceptKey(std::string & clientKey) {
SHA1Final(&sha1HashBin[0], &ctx);
#endif
std::string key = base64_encode(sha1HashBin, 20);
String key = base64_encode(sha1HashBin, 20);
key.trim();
return key;
@ -565,9 +563,9 @@ std::string WebSockets::acceptKey(std::string & clientKey) {
* base64_encode
* @param data uint8_t *
* @param length size_t
* @return base64 encoded std::string
* @return base64 encoded String
*/
std::string WebSockets::base64_encode(uint8_t * data, size_t length) {
String WebSockets::base64_encode(uint8_t * data, size_t length) {
size_t size = ((length * 1.6f) + 1);
char * buffer = (char *)malloc(size);
if(buffer) {
@ -576,11 +574,11 @@ std::string WebSockets::base64_encode(uint8_t * data, size_t length) {
int len = base64_encode_block((const char *)&data[0], length, &buffer[0], &_state);
len = base64_encode_blockend((buffer + len), &_state);
std::string base64 = std::string(buffer);
String base64 = String(buffer);
free(buffer);
return base64;
}
return std::string("-FAIL-");
return String("-FAIL-");
}
/**

View File

@ -25,14 +25,12 @@
#ifndef WEBSOCKETS_H_
#define WEBSOCKETS_H_
#include <string>
#ifdef STM32_DEVICE
#include <application.h>
#define bit(b) (1UL << (b)) // Taken directly from Arduino.h
#else
//#include <Arduino.h>
//#include <IPAddress.h>
#include <Arduino.h>
#include <IPAddress.h>
#endif
#ifdef ARDUINO_ARCH_AVR
@ -188,8 +186,8 @@
#elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
//#include <WiFi.h>
//#include <WiFiClientSecure.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#define SSL_AXTLS
#define WEBSOCKETS_NETWORK_CLASS WiFiClient
#define WEBSOCKETS_NETWORK_SSL_CLASS WiFiClientSecure
@ -286,28 +284,28 @@ typedef struct {
WEBSOCKETS_NETWORK_SSL_CLASS * ssl;
#endif
std::string cUrl; ///< http url
String cUrl; ///< http url
uint16_t cCode = 0; ///< http code
bool cIsClient = false; ///< will be used for masking
bool cIsUpgrade = false; ///< Connection == Upgrade
bool cIsWebsocket = false; ///< Upgrade == websocket
std::string cSessionId; ///< client Set-Cookie (session id)
std::string cKey; ///< client Sec-WebSocket-Key
std::string cAccept; ///< client Sec-WebSocket-Accept
std::string cProtocol; ///< client Sec-WebSocket-Protocol
std::string cExtensions; ///< client Sec-WebSocket-Extensions
String cSessionId; ///< client Set-Cookie (session id)
String cKey; ///< client Sec-WebSocket-Key
String cAccept; ///< client Sec-WebSocket-Accept
String cProtocol; ///< client Sec-WebSocket-Protocol
String cExtensions; ///< client Sec-WebSocket-Extensions
uint16_t cVersion = 0; ///< client Sec-WebSocket-Version
uint8_t cWsRXsize = 0; ///< State of the RX
uint8_t cWsHeader[WEBSOCKETS_MAX_HEADER_SIZE]; ///< RX WS Message buffer
WSMessageHeader_t cWsHeaderDecode;
std::string base64Authorization; ///< Base64 encoded Auth request
std::string plainAuthorization; ///< Base64 encoded Auth request
String base64Authorization; ///< Base64 encoded Auth request
String plainAuthorization; ///< Base64 encoded Auth request
std::string extraHeaders;
String extraHeaders;
bool cHttpHeadersValid = false; ///< non-websocket http header validity indicator
size_t cMandatoryHeadersCount; ///< non-websocket mandatory http headers present count
@ -320,7 +318,7 @@ typedef struct {
uint8_t pongTimeoutCount = 0; // current pong timeout count
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
std::string cHttpLine; ///< HTTP header lines
String cHttpLine; ///< HTTP header lines
#endif
} WSclient_t;
@ -352,8 +350,8 @@ class WebSockets {
void handleWebsocketCb(WSclient_t * client);
void handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t * payload);
std::string acceptKey(std::string & clientKey);
std::string base64_encode(uint8_t * data, size_t length);
String acceptKey(String & clientKey);
String base64_encode(uint8_t * data, size_t length);
bool readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWaitCb cb);
virtual size_t write(WSclient_t * client, uint8_t * out, size_t n);

View File

@ -25,8 +25,6 @@
#ifndef __WEBSOCKETS4WEBSERVER_H
#define __WEBSOCKETS4WEBSERVER_H
#include <string>
#include <WebSocketsServer.h>
#include <ESP8266WebServer.h>
@ -34,15 +32,15 @@
class WebSockets4WebServer : public WebSocketsServerCore {
public:
WebSockets4WebServer(const std::string & origin = "", const std::string & protocol = "arduino")
WebSockets4WebServer(const String & origin = "", const String & protocol = "arduino")
: WebSocketsServerCore(origin, protocol) {
begin();
}
ESP8266WebServer::HookFunction hookForWebserver(const std::string & wsRootDir, WebSocketServerEvent event) {
ESP8266WebServer::HookFunction hookForWebserver(const String & wsRootDir, WebSocketServerEvent event) {
onEvent(event);
return [&, wsRootDir](const std::string & method, const std::string & url, WiFiClient * tcpClient, ESP8266WebServer::ContentTypeFunction contentType) {
return [&, wsRootDir](const String & method, const String & url, WiFiClient * tcpClient, ESP8266WebServer::ContentTypeFunction contentType) {
(void)contentType;
if(!(method == "GET" && url.indexOf(wsRootDir) == 0)) {
@ -57,7 +55,7 @@ class WebSockets4WebServer : public WebSocketsServerCore {
if(client) {
// give "GET <url>"
std::string headerLine;
String headerLine;
headerLine.reserve(url.length() + 5);
headerLine = "GET ";
headerLine += url;

View File

@ -22,11 +22,8 @@
*
*/
#include "WebSocketsClient.h"
#include <string>
#include "WebSockets.h"
#include "WebSocketsClient.h"
WebSocketsClient::WebSocketsClient() {
_cbEvent = NULL;
@ -93,7 +90,7 @@ void WebSocketsClient::begin(const char * host, uint16_t port, const char * url,
DEBUG_WEBSOCKETS("[WS-Client] Websocket Version: " WEBSOCKETS_VERSION "\n");
}
void WebSocketsClient::begin(std::string host, uint16_t port, std::string url, std::string protocol) {
void WebSocketsClient::begin(String host, uint16_t port, String url, String protocol) {
begin(host.c_str(), port, url.c_str(), protocol.c_str());
}
@ -110,7 +107,7 @@ void WebSocketsClient::beginSSL(const char * host, uint16_t port, const char * u
_CA_cert = NULL;
}
void WebSocketsClient::beginSSL(std::string host, uint16_t port, std::string url, std::string fingerprint, std::string protocol) {
void WebSocketsClient::beginSSL(String host, uint16_t port, String url, String fingerprint, String protocol) {
beginSSL(host.c_str(), port, url.c_str(), fingerprint.c_str(), protocol.c_str());
}
@ -156,7 +153,7 @@ void WebSocketsClient::beginSocketIO(const char * host, uint16_t port, const cha
_client.isSocketIO = true;
}
void WebSocketsClient::beginSocketIO(std::string host, uint16_t port, std::string url, std::string protocol) {
void WebSocketsClient::beginSocketIO(String host, uint16_t port, String url, String protocol) {
beginSocketIO(host.c_str(), port, url.c_str(), protocol.c_str());
}
@ -168,7 +165,7 @@ void WebSocketsClient::beginSocketIOSSL(const char * host, uint16_t port, const
_fingerprint = SSL_FINGERPRINT_NULL;
}
void WebSocketsClient::beginSocketIOSSL(std::string host, uint16_t port, std::string url, std::string protocol) {
void WebSocketsClient::beginSocketIOSSL(String host, uint16_t port, String url, String protocol) {
beginSocketIOSSL(host.c_str(), port, url.c_str(), protocol.c_str());
}
@ -323,7 +320,7 @@ bool WebSocketsClient::sendTXT(const char * payload, size_t length) {
return sendTXT((uint8_t *)payload, length);
}
bool WebSocketsClient::sendTXT(std::string & payload) {
bool WebSocketsClient::sendTXT(String & payload) {
return sendTXT((uint8_t *)payload.c_str(), payload.length());
}
@ -368,7 +365,7 @@ bool WebSocketsClient::sendPing(uint8_t * payload, size_t length) {
return false;
}
bool WebSocketsClient::sendPing(std::string & payload) {
bool WebSocketsClient::sendPing(String & payload) {
return sendPing((uint8_t *)payload.c_str(), payload.length());
}
@ -389,7 +386,7 @@ void WebSocketsClient::disconnect(void) {
*/
void WebSocketsClient::setAuthorization(const char * user, const char * password) {
if(user && password) {
std::string auth = user;
String auth = user;
auth += ":";
auth += password;
_client.base64Authorization = base64_encode((uint8_t *)auth.c_str(), auth.length());
@ -568,13 +565,13 @@ void WebSocketsClient::handleClientData(void) {
if(len > 0) {
switch(_client.status) {
case WSC_HEADER: {
std::string headerLine = _client.tcp->readStringUntil('\n');
String headerLine = _client.tcp->readStringUntil('\n');
handleHeader(&_client, &headerLine);
} break;
case WSC_BODY: {
char buf[256] = { 0 };
_client.tcp->readBytes(&buf[0], std::min((size_t)len, sizeof(buf)));
std::string bodyLine = buf;
String bodyLine = buf;
handleHeader(&_client, &bodyLine);
} break;
case WSC_CONNECTED:
@ -610,9 +607,9 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
unsigned long start = micros();
#endif
std::string handshake;
String handshake;
bool ws_header = true;
std::string url = client->cUrl;
String url = client->cUrl;
if(client->isSocketIO) {
if(client->cSessionId.length() == 0) {
@ -685,13 +682,13 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
* handle the WebSocket header reading
* @param client WSclient_t * ptr to the client struct
*/
void WebSocketsClient::handleHeader(WSclient_t * client, std::string * headerLine) {
void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
headerLine->trim(); // remove \r
// this code handels the http body for Socket.IO V3 requests
if(headerLine->length() > 0 && client->isSocketIO && client->status == WSC_BODY && client->cSessionId.length() == 0) {
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] socket.io json: %s\n", headerLine->c_str());
std::string sid_begin = WEBSOCKETS_STRING("\"sid\":\"");
String sid_begin = WEBSOCKETS_STRING("\"sid\":\"");
if(headerLine->indexOf(sid_begin) > -1) {
int start = headerLine->indexOf(sid_begin) + sid_begin.length();
int end = headerLine->indexOf('"', start);
@ -711,8 +708,8 @@ void WebSocketsClient::handleHeader(WSclient_t * client, std::string * headerLin
// "HTTP/1.1 101 Switching Protocols"
client->cCode = headerLine->substring(9, headerLine->indexOf(' ', 9)).toInt();
} else if(headerLine->indexOf(':') >= 0) {
std::string headerName = headerLine->substring(0, headerLine->indexOf(':'));
std::string headerValue = headerLine->substring(headerLine->indexOf(':') + 1);
String headerName = headerLine->substring(0, headerLine->indexOf(':'));
String headerValue = headerLine->substring(headerLine->indexOf(':') + 1);
// remove space in the beginning (RFC2616)
if(headerValue[0] == ' ') {
@ -803,7 +800,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client, std::string * headerLin
ok = false;
} else {
// generate Sec-WebSocket-Accept key for check
std::string sKey = acceptKey(client->cKey);
String sKey = acceptKey(client->cKey);
if(sKey != client->cAccept) {
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Sec-WebSocket-Accept is wrong\n");
ok = false;

View File

@ -25,8 +25,6 @@
#ifndef WEBSOCKETSCLIENT_H_
#define WEBSOCKETSCLIENT_H_
#include <string>
#include "WebSockets.h"
class WebSocketsClient : protected WebSockets {
@ -41,13 +39,13 @@ class WebSocketsClient : protected WebSockets {
virtual ~WebSocketsClient(void);
void begin(const char * host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
void begin(std::string host, uint16_t port, std::string url = "/", std::string protocol = "arduino");
void begin(String host, uint16_t port, String url = "/", String protocol = "arduino");
void begin(IPAddress host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
#if defined(HAS_SSL)
#ifdef SSL_AXTLS
void beginSSL(const char * host, uint16_t port, const char * url = "/", const char * fingerprint = "", const char * protocol = "arduino");
void beginSSL(std::string host, uint16_t port, std::string url = "/", std::string fingerprint = "", std::string protocol = "arduino");
void beginSSL(String host, uint16_t port, String url = "/", String fingerprint = "", String protocol = "arduino");
#else
void beginSSL(const char * host, uint16_t port, const char * url = "/", const uint8_t * fingerprint = NULL, const char * protocol = "arduino");
void beginSslWithCA(const char * host, uint16_t port, const char * url = "/", BearSSL::X509List * CA_cert = NULL, const char * protocol = "arduino");
@ -58,11 +56,11 @@ class WebSocketsClient : protected WebSockets {
#endif
void beginSocketIO(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
void beginSocketIO(std::string host, uint16_t port, std::string url = "/socket.io/?EIO=3", std::string protocol = "arduino");
void beginSocketIO(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
#if defined(HAS_SSL)
void beginSocketIOSSL(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
void beginSocketIOSSL(std::string host, uint16_t port, std::string url = "/socket.io/?EIO=3", std::string protocol = "arduino");
void beginSocketIOSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
void beginSocketIOSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * CA_cert = NULL, const char * protocol = "arduino");
#if defined(SSL_BARESSL)
@ -83,14 +81,14 @@ class WebSocketsClient : protected WebSockets {
bool sendTXT(const uint8_t * payload, size_t length = 0);
bool sendTXT(char * payload, size_t length = 0, bool headerToPayload = false);
bool sendTXT(const char * payload, size_t length = 0);
bool sendTXT(std::string & payload);
bool sendTXT(String & payload);
bool sendTXT(char payload);
bool sendBIN(uint8_t * payload, size_t length, bool headerToPayload = false);
bool sendBIN(const uint8_t * payload, size_t length);
bool sendPing(uint8_t * payload = NULL, size_t length = 0);
bool sendPing(std::string & payload);
bool sendPing(String & payload);
void disconnect(void);
@ -107,12 +105,12 @@ class WebSocketsClient : protected WebSockets {
bool isConnected(void);
protected:
std::string _host;
String _host;
uint16_t _port;
#if defined(HAS_SSL)
#ifdef SSL_AXTLS
std::string _fingerprint;
String _fingerprint;
const char * _CA_cert;
#define SSL_FINGERPRINT_IS_SET (_fingerprint.length())
#define SSL_FINGERPRINT_NULL ""
@ -144,7 +142,7 @@ class WebSocketsClient : protected WebSockets {
#endif
void sendHeader(WSclient_t * client);
void handleHeader(WSclient_t * client, std::string * headerLine);
void handleHeader(WSclient_t * client, String * headerLine);
void connectedCb();
void connectFailedCb();

View File

@ -22,13 +22,10 @@
*
*/
#include "WebSockets.h"
#include "WebSocketsServer.h"
#include <string>
#include "WebSockets.h"
WebSocketsServerCore::WebSocketsServerCore(const std::string & origin, const std::string & protocol) {
WebSocketsServerCore::WebSocketsServerCore(const String & origin, const String & protocol) {
_origin = origin;
_protocol = protocol;
_runnning = false;
@ -43,7 +40,7 @@ WebSocketsServerCore::WebSocketsServerCore(const std::string & origin, const std
_mandatoryHttpHeaderCount = 0;
}
WebSocketsServer::WebSocketsServer(uint16_t port, const std::string & origin, const std::string & protocol)
WebSocketsServer::WebSocketsServer(uint16_t port, const String & origin, const String & protocol)
: WebSocketsServerCore(origin, protocol) {
_port = port;
@ -133,7 +130,7 @@ void WebSocketsServerCore::onValidateHttpHeader(
delete[] _mandatoryHttpHeaders;
_mandatoryHttpHeaderCount = mandatoryHttpHeaderCount;
_mandatoryHttpHeaders = new std::string[_mandatoryHttpHeaderCount];
_mandatoryHttpHeaders = new String[_mandatoryHttpHeaderCount];
for(size_t i = 0; i < _mandatoryHttpHeaderCount; i++) {
_mandatoryHttpHeaders[i] = mandatoryHttpHeaders[i];
@ -174,7 +171,7 @@ bool WebSocketsServerCore::sendTXT(uint8_t num, const char * payload, size_t len
return sendTXT(num, (uint8_t *)payload, length);
}
bool WebSocketsServerCore::sendTXT(uint8_t num, std::string & payload) {
bool WebSocketsServerCore::sendTXT(uint8_t num, String & payload) {
return sendTXT(num, (uint8_t *)payload.c_str(), payload.length());
}
@ -216,7 +213,7 @@ bool WebSocketsServerCore::broadcastTXT(const char * payload, size_t length) {
return broadcastTXT((uint8_t *)payload, length);
}
bool WebSocketsServerCore::broadcastTXT(std::string & payload) {
bool WebSocketsServerCore::broadcastTXT(String & payload) {
return broadcastTXT((uint8_t *)payload.c_str(), payload.length());
}
@ -287,7 +284,7 @@ bool WebSocketsServerCore::sendPing(uint8_t num, uint8_t * payload, size_t lengt
return false;
}
bool WebSocketsServerCore::sendPing(uint8_t num, std::string & payload) {
bool WebSocketsServerCore::sendPing(uint8_t num, String & payload) {
return sendPing(num, (uint8_t *)payload.c_str(), payload.length());
}
@ -312,7 +309,7 @@ bool WebSocketsServerCore::broadcastPing(uint8_t * payload, size_t length) {
return ret;
}
bool WebSocketsServerCore::broadcastPing(std::string & payload) {
bool WebSocketsServerCore::broadcastPing(String & payload) {
return broadcastPing((uint8_t *)payload.c_str(), payload.length());
}
@ -350,7 +347,7 @@ void WebSocketsServerCore::disconnect(uint8_t num) {
*/
void WebSocketsServerCore::setAuthorization(const char * user, const char * password) {
if(user && password) {
std::string auth = user;
String auth = user;
auth += ":";
auth += password;
_base64Authorization = base64_encode((uint8_t *)auth.c_str(), auth.length());
@ -665,7 +662,7 @@ void WebSocketsServerCore::handleClientData(void) {
//DEBUG_WEBSOCKETS("[WS-Server][%d][handleClientData] len: %d\n", client->num, len);
switch(client->status) {
case WSC_HEADER: {
std::string headerLine = client->tcp->readStringUntil('\n');
String headerLine = client->tcp->readStringUntil('\n');
handleHeader(client, &headerLine);
} break;
case WSC_CONNECTED:
@ -688,9 +685,9 @@ void WebSocketsServerCore::handleClientData(void) {
/*
* returns an indicator whether the given named header exists in the configured _mandatoryHttpHeaders collection
* @param headerName std::string ///< the name of the header being checked
* @param headerName String ///< the name of the header being checked
*/
bool WebSocketsServerCore::hasMandatoryHeader(std::string headerName) {
bool WebSocketsServerCore::hasMandatoryHeader(String headerName) {
for(size_t i = 0; i < _mandatoryHttpHeaderCount; i++) {
if(_mandatoryHttpHeaders[i].equalsIgnoreCase(headerName))
return true;
@ -701,9 +698,9 @@ bool WebSocketsServerCore::hasMandatoryHeader(std::string headerName) {
/**
* handles http header reading for WebSocket upgrade
* @param client WSclient_t * ///< pointer to the client struct
* @param headerLine std::string ///< the header being read / processed
* @param headerLine String ///< the header being read / processed
*/
void WebSocketsServerCore::handleHeader(WSclient_t * client, std::string * headerLine) {
void WebSocketsServerCore::handleHeader(WSclient_t * client, String * headerLine) {
static const char * NEW_LINE = "\r\n";
headerLine->trim(); // remove \r
@ -721,8 +718,8 @@ void WebSocketsServerCore::handleHeader(WSclient_t * client, std::string * heade
client->cMandatoryHeadersCount = 0;
} else if(headerLine->indexOf(':') >= 0) {
std::string headerName = headerLine->substring(0, headerLine->indexOf(':'));
std::string headerValue = headerLine->substring(headerLine->indexOf(':') + 1);
String headerName = headerLine->substring(0, headerLine->indexOf(':'));
String headerValue = headerLine->substring(headerLine->indexOf(':') + 1);
// remove space in the beginning (RFC2616)
if(headerValue[0] == ' ') {
@ -798,7 +795,7 @@ void WebSocketsServerCore::handleHeader(WSclient_t * client, std::string * heade
}
if(_base64Authorization.length() > 0) {
std::string auth = WEBSOCKETS_STRING("Basic ");
String auth = WEBSOCKETS_STRING("Basic ");
auth += _base64Authorization;
if(auth != client->base64Authorization) {
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] HTTP Authorization failed!\n", client->num);
@ -811,13 +808,13 @@ void WebSocketsServerCore::handleHeader(WSclient_t * client, std::string * heade
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] Websocket connection incoming.\n", client->num);
// generate Sec-WebSocket-Accept key
std::string sKey = acceptKey(client->cKey);
String sKey = acceptKey(client->cKey);
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] - sKey: %s\n", client->num, sKey.c_str());
client->status = WSC_CONNECTED;
std::string handshake = WEBSOCKETS_STRING(
String handshake = WEBSOCKETS_STRING(
"HTTP/1.1 101 Switching Protocols\r\n"
"Server: arduino-WebSocketsServer\r\n"
"Upgrade: websocket\r\n"

View File

@ -25,8 +25,6 @@
#ifndef WEBSOCKETSSERVER_H_
#define WEBSOCKETSSERVER_H_
#include <string>
#include "WebSockets.h"
#ifndef WEBSOCKETS_SERVER_CLIENT_MAX
@ -35,7 +33,7 @@
class WebSocketsServerCore : protected WebSockets {
public:
WebSocketsServerCore(const std::string & origin = "", const std::string & protocol = "arduino");
WebSocketsServerCore(const String & origin = "", const String & protocol = "arduino");
virtual ~WebSocketsServerCore(void);
void begin(void);
@ -43,10 +41,10 @@ class WebSocketsServerCore : protected WebSockets {
#ifdef __AVR__
typedef void (*WebSocketServerEvent)(uint8_t num, WStype_t type, uint8_t * payload, size_t length);
typedef bool (*WebSocketServerHttpHeaderValFunc)(std::string headerName, std::string headerValue);
typedef bool (*WebSocketServerHttpHeaderValFunc)(String headerName, String headerValue);
#else
typedef std::function<void(uint8_t num, WStype_t type, uint8_t * payload, size_t length)> WebSocketServerEvent;
typedef std::function<bool(std::string headerName, std::string headerValue)> WebSocketServerHttpHeaderValFunc;
typedef std::function<bool(String headerName, String headerValue)> WebSocketServerHttpHeaderValFunc;
#endif
void onEvent(WebSocketServerEvent cbEvent);
@ -59,13 +57,13 @@ class WebSocketsServerCore : protected WebSockets {
bool sendTXT(uint8_t num, const uint8_t * payload, size_t length = 0);
bool sendTXT(uint8_t num, char * payload, size_t length = 0, bool headerToPayload = false);
bool sendTXT(uint8_t num, const char * payload, size_t length = 0);
bool sendTXT(uint8_t num, std::string & payload);
bool sendTXT(uint8_t num, String & payload);
bool broadcastTXT(uint8_t * payload, size_t length = 0, bool headerToPayload = false);
bool broadcastTXT(const uint8_t * payload, size_t length = 0);
bool broadcastTXT(char * payload, size_t length = 0, bool headerToPayload = false);
bool broadcastTXT(const char * payload, size_t length = 0);
bool broadcastTXT(std::string & payload);
bool broadcastTXT(String & payload);
bool sendBIN(uint8_t num, uint8_t * payload, size_t length, bool headerToPayload = false);
bool sendBIN(uint8_t num, const uint8_t * payload, size_t length);
@ -74,10 +72,10 @@ class WebSocketsServerCore : protected WebSockets {
bool broadcastBIN(const uint8_t * payload, size_t length);
bool sendPing(uint8_t num, uint8_t * payload = NULL, size_t length = 0);
bool sendPing(uint8_t num, std::string & payload);
bool sendPing(uint8_t num, String & payload);
bool broadcastPing(uint8_t * payload = NULL, size_t length = 0);
bool broadcastPing(std::string & payload);
bool broadcastPing(String & payload);
void disconnect(void);
void disconnect(uint8_t num);
@ -103,10 +101,10 @@ class WebSocketsServerCore : protected WebSockets {
WSclient_t * newClient(WEBSOCKETS_NETWORK_CLASS * TCPclient);
protected:
std::string _origin;
std::string _protocol;
std::string _base64Authorization; ///< Base64 encoded Auth request
std::string * _mandatoryHttpHeaders;
String _origin;
String _protocol;
String _base64Authorization; ///< Base64 encoded Auth request
String * _mandatoryHttpHeaders;
size_t _mandatoryHttpHeaderCount;
WSclient_t _clients[WEBSOCKETS_SERVER_CLIENT_MAX];
@ -129,7 +127,7 @@ class WebSocketsServerCore : protected WebSockets {
void handleClientData(void);
#endif
void handleHeader(WSclient_t * client, std::string * headerLine);
void handleHeader(WSclient_t * client, String * headerLine);
void handleHBPing(WSclient_t * client); // send ping in specified intervals
@ -192,7 +190,7 @@ class WebSocketsServerCore : protected WebSockets {
* This mechanism can be used to enable custom authentication schemes e.g. test the value
* of a session cookie to determine if a user is logged on / authenticated
*/
virtual bool execHttpHeaderValidation(std::string headerName, std::string headerValue) {
virtual bool execHttpHeaderValidation(String headerName, String headerValue) {
if(_httpHeaderValidationFunc) {
//return the value of the custom http header validation function
return _httpHeaderValidationFunc(headerName, headerValue);
@ -213,14 +211,14 @@ class WebSocketsServerCore : protected WebSockets {
private:
/*
* returns an indicator whether the given named header exists in the configured _mandatoryHttpHeaders collection
* @param headerName std::string ///< the name of the header being checked
* @param headerName String ///< the name of the header being checked
*/
bool hasMandatoryHeader(std::string headerName);
bool hasMandatoryHeader(String headerName);
};
class WebSocketsServer : public WebSocketsServerCore {
public:
WebSocketsServer(uint16_t port, const std::string & origin = "", const std::string & protocol = "arduino");
WebSocketsServer(uint16_t port, const String & origin = "", const String & protocol = "arduino");
virtual ~WebSocketsServer(void);
void begin(void);

View File

@ -1,6 +1,6 @@
/**
* @file WebSocketsVersion.h
* @date 08.03.2021
* @date 09.02.2021
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
@ -25,12 +25,12 @@
#ifndef WEBSOCKETSVERSION_H_
#define WEBSOCKETSVERSION_H_
#define WEBSOCKETS_VERSION "2.3.6"
#define WEBSOCKETS_VERSION "2.3.5"
#define WEBSOCKETS_VERSION_MAJOR 2
#define WEBSOCKETS_VERSION_MINOR 3
#define WEBSOCKETS_VERSION_PATCH 6
#define WEBSOCKETS_VERSION_PATCH 5
#define WEBSOCKETS_VERSION_INT 2003006
#define WEBSOCKETS_VERSION_INT 2003005
#endif /* WEBSOCKETSVERSION_H_ */