vweb: remove a duplicate function parse_multipart_form

pull/11471/head
Alexander Medvednikov 2021-09-11 04:21:10 +03:00
parent 07bd61582c
commit 1546090131
3 changed files with 3 additions and 39 deletions

View File

@ -261,7 +261,7 @@ struct MultiplePathAttributesError {
code int code int
} }
fn parse_multipart_form(body string, boundary string) (map[string]string, map[string][]FileData) { pub fn parse_multipart_form(body string, boundary string) (map[string]string, map[string][]FileData) {
sections := body.split(boundary) sections := body.split(boundary)
fields := sections[1..sections.len - 1] fields := sections[1..sections.len - 1]
mut form := map[string]string{} mut form := map[string]string{}

View File

@ -85,42 +85,6 @@ fn parse_form(body string) map[string]string {
// ... // ...
} }
fn parse_multipart_form(body string, boundary string) (map[string]string, map[string][]FileData) {
sections := body.split(boundary)
fields := sections[1..sections.len - 1]
mut form := map[string]string{}
mut files := map[string][]FileData{}
for field in fields {
// TODO: do not split into lines; do same parsing for HTTP body
lines := field.split('\n')[1..]
disposition := parse_disposition(lines[0])
// Grab everything between the double quotes
name := disposition['name'] or { continue }
// Parse files
// TODO: filename*
if 'filename' in disposition {
filename := disposition['filename']
// Parse Content-Type header
if lines.len == 1 || !lines[1].to_lower().starts_with('content-type:') {
continue
}
mut ct := lines[1].split_nth(':', 2)[1]
ct = ct.trim_left(' \t').trim_right('\r')
data := lines_to_string(field.len, lines, 3, lines.len - 1)
files[name] << FileData{
filename: filename
content_type: ct
data: data
}
continue
}
data := lines_to_string(field.len, lines, 2, lines.len - 1)
form[name] = data
}
return form, files
}
// Parse the Content-Disposition header of a multipart form // Parse the Content-Disposition header of a multipart form
// Returns a map of the key="value" pairs // Returns a map of the key="value" pairs
// Example: parse_disposition('Content-Disposition: form-data; name="a"; filename="b"') == {'name': 'a', 'filename': 'b'} // Example: parse_disposition('Content-Disposition: form-data; name="a"; filename="b"') == {'name': 'a', 'filename': 'b'}

View File

@ -141,7 +141,7 @@ pub mut:
static_mime_types map[string]string static_mime_types map[string]string
form map[string]string form map[string]string
query map[string]string query map[string]string
files map[string][]FileData files map[string][]http.FileData
header http.Header // response headers header http.Header // response headers
done bool done bool
page_gen_start i64 page_gen_start i64
@ -427,7 +427,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
send_string(mut conn, vweb.http_400.bytestr()) or {} send_string(mut conn, vweb.http_400.bytestr()) or {}
return return
} }
form, files := parse_multipart_form(req.data, boundary[0][9..]) form, files := http.parse_multipart_form(req.data, boundary[0][9..])
for k, v in form { for k, v in form {
app.form[k] = v app.form[k] = v
} }