]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Replace the HTTP/2 session's ad-hoc buffer with isc_buffer_t
authorArtem Boldariev <artem@boldariev.com>
Mon, 19 Jul 2021 12:20:30 +0000 (15:20 +0300)
committerArtem Boldariev <artem@boldariev.com>
Mon, 19 Jul 2021 12:26:53 +0000 (15:26 +0300)
This commit replaces a static ad-hoc HTTP/2 session's temporary buffer
with a realloc-able isc_buffer_t object, which is being allocated on
as needed basis, lowering the memory consumption somewhat. The buffer
is needed in very rare cases, so allocating it prematurely is not
wise.

Also, it fixes a bug in http_readcb() where the ad-hoc buffer appeared
to be improperly used, leading to a situation when the processed data
from the receiving regions can be processed twice, while unprocessed
data will never be processed.

lib/isc/netmgr/http.c

index a8132e69a91771b73fa977a164e5f8ea1b5ff61a..be90bef06b27bbbc144bb6e3a47b346e30b5034c 100644 (file)
@@ -139,8 +139,7 @@ struct isc_nm_http_session {
        isc_nmhandle_t *client_httphandle;
        isc_nmsocket_t *serversocket;
 
-       uint8_t buf[MAX_DNS_MESSAGE_SIZE];
-       size_t bufsize;
+       isc_buffer_t *buf;
 
        isc_tlsctx_t *tlsctx;
        uint32_t max_concurrent_streams;
@@ -318,6 +317,10 @@ isc__nm_httpsession_detach(isc_nm_http_session_t **sessionp) {
                session->ngsession = NULL;
        }
 
+       if (session->buf != NULL) {
+               isc_buffer_free(&session->buf);
+       }
+
        /* We need an acquire memory barrier here */
        (void)isc_refcount_current(&session->references);
 
@@ -952,10 +955,14 @@ http_readcb(isc_nmhandle_t *handle, isc_result_t result, isc_region_t *region,
        }
 
        if ((size_t)readlen < region->length) {
-               INSIST(session->bufsize == 0);
-               INSIST(region->length - readlen < MAX_DNS_MESSAGE_SIZE);
-               memmove(session->buf, region->base, region->length - readlen);
-               session->bufsize = region->length - readlen;
+               size_t unread_size = region->length - readlen;
+               if (session->buf == NULL) {
+                       isc_buffer_allocate(session->mctx, &session->buf,
+                                           unread_size);
+                       isc_buffer_setautorealloc(session->buf, true);
+               }
+               isc__buffer_putmem(session->buf, region->base + readlen,
+                                  unread_size);
                isc_nm_pauseread(session->handle);
        }
 
@@ -1240,18 +1247,18 @@ http_do_bio(isc_nm_http_session_t *session, isc_nmhandle_t *send_httphandle,
                        /* We have not yet started reading from this handle */
                        isc_nm_read(session->handle, http_readcb, session);
                        session->reading = true;
-               } else if (session->bufsize > 0) {
+               } else if (session->buf != NULL) {
+                       size_t remaining =
+                               isc_buffer_remaininglength(session->buf);
                        /* Leftover data in the buffer, use it */
                        size_t readlen = nghttp2_session_mem_recv(
-                               session->ngsession, session->buf,
-                               session->bufsize);
+                               session->ngsession,
+                               isc_buffer_current(session->buf), remaining);
 
-                       if (readlen == session->bufsize) {
-                               session->bufsize = 0;
+                       if (readlen == remaining) {
+                               isc_buffer_free(&session->buf);
                        } else {
-                               memmove(session->buf, session->buf + readlen,
-                                       session->bufsize - readlen);
-                               session->bufsize -= readlen;
+                               isc_buffer_add(session->buf, readlen);
                        }
 
                        http_do_bio(session, send_httphandle, send_cb,