mirror of
https://github.com/Links2004/arduinoWebSockets.git
synced 2025-07-16 00:32:06 +02:00
fix code style
This commit is contained in:
@ -25,7 +25,6 @@
|
|||||||
#include "WebSockets.h"
|
#include "WebSockets.h"
|
||||||
#include "WebSocketsClient.h"
|
#include "WebSocketsClient.h"
|
||||||
|
|
||||||
|
|
||||||
WebSocketsClient::WebSocketsClient() {
|
WebSocketsClient::WebSocketsClient() {
|
||||||
_cbEvent = NULL;
|
_cbEvent = NULL;
|
||||||
_client.num = 0;
|
_client.num = 0;
|
||||||
@ -85,7 +84,7 @@ void WebSocketsClient::begin(String host, uint16_t port, String url, String prot
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WebSocketsClient::begin(IPAddress host, uint16_t port, const char * url, const char * protocol) {
|
void WebSocketsClient::begin(IPAddress host, uint16_t port, const char * url, const char * protocol) {
|
||||||
return begin(host.toString().c_str(), port, url, protocol);
|
return begin(host.toString().c_str(), port, url, protocol);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
|
||||||
@ -128,10 +127,10 @@ void WebSocketsClient::beginSocketIOSSL(String host, uint16_t port, String url,
|
|||||||
*/
|
*/
|
||||||
void WebSocketsClient::loop(void) {
|
void WebSocketsClient::loop(void) {
|
||||||
if(!clientIsConnected(&_client)) {
|
if(!clientIsConnected(&_client)) {
|
||||||
// do not flood the server
|
// do not flood the server
|
||||||
if((millis() - _lastConnectionFail) < _reconnectInterval) {
|
if((millis() - _lastConnectionFail) < _reconnectInterval) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
|
||||||
if(_client.isSSL) {
|
if(_client.isSSL) {
|
||||||
@ -252,7 +251,6 @@ bool WebSocketsClient::sendPing(String & payload) {
|
|||||||
return sendPing((uint8_t *) payload.c_str(), payload.length());
|
return sendPing((uint8_t *) payload.c_str(), payload.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* disconnect one client
|
* disconnect one client
|
||||||
* @param num uint8_t client id
|
* @param num uint8_t client id
|
||||||
@ -273,7 +271,7 @@ void WebSocketsClient::setAuthorization(const char * user, const char * password
|
|||||||
String auth = user;
|
String auth = user;
|
||||||
auth += ":";
|
auth += ":";
|
||||||
auth += password;
|
auth += password;
|
||||||
_client.base64Authorization = base64_encode((uint8_t *)auth.c_str(), auth.length());
|
_client.base64Authorization = base64_encode((uint8_t *) auth.c_str(), auth.length());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -297,14 +295,13 @@ void WebSocketsClient::setExtraHeaders(const char * extraHeaders) {
|
|||||||
_client.extraHeaders = extraHeaders;
|
_client.extraHeaders = extraHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set the reconnect Interval
|
* set the reconnect Interval
|
||||||
* how long to wait after a connection initiate failed
|
* how long to wait after a connection initiate failed
|
||||||
* @param time in ms
|
* @param time in ms
|
||||||
*/
|
*/
|
||||||
void WebSocketsClient::setReconnectInterval(unsigned long time) {
|
void WebSocketsClient::setReconnectInterval(unsigned long time) {
|
||||||
_reconnectInterval = time;
|
_reconnectInterval = time;
|
||||||
}
|
}
|
||||||
|
|
||||||
//#################################################################################
|
//#################################################################################
|
||||||
@ -328,9 +325,9 @@ void WebSocketsClient::messageReceived(WSclient_t * client, WSopcode_t opcode, u
|
|||||||
case WSop_binary:
|
case WSop_binary:
|
||||||
type = fin ? WStype_BIN : WStype_FRAGMENT_BIN_START;
|
type = fin ? WStype_BIN : WStype_FRAGMENT_BIN_START;
|
||||||
break;
|
break;
|
||||||
case WSop_continuation:
|
case WSop_continuation:
|
||||||
type = fin ? WStype_FRAGMENT_FIN : WStype_FRAGMENT;
|
type = fin ? WStype_FRAGMENT_FIN : WStype_FRAGMENT;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
runCbEvent(type, payload, length);
|
runCbEvent(type, payload, length);
|
||||||
@ -429,8 +426,7 @@ void WebSocketsClient::handleClientData(void) {
|
|||||||
int len = _client.tcp->available();
|
int len = _client.tcp->available();
|
||||||
if(len > 0) {
|
if(len > 0) {
|
||||||
switch(_client.status) {
|
switch(_client.status) {
|
||||||
case WSC_HEADER:
|
case WSC_HEADER: {
|
||||||
{
|
|
||||||
String headerLine = _client.tcp->readStringUntil('\n');
|
String headerLine = _client.tcp->readStringUntil('\n');
|
||||||
handleHeader(&_client, &headerLine);
|
handleHeader(&_client, &headerLine);
|
||||||
}
|
}
|
||||||
@ -449,14 +445,13 @@ void WebSocketsClient::handleClientData(void) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* send the WebSocket header to Server
|
* send the WebSocket header to Server
|
||||||
* @param client WSclient_t * ptr to the client struct
|
* @param client WSclient_t * ptr to the client struct
|
||||||
*/
|
*/
|
||||||
void WebSocketsClient::sendHeader(WSclient_t * client) {
|
void WebSocketsClient::sendHeader(WSclient_t * client) {
|
||||||
|
|
||||||
static const char * NEW_LINE = "\r\n";
|
static const char * NEW_LINE = "\r\n";
|
||||||
|
|
||||||
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header...\n");
|
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header...\n");
|
||||||
|
|
||||||
@ -477,64 +472,64 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
|
|||||||
String url = client->cUrl;
|
String url = client->cUrl;
|
||||||
|
|
||||||
if(client->isSocketIO) {
|
if(client->isSocketIO) {
|
||||||
if(client->cSessionId.length() == 0) {
|
if(client->cSessionId.length() == 0) {
|
||||||
url += WEBSOCKETS_STRING("&transport=polling");
|
url += WEBSOCKETS_STRING("&transport=polling");
|
||||||
ws_header = false;
|
ws_header = false;
|
||||||
} else {
|
} else {
|
||||||
url += WEBSOCKETS_STRING("&transport=websocket&sid=");
|
url += WEBSOCKETS_STRING("&transport=websocket&sid=");
|
||||||
url += client->cSessionId;
|
url += client->cSessionId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handshake = WEBSOCKETS_STRING("GET ");
|
handshake = WEBSOCKETS_STRING("GET ");
|
||||||
handshake += url + WEBSOCKETS_STRING(" HTTP/1.1\r\n"
|
handshake += url + WEBSOCKETS_STRING(" HTTP/1.1\r\n"
|
||||||
"Host: ");
|
"Host: ");
|
||||||
handshake += _host + ":" + _port + NEW_LINE;
|
handshake += _host + ":" + _port + NEW_LINE;
|
||||||
|
|
||||||
if(ws_header) {
|
if(ws_header) {
|
||||||
handshake += WEBSOCKETS_STRING("Connection: Upgrade\r\n"
|
handshake += WEBSOCKETS_STRING("Connection: Upgrade\r\n"
|
||||||
"Upgrade: websocket\r\n"
|
"Upgrade: websocket\r\n"
|
||||||
"Sec-WebSocket-Version: 13\r\n"
|
"Sec-WebSocket-Version: 13\r\n"
|
||||||
"Sec-WebSocket-Key: ");
|
"Sec-WebSocket-Key: ");
|
||||||
handshake += client->cKey + NEW_LINE;
|
handshake += client->cKey + NEW_LINE;
|
||||||
|
|
||||||
if(client->cProtocol.length() > 0) {
|
if(client->cProtocol.length() > 0) {
|
||||||
handshake += WEBSOCKETS_STRING("Sec-WebSocket-Protocol: ");
|
handshake += WEBSOCKETS_STRING("Sec-WebSocket-Protocol: ");
|
||||||
handshake +=client->cProtocol + NEW_LINE;
|
handshake += client->cProtocol + NEW_LINE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(client->cExtensions.length() > 0) {
|
if(client->cExtensions.length() > 0) {
|
||||||
handshake += WEBSOCKETS_STRING("Sec-WebSocket-Extensions: ");
|
handshake += WEBSOCKETS_STRING("Sec-WebSocket-Extensions: ");
|
||||||
handshake +=client->cExtensions + NEW_LINE;
|
handshake += client->cExtensions + NEW_LINE;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
handshake += WEBSOCKETS_STRING("Connection: keep-alive\r\n");
|
handshake += WEBSOCKETS_STRING("Connection: keep-alive\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// add extra headers; by default this includes "Origin: file://"
|
// add extra headers; by default this includes "Origin: file://"
|
||||||
if (client->extraHeaders) {
|
if(client->extraHeaders) {
|
||||||
handshake += client->extraHeaders + NEW_LINE;
|
handshake += client->extraHeaders + NEW_LINE;
|
||||||
}
|
}
|
||||||
|
|
||||||
handshake += WEBSOCKETS_STRING("User-Agent: arduino-WebSocket-Client\r\n");
|
handshake += WEBSOCKETS_STRING("User-Agent: arduino-WebSocket-Client\r\n");
|
||||||
|
|
||||||
if(client->base64Authorization.length() > 0) {
|
if(client->base64Authorization.length() > 0) {
|
||||||
handshake += WEBSOCKETS_STRING("Authorization: Basic ");
|
handshake += WEBSOCKETS_STRING("Authorization: Basic ");
|
||||||
handshake += client->base64Authorization + NEW_LINE;
|
handshake += client->base64Authorization + NEW_LINE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(client->plainAuthorization.length() > 0) {
|
if(client->plainAuthorization.length() > 0) {
|
||||||
handshake += WEBSOCKETS_STRING("Authorization: ");
|
handshake += WEBSOCKETS_STRING("Authorization: ");
|
||||||
handshake += client->plainAuthorization + NEW_LINE;
|
handshake += client->plainAuthorization + NEW_LINE;
|
||||||
}
|
}
|
||||||
|
|
||||||
handshake += NEW_LINE;
|
handshake += NEW_LINE;
|
||||||
|
|
||||||
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] handshake %s", (uint8_t*)handshake.c_str());
|
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] handshake %s", (uint8_t* )handshake.c_str());
|
||||||
write(client, (uint8_t*)handshake.c_str(), handshake.length());
|
write(client, (uint8_t*) handshake.c_str(), handshake.length());
|
||||||
|
|
||||||
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
||||||
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine)));
|
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine)));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header... Done (%luus).\n", (micros() - start));
|
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] sending header... Done (%luus).\n", (micros() - start));
|
||||||
@ -547,50 +542,50 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
|
|||||||
*/
|
*/
|
||||||
void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
|
void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
|
||||||
|
|
||||||
headerLine->trim(); // remove \r
|
headerLine->trim(); // remove \r
|
||||||
|
|
||||||
if(headerLine->length() > 0) {
|
if(headerLine->length() > 0) {
|
||||||
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] RX: %s\n", headerLine->c_str());
|
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] RX: %s\n", headerLine->c_str());
|
||||||
|
|
||||||
if(headerLine->startsWith(WEBSOCKETS_STRING("HTTP/1."))) {
|
if(headerLine->startsWith(WEBSOCKETS_STRING("HTTP/1."))) {
|
||||||
// "HTTP/1.1 101 Switching Protocols"
|
// "HTTP/1.1 101 Switching Protocols"
|
||||||
client->cCode = headerLine->substring(9, headerLine->indexOf(' ', 9)).toInt();
|
client->cCode = headerLine->substring(9, headerLine->indexOf(' ', 9)).toInt();
|
||||||
} else if(headerLine->indexOf(':')) {
|
} else if(headerLine->indexOf(':')) {
|
||||||
String headerName = headerLine->substring(0, headerLine->indexOf(':'));
|
String headerName = headerLine->substring(0, headerLine->indexOf(':'));
|
||||||
String headerValue = headerLine->substring(headerLine->indexOf(':') + 1);
|
String headerValue = headerLine->substring(headerLine->indexOf(':') + 1);
|
||||||
|
|
||||||
// remove space in the beginning (RFC2616)
|
// remove space in the beginning (RFC2616)
|
||||||
if(headerValue[0] == ' ') {
|
if(headerValue[0] == ' ') {
|
||||||
headerValue.remove(0, 1);
|
headerValue.remove(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Connection"))) {
|
if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Connection"))) {
|
||||||
if(headerValue.equalsIgnoreCase(WEBSOCKETS_STRING("upgrade"))) {
|
if(headerValue.equalsIgnoreCase(WEBSOCKETS_STRING("upgrade"))) {
|
||||||
client->cIsUpgrade = true;
|
client->cIsUpgrade = true;
|
||||||
}
|
}
|
||||||
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Upgrade"))) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Upgrade"))) {
|
||||||
if(headerValue.equalsIgnoreCase(WEBSOCKETS_STRING("websocket"))) {
|
if(headerValue.equalsIgnoreCase(WEBSOCKETS_STRING("websocket"))) {
|
||||||
client->cIsWebsocket = true;
|
client->cIsWebsocket = true;
|
||||||
}
|
}
|
||||||
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Accept"))) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Accept"))) {
|
||||||
client->cAccept = headerValue;
|
client->cAccept = headerValue;
|
||||||
client->cAccept.trim(); // see rfc6455
|
client->cAccept.trim(); // see rfc6455
|
||||||
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Protocol"))) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Protocol"))) {
|
||||||
client->cProtocol = headerValue;
|
client->cProtocol = headerValue;
|
||||||
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Extensions"))) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Extensions"))) {
|
||||||
client->cExtensions = headerValue;
|
client->cExtensions = headerValue;
|
||||||
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Version"))) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Version"))) {
|
||||||
client->cVersion = headerValue.toInt();
|
client->cVersion = headerValue.toInt();
|
||||||
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Set-Cookie"))) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Set-Cookie"))) {
|
||||||
if(headerValue.indexOf(WEBSOCKETS_STRING("HttpOnly")) > -1) {
|
if(headerValue.indexOf(WEBSOCKETS_STRING("HttpOnly")) > -1) {
|
||||||
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1, headerValue.indexOf(";"));
|
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1, headerValue.indexOf(";"));
|
||||||
} else {
|
} else {
|
||||||
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1);
|
client->cSessionId = headerValue.substring(headerValue.indexOf('=') + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Header error (%s)\n", headerLine->c_str());
|
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Header error (%s)\n", headerLine->c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
(*headerLine) = "";
|
(*headerLine) = "";
|
||||||
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
||||||
@ -657,17 +652,17 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
|
|||||||
|
|
||||||
runCbEvent(WStype_CONNECTED, (uint8_t *) client->cUrl.c_str(), client->cUrl.length());
|
runCbEvent(WStype_CONNECTED, (uint8_t *) client->cUrl.c_str(), client->cUrl.length());
|
||||||
|
|
||||||
} else if(clientIsConnected(client) && client->isSocketIO && client->cSessionId.length() > 0) {
|
} else if(clientIsConnected(client) && client->isSocketIO && client->cSessionId.length() > 0) {
|
||||||
sendHeader(client);
|
sendHeader(client);
|
||||||
} else {
|
} else {
|
||||||
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] no Websocket connection close.\n");
|
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] no Websocket connection close.\n");
|
||||||
_lastConnectionFail = millis();
|
_lastConnectionFail = millis();
|
||||||
if(clientIsConnected(client)) {
|
if(clientIsConnected(client)) {
|
||||||
write(client, "This is a webSocket client!");
|
write(client, "This is a webSocket client!");
|
||||||
}
|
}
|
||||||
clientDisconnect(client);
|
clientDisconnect(client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebSocketsClient::connectedCb() {
|
void WebSocketsClient::connectedCb() {
|
||||||
@ -676,15 +671,15 @@ void WebSocketsClient::connectedCb() {
|
|||||||
|
|
||||||
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
||||||
_client.tcp->onDisconnect(std::bind([](WebSocketsClient * c, AsyncTCPbuffer * obj, WSclient_t * client) -> bool {
|
_client.tcp->onDisconnect(std::bind([](WebSocketsClient * c, AsyncTCPbuffer * obj, WSclient_t * client) -> bool {
|
||||||
DEBUG_WEBSOCKETS("[WS-Server][%d] Disconnect client\n", client->num);
|
DEBUG_WEBSOCKETS("[WS-Server][%d] Disconnect client\n", client->num);
|
||||||
client->status = WSC_NOT_CONNECTED;
|
client->status = WSC_NOT_CONNECTED;
|
||||||
client->tcp = NULL;
|
client->tcp = NULL;
|
||||||
|
|
||||||
// reconnect
|
// reconnect
|
||||||
c->asyncConnect();
|
c->asyncConnect();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}, this, std::placeholders::_1, &_client));
|
}, this, std::placeholders::_1, &_client));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
_client.status = WSC_HEADER;
|
_client.status = WSC_HEADER;
|
||||||
@ -711,7 +706,6 @@ void WebSocketsClient::connectedCb() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void WebSocketsClient::connectFailedCb() {
|
void WebSocketsClient::connectFailedCb() {
|
||||||
DEBUG_WEBSOCKETS("[WS-Client] connection to %s:%u Faild\n", _host.c_str(), _port);
|
DEBUG_WEBSOCKETS("[WS-Client] connection to %s:%u Faild\n", _host.c_str(), _port);
|
||||||
}
|
}
|
||||||
@ -730,26 +724,26 @@ void WebSocketsClient::asyncConnect() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tcpclient->onDisconnect([](void *obj, AsyncClient* c) {
|
tcpclient->onDisconnect([](void *obj, AsyncClient* c) {
|
||||||
c->free();
|
c->free();
|
||||||
delete c;
|
delete c;
|
||||||
});
|
});
|
||||||
|
|
||||||
tcpclient->onConnect(std::bind([](WebSocketsClient * ws , AsyncClient * tcp) {
|
tcpclient->onConnect(std::bind([](WebSocketsClient * ws , AsyncClient * tcp) {
|
||||||
ws->_client.tcp = new AsyncTCPbuffer(tcp);
|
ws->_client.tcp = new AsyncTCPbuffer(tcp);
|
||||||
if(!ws->_client.tcp) {
|
if(!ws->_client.tcp) {
|
||||||
DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!\n");
|
DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!\n");
|
||||||
ws->connectFailedCb();
|
ws->connectFailedCb();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ws->connectedCb();
|
ws->connectedCb();
|
||||||
}, this, std::placeholders::_2));
|
}, this, std::placeholders::_2));
|
||||||
|
|
||||||
tcpclient->onError(std::bind([](WebSocketsClient * ws , AsyncClient * tcp) {
|
tcpclient->onError(std::bind([](WebSocketsClient * ws , AsyncClient * tcp) {
|
||||||
ws->connectFailedCb();
|
ws->connectFailedCb();
|
||||||
|
|
||||||
// reconnect
|
// reconnect
|
||||||
ws->asyncConnect();
|
ws->asyncConnect();
|
||||||
}, this, std::placeholders::_2));
|
}, this, std::placeholders::_2));
|
||||||
|
|
||||||
if(!tcpclient->connect(_host.c_str(), _port)) {
|
if(!tcpclient->connect(_host.c_str(), _port)) {
|
||||||
connectFailedCb();
|
connectFailedCb();
|
||||||
|
@ -712,60 +712,60 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
|
|||||||
|
|
||||||
static const char * NEW_LINE = "\r\n";
|
static const char * NEW_LINE = "\r\n";
|
||||||
|
|
||||||
headerLine->trim(); // remove \r
|
headerLine->trim(); // remove \r
|
||||||
|
|
||||||
if(headerLine->length() > 0) {
|
if(headerLine->length() > 0) {
|
||||||
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] RX: %s\n", client->num, headerLine->c_str());
|
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] RX: %s\n", client->num, headerLine->c_str());
|
||||||
|
|
||||||
// websocket requests always start with GET see rfc6455
|
// websocket requests always start with GET see rfc6455
|
||||||
if(headerLine->startsWith("GET ")) {
|
if(headerLine->startsWith("GET ")) {
|
||||||
|
|
||||||
// cut URL out
|
// cut URL out
|
||||||
client->cUrl = headerLine->substring(4, headerLine->indexOf(' ', 4));
|
client->cUrl = headerLine->substring(4, headerLine->indexOf(' ', 4));
|
||||||
|
|
||||||
//reset non-websocket http header validation state for this client
|
//reset non-websocket http header validation state for this client
|
||||||
client->cHttpHeadersValid = true;
|
client->cHttpHeadersValid = true;
|
||||||
client->cMandatoryHeadersCount = 0;
|
client->cMandatoryHeadersCount = 0;
|
||||||
|
|
||||||
} else if(headerLine->indexOf(':')) {
|
} else if(headerLine->indexOf(':')) {
|
||||||
String headerName = headerLine->substring(0, headerLine->indexOf(':'));
|
String headerName = headerLine->substring(0, headerLine->indexOf(':'));
|
||||||
String headerValue = headerLine->substring(headerLine->indexOf(':') + 1);
|
String headerValue = headerLine->substring(headerLine->indexOf(':') + 1);
|
||||||
|
|
||||||
// remove space in the beginning (RFC2616)
|
// remove space in the beginning (RFC2616)
|
||||||
if(headerValue[0] == ' ') {
|
if(headerValue[0] == ' ') {
|
||||||
headerValue.remove(0, 1);
|
headerValue.remove(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Connection"))) {
|
if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Connection"))) {
|
||||||
headerValue.toLowerCase();
|
headerValue.toLowerCase();
|
||||||
if(headerValue.indexOf(WEBSOCKETS_STRING("upgrade")) >= 0) {
|
if(headerValue.indexOf(WEBSOCKETS_STRING("upgrade")) >= 0) {
|
||||||
client->cIsUpgrade = true;
|
client->cIsUpgrade = true;
|
||||||
}
|
}
|
||||||
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Upgrade"))) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Upgrade"))) {
|
||||||
if(headerValue.equalsIgnoreCase(WEBSOCKETS_STRING("websocket"))) {
|
if(headerValue.equalsIgnoreCase(WEBSOCKETS_STRING("websocket"))) {
|
||||||
client->cIsWebsocket = true;
|
client->cIsWebsocket = true;
|
||||||
}
|
}
|
||||||
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Version"))) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Version"))) {
|
||||||
client->cVersion = headerValue.toInt();
|
client->cVersion = headerValue.toInt();
|
||||||
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Key"))) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Key"))) {
|
||||||
client->cKey = headerValue;
|
client->cKey = headerValue;
|
||||||
client->cKey.trim(); // see rfc6455
|
client->cKey.trim(); // see rfc6455
|
||||||
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Protocol"))) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Protocol"))) {
|
||||||
client->cProtocol = headerValue;
|
client->cProtocol = headerValue;
|
||||||
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Extensions"))) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Sec-WebSocket-Extensions"))) {
|
||||||
client->cExtensions = headerValue;
|
client->cExtensions = headerValue;
|
||||||
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Authorization"))) {
|
} else if(headerName.equalsIgnoreCase(WEBSOCKETS_STRING("Authorization"))) {
|
||||||
client->base64Authorization = headerValue;
|
client->base64Authorization = headerValue;
|
||||||
} else {
|
} else {
|
||||||
client->cHttpHeadersValid &= execHttpHeaderValidation(headerName, headerValue);
|
client->cHttpHeadersValid &= execHttpHeaderValidation(headerName, headerValue);
|
||||||
if (_mandatoryHttpHeaderCount > 0 && hasMandatoryHeader(headerName)) {
|
if(_mandatoryHttpHeaderCount > 0 && hasMandatoryHeader(headerName)) {
|
||||||
client->cMandatoryHeadersCount++;
|
client->cMandatoryHeadersCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Header error (%s)\n", headerLine->c_str());
|
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Header error (%s)\n", headerLine->c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
(*headerLine) = "";
|
(*headerLine) = "";
|
||||||
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
||||||
@ -787,11 +787,11 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
|
|||||||
|
|
||||||
bool ok = (client->cIsUpgrade && client->cIsWebsocket);
|
bool ok = (client->cIsUpgrade && client->cIsWebsocket);
|
||||||
|
|
||||||
if(ok) {
|
if(ok) {
|
||||||
if(client->cUrl.length() == 0) {
|
if(client->cUrl.length() == 0) {
|
||||||
ok = false;
|
ok = false;
|
||||||
}
|
}
|
||||||
if(client->cKey.length() == 0) {
|
if(client->cKey.length() == 0) {
|
||||||
ok = false;
|
ok = false;
|
||||||
}
|
}
|
||||||
if(client->cVersion != 13) {
|
if(client->cVersion != 13) {
|
||||||
|
Reference in New Issue
Block a user