add HTTP Basic Authorization to WS Client and Server

see: #55
This commit is contained in:
Markus Sattler
2016-02-17 17:56:03 +01:00
parent 2d87bfa3d6
commit c57a4c19ab
6 changed files with 106 additions and 2 deletions

View File

@ -25,6 +25,7 @@
#include "WebSockets.h"
#include "WebSocketsClient.h"
WebSocketsClient::WebSocketsClient() {
_cbEvent = NULL;
_client.num = 0;
@ -60,6 +61,7 @@ void WebSocketsClient::begin(const char *host, uint16_t port, const char * url,
_client.cProtocol = protocol;
_client.cExtensions = "";
_client.cVersion = 0;
_client.base64Authorization = "";
#ifdef ESP8266
randomSeed(RANDOM_REG32);
@ -202,6 +204,30 @@ void WebSocketsClient::disconnect(void) {
}
}
/**
* set the Authorizatio for the http request
* @param user const char *
* @param password const char *
*/
void WebSocketsClient::setAuthorization(const char * user, const char * password) {
if(user && password) {
String auth = user;
auth += ":";
auth += password;
_client.base64Authorization = base64_encode((uint8_t *)auth.c_str(), auth.length());
}
}
/**
* set the Authorizatio for the http request
* @param auth const char * base64
*/
void WebSocketsClient::setAuthorization(const char * auth) {
if(auth) {
_client.base64Authorization = auth;
}
}
//#################################################################################
//#################################################################################
//#################################################################################
@ -374,6 +400,10 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
handshake += "Sec-WebSocket-Extensions: " + client->cExtensions + "\r\n";
}
if(client->base64Authorization.length() > 0) {
handshake += "Authorization: Basic " + client->base64Authorization + "\r\n";
}
handshake += "\r\n";
client->tcp->write(handshake.c_str(), handshake.length());