From 66534e6a48d67a3d9ddfeb5bd2c922f498d314ae Mon Sep 17 00:00:00 2001 From: Kazuho Oku Date: Tue, 23 Feb 2021 10:39:24 +0900 Subject: [PATCH] clarify where the bytes that follow the chunked encoding are stored, as well as adding tests --- picohttpparser.h | 4 ++-- test.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/picohttpparser.h b/picohttpparser.h index 0849f84..07537cf 100644 --- a/picohttpparser.h +++ b/picohttpparser.h @@ -72,8 +72,8 @@ struct phr_chunked_decoder { * repeatedly call the function while it returns -2 (incomplete) every time * supplying newly arrived data. If the end of the chunked-encoded data is * found, the function returns a non-negative number indicating the number of - * octets left undecoded at the tail of the supplied buffer. Returns -1 on - * error. + * octets left undecoded, that starts from the offset returned by `*bufsz`. + * Returns -1 on error. */ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_t *bufsz); diff --git a/test.c b/test.c index d75b9b2..2c589f3 100644 --- a/test.c +++ b/test.c @@ -437,6 +437,25 @@ static void test_chunked_consume_trailer(void) } } +static void test_chunked_leftdata(void) +{ +#define NEXT_REQ "GET / HTTP/1.1\r\n\r\n" + + struct phr_chunked_decoder dec = {0}; + dec.consume_trailer = 1; + char buf[] = "5\r\nabcde\r\n0\r\n\r\n" NEXT_REQ; + size_t bufsz = sizeof(buf) - 1; + + ssize_t ret = phr_decode_chunked(&dec, buf, &bufsz); + ok(ret >= 0); + ok(bufsz == 5); + ok(memcmp(buf, "abcde", 5) == 0); + ok(ret == sizeof(NEXT_REQ) - 1); + ok(memcmp(buf + bufsz, NEXT_REQ, sizeof(NEXT_REQ) - 1) == 0); + +#undef NEXT_REQ +} + int main(void) { long pagesize = sysconf(_SC_PAGESIZE); @@ -452,6 +471,7 @@ int main(void) subtest("headers", test_headers); subtest("chunked", test_chunked); subtest("chunked-consume-trailer", test_chunked_consume_trailer); + subtest("chunked-leftdata", test_chunked_leftdata); munmap(inputbuf - pagesize * 2, pagesize * 3);