diff --git a/include/libwebsockets/lws-http.h b/include/libwebsockets/lws-http.h index eb3d826eb..77fa49cc2 100644 --- a/include/libwebsockets/lws-http.h +++ b/include/libwebsockets/lws-http.h @@ -653,6 +653,18 @@ lws_http_redirect(struct lws *wsi, int code, const unsigned char *loc, int len, LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_http_transaction_completed(struct lws *wsi); +/** + * lws_http_headers_detach() - drop the associated headers storage and allow + * it to be reused by another connection + * \param wsi: http connection + * + * Returns 1 if the HTTP connection must close now + * Returns 0 and resets connection to wait for new HTTP header / + * transaction if possible + */ +LWS_VISIBLE LWS_EXTERN int +lws_http_headers_detach(struct lws *wsi); + /** * lws_http_compression_apply() - apply an http compression transform * diff --git a/lib/roles/http/header.c b/lib/roles/http/header.c index 8db8ef8b9..996777859 100644 --- a/lib/roles/http/header.c +++ b/lib/roles/http/header.c @@ -526,4 +526,8 @@ lws_http_compression_apply(struct lws *wsi, const char *name, } #endif - +int +lws_http_headers_detach(struct lws *wsi) +{ + return lws_header_table_detach(wsi, 0); +} diff --git a/minimal-examples/http-server/minimal-http-server-sse-ring/minimal-http-server-sse-ring.c b/minimal-examples/http-server/minimal-http-server-sse-ring/minimal-http-server-sse-ring.c index 7bccb990e..3176cd70d 100644 --- a/minimal-examples/http-server/minimal-http-server-sse-ring/minimal-http-server-sse-ring.c +++ b/minimal-examples/http-server/minimal-http-server-sse-ring/minimal-http-server-sse-ring.c @@ -215,6 +215,18 @@ callback_sse(struct lws *wsi, enum lws_callback_reasons reason, void *user, pss->tail = lws_ring_get_oldest_tail(vhd->ring); pss->wsi = wsi; + /* + * Drop the ah that contains the headers associated with + * this connection... ah are a scarce resource, if we don't + * drop it lws will forcibly drop the whole connection to free + * the ah after 5 minutes or so. + * + * If the content of any http headers are important to you for + * deciding what to send, copy them out to the pss before + * doing the below to drop the ah. + */ + lws_http_headers_detach(wsi); + /* Unlike a normal http connection, we don't want any specific * timeout. We want to stay up until the client drops us */ diff --git a/minimal-examples/http-server/minimal-http-server-sse/minimal-http-server-sse.c b/minimal-examples/http-server/minimal-http-server-sse/minimal-http-server-sse.c index 1092dd56a..74414df0e 100644 --- a/minimal-examples/http-server/minimal-http-server-sse/minimal-http-server-sse.c +++ b/minimal-examples/http-server/minimal-http-server-sse/minimal-http-server-sse.c @@ -70,6 +70,18 @@ callback_sse(struct lws *wsi, enum lws_callback_reasons reason, void *user, lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); + /* + * Drop the ah that contains the headers associated with + * this connection... ah are a scarce resource, if we don't + * drop it lws will forcibly drop the whole connection to free + * the ah after 5 minutes or so. + * + * If the content of any http headers are important to you for + * deciding what to send, copy them out to the pss before + * doing the below to drop the ah. + */ + lws_http_headers_detach(wsi); + /* write the body separately */ lws_callback_on_writable(wsi);