picoev: make compile, add header parsing

pull/5257/head
Sumeet Chhetri 2020-06-07 04:53:30 +05:30 committed by GitHub
parent d62d0c40d2
commit 442030a7c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 17 deletions

View File

@ -231,7 +231,7 @@ pub fn v_build_failing_skipped(zargs string, folder string, skipped []string) bo
mut mains := []string{}
for f in files {
if !f.contains('modules') && !f.contains('preludes') {
if f.contains('life_gg') || f.contains('/graph.v') || f.contains('/vweb/') || f.contains('/pg/') || f.contains('rune.v') || f.contains('/pico') {
if f.contains('life_gg') || f.contains('/graph.v') || f.contains('/vweb/') || f.contains('/pg/') || f.contains('rune.v') {
continue
}

View File

@ -23,10 +23,18 @@ fn hello_response() string {
fn callback(req picohttpparser.Request, mut res picohttpparser.Response) {
if picohttpparser.cmpn(req.method, 'GET ', 4) {
if picohttpparser.cmp(req.path, '/t') {
res.http_ok().header_server().header_date().plain().body(hello_response())
res.http_ok()
res.header_server()
res.header_date()
res.plain()
res.body(hello_response())
}
else if picohttpparser.cmp(req.path, '/j') {
res.http_ok().header_server().header_date().json().body(json_response())
res.http_ok()
res.header_server()
res.header_date()
res.json()
res.body(json_response())
}
else {
res.http_404()

View File

@ -39,12 +39,12 @@ extern "C" {
/* contains name and value of a header (name == NULL if is a continuing line
* of a multiline header */
struct phr_header {
typedef struct phr_header {
const char *name;
size_t name_len;
const char *value;
size_t value_len;
};
}phr_header;
/* returns number of bytes consumed if successful, -2 if request is partial,
* -1 if failed */

View File

@ -40,6 +40,8 @@ mut:
struct C.sockaddr_storage {}
fn C.atoi() int
fn C.strncasecmp() int
fn C.socket() int
fn C.setsockopt() int
fn C.htonl() int
@ -51,14 +53,14 @@ fn C.getaddrinfo() int
fn C.connect() int
fn C.send() int
fn C.recv() int
fn C.read() int
//fn C.read() int
fn C.shutdown() int
fn C.close() int
//fn C.close() int
fn C.ntohs() int
fn C.getsockname() int
fn C.fcntl() int
fn C.write() int
//fn C.write() int
struct C.picoev_loop {}
@ -148,14 +150,28 @@ fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
}
mut req := picohttpparser.Request{}
for {
pret := req.parse_request_path_pipeline(s)
pret := req.parse_request(s, 100)
if pret <= 0 && s.len > 0 {
C.memmove(buf, s.str, s.len)
p.idx[fd] = s.len
p.oidx[fd] = int(res.buf - res.buf_start)
p.oidx[fd] = int(res.buf) - int(res.buf_start)
break
}
p.cb(req, mut res)
if req.method.str[0]==`p` || req.method.str[0]==`P` || req.method.str[0]==`d` || req.method.str[0]==`D` {
mut j := 0
for {
if j == req.num_headers {
break
}
if req.headers[j].name_len == 14 && C.strncasecmp(req.headers[j].name, "content-length", 14) == 0 {
//cont_length := C.atoi(tos(req.headers[j].value, req.headers[j].value_len).str)
//println('$cont_length')
//TODO need to maintain state of incomplete request to collect body later
}
j = j+1
}
}
p.cb(req, mut &res)
if pret >= s.len {
p.idx[fd] = 0
p.oidx[fd] = 0
@ -218,7 +234,7 @@ pub fn new(port int, cb voidptr) &Picoev {
}
C.picoev_add(loop, fd, C.PICOEV_READ, 0, accept_callback, pv)
go update_date(pv)
go update_date(mut pv)
return pv
}

View File

@ -4,15 +4,16 @@ pub struct Request {
pub mut:
method string
path string
headers &C.phr_header_t
headers[100] C.phr_header
num_headers u64
body string
}
[inline]
pub fn (mut r Request) parse_request(s string, headers &C.phr_header_t, max_headers int) int {
pub fn (mut r Request) parse_request(s string, max_headers int) int {
method_len := u64(0)
path_len := u64(0)
minor_version := 0
@ -23,13 +24,12 @@ pub fn (mut r Request) parse_request(s string, headers &C.phr_header_t, max_head
&r.method, &method_len,
&r.path, &path_len,
&minor_version,
headers, &num_headers,
r.headers, &num_headers,
0
)
if pret > 0 {
r.method = tos(r.method.str, int(method_len))
r.path = tos(r.path.str, int(path_len))
r.headers = headers
r.num_headers = num_headers
}
return pret

View File

@ -88,7 +88,7 @@ pub fn (mut r Response) raw(response string) {
[inline]
pub fn (mut r Response) end() int {
n := int(r.buf - r.buf_start)
n := int(r.buf) - int(r.buf_start)
if C.write(r.fd, r.buf_start, n) != n {
return -1
}