2021-10-03 16:26:44 +03:00
|
|
|
module vweb
|
|
|
|
|
|
|
|
import net.urllib
|
|
|
|
import net.http
|
|
|
|
|
|
|
|
// Parsing function attributes for methods and path.
|
2023-05-29 03:11:10 +03:00
|
|
|
fn parse_attrs(name string, attrs []string) !([]http.Method, string, string, string) {
|
2021-10-03 16:26:44 +03:00
|
|
|
if attrs.len == 0 {
|
2023-05-29 03:11:10 +03:00
|
|
|
return [http.Method.get], '/${name}', '', ''
|
2021-10-03 16:26:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mut x := attrs.clone()
|
|
|
|
mut methods := []http.Method{}
|
2023-03-26 02:57:42 +03:00
|
|
|
mut middleware := ''
|
2021-10-03 16:26:44 +03:00
|
|
|
mut path := ''
|
2023-05-29 03:11:10 +03:00
|
|
|
mut host := ''
|
2021-10-03 16:26:44 +03:00
|
|
|
|
|
|
|
for i := 0; i < x.len; {
|
|
|
|
attr := x[i]
|
|
|
|
attru := attr.to_upper()
|
|
|
|
m := http.method_from_str(attru)
|
|
|
|
if attru == 'GET' || m != .get {
|
|
|
|
methods << m
|
|
|
|
x.delete(i)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if attr.starts_with('/') {
|
|
|
|
if path != '' {
|
2022-10-29 06:34:45 +03:00
|
|
|
return http.MultiplePathAttributesError{}
|
2021-10-03 16:26:44 +03:00
|
|
|
}
|
|
|
|
path = attr
|
|
|
|
x.delete(i)
|
|
|
|
continue
|
|
|
|
}
|
2023-03-26 02:57:42 +03:00
|
|
|
if attr.starts_with('middleware:') {
|
|
|
|
middleware = attr.all_after('middleware:').trim_space()
|
|
|
|
x.delete(i)
|
|
|
|
continue
|
|
|
|
}
|
2023-05-29 03:11:10 +03:00
|
|
|
if attr.starts_with('host:') {
|
|
|
|
host = attr.all_after('host:').trim_space()
|
|
|
|
x.delete(i)
|
|
|
|
continue
|
|
|
|
}
|
2021-10-03 16:26:44 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
if x.len > 0 {
|
2022-10-29 06:34:45 +03:00
|
|
|
return http.UnexpectedExtraAttributeError{
|
2022-02-11 16:52:33 +03:00
|
|
|
attributes: x
|
2022-10-29 06:34:45 +03:00
|
|
|
}
|
2021-10-03 16:26:44 +03:00
|
|
|
}
|
|
|
|
if methods.len == 0 {
|
|
|
|
methods = [http.Method.get]
|
|
|
|
}
|
|
|
|
if path == '' {
|
2022-11-15 16:53:13 +03:00
|
|
|
path = '/${name}'
|
2021-10-03 16:26:44 +03:00
|
|
|
}
|
2023-05-29 03:11:10 +03:00
|
|
|
// Make path and host lowercase for case-insensitive comparisons
|
|
|
|
return methods, path.to_lower(), middleware, host.to_lower()
|
2021-10-03 16:26:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_query_from_url(url urllib.URL) map[string]string {
|
|
|
|
mut query := map[string]string{}
|
2022-02-09 18:36:12 +03:00
|
|
|
for qvalue in url.query().data {
|
|
|
|
query[qvalue.key] = qvalue.value
|
2021-10-03 16:26:44 +03:00
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2023-07-14 16:07:52 +03:00
|
|
|
const boundary_start = 'boundary='
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn parse_form_from_request(request http.Request) !(map[string]string, map[string][]http.FileData) {
|
2023-07-14 16:07:52 +03:00
|
|
|
if request.method !in [http.Method.post, .put, .patch] {
|
|
|
|
return map[string]string{}, map[string][]http.FileData{}
|
|
|
|
}
|
|
|
|
ct := request.header.get(.content_type) or { '' }.split(';').map(it.trim_left(' \t'))
|
|
|
|
if 'multipart/form-data' in ct {
|
|
|
|
boundaries := ct.filter(it.starts_with(vweb.boundary_start))
|
|
|
|
if boundaries.len != 1 {
|
|
|
|
return error('detected more that one form-data boundary')
|
|
|
|
}
|
|
|
|
boundary := boundaries[0].all_after(vweb.boundary_start)
|
|
|
|
if boundary.len > 0 && boundary[0] == `"` {
|
|
|
|
// quotes are send by our http.post_multipart_form/2:
|
|
|
|
return http.parse_multipart_form(request.data, boundary.trim('"'))
|
2021-10-03 16:26:44 +03:00
|
|
|
}
|
2023-07-14 16:07:52 +03:00
|
|
|
// Firefox and other browsers, do not use quotes around the boundary:
|
|
|
|
return http.parse_multipart_form(request.data, boundary)
|
2021-10-03 16:26:44 +03:00
|
|
|
}
|
2023-07-14 16:07:52 +03:00
|
|
|
return http.parse_form(request.data), map[string][]http.FileData{}
|
2021-10-03 16:26:44 +03:00
|
|
|
}
|