2020-01-23 03:26:30 +01:00
|
|
|
module picohttpparser
|
|
|
|
|
|
|
|
pub struct Request {
|
2021-04-24 12:21:30 +02:00
|
|
|
mut:
|
2021-05-03 15:55:13 +02:00
|
|
|
prev_len int
|
2020-05-20 05:36:46 +02:00
|
|
|
pub mut:
|
2021-04-04 21:43:13 +02:00
|
|
|
method string
|
|
|
|
path string
|
|
|
|
headers [100]C.phr_header
|
2020-01-23 03:26:30 +01:00
|
|
|
num_headers u64
|
2021-04-04 21:43:13 +02:00
|
|
|
body string
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-06-07 01:23:30 +02:00
|
|
|
pub fn (mut r Request) parse_request(s string, max_headers int) int {
|
2021-04-05 09:02:47 +02:00
|
|
|
method_len := size_t(0)
|
|
|
|
path_len := size_t(0)
|
2020-01-23 03:26:30 +01:00
|
|
|
minor_version := 0
|
2021-04-05 09:02:47 +02:00
|
|
|
num_headers := size_t(max_headers)
|
2020-01-23 03:26:30 +01:00
|
|
|
|
2021-04-04 21:43:13 +02:00
|
|
|
pret := C.phr_parse_request(s.str, s.len, PPchar(&r.method.str), &method_len, PPchar(&r.path.str),
|
2021-04-24 12:21:30 +02:00
|
|
|
&path_len, &minor_version, &r.headers[0], &num_headers, r.prev_len)
|
2020-01-23 03:26:30 +01:00
|
|
|
if pret > 0 {
|
2021-02-15 16:15:52 +01:00
|
|
|
unsafe {
|
|
|
|
r.method = tos(r.method.str, int(method_len))
|
|
|
|
r.path = tos(r.path.str, int(path_len))
|
|
|
|
}
|
2021-04-05 09:02:47 +02:00
|
|
|
r.num_headers = u64(num_headers)
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
2021-05-03 15:55:13 +02:00
|
|
|
r.body = unsafe { (&s.str[pret]).vstring_literal_with_len(s.len - pret) }
|
2021-04-24 12:21:30 +02:00
|
|
|
r.prev_len = s.len
|
2020-01-23 03:26:30 +01:00
|
|
|
return pret
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Request) parse_request_path(s string) int {
|
2021-04-05 09:02:47 +02:00
|
|
|
method_len := size_t(0)
|
|
|
|
path_len := size_t(0)
|
2020-01-23 03:26:30 +01:00
|
|
|
|
2021-04-04 21:43:13 +02:00
|
|
|
pret := C.phr_parse_request_path(s.str, s.len, PPchar(&r.method.str), &method_len,
|
|
|
|
PPchar(&r.path.str), &path_len)
|
2020-01-23 03:26:30 +01:00
|
|
|
if pret > 0 {
|
2021-02-15 16:15:52 +01:00
|
|
|
unsafe {
|
|
|
|
r.method = tos(r.method.str, int(method_len))
|
|
|
|
r.path = tos(r.path.str, int(path_len))
|
|
|
|
}
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
return pret
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Request) parse_request_path_pipeline(s string) int {
|
2021-04-05 09:02:47 +02:00
|
|
|
method_len := size_t(0)
|
|
|
|
path_len := size_t(0)
|
2020-01-23 03:26:30 +01:00
|
|
|
|
2021-04-04 21:43:13 +02:00
|
|
|
pret := C.phr_parse_request_path_pipeline(s.str, s.len, PPchar(&r.method.str), &method_len,
|
|
|
|
PPchar(&r.path.str), &path_len)
|
2020-01-23 03:26:30 +01:00
|
|
|
if pret > 0 {
|
2021-02-15 16:15:52 +01:00
|
|
|
unsafe {
|
|
|
|
r.method = tos(r.method.str, int(method_len))
|
|
|
|
r.path = tos(r.path.str, int(path_len))
|
|
|
|
}
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
return pret
|
|
|
|
}
|