Commit a8d9f1d3 authored by Alex Hultman's avatar Alex Hultman

Pass maxPayload to inflate, fix one class/struct mismatch

parent d7393cc0
......@@ -39,9 +39,9 @@ protected:
using errorType = typename std::conditional<isServer, int, void *>::type;
std::function<void(errorType)> errorHandler;
unsigned int maxPayload;
Hub *hub;
int extensionOptions;
unsigned int maxPayload;
Timer *timer = nullptr, *httpTimer = nullptr;
std::string userPingMessage;
std::stack<Poll *> iterators;
......
......@@ -2,11 +2,9 @@
#include "HTTPSocket.h"
#include <openssl/sha.h>
static const int INFLATE_LESS_THAN_ROUGHLY = 16777216;
namespace uWS {
char *Hub::inflate(char *data, size_t &length) {
char *Hub::inflate(char *data, size_t &length, size_t maxPayload) {
dynamicInflationBuffer.clear();
inflationStream.next_in = (Bytef *) data;
......@@ -22,11 +20,11 @@ char *Hub::inflate(char *data, size_t &length) {
}
dynamicInflationBuffer.append(inflationBuffer, LARGE_BUFFER_SIZE - inflationStream.avail_out);
} while (err == Z_BUF_ERROR && dynamicInflationBuffer.length() <= INFLATE_LESS_THAN_ROUGHLY);
} while (err == Z_BUF_ERROR && dynamicInflationBuffer.length() <= maxPayload);
inflateReset(&inflationStream);
if ((err != Z_BUF_ERROR && err != Z_OK) || dynamicInflationBuffer.length() > INFLATE_LESS_THAN_ROUGHLY) {
if ((err != Z_BUF_ERROR && err != Z_OK) || dynamicInflationBuffer.length() > maxPayload) {
length = 0;
return nullptr;
}
......
......@@ -20,7 +20,7 @@ protected:
z_stream inflationStream = {};
char *inflationBuffer;
char *inflate(char *data, size_t &length);
char *inflate(char *data, size_t &length, size_t maxPayload);
std::string dynamicInflationBuffer;
static const int LARGE_BUFFER_SIZE = 300 * 1024;
......@@ -88,8 +88,8 @@ public:
using Group<SERVER>::onHttpUpgrade;
using Group<SERVER>::onCancelledHttpRequest;
friend class WebSocket<SERVER>;
friend class WebSocket<CLIENT>;
friend struct WebSocket<SERVER>;
friend struct WebSocket<CLIENT>;
};
}
......
......@@ -295,13 +295,13 @@ void WebSocket<isServer>::onEnd(uS::Socket *s) {
template <bool isServer>
bool WebSocket<isServer>::handleFragment(char *data, size_t length, unsigned int remainingBytes, int opCode, bool fin, WebSocketState<isServer> *webSocketState) {
WebSocket<isServer> *webSocket = static_cast<WebSocket<isServer> *>(webSocketState);
Group<isServer> *group = Group<isServer>::from(webSocket);
if (opCode < 3) {
if (!remainingBytes && fin && !webSocket->fragmentBuffer.length()) {
if (webSocket->compressionStatus == WebSocket<isServer>::CompressionStatus::COMPRESSED_FRAME) {
webSocket->compressionStatus = WebSocket<isServer>::CompressionStatus::ENABLED;
Hub *hub = ((Group<isServer> *) webSocket->nodeData)->hub;
data = hub->inflate(data, length);
data = group->hub->inflate(data, length, group->maxPayload);
if (!data) {
forceClose(webSocketState);
return true;
......@@ -313,7 +313,7 @@ bool WebSocket<isServer>::handleFragment(char *data, size_t length, unsigned int
return true;
}
((Group<isServer> *) webSocket->nodeData)->messageHandler(webSocket, data, length, (OpCode) opCode);
group->messageHandler(webSocket, data, length, (OpCode) opCode);
if (webSocket->isClosed() || webSocket->isShuttingDown()) {
return true;
}
......@@ -323,9 +323,8 @@ bool WebSocket<isServer>::handleFragment(char *data, size_t length, unsigned int
length = webSocket->fragmentBuffer.length();
if (webSocket->compressionStatus == WebSocket<isServer>::CompressionStatus::COMPRESSED_FRAME) {
webSocket->compressionStatus = WebSocket<isServer>::CompressionStatus::ENABLED;
Hub *hub = ((Group<isServer> *) webSocket->nodeData)->hub;
webSocket->fragmentBuffer.append("....");
data = hub->inflate((char *) webSocket->fragmentBuffer.data(), length);
data = group->hub->inflate((char *) webSocket->fragmentBuffer.data(), length, group->maxPayload);
if (!data) {
forceClose(webSocketState);
return true;
......@@ -339,7 +338,7 @@ bool WebSocket<isServer>::handleFragment(char *data, size_t length, unsigned int
return true;
}
((Group<isServer> *) webSocket->nodeData)->messageHandler(webSocket, data, length, (OpCode) opCode);
group->messageHandler(webSocket, data, length, (OpCode) opCode);
if (webSocket->isClosed() || webSocket->isShuttingDown()) {
return true;
}
......@@ -355,12 +354,12 @@ bool WebSocket<isServer>::handleFragment(char *data, size_t length, unsigned int
} else {
if (opCode == PING) {
webSocket->send(data, length, (OpCode) OpCode::PONG);
((Group<isServer> *) webSocket->nodeData)->pingHandler(webSocket, data, length);
group->pingHandler(webSocket, data, length);
if (webSocket->isClosed() || webSocket->isShuttingDown()) {
return true;
}
} else if (opCode == PONG) {
((Group<isServer> *) webSocket->nodeData)->pongHandler(webSocket, data, length);
group->pongHandler(webSocket, data, length);
if (webSocket->isClosed() || webSocket->isShuttingDown()) {
return true;
}
......@@ -379,12 +378,12 @@ bool WebSocket<isServer>::handleFragment(char *data, size_t length, unsigned int
} else {
if (opCode == PING) {
webSocket->send(controlBuffer, webSocket->controlTipLength, (OpCode) OpCode::PONG);
((Group<isServer> *) webSocket->nodeData)->pingHandler(webSocket, controlBuffer, webSocket->controlTipLength);
group->pingHandler(webSocket, controlBuffer, webSocket->controlTipLength);
if (webSocket->isClosed() || webSocket->isShuttingDown()) {
return true;
}
} else if (opCode == PONG) {
((Group<isServer> *) webSocket->nodeData)->pongHandler(webSocket, controlBuffer, webSocket->controlTipLength);
group->pongHandler(webSocket, controlBuffer, webSocket->controlTipLength);
if (webSocket->isClosed() || webSocket->isShuttingDown()) {
return true;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment