Skip to content

Commit

Permalink
ws: do not print raw data
Browse files Browse the repository at this point in the history
* error code is not buffered through our handler object.
  while espasync code does strlen, what it returns is mostly random.
  (old request parts, weird data, etc.)
* reworked buffer class to avoid an extra allocation on top of the
  existing vector data buffer. plus, simple function pointer as well
  • Loading branch information
mcspr committed Oct 29, 2022
1 parent f1d5b62 commit 7296eca
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 112 deletions.
86 changes: 86 additions & 0 deletions code/espurna/libs/WebSocketIncomingBuffer.h
@@ -0,0 +1,86 @@
/*
WebSocketIncommingBuffer
Code by Hermann Kraus (https://bitbucket.org/hermr2d2/)
and slightly modified.
*/

#pragma once

#include <ESPAsyncWebServer.h>

struct WebSocketIncomingBuffer {
static constexpr size_t MessageSizeMax { 4096 };
using Callback = void(*)(AsyncWebSocketClient* client, uint8_t* data, size_t len);

WebSocketIncomingBuffer() = delete;
WebSocketIncomingBuffer(Callback cb, bool terminate_string, bool cb_on_fragments) :
_cb(cb),
_cb_on_fragments(cb_on_fragments),
_terminate_string(terminate_string)
{}

explicit WebSocketIncomingBuffer(Callback cb) :
WebSocketIncomingBuffer(cb, true, false)
{}

void data_event(AsyncWebSocketClient *client, AwsFrameInfo *info, uint8_t *data, size_t len) {
if ((info->final || _cb_on_fragments)
&& !_terminate_string
&& info->index == 0
&& info->len == len)
{

/* The whole message is in a single frame and we got all of it's
data therefore we can parse it without copying the data first.*/
_cb(client, data, len);
return;
}

if (info->len > MessageSizeMax) {
return;
}

/* Check if previous fragment was discarded because it was too long. */
//if (!_cb_on_fragments && info->num > 0 && !_buffer) return;

if (info->index == 0) {
if (_cb_on_fragments) {
_buffer.reserve(info->len + 1);
} else {
/* The current fragment would lead to a message which is
too long. So discard everything received so far. */
const size_t reserve = info->len + _buffer.size() + 1;
if (reserve > MessageSizeMax) {
_buffer = Buffer();
return;
}

_buffer.reserve(reserve);
}
}

//assert(_buffer->size() == info->index);
_buffer.insert(_buffer.end(), data, data + len);
if (info->index + len == info->len
&& (info->final || _cb_on_fragments))
{
// Frame/message complete
if (_terminate_string) {
_buffer.push_back(0);
}
_cb(client, _buffer.data(), _buffer.size());
_buffer.clear();
}
}

private:
using Buffer = std::vector<uint8_t>;
Buffer _buffer;

Callback _cb;
bool _cb_on_fragments;
bool _terminate_string;
};
87 changes: 0 additions & 87 deletions code/espurna/libs/WebSocketIncommingBuffer.h

This file was deleted.

61 changes: 36 additions & 25 deletions code/espurna/ws.cpp
Expand Up @@ -21,7 +21,7 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include "wifi.h"
#include "ws_internal.h"

#include "libs/WebSocketIncommingBuffer.h"
#include "libs/WebSocketIncomingBuffer.h"

// -----------------------------------------------------------------------------
// Helpers / utility functions
Expand Down Expand Up @@ -519,7 +519,7 @@ void _wsPostParse(uint32_t client_id, bool save, bool reload) {

void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {

//DEBUG_MSG_P(PSTR("[WEBSOCKET] Parsing: %s\n"), length ? (char*) payload : "");
//DEBUG_MSG_P(PSTR("[WEBSOCKET] Parsing: %.*s\n"), length, reinterpret_cast<cont char*>(payload));

// Get client ID
uint32_t client_id = client->id();
Expand Down Expand Up @@ -684,49 +684,60 @@ void _wsConnected(uint32_t client_id) {
wsPostSequence(client_id, _ws_callbacks.on_data);
}

void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){

if (type == WS_EVT_CONNECT) {

client->_tempObject = nullptr;
String ip = client->remoteIP().toString();

void _wsEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) {
switch (type) {
case WS_EVT_CONNECT:
{
const auto ip = client->remoteIP().toString();
#ifndef NOWSAUTH
if (!_wsAuth(client)) {
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u session expired for %s\n"), client->id(), ip.c_str());
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u session expired for %s\n"),
client->id(), ip.c_str());
client->close();
return;
}
#endif

DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u connected, ip: %s, url: %s\n"), client->id(), ip.c_str(), server->url());
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u connected, ip: %s, url: %s\n"),
client->id(), ip.c_str(), server->url());

_wsConnected(client->id());
_wsResetUpdateTimer();
client->_tempObject = new WebSocketIncommingBuffer(_wsParse, true);

} else if(type == WS_EVT_DISCONNECT) {
client->_tempObject = new WebSocketIncomingBuffer(_wsParse);
break;
}

case WS_EVT_DISCONNECT:
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u disconnected\n"), client->id());
if (client->_tempObject) {
delete (WebSocketIncommingBuffer *) client->_tempObject;
auto* ptr = reinterpret_cast<WebSocketIncomingBuffer*>(client->_tempObject);
delete ptr;
client->_tempObject = nullptr;
}
wifiApCheck();
break;

} else if(type == WS_EVT_ERROR) {
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u error(%u): %s\n"), client->id(), *((uint16_t*)arg), (char*)data);
case WS_EVT_ERROR:
{
uint16_t code;
std::memcpy(&code, arg, 2);
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u error(%hu)\n"), client->id(), code);
break;
}

} else if(type == WS_EVT_PONG) {
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u pong(%u): %s\n"), client->id(), len, len ? (char*) data : "");
case WS_EVT_PONG:
break;

} else if(type == WS_EVT_DATA) {
//DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u data(%u): %s\n"), client->id(), len, len ? (char*) data : "");
if (!client->_tempObject) return;
WebSocketIncommingBuffer *buffer = (WebSocketIncommingBuffer *)client->_tempObject;
AwsFrameInfo * info = (AwsFrameInfo*)arg;
buffer->data_event(client, info, data, len);
case WS_EVT_DATA:
if (client->_tempObject) {
auto *buffer = reinterpret_cast<WebSocketIncomingBuffer*>(client->_tempObject);
AwsFrameInfo * info = (AwsFrameInfo*)arg;
buffer->data_event(client, info, data, len);
}
break;

}

}

void _wsHandlePostponedCallbacks(bool connected) {
Expand Down

0 comments on commit 7296eca

Please sign in to comment.