Code Review
/
src
/
app-framework-binder.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
review
|
tree
raw
|
inline
| side by side
websocket: Tune maximum received length
[src/app-framework-binder.git]
/
src
/
websock.c
diff --git
a/src/websock.c
b/src/websock.c
index
1b886c1
..
4d8ffbf
100644
(file)
--- a/
src/websock.c
+++ b/
src/websock.c
@@
-1,5
+1,5
@@
/*
/*
- * Copyright (C) 2016 "IoT.bzh"
+ * Copyright (C) 2016
, 2017
"IoT.bzh"
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@
-30,6
+30,9
@@
#include "websock.h"
#define BLOCK_DATA_SIZE 4096
#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)
#define FRAME_GET_FIN(BYTE) (((BYTE) >> 7) & 0x01)
#define FRAME_GET_RSV1(BYTE) (((BYTE) >> 6) & 0x01)
@@
-45,7
+48,7
@@
#define FRAME_SET_RSV3(BYTE) (((BYTE) & 0x01) << 4)
#define FRAME_SET_OPCODE(BYTE) ((BYTE) & 0x0F)
#define FRAME_SET_MASK(BYTE) (((BYTE) & 0x01) << 7)
#define FRAME_SET_RSV3(BYTE) (((BYTE) & 0x01) << 4)
#define FRAME_SET_OPCODE(BYTE) ((BYTE) & 0x0F)
#define FRAME_SET_MASK(BYTE) (((BYTE) & 0x01) << 7)
-#define FRAME_SET_LENGTH(X64, IDX) (unsigned char)((
(X64) >> ((IDX)*8)) & 0xFF
)
+#define FRAME_SET_LENGTH(X64, IDX) (unsigned char)((
sizeof(X64)) <= (IDX) ? 0 : (((X64) >> ((IDX)*8)) & 0xFF)
)
#define OPCODE_CONTINUATION 0x0
#define OPCODE_TEXT 0x1
#define OPCODE_CONTINUATION 0x0
#define OPCODE_TEXT 0x1
@@
-59,6
+62,8
@@
#define STATE_LENGTH 2
#define STATE_DATA 3
#define STATE_LENGTH 2
#define STATE_DATA 3
+static size_t default_maxlength = WEBSOCKET_DEFAULT_MAXLENGTH;
+
struct websock {
int state;
uint64_t maxlength;
struct websock {
int state;
uint64_t maxlength;
@@
-303,7
+308,7
@@
static int check_control_header(struct websock *ws)
return 1;
}
return 1;
}
-int websock_dispatch(struct websock *ws)
+int websock_dispatch(struct websock *ws
, int loop
)
{
uint16_t code;
loop:
{
uint16_t code;
loop:
@@
-312,6
+317,7
@@
loop:
ws->lenhead = 0;
ws->szhead = 2;
ws->state = STATE_START;
ws->lenhead = 0;
ws->szhead = 2;
ws->state = STATE_START;
+ /*@fallthrough@*/
case STATE_START:
/* read the header */
case STATE_START:
/* read the header */
@@
-342,12
+348,15
@@
loop:
switch (FRAME_GET_PAYLOAD_LEN(ws->header[1])) {
case 127:
ws->szhead += 6;
switch (FRAME_GET_PAYLOAD_LEN(ws->header[1])) {
case 127:
ws->szhead += 6;
+ /*@fallthrough@*/
case 126:
ws->szhead += 2;
case 126:
ws->szhead += 2;
+ /*@fallthrough@*/
default:
ws->szhead += 4 * FRAME_GET_MASK(ws->header[1]);
}
ws->state = STATE_LENGTH;
default:
ws->szhead += 4 * FRAME_GET_MASK(ws->header[1]);
}
ws->state = STATE_LENGTH;
+ /*@fallthrough@*/
case STATE_LENGTH:
/* continue to read the header */
case STATE_LENGTH:
/* continue to read the header */
@@
-418,16
+427,22
@@
loop:
ws->itf->on_continue(ws->closure,
FRAME_GET_FIN(ws->header[0]),
(size_t) ws->length);
ws->itf->on_continue(ws->closure,
FRAME_GET_FIN(ws->header[0]),
(size_t) ws->length);
+ if (!loop)
+ return 0;
break;
case OPCODE_TEXT:
ws->itf->on_text(ws->closure,
FRAME_GET_FIN(ws->header[0]),
(size_t) ws->length);
break;
case OPCODE_TEXT:
ws->itf->on_text(ws->closure,
FRAME_GET_FIN(ws->header[0]),
(size_t) ws->length);
+ if (!loop)
+ return 0;
break;
case OPCODE_BINARY:
ws->itf->on_binary(ws->closure,
FRAME_GET_FIN(ws->header[0]),
(size_t) ws->length);
break;
case OPCODE_BINARY:
ws->itf->on_binary(ws->closure,
FRAME_GET_FIN(ws->header[0]),
(size_t) ws->length);
+ if (!loop)
+ return 0;
break;
case OPCODE_CLOSE:
if (ws->length == 0)
break;
case OPCODE_CLOSE:
if (ws->length == 0)
@@
-447,6
+462,8
@@
loop:
websock_pong(ws, NULL, 0);
}
ws->state = STATE_INIT;
websock_pong(ws, NULL, 0);
}
ws->state = STATE_INIT;
+ if (!loop)
+ return 0;
break;
case OPCODE_PONG:
if (ws->itf->on_pong)
break;
case OPCODE_PONG:
if (ws->itf->on_pong)
@@
-454,6
+471,8
@@
loop:
else
websock_drop(ws);
ws->state = STATE_INIT;
else
websock_drop(ws);
ws->state = STATE_INIT;
+ if (!loop)
+ return 0;
break;
default:
goto protocol_error;
break;
default:
goto protocol_error;
@@
-537,7
+556,7
@@
int websock_drop(struct websock *ws)
char buffer[8000];
while (ws->length)
char buffer[8000];
while (ws->length)
- if (w
s
_read(ws, buffer, sizeof buffer) < 0)
+ if (w
ebsock
_read(ws, buffer, sizeof buffer) < 0)
return -1;
return 0;
}
return -1;
return 0;
}
@@
-548,7
+567,7
@@
struct websock *websock_create_v13(const struct websock_itf *itf, void *closure)
if (result) {
result->itf = itf;
result->closure = closure;
if (result) {
result->itf = itf;
result->closure = closure;
- result->maxlength =
65000
;
+ result->maxlength =
default_maxlength
;
}
return result;
}
}
return result;
}
@@
-557,3
+576,13
@@
void websock_destroy(struct websock *ws)
{
free(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;
+}