diff --git a/changelog b/changelog index a7eac226248572278187829785fe0b94895f4bf0..2ab975bcbde07102acbec92eb929e1eb7aa44b2e 100644 --- a/changelog +++ b/changelog @@ -4,7 +4,7 @@ Changelog User api additions ------------------ -The info struct gained two new members +1) The info struct gained two new members - max_http_header_data: 0 for default (1024) or set the maximum amount of known http header payload that lws can deal with. Payload in unknown http @@ -25,7 +25,7 @@ callback after the HTTP handshake... for ws connections that is ESTABLISHED and for HTTP connections the HTTP callback. So these settings are not related to the maximum number of simultaneous -connections but the number of HTTP handshakes that may be expected or ongoing, +connections, but the number of HTTP handshakes that may be expected or ongoing, or have just completed, at one time. The reason it's useful is it changes the memory allocation for header processing to be one-time at context creation instead of every time there is a new connection, and gives you control over @@ -35,6 +35,24 @@ Setting max_http_header_pool to 1 is fine it will just queue incoming connections before the accept as necessary, you can still have as many simultaneous post-header connections as you like. +User api changes +---------------- + +1) LWS_SEND_BUFFER_POST_PADDING is now 0 and deprecated. You can remove it; if +you still use it, obviously it does nothing. Old binary code with nonzero +LWS_SEND_BUFFER_POST_PADDING is perfectly compatible, the old code just +allocated a buffer bigger than needed. + +The example apps no longer user it. + +The only path who made use of it was sending with LWS_WRITE_CLOSE. + +If you use LWS_WRITE_CLOSE by hand in your user code, you need to allow an +extra 2 bytes space at the end of your buffer. This ONLY applies to +LWS_WRITE_CLOSE, which you normally don't send directly, but cause by returning +nonzero from a callback letting the library actually send it. + + v1.6.0-chrome48-firefox42 ======================= diff --git a/lib/client.c b/lib/client.c index 86ce7223ab59169078f604cbe10dd27a7c658bfe..d712e02385918dd2a1193c11ef2cf5557534cd6c 100644 --- a/lib/client.c +++ b/lib/client.c @@ -743,7 +743,7 @@ check_accept: n = wsi->protocol->rx_buffer_size; if (!n) n = LWS_MAX_SOCKET_IO_BUF; - n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING; + n += LWS_SEND_BUFFER_PRE_PADDING; wsi->u.ws.rx_user_buffer = lws_malloc(n); if (!wsi->u.ws.rx_user_buffer) { lwsl_err("Out of Mem allocating rx buffer %d\n", n); diff --git a/lib/extension-deflate-frame.c b/lib/extension-deflate-frame.c index 77cb6602f50b3fa944605dcca555f54ad9844c47..eebc053be12a3345713d18557da9b589ba8b9d8f 100644 --- a/lib/extension-deflate-frame.c +++ b/lib/extension-deflate-frame.c @@ -55,13 +55,11 @@ int lws_extension_callback_deflate_frame( conn->compressed_out = 0; conn->buf_pre = NULL; conn->buf_in = lws_malloc(LWS_SEND_BUFFER_PRE_PADDING + - conn->buf_in_length + - LWS_SEND_BUFFER_POST_PADDING); + conn->buf_in_length); if (!conn->buf_in) goto bail; conn->buf_out = lws_malloc(LWS_SEND_BUFFER_PRE_PADDING + - conn->buf_out_length + - LWS_SEND_BUFFER_POST_PADDING); + conn->buf_out_length); if (!conn->buf_out) goto bail; lwsl_ext("zlibs constructed\n"); @@ -177,8 +175,7 @@ bail: } conn->buf_in = lws_realloc(conn->buf_in, LWS_SEND_BUFFER_PRE_PADDING + - conn->buf_in_length + - LWS_SEND_BUFFER_POST_PADDING); + conn->buf_in_length); if (!conn->buf_in) { lwsl_err("Out of memory\n"); return -1; @@ -240,8 +237,7 @@ bail: } conn->buf_out = lws_realloc(conn->buf_out, LWS_SEND_BUFFER_PRE_PADDING + - conn->buf_out_length + - LWS_SEND_BUFFER_POST_PADDING); + conn->buf_out_length); if (!conn->buf_out) { lwsl_err("Out of memory\n"); return -1; diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 58a5e204fd17028e8ccf616b95604715fbb2fc44..6b04d810a4fe09d8c1e5ac58b9e28552f9947a9e 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -74,8 +74,7 @@ lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason) struct lws_context *context = wsi->context; int n, m, ret, old_state; struct lws_tokens eff_buf; - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 + - LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4]; if (!wsi) return; diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 1c6a332058b870b9369fc907fb9e51191e300424..f4761e9666a560d3f68256ff7dad114a7f97164a 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -1396,9 +1396,17 @@ lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs); /* * IMPORTANT NOTICE! * - * When sending with websocket protocol (LWS_WRITE_TEXT or LWS_WRITE_BINARY) + * When sending with websocket protocol + * + * LWS_WRITE_TEXT, + * LWS_WRITE_BINARY, + * LWS_WRITE_CONTINUATION, + * LWS_WRITE_CLOSE, + * LWS_WRITE_PING, + * LWS_WRITE_PONG + * * the send buffer has to have LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE - * buf, and LWS_SEND_BUFFER_POST_PADDING bytes valid AFTER (buf + len). + * the buffer pointer you pass to lws_write(). * * This allows us to add protocol info before and after the data, and send as * one packet on the network without payload copying, for maximum efficiency. @@ -1406,25 +1414,33 @@ lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs); * So for example you need this kind of code to use lws_write with a * 128-byte payload * - * char buf[LWS_SEND_BUFFER_PRE_PADDING + 128 + LWS_SEND_BUFFER_POST_PADDING]; + * char buf[LWS_SEND_BUFFER_PRE_PADDING + 128]; * * // fill your part of the buffer... for example here it's all zeros * memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 0, 128); * * lws_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 128, LWS_WRITE_TEXT); * - * When sending LWS_WRITE_HTTP, there is no protocol addition and you can just - * use the whole buffer without taking care of the above. - */ - -/* - * this is the frame nonce plus two header plus 8 length - * there's an additional two for mux extension per mux nesting level - * 2 byte prepend on close will already fit because control frames cannot use - * the big length style - */ - -/* + * When sending + * + * LWS_WRITE_CLOSE + * + * only, you must allow your buffer to be 2 bytes longer than otherwise + * needed. + * + * When sending HTTP, with + * + * LWS_WRITE_HTTP, + * LWS_WRITE_HTTP_HEADERS + * LWS_WRITE_HTTP_FINAL + * + * there is no protocol data prepended, and don't need to take care about the + * LWS_SEND_BUFFER_PRE_PADDING bytes valid before the buffer pointer. + * + * LWS_SEND_BUFFER_PRE_PADDING is at least the frame nonce + 2 header + 8 length + * LWS_SEND_BUFFER_POST_PADDING is deprecated, it's now 0 and can be left off. + * The example apps no longer use it. + * * Pad LWS_SEND_BUFFER_PRE_PADDING to the CPU word size, so that word references * to the address immediately after the padding won't cause an unaligned access * error. Sometimes for performance reasons the recommended padding is even @@ -1446,7 +1462,7 @@ lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs); #define _LWS_PAD(n) (((n) % _LWS_PAD_SIZE) ? \ ((n) + (_LWS_PAD_SIZE - ((n) % _LWS_PAD_SIZE))) : (n)) #define LWS_SEND_BUFFER_PRE_PADDING _LWS_PAD(4 + 10) -#define LWS_SEND_BUFFER_POST_PADDING 4 +#define LWS_SEND_BUFFER_POST_PADDING 0 LWS_VISIBLE LWS_EXTERN int lws_write(struct lws *wsi, unsigned char *buf, size_t len, diff --git a/lib/output.c b/lib/output.c index eb164c94bd16130685335493583b8d723d0c7287..b25487cebce6fc3d8901a3f4b26112ddce761efe 100644 --- a/lib/output.c +++ b/lib/output.c @@ -213,10 +213,8 @@ handle_truncated_send: * @wsi: Websocket instance (available from user callback) * @buf: The data to send. For data being sent on a websocket * connection (ie, not default http), this buffer MUST have - * LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer - * and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid - * in the buffer after (buf + len). This is so the protocol - * header and trailer data can be added in-situ. + * LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer. + * This is so the protocol header data can be added in-situ. * @len: Count of the data bytes in the payload starting from buf * @protocol: Use LWS_WRITE_HTTP to reply to an http connection, and one * of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate diff --git a/lib/server.c b/lib/server.c index aacdeb96329192ebaa0dd25a73cf76c3d958b71a..fa99f132ef8c47786e514ff6da44f9f3cd9621be 100644 --- a/lib/server.c +++ b/lib/server.c @@ -579,7 +579,7 @@ upgrade_ws: n = wsi->protocol->rx_buffer_size; if (!n) n = LWS_MAX_SOCKET_IO_BUF; - n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING; + n += LWS_SEND_BUFFER_PRE_PADDING; wsi->u.ws.rx_user_buffer = lws_malloc(n); if (!wsi->u.ws.rx_user_buffer) { lwsl_err("Out of Mem allocating rx buffer %d\n", n); diff --git a/test-server/test-client.c b/test-server/test-client.c index 7ef91e71bc59fb993fa018981f6e40ad4c85b8f4..726615a5eeedab602ea5e527efef0502252675cb 100644 --- a/test-server/test-client.c +++ b/test-server/test-client.c @@ -134,8 +134,7 @@ static int callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 + - LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096]; unsigned int rands[4]; int l = 0; int n; diff --git a/test-server/test-echo.c b/test-server/test-echo.c index a13f3e109e96056fe0af7cfa1a5ab89a65fb397d..47984ac8a98376d01a3c8a0bf6e04b55056b24dc 100644 --- a/test-server/test-echo.c +++ b/test-server/test-echo.c @@ -47,7 +47,7 @@ static int versa, state; #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server" struct per_session_data__echo { - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_ECHO_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_ECHO_PAYLOAD]; unsigned int len; unsigned int index; }; diff --git a/test-server/test-fraggle.c b/test-server/test-fraggle.c index 6dd3590345b3c0e92323e22fc4a7b145e0ffb6a8..d2ab02786fd424f28d785bdc196cce9771b3a561 100644 --- a/test-server/test-fraggle.c +++ b/test-server/test-fraggle.c @@ -57,8 +57,7 @@ callback_fraggle(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { int n; - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 8000 + - LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 8000]; struct per_session_data__fraggle *psf = user; int chunk; int write_mode = LWS_WRITE_CONTINUATION; diff --git a/test-server/test-ping.c b/test-server/test-ping.c index 2a685a18676086ae42739f597fad69490499afc1..16065f14a9f4422bd781c5fa1b0270c7db8e08f0 100644 --- a/test-server/test-ping.c +++ b/test-server/test-ping.c @@ -57,8 +57,7 @@ static unsigned int interval_us = 1000000; static unsigned int size = 64; static int flood; static const char *address; -static unsigned char pingbuf[LWS_SEND_BUFFER_PRE_PADDING + MAX_MIRROR_PAYLOAD + - LWS_SEND_BUFFER_POST_PADDING]; +static unsigned char pingbuf[LWS_SEND_BUFFER_PRE_PADDING + MAX_MIRROR_PAYLOAD]; static char peer_name[128]; static unsigned long started; static int screen_width = 80; diff --git a/test-server/test-server-dumb-increment.c b/test-server/test-server-dumb-increment.c index d06d32316a15f9c5666ce81bc2b3dca3d74e7b04..e83a12bed63cbd66648affe06d688fdaf0c9b16f 100644 --- a/test-server/test-server-dumb-increment.c +++ b/test-server/test-server-dumb-increment.c @@ -26,8 +26,7 @@ int callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 + - LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512]; struct per_session_data__dumb_increment *pss = (struct per_session_data__dumb_increment *)user; unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING]; diff --git a/test-server/test-server-mirror.c b/test-server/test-server-mirror.c index cb7bcc581b11eca1caf51c1bbe3b7a6b3f24fdb9..a92922fa17a1f14604a15e4f42a4b78294c3cb99 100644 --- a/test-server/test-server-mirror.c +++ b/test-server/test-server-mirror.c @@ -79,11 +79,12 @@ callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason, pss->ringbuffer_tail++; if (((ringbuffer_head - pss->ringbuffer_tail) & - (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15)) + (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15)) lws_rx_flow_allow_all_protocol(lws_get_context(wsi), lws_get_protocol(wsi)); - if (lws_partial_buffered(wsi) || lws_send_pipe_choked(wsi)) { + if (lws_partial_buffered(wsi) || + lws_send_pipe_choked(wsi)) { lws_callback_on_writable(wsi); break; } @@ -101,8 +102,7 @@ callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason, free(ringbuffer[ringbuffer_head].payload); ringbuffer[ringbuffer_head].payload = - malloc(LWS_SEND_BUFFER_PRE_PADDING + len + - LWS_SEND_BUFFER_POST_PADDING); + malloc(LWS_SEND_BUFFER_PRE_PADDING + len); ringbuffer[ringbuffer_head].len = len; memcpy((char *)ringbuffer[ringbuffer_head].payload + LWS_SEND_BUFFER_PRE_PADDING, in, len); @@ -121,7 +121,7 @@ choke: done: lws_callback_on_writable_all_protocol(lws_get_context(wsi), - lws_get_protocol(wsi)); + lws_get_protocol(wsi)); break; /*