diff --git a/.gitmodules b/.gitmodules index 86bacf7..f1adb72 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,8 +1,8 @@ [submodule "picoev/src"] path = picoev/src url = https://github.com/S-YOU/picoev - branch = v0.0.1 + branch = dev [submodule "picohttpparser/src"] path = picohttpparser/src url = https://github.com/S-YOU/picohttpparser - branch = v0.0.1 + branch = dev diff --git a/main.v b/main.v index 04e6d78..9672498 100644 --- a/main.v +++ b/main.v @@ -22,10 +22,10 @@ fn hello_response() string { pub fn callback(req hp.Request, res mut hp.Response) { if hp.cmpn(req.method, 'GET ', 4) { if hp.cmp(req.path, '/t') { - res.http_ok().header_server().header_date().plain().body(hello_response()) + res.http_ok_plain(hello_response()) } else if hp.cmp(req.path, '/j') { - res.http_ok().header_server().header_date().json().body(json_response()) + res.http_ok_json(json_response()) } else { res.http_404() diff --git a/picohttpparser/response.v b/picohttpparser/response.v index 2540fb7..7b0f8bb 100644 --- a/picohttpparser/response.v +++ b/picohttpparser/response.v @@ -94,3 +94,31 @@ pub fn (r mut Response) end() int { } return n } + +[inline] +pub fn (r mut Response) http_ok_plain(s string) &Response { + mut buf := r.buf + cpy_str(buf, "HTTP/1.1 200 OK\r\nServer: V\r\nDate: ") + cpy(buf + 34, r.date, 29) + cpy_str(buf + 63, "\r\nContent-Type: text/plain\r\nContent-Length: ") + buf += 107 + buf += C.u64toa(buf, s.len) + buf += cpy_str(buf, "\r\n\r\n") + buf += cpy_str(buf, s) + r.buf = buf + return r +} + +[inline] +pub fn (r mut Response) http_ok_json(s string) &Response { + mut buf := r.buf + cpy_str(buf, "HTTP/1.1 200 OK\r\nServer: V\r\nDate: ") + cpy(buf + 34, r.date, 29) + cpy_str(buf + 63, "\r\nContent-Type: application/json\r\nContent-Length: ") + buf += 113 + buf += C.u64toa(buf, s.len) + buf += cpy_str(buf, "\r\n\r\n") + buf += cpy_str(buf, s) + r.buf = buf + return r +}