vweb: read the entire request body from buffered reader (#9644)

pull/9647/head
Miccah 2021-04-09 02:53:33 -05:00 committed by GitHub
parent e93a52a267
commit 67ec33218e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -36,7 +36,10 @@ fn parse_request(mut reader io.BufferedReader) ?http.Request {
n := length.int() n := length.int()
if n > 0 { if n > 0 {
body = []byte{len: n} 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() h.free()

View File

@ -12,7 +12,9 @@ fn (mut s StringReader) read(mut buf []byte) ?int {
if s.place >= s.text.len { if s.place >= s.text.len {
return none 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 s.place += n
return n return n
} }
@ -135,3 +137,11 @@ ${contents[1]}
names[1]: contents[1] + '\n' 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
}