commit 35cca6c55330a7856d646faee8f4677796fe0b54 from: Omar Polo date: Thu Sep 07 17:23:35 2023 UTC amused-web: allocate lazily the (double) buffer for chunking commit - 8701aaaaba7c99c87459f1d34a7b1acdd897f19c commit + 35cca6c55330a7856d646faee8f4677796fe0b54 blob - cf1886b7394788345da11d0363983447cd901bc8 blob + d4bb9f21482cb2d263537bbf911346ba36f62299 --- web/http.c +++ web/http.c @@ -315,9 +315,25 @@ http_write(struct client *clt, const char *d, size_t l if (clt->err) return -1; + + if (!clt->bio.chunked) { + if (bufio_compose(&clt->bio, d, len) == -1) { + clt->err = 1; + return -1; + } + return 0; + } + if (clt->buf == NULL) { + clt->cap = 1024; + if ((clt->buf = malloc(clt->cap)) == NULL) { + clt->err = 1; + return -1; + } + } + while (len > 0) { - avail = sizeof(clt->buf) - clt->len; + avail = clt->cap - clt->len; if (avail > len) avail = len; @@ -325,7 +341,7 @@ http_write(struct client *clt, const char *d, size_t l clt->len += avail; len -= avail; d += avail; - if (clt->len == sizeof(clt->buf)) { + if (clt->len == clt->cap) { if (http_flush(clt) == -1) return -1; } @@ -437,6 +453,7 @@ http_close(struct client *clt) void http_free(struct client *clt) { + free(clt->buf); free(clt->req.path); free(clt->req.secret); free(clt->req.ctype); blob - 781a9319946305f5b6e5d037ebe3cd9c85b16221 blob + 514b988b6d81110b702843d8f390bb3d6e649c02 --- web/http.h +++ web/http.h @@ -56,8 +56,9 @@ typedef void (*route_fn)(struct client *); TAILQ_HEAD(clthead, client); struct client { - char buf[1024]; + char *buf; size_t len; + size_t cap; struct bufio bio; struct request req; int err;