1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/vweb/request.v

89 lines
2.0 KiB
V
Raw Normal View History

module vweb
import io
import net.http
import net.urllib
pub fn parse_request(mut reader io.BufferedReader) ?http.Request {
// request line
mut line := reader.read_line() ?
method, target, version := parse_request_line(line) ?
// headers
2021-03-01 13:50:52 +03:00
mut h := http.new_header()
line = reader.read_line() ?
for line != '' {
2021-03-01 13:50:52 +03:00
key, value := parse_header(line) ?
h.add_str(key, value) ?
line = reader.read_line() ?
}
2021-03-01 13:50:52 +03:00
// create map[string]string from headers
// TODO: replace headers and lheaders with http.Header type
mut headers := map[string]string{}
mut lheaders := map[string]string{}
for key in h.keys() {
values := h.values_str(key).join('; ')
headers[key] = values
lheaders[key.to_lower()] = values
}
// body
mut body := []byte{}
2021-03-01 13:50:52 +03:00
if length := h.get(.content_length) {
n := length.int()
if n > 0 {
body = []byte{len: n}
reader.read(mut body) or { }
}
}
return http.Request{
method: method
url: target.str()
2021-03-01 13:50:52 +03:00
headers: headers
lheaders: lheaders
data: body.bytestr()
version: version
}
}
fn parse_request_line(s string) ?(http.Method, urllib.URL, http.Version) {
words := s.split(' ')
if words.len != 3 {
return error('malformed request line')
}
method := http.method_from_str(words[0])
target := urllib.parse(words[1]) ?
version := http.version_from_str(words[2])
if version == .unknown {
return error('unsupported version')
}
return method, target, version
}
2021-03-01 13:50:52 +03:00
fn parse_header(s string) ?(string, string) {
if ':' !in s {
return error('missing colon in header')
}
words := s.split_nth(':', 2)
if !is_token(words[0]) {
return error('invalid character in header name')
}
// TODO: parse quoted text according to the RFC
2021-03-01 13:50:52 +03:00
return words[0], words[1].trim_left(' \t')
}
// TODO: use map for faster lookup (untested)
const token_chars = r"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'*+-.^_`|~".bytes()
fn is_token(s string) bool {
for c in s {
if c !in vweb.token_chars {
return false
}
}
return true
}