]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Use isc_buffer_t to keep track of outgoing response
authorArtem Boldariev <artem@boldariev.com>
Wed, 21 Jul 2021 20:23:58 +0000 (23:23 +0300)
committerArtem Boldariev <artem@boldariev.com>
Thu, 12 Aug 2021 11:28:16 +0000 (14:28 +0300)
This commit gets rid of custom code taking care of response buffering
by replacing the custom code with isc_buffer_t. Also, it gets rid of
an unnecessary memory copying when sending a response.

lib/isc/netmgr/http.c
lib/isc/netmgr/netmgr-int.h

index 4721bd71bd477c731dbf1659ddb44132a8167207..3542f9f68e8a341a2cacdf135eebdc880fd8655d 100644 (file)
@@ -1661,13 +1661,12 @@ server_on_begin_headers_callback(nghttp2_session *ngsession,
                           isc_nm_httpsocket,
                           (isc_sockaddr_t *)&session->handle->sock->iface);
        socket->peer = session->handle->sock->peer;
-       socket->h2 = (isc_nmsocket_h2_t){
-               .buf = isc_mem_allocate(session->mctx, MAX_DNS_MESSAGE_SIZE),
-               .psock = socket,
-               .stream_id = frame->hd.stream_id,
-               .headers_error_code = ISC_HTTP_ERROR_SUCCESS
-       };
+       socket->h2 = (isc_nmsocket_h2_t){ .psock = socket,
+                                         .stream_id = frame->hd.stream_id,
+                                         .headers_error_code =
+                                                 ISC_HTTP_ERROR_SUCCESS };
        isc_buffer_initnull(&socket->h2.rbuf);
+       isc_buffer_initnull(&socket->h2.wbuf);
        session->nsstreams++;
        isc__nm_httpsession_attach(session, &socket->h2.session);
        socket->tid = session->handle->sock->tid;
@@ -1928,14 +1927,18 @@ server_read_callback(nghttp2_session *ngsession, int32_t stream_id,
        UNUSED(ngsession);
        UNUSED(session);
 
-       buflen = socket->h2.bufsize - socket->h2.bufpos;
+       buflen = isc_buffer_remaininglength(&socket->h2.wbuf);
        if (buflen > length) {
                buflen = length;
        }
 
-       memmove(buf, socket->h2.buf + socket->h2.bufpos, buflen);
-       socket->h2.bufpos += buflen;
-       if (socket->h2.bufpos == socket->h2.bufsize) {
+       if (buflen > 0) {
+               (void)memmove(buf, isc_buffer_current(&socket->h2.wbuf),
+                             buflen);
+               isc_buffer_forward(&socket->h2.wbuf, buflen);
+       }
+
+       if (isc_buffer_remaininglength(&socket->h2.wbuf) == 0) {
                *data_flags |= NGHTTP2_DATA_FLAG_EOF;
        }
 
@@ -2055,6 +2058,7 @@ server_on_request_recv(nghttp2_session *ngsession,
        isc_result_t result;
        isc_http_error_responses_t code = ISC_HTTP_ERROR_SUCCESS;
        isc_region_t data;
+       uint8_t tmp_buf[MAX_DNS_MESSAGE_SIZE];
 
        code = socket->h2.headers_error_code;
        if (code != ISC_HTTP_ERROR_SUCCESS) {
@@ -2081,15 +2085,14 @@ server_on_request_recv(nghttp2_session *ngsession,
 
        if (socket->h2.request_type == ISC_HTTP_REQ_GET) {
                isc_buffer_t decoded_buf;
-               isc__buffer_init(&decoded_buf, socket->h2.buf,
-                                MAX_DNS_MESSAGE_SIZE);
+               isc_buffer_init(&decoded_buf, tmp_buf, sizeof(tmp_buf));
                if (isc_base64_decodestring(socket->h2.query_data,
                                            &decoded_buf) != ISC_R_SUCCESS)
                {
                        code = ISC_HTTP_ERROR_BAD_REQUEST;
                        goto error;
                }
-               isc__buffer_usedregion(&decoded_buf, &data);
+               isc_buffer_usedregion(&decoded_buf, &data);
        } else if (socket->h2.request_type == ISC_HTTP_REQ_POST) {
                INSIST(socket->h2.content_length > 0);
                isc_buffer_usedregion(&socket->h2.rbuf, &data);
@@ -2184,8 +2187,8 @@ server_httpsend(isc_nmhandle_t *handle, isc_nmsocket_t *sock,
        INSIST(VALID_NMHANDLE(handle->httpsession->handle));
        INSIST(VALID_NMSOCK(handle->httpsession->handle->sock));
 
-       memmove(sock->h2.buf, req->uvbuf.base, req->uvbuf.len);
-       sock->h2.bufsize = req->uvbuf.len;
+       isc_buffer_init(&sock->h2.wbuf, req->uvbuf.base, req->uvbuf.len);
+       isc_buffer_add(&sock->h2.wbuf, req->uvbuf.len);
 
        len = snprintf(sock->h2.clenbuf, sizeof(sock->h2.clenbuf), "%lu",
                       (unsigned long)req->uvbuf.len);
@@ -3025,11 +3028,6 @@ isc__nm_http_cleanup_data(isc_nmsocket_t *sock) {
 
                INSIST(sock->h2.connect.cstream == NULL);
 
-               if (sock->h2.buf != NULL) {
-                       isc_mem_free(sock->mgr->mctx, sock->h2.buf);
-                       sock->h2.buf = NULL;
-               }
-
                if (isc_buffer_base(&sock->h2.rbuf) != NULL) {
                        void *base = isc_buffer_base(&sock->h2.rbuf);
                        isc_mem_free(sock->mgr->mctx, base);
index 857c34aaa1e56fbb3dc88645482d5b3b0f470afd..c75ddddfb8ca646e3f699c2df8b26f60f302a80f 100644 (file)
@@ -800,10 +800,8 @@ typedef struct isc_nmsocket_h2 {
        bool query_too_large;
        isc_nm_httphandler_t *handler;
 
-       uint8_t *buf;
-       size_t bufsize;
-       size_t bufpos;
        isc_buffer_t rbuf;
+       isc_buffer_t wbuf;
 
        int32_t stream_id;
        isc_nm_http_session_t *session;