1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00

test server align rxbuf with permessage deflate rx buf size

Add a test html button that will send 9KB of junk to confirm it

https://github.com/warmcat/libwebsockets/issues/480

permessage-deflate now checks the protocol rx buffer size for being
>=128, if not, permessage-deflate is disabled on that connection.

If it is >=128 but less than the zlib decompress buffer size, the
zlib decompress buffer size for that connection is reduced to the
nearest power of two of the protocol rx buf size.

To test this, dumb_increment is left violating the >= 128 rx buffer
size and permessage-deflte can be seen to be disabled on his
connections in the test html.

Signed-off-by: Andy Green <andy@warmcat.com>
This commit is contained in:
Andy Green 2016-04-01 08:43:13 +08:00
parent bbcd24167f
commit 1c428c3154
5 changed files with 60 additions and 15 deletions

View file

@ -671,10 +671,14 @@ check_extensions:
/* allow him to construct his ext instance */
ext->callback(lws_get_context(wsi), ext, wsi,
if (ext->callback(lws_get_context(wsi), ext, wsi,
LWS_EXT_CB_CLIENT_CONSTRUCT,
(void *)&wsi->act_ext_user[wsi->count_act_ext],
(void *)&opts, 0);
(void *)&opts, 0)) {
lwsl_notice(" ext %s failed construction\n", ext_name);
ext++;
continue;
}
/*
* allow the user code to override ext defaults if it

View file

@ -75,6 +75,17 @@ lws_extension_callback_pm_deflate(struct lws_context *context,
case LWS_EXT_CB_CLIENT_CONSTRUCT:
case LWS_EXT_CB_CONSTRUCT:
n = LWS_MAX_SOCKET_IO_BUF;
if (wsi->protocol->rx_buffer_size)
n = wsi->protocol->rx_buffer_size;
if (n < 128) {
lwsl_err(" permessage-deflate requires the protocol (%s) to have an RX buffer >= 128\n",
wsi->protocol->name);
return -1;
}
/* fill in **user */
priv = lws_zalloc(sizeof(*priv));
*((void **)user) = priv;
@ -102,6 +113,23 @@ lws_extension_callback_pm_deflate(struct lws_context *context,
priv->args[PMD_TX_BUF_PWR2] = 10; /* ie, 1024 */
priv->args[PMD_COMP_LEVEL] = 1;
priv->args[PMD_MEM_LEVEL] = 8;
/* cap the RX buf at the nearest power of 2 to protocol rx buf */
n = LWS_MAX_SOCKET_IO_BUF;
if (wsi->protocol->rx_buffer_size)
n = wsi->protocol->rx_buffer_size;
extra = 7;
while (n >= 1 << (extra + 1))
extra++;
if (extra < priv->args[PMD_RX_BUF_PWR2]) {
priv->args[PMD_RX_BUF_PWR2] = extra;
lwsl_err(" Capping pmd rx to %d\n", 1 << extra);
}
lwsl_err(" ok\n");
break;
case LWS_EXT_CB_DESTROY:

View file

@ -123,11 +123,6 @@ lws_extension_server_handshake(struct lws *wsi, char **p)
/* apply it */
if (ext_count)
*(*p)++ = ',';
else
LWS_CPYAPP(*p, "\x0d\x0aSec-WebSocket-Extensions: ");
*p += sprintf(*p, "%s", ext_name);
ext_count++;
/* instantiate the extension on this conn */
@ -136,10 +131,21 @@ lws_extension_server_handshake(struct lws *wsi, char **p)
/* allow him to construct his context */
ext->callback(lws_get_context(wsi), ext, wsi,
if (ext->callback(lws_get_context(wsi), ext, wsi,
LWS_EXT_CB_CONSTRUCT,
(void *)&wsi->act_ext_user[wsi->count_act_ext],
NULL, 0);
NULL, 0)) {
lwsl_notice("ext %s failed construction\n", ext_name);
ext_count--;
ext++;
continue;
}
if (ext_count > 1)
*(*p)++ = ',';
else
LWS_CPYAPP(*p, "\x0d\x0aSec-WebSocket-Extensions: ");
*p += sprintf(*p, "%s", ext_name);
wsi->count_act_ext++;
lwsl_parser("count_act_ext <- %d\n", wsi->count_act_ext);

View file

@ -89,25 +89,25 @@ static struct lws_protocols protocols[] = {
"dumb-increment-protocol",
callback_dumb_increment,
sizeof(struct per_session_data__dumb_increment),
10,
10, /* rx buf size must be >= permessage-deflate rx size */
},
{
"lws-mirror-protocol",
callback_lws_mirror,
sizeof(struct per_session_data__lws_mirror),
128,
128, /* rx buf size must be >= permessage-deflate rx size */
},
{
"lws-echogen",
callback_lws_echogen,
sizeof(struct per_session_data__echogen),
128,
128, /* rx buf size must be >= permessage-deflate rx size */
},
{
"lws-status",
callback_lws_status,
sizeof(struct per_session_data__lws_status),
128,
128, /* rx buf size must be >= permessage-deflate rx size */
},
{ NULL, NULL, 0, 0 } /* terminator */
};

View file

@ -127,6 +127,7 @@ to zero just this connection's number.
<td align=center><div id=number style="font-size:120%;"> </div></td>
<td align=center>
<input type=button id=offset value="Reset counter" onclick="reset();" >
<input type=button id=junk value="Send junk" onclick="junk();" >
</td>
</tr>
</table>
@ -477,7 +478,7 @@ document.getElementById("number").textContent = get_appropriate_ws_url();
document.getElementById("s_statustd").style.backgroundColor = "#40ff40";
document.getElementById("s_status").innerHTML =
" <b>websocket connection opened</b><br>" +
san(socket_di.extensions);
san(socket_status.extensions);
}
socket_status.onmessage =function got_packet(msg) {
@ -514,6 +515,12 @@ function reset() {
socket_di.send("reset\n");
}
function junk() {
for(var word = ''; word.length < 9000; word += 'a'){}
socket_di.send(word);
}
var socket_ot;
function ot_open() {
@ -579,7 +586,7 @@ function ot_req_close() {
document.getElementById("wslm_statustd").style.backgroundColor = "#40ff40";
document.getElementById("wslm_status").innerHTML =
" <b>websocket connection opened</b><br>" +
san(socket_di.extensions);
san(socket_lm.extensions);
}
socket_lm.onmessage =function got_packet(msg) {