From e46c64bf423f2e7ac726217a6aa83ce7947f4040 Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Tue, 26 Oct 2021 11:54:31 +1100 Subject: [PATCH] Consume the HTTP headers after processing a request Remember the amount of space consumed by the HTTP headers, then move any trailing data to the start of the httpd->recvbuf once we have finished processing the request. --- lib/isc/httpd.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/isc/httpd.c b/lib/isc/httpd.c index c9f78ef331..9596b8e0af 100644 --- a/lib/isc/httpd.c +++ b/lib/isc/httpd.c @@ -87,6 +87,7 @@ struct isc_httpd { */ char recvbuf[HTTP_RECVLEN]; /*%< receive buffer */ uint32_t recvlen; /*%< length recv'd */ + uint32_t consume; /*%< length of last command */ char *headers; /*%< set in process_request() */ method_t method; char *url; @@ -417,9 +418,12 @@ process_request(isc_httpd_t *httpd, isc_region_t *region, size_t *buflen) { if (s == NULL) { s = strstr(httpd->recvbuf, "\n\n"); delim = 1; - } - if (s == NULL) { - return (ISC_R_NOTFOUND); + if (s == NULL) { + return (ISC_R_NOTFOUND); + } + httpd->consume = s + 2 - httpd->recvbuf; + } else { + httpd->consume = s + 4 - httpd->recvbuf; } /* @@ -580,7 +584,6 @@ process_request(isc_httpd_t *httpd, isc_region_t *region, size_t *buflen) { * to the buffer. */ *urlend = 0; - httpd->recvlen = 0; return (ISC_R_SUCCESS); } @@ -602,6 +605,7 @@ httpd_reset(void *arg) { httpd->recvbuf[0] = 0; httpd->recvlen = 0; + httpd->consume = 0; httpd->headers = NULL; httpd->method = METHOD_UNKNOWN; httpd->url = NULL; @@ -952,6 +956,17 @@ httpd_request(isc_nmhandle_t *handle, isc_result_t eresult, result = isc_buffer_copyregion(httpd->sendbuffer, &r); RUNTIME_CHECK(result == ISC_R_SUCCESS); + /* Consume the request from the recv buffer. */ + if (httpd->consume != 0U) { + INSIST(httpd->consume <= httpd->recvlen); + if (httpd->consume < httpd->recvlen) { + memmove(httpd->recvbuf, httpd->recvbuf + httpd->consume, + httpd->recvlen - httpd->consume); + } + httpd->recvlen -= httpd->consume; + httpd->consume = 0; + } + /* * Determine total response size. */