From 6af240ddd3350874aa918591948dc1902169d7f7 Mon Sep 17 00:00:00 2001
From: José Bollo <jose.bollo@iot.bzh>
Date: Mon, 23 Oct 2017 14:11:04 +0200
Subject: websocket: Tune maximum received length
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This commit increase the count of data that
is accepted by default from 65,000 to 1,048,500.

It also offers new functions to tune that value.

Change-Id: Iecf0b8c308e8287582819a8769859c39e46919c2
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
---
 src/websock.c | 17 ++++++++++++++++-
 src/websock.h |  2 ++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/websock.c b/src/websock.c
index 1b127c8b..8518b721 100644
--- a/src/websock.c
+++ b/src/websock.c
@@ -30,6 +30,9 @@
 #include "websock.h"
 
 #define BLOCK_DATA_SIZE              4096
+#if !defined(WEBSOCKET_DEFAULT_MAXLENGTH)
+#  define WEBSOCKET_DEFAULT_MAXLENGTH 1048500  /* 76 less than 1M, probably enougth for headers */
+#endif
 
 #define FRAME_GET_FIN(BYTE)         (((BYTE) >> 7) & 0x01)
 #define FRAME_GET_RSV1(BYTE)        (((BYTE) >> 6) & 0x01)
@@ -59,6 +62,8 @@
 #define STATE_LENGTH  2
 #define STATE_DATA    3
 
+static size_t default_maxlength = WEBSOCKET_DEFAULT_MAXLENGTH;
+
 struct websock {
 	int state;
 	uint64_t maxlength;
@@ -558,7 +563,7 @@ struct websock *websock_create_v13(const struct websock_itf *itf, void *closure)
 	if (result) {
 		result->itf = itf;
 		result->closure = closure;
-		result->maxlength = 65000;
+		result->maxlength = default_maxlength;
 	}
 	return result;
 }
@@ -567,3 +572,13 @@ void websock_destroy(struct websock *ws)
 {
 	free(ws);
 }
+
+void websock_set_default_max_length(size_t maxlen)
+{
+	default_maxlength = maxlen;
+}
+
+void websock_set_max_length(struct websock *ws, size_t maxlen)
+{
+	ws->maxlength = (uint64_t)maxlen;
+}
diff --git a/src/websock.h b/src/websock.h
index da44a88b..3be89dfe 100644
--- a/src/websock.h
+++ b/src/websock.h
@@ -75,3 +75,5 @@ extern int websock_dispatch(struct websock *ws, int loop);
 extern struct websock *websock_create_v13(const struct websock_itf *itf, void *closure);
 extern void websock_destroy(struct websock *ws);
 
+extern void websock_set_default_max_length(size_t maxlen);
+extern void websock_set_max_length(struct websock *ws, size_t maxlen);
-- 
cgit