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

67 lines
1.6 KiB
V
Raw Normal View History

2020-01-23 05:26:30 +03:00
module picohttpparser
pub struct Request {
2021-04-24 13:21:30 +03:00
mut:
prev_len int
pub mut:
method string
path string
headers [100]C.phr_header
2020-01-23 05:26:30 +03:00
num_headers u64
body string
2020-01-23 05:26:30 +03:00
}
[inline]
pub fn (mut r Request) parse_request(s string, max_headers int) int {
2021-09-08 13:09:32 +03:00
method_len := usize(0)
path_len := usize(0)
2020-01-23 05:26:30 +03:00
minor_version := 0
2021-09-08 13:09:32 +03:00
num_headers := usize(max_headers)
2020-01-23 05:26:30 +03:00
pret := C.phr_parse_request(&char(s.str), s.len, voidptr(&r.method.str), &method_len,
voidptr(&r.path.str), &path_len, &minor_version, &r.headers[0], &num_headers,
r.prev_len)
2020-01-23 05:26:30 +03:00
if pret > 0 {
unsafe {
r.method = tos(r.method.str, int(method_len))
r.path = tos(r.path.str, int(path_len))
}
r.num_headers = u64(num_headers)
2020-01-23 05:26:30 +03:00
}
r.body = unsafe { (&s.str[pret]).vstring_literal_with_len(s.len - pret) }
2021-04-24 13:21:30 +03:00
r.prev_len = s.len
2020-01-23 05:26:30 +03:00
return pret
}
[inline]
2020-05-17 14:51:18 +03:00
pub fn (mut r Request) parse_request_path(s string) int {
2021-09-08 13:09:32 +03:00
method_len := usize(0)
path_len := usize(0)
2020-01-23 05:26:30 +03:00
pret := C.phr_parse_request_path(&char(s.str), s.len, voidptr(&r.method.str), &method_len,
voidptr(&r.path.str), &path_len)
2020-01-23 05:26:30 +03:00
if pret > 0 {
unsafe {
r.method = tos(r.method.str, int(method_len))
r.path = tos(r.path.str, int(path_len))
}
2020-01-23 05:26:30 +03:00
}
return pret
}
[inline]
2020-05-17 14:51:18 +03:00
pub fn (mut r Request) parse_request_path_pipeline(s string) int {
2021-09-08 13:09:32 +03:00
method_len := usize(0)
path_len := usize(0)
2020-01-23 05:26:30 +03:00
pret := C.phr_parse_request_path_pipeline(&char(s.str), s.len, voidptr(&r.method.str),
&method_len, voidptr(&r.path.str), &path_len)
2020-01-23 05:26:30 +03:00
if pret > 0 {
unsafe {
r.method = tos(r.method.str, int(method_len))
r.path = tos(r.path.str, int(path_len))
}
2020-01-23 05:26:30 +03:00
}
return pret
}