Skip to content

Commit

Permalink
Merge pull request #72 from h2o/kazuho/clarify-how-bytes-are-left-in-…
Browse files Browse the repository at this point in the history
…chunked-decoder

clarify where the bytes that follow the chunked encoding are stored
  • Loading branch information
kazuho committed Feb 24, 2021
2 parents ef425b1 + 66534e6 commit 066d2b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions picohttpparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
20 changes: 20 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit 066d2b1

Please sign in to comment.