vweb: fix parsing form data (#10468)

pull/10474/head^2
Dialga 2021-06-16 03:28:16 +12:00 committed by GitHub
parent f3408a2484
commit 7201d5db59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 4 deletions

View File

@ -93,7 +93,7 @@ fn parse_multipart_form(body string, boundary string) (map[string]string, map[st
for field in fields { for field in fields {
// TODO: do not split into lines; do same parsing for HTTP body // TODO: do not split into lines; do same parsing for HTTP body
lines := field.split_into_lines()[1..] lines := field.split('\n')[1..]
disposition := parse_disposition(lines[0]) disposition := parse_disposition(lines[0])
// Grab everything between the double quotes // Grab everything between the double quotes
name := disposition['name'] or { continue } name := disposition['name'] or { continue }
@ -106,7 +106,7 @@ fn parse_multipart_form(body string, boundary string) (map[string]string, map[st
continue continue
} }
mut ct := lines[1].split_nth(':', 2)[1] mut ct := lines[1].split_nth(':', 2)[1]
ct = ct.trim_left(' \t') ct = ct.trim_left(' \t').trim_right('\r')
data := lines_to_string(field.len, lines, 3, lines.len - 1) data := lines_to_string(field.len, lines, 3, lines.len - 1)
files[name] << FileData{ files[name] << FileData{
filename: filename filename: filename
@ -131,7 +131,7 @@ fn parse_disposition(line string) map[string]string {
if kv.len != 2 { if kv.len != 2 {
continue continue
} }
key, value := kv[0].to_lower().trim_left(' \t'), kv[1] key, value := kv[0].to_lower().trim_left(' \t'), kv[1].trim_right('\r')
if value.starts_with('"') && value.ends_with('"') { if value.starts_with('"') && value.ends_with('"') {
data[key] = value[1..value.len - 1] data[key] = value[1..value.len - 1]
} else { } else {
@ -148,6 +148,9 @@ fn lines_to_string(len int, lines []string, start int, end int) string {
sb.writeln(lines[i]) sb.writeln(lines[i])
} }
sb.cut_last(1) // last newline sb.cut_last(1) // last newline
if sb.last_n(1) == '\r' {
sb.cut_last(1)
}
res := sb.str() res := sb.str()
unsafe { sb.free() } unsafe { sb.free() }
return res return res

View File

@ -130,7 +130,7 @@ ${contents[1]}
} }
fn test_parse_large_body() ? { fn test_parse_large_body() ? {
body := 'A'.repeat(101) // greater than max_bytes body := 'ABCEF\r\n'.repeat(1024 * 1024) // greater than max_bytes
req := 'GET / HTTP/1.1\r\nContent-Length: $body.len\r\n\r\n$body' req := 'GET / HTTP/1.1\r\nContent-Length: $body.len\r\n\r\n$body'
result := parse_request(mut reader(req)) ? result := parse_request(mut reader(req)) ?
assert result.data.len == body.len assert result.data.len == body.len