diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 6175a3d2d..3e9493926 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -1712,6 +1712,9 @@ lws_client_connect_via_info(struct lws_client_connect_info * ccinfo); LWS_VISIBLE LWS_EXTERN struct lws * lws_adopt_socket(struct lws_context *context, lws_sockfd_type accept_fd); +LWS_VISIBLE LWS_EXTERN struct lws * +lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd, + const char *readbuf, size_t len); LWS_VISIBLE LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT lws_canonical_hostname(struct lws_context *context); diff --git a/lib/parsers.c b/lib/parsers.c index 2547ec903..c78fb92e6 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -81,6 +81,16 @@ lws_header_table_reset(struct lws *wsi) /* since we will restart the ah, our new headers are not completed */ wsi->hdr_parsing_completed = 0; + + /* + * if we inherited pending rx (from socket adoption deferred + * processing), apply and free it. + */ + if (wsi->u.hdr.preamble_rx) { + memcpy(ah->rx, wsi->u.hdr.preamble_rx, wsi->u.hdr.preamble_rx_len); + ah->rxlen = wsi->u.hdr.preamble_rx_len; + lws_free_set_NULL(wsi->u.hdr.preamble_rx); + } } int LWS_WARN_UNUSED_RESULT @@ -179,6 +189,9 @@ int lws_header_table_detach(struct lws *wsi) (void *)wsi, (void *)wsi->u.hdr.ah, wsi->tsi, pt->ah_count_in_use); + if (wsi->u.hdr.preamble_rx) + lws_free_set_NULL(wsi->u.hdr.preamble_rx); + /* may not be detached while he still has unprocessed rx */ if (ah && ah->rxpos != ah->rxlen) { lwsl_err("%s: %p: rxpos:%d, rxlen:%d\n", __func__, wsi, diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index af62e8ff8..315c32e17 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -940,6 +940,8 @@ struct _lws_header_related { /* MUST be first in struct */ struct allocated_headers *ah; struct lws *ah_wait_list; + unsigned char *preamble_rx; + unsigned int preamble_rx_len; enum uri_path_states ups; enum uri_esc_states ues; short lextable_pos; diff --git a/lib/server.c b/lib/server.c index cc846d4d6..92a8eedce 100644 --- a/lib/server.c +++ b/lib/server.c @@ -821,6 +821,80 @@ fail: return NULL; } +/** + * lws_adopt_socket_readbuf() - adopt foreign socket and first rx as if listen socket accepted it + * @context: lws context + * @accept_fd: fd of already-accepted socket to adopt + * @readbuf: NULL or pointer to data that must be drained before reading from + * accept_fd + * @len: The length of the data held at @readbuf + * + * Either returns new wsi bound to accept_fd, or closes accept_fd and + * returns NULL, having cleaned up any new wsi pieces. + * + * LWS adopts the socket in http serving mode, it's ready to accept an upgrade + * to ws or just serve http. + * + * If your external code did not already read from the socket, you can use + * lws_adopt_socket() instead. + * + * This api is guaranteed to use the data at @readbuf first, before reading from + * the socket. + * + * @readbuf is limited to the size of the ah rx buf, currently 2048 bytes. + */ + +LWS_VISIBLE LWS_EXTERN struct lws * +lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd, + const char *readbuf, size_t len) +{ + struct lws *wsi = lws_adopt_socket(context, accept_fd); + struct allocated_headers *ah; + + if (!wsi) + return NULL; + + if (!readbuf) + return wsi; + + if (len > sizeof(ah->rx)) { + lwsl_err("%s: rx in too big\n", __func__); + goto bail; + } + /* + * we can't process the initial read data until we can attach an ah. + * + * if one is available, get it and place the data in his ah rxbuf... + * wsi with ah that have pending rxbuf get auto-POLLIN service. + */ + if (!lws_header_table_attach(wsi)) { + ah = wsi->u.hdr.ah; + memcpy(ah->rx, readbuf, len); + ah->rxpos = 0; + ah->rxlen = len; + + return wsi; + } + + /* + * hum if no ah came, we are on the wait list and must defer + * dealing with this until the ah arrives. + * + * later successful lws_header_table_attach() will apply the + * below to the rx buffer. + */ + + wsi->u.hdr.preamble_rx = lws_malloc(len); + wsi->u.hdr.preamble_rx_len = len; + + return wsi; + +bail: + lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS); + + return NULL; +} + LWS_VISIBLE int lws_server_socket_service(struct lws_context *context, struct lws *wsi, struct lws_pollfd *pollfd) diff --git a/libwebsockets-api-doc.html b/libwebsockets-api-doc.html index 13d3f0069..640ce8e58 100644 --- a/libwebsockets-api-doc.html +++ b/libwebsockets-api-doc.html @@ -790,6 +790,42 @@ LWS adopts the socket in http serving mode, it's ready to accept an upgrade to ws or just serve http.
+Either returns new wsi bound to accept_fd, or closes accept_fd and +returns NULL, having cleaned up any new wsi pieces. +++LWS adopts the socket in http serving mode, it's ready to accept an upgrade +to ws or just serve http. +
+If your external code did not already read from the socket, you can use +lws_adopt_socket instead. +
+This api is guaranteed to use the data at readbuf first, before reading from +the socket. +
+readbuf is limited to the size of the ah rx buf, currently 2048 bytes. +