Skip to content

Commit 67ec332

Browse files
authored
vweb: read the entire request body from buffered reader (#9644)
1 parent e93a52a commit 67ec332

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

vlib/vweb/request.v

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ fn parse_request(mut reader io.BufferedReader) ?http.Request {
3636
n := length.int()
3737
if n > 0 {
3838
body = []byte{len: n}
39-
reader.read(mut body) or {}
39+
mut count := 0
40+
for count < body.len {
41+
count += reader.read(mut body[count..]) or { break }
42+
}
4043
}
4144
}
4245
h.free()

vlib/vweb/request_test.v

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ fn (mut s StringReader) read(mut buf []byte) ?int {
1212
if s.place >= s.text.len {
1313
return none
1414
}
15-
n := copy(buf, s.text[s.place..].bytes())
15+
max_bytes := 100
16+
end := if s.place + max_bytes >= s.text.len { s.text.len } else { s.place + max_bytes }
17+
n := copy(buf, s.text[s.place..end].bytes())
1618
s.place += n
1719
return n
1820
}
@@ -135,3 +137,11 @@ ${contents[1]}
135137
names[1]: contents[1] + '\n'
136138
}
137139
}
140+
141+
fn test_parse_large_body() ? {
142+
body := 'A'.repeat(101) // greater than max_bytes
143+
req := 'GET / HTTP/1.1\r\nContent-Length: $body.len\r\n\r\n$body'
144+
result := parse_request(mut reader(req)) ?
145+
assert result.data.len == body.len
146+
assert result.data == body
147+
}

0 commit comments

Comments
 (0)