# HG changeset patch # User Olaf Wintermann # Date 1651779970 -7200 # Node ID 6f82ede01e1cb656b6c9106c6fc12c6f987b05d8 # Parent ab26980faad602e01575bfd5ec93f669e9cf53e7 simplify request input buffer diff -r ab26980faad6 -r 6f82ede01e1c src/server/daemon/httprequest.c --- a/src/server/daemon/httprequest.c Tue May 03 21:41:00 2022 +0200 +++ b/src/server/daemon/httprequest.c Thu May 05 21:46:10 2022 +0200 @@ -124,6 +124,8 @@ sn->netbuf = inbuf; sn->sn.csd = http; sn->sn.ssl = ssl; + sn->sn.inbuf = inbuf; + sn->sn.inbuf->sd = http; return 0; } @@ -377,40 +379,22 @@ //printf("request body length: %d\n", ctlen); - netbuf *nb = request->netbuf; + netbuf *nb = sn->netbuf; + HttpStream *net_io = (HttpStream*)sn->sn.csd; - // create new netbuf - HttpStream *net_io = (HttpStream*)httpstream_new(pool, io); - net_io->max_read = ctlen; - - sn->sn.inbuf = pool_malloc(pool, sizeof(netbuf)); - sn->sn.inbuf->sd = net_io; - sn->sn.inbuf->pos = 0; - - // prepare buffer - int cur_input_len = nb->cursize - nb->pos; + // how many bytes are already read and in the buffer + int cur_input_available = nb->cursize - nb->pos; - if(cur_input_len >= ctlen) { - /* - * all data is already in the primary input buffer - * just link the new netbuf to the primary buffer - */ - sn->sn.inbuf->maxsize = ctlen; - sn->sn.inbuf->cursize = ctlen; - sn->sn.inbuf->inbuf = nb->inbuf + nb->pos; + if(cur_input_available >= ctlen) { + // we have the whole request body in the buffer and + // maybe even more + // no more read from the socket is necessary to get the body, + // therefore disable it + net_io->max_read = 0; } else { - sn->sn.inbuf->maxsize = (ctlen > 2048) ? (2048) : (ctlen); - sn->sn.inbuf->inbuf = pool_malloc(pool, sn->sn.inbuf->maxsize); - - if(cur_input_len > 0) { - // we have read a part of the request body -> copy to netbuf - memcpy(sn->sn.inbuf->inbuf, nb->inbuf+nb->pos, cur_input_len); - } - - sn->sn.inbuf->cursize = cur_input_len; + // read still required to get the complete request body + net_io->max_read = ctlen - cur_input_available; } - } else { - sn->sn.inbuf = NULL; } // diff -r ab26980faad6 -r 6f82ede01e1c src/server/util/io.c --- a/src/server/util/io.c Tue May 03 21:41:00 2022 +0200 +++ b/src/server/util/io.c Thu May 05 21:46:10 2022 +0200 @@ -304,7 +304,7 @@ } ssize_t net_http_read(HttpStream *st, void *buf, size_t nbytes) { - if(st->max_read != 0 && st->read >= st->max_read) { + if(st->read >= st->max_read) { return 0; } ssize_t r = st->fd->read(st->fd, buf, nbytes);