From 67ec33218eca2c40c58e0f9c70a466192017c8ea Mon Sep 17 00:00:00 2001 From: Miccah Date: Fri, 9 Apr 2021 02:53:33 -0500 Subject: [PATCH] vweb: read the entire request body from buffered reader (#9644) --- vlib/vweb/request.v | 5 ++++- vlib/vweb/request_test.v | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/vlib/vweb/request.v b/vlib/vweb/request.v index 88dae2ec61..c5ac970a12 100644 --- a/vlib/vweb/request.v +++ b/vlib/vweb/request.v @@ -36,7 +36,10 @@ fn parse_request(mut reader io.BufferedReader) ?http.Request { n := length.int() if n > 0 { body = []byte{len: n} - reader.read(mut body) or {} + mut count := 0 + for count < body.len { + count += reader.read(mut body[count..]) or { break } + } } } h.free() diff --git a/vlib/vweb/request_test.v b/vlib/vweb/request_test.v index 1abe355566..b1275ad4c8 100644 --- a/vlib/vweb/request_test.v +++ b/vlib/vweb/request_test.v @@ -12,7 +12,9 @@ fn (mut s StringReader) read(mut buf []byte) ?int { if s.place >= s.text.len { return none } - n := copy(buf, s.text[s.place..].bytes()) + max_bytes := 100 + end := if s.place + max_bytes >= s.text.len { s.text.len } else { s.place + max_bytes } + n := copy(buf, s.text[s.place..end].bytes()) s.place += n return n } @@ -135,3 +137,11 @@ ${contents[1]} names[1]: contents[1] + '\n' } } + +fn test_parse_large_body() ? { + body := 'A'.repeat(101) // greater than max_bytes + req := 'GET / HTTP/1.1\r\nContent-Length: $body.len\r\n\r\n$body' + result := parse_request(mut reader(req)) ? + assert result.data.len == body.len + assert result.data == body +}