Skip to content

Commit

Permalink
tune header buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
S-YOU committed Jan 22, 2020
1 parent 584c029 commit 44ab721
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .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
4 changes: 2 additions & 2 deletions main.v
Expand Up @@ -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()
Expand Down
28 changes: 28 additions & 0 deletions picohttpparser/response.v
Expand Up @@ -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
}

0 comments on commit 44ab721

Please sign in to comment.