Thu, 05 May 2022 21:46:10 +0200
simplify request input buffer
src/server/daemon/httprequest.c | file | annotate | diff | comparison | revisions | |
src/server/util/io.c | file | annotate | diff | comparison | revisions |
--- 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; } //
--- 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);