From 442030a7c8e951775745326f9d6d7a891cb60b21 Mon Sep 17 00:00:00 2001 From: Sumeet Chhetri Date: Sun, 7 Jun 2020 04:53:30 +0530 Subject: [PATCH] picoev: make compile, add header parsing --- cmd/tools/modules/testing/common.v | 2 +- examples/pico/pico.v | 12 ++++++-- .../picohttpparser/src/picohttpparser.h | 4 +-- vlib/picoev/picoev.v | 30 ++++++++++++++----- vlib/picohttpparser/request.v | 8 ++--- vlib/picohttpparser/response.v | 2 +- 6 files changed, 41 insertions(+), 17 deletions(-) diff --git a/cmd/tools/modules/testing/common.v b/cmd/tools/modules/testing/common.v index 5a7cb363b..0f10ce8bd 100644 --- a/cmd/tools/modules/testing/common.v +++ b/cmd/tools/modules/testing/common.v @@ -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 } diff --git a/examples/pico/pico.v b/examples/pico/pico.v index 43b430fda..77c2bdec8 100644 --- a/examples/pico/pico.v +++ b/examples/pico/pico.v @@ -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() diff --git a/thirdparty/picohttpparser/src/picohttpparser.h b/thirdparty/picohttpparser/src/picohttpparser.h index 0849f8449..8121b128f 100644 --- a/thirdparty/picohttpparser/src/picohttpparser.h +++ b/thirdparty/picohttpparser/src/picohttpparser.h @@ -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 */ diff --git a/vlib/picoev/picoev.v b/vlib/picoev/picoev.v index cb25a6d64..1b46f203a 100644 --- a/vlib/picoev/picoev.v +++ b/vlib/picoev/picoev.v @@ -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 } diff --git a/vlib/picohttpparser/request.v b/vlib/picohttpparser/request.v index 8aff6c3bf..9d8a182dd 100644 --- a/vlib/picohttpparser/request.v +++ b/vlib/picohttpparser/request.v @@ -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 diff --git a/vlib/picohttpparser/response.v b/vlib/picohttpparser/response.v index dcc44a830..bf4642bc3 100644 --- a/vlib/picohttpparser/response.v +++ b/vlib/picohttpparser/response.v @@ -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 } -- 2.30.2