1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

pico.v and dependencies

This commit is contained in:
S-YOU
2020-01-23 11:26:30 +09:00
committed by Alexander Medvednikov
parent 5c6032d272
commit 7b345e207d
19 changed files with 2475 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
module picohttpparser
[inline]
fn cpy_str(dst byteptr, src string) int {
C.memcpy(dst, src.str, src.len)
return src.len
}
[inline]
fn cpy(dst, src byteptr, len int) int {
C.memcpy(dst, src, len)
return len
}
[inline]
pub fn cmp(dst, src string) bool {
if dst.len != src.len { return false }
return C.memcmp(dst.str, src.str, src.len) == 0
}
[inline]
pub fn cmpn(dst, src string, n int) bool {
return C.memcmp(dst.str, src.str, n) == 0
}

View File

@@ -0,0 +1,29 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module picohttpparser
#flag -I @VROOT/thirdparty/picohttpparser
#flag -L @VROOT/thirdparty/picohttpparser
#flag @VROOT/thirdparty/picohttpparser/picohttpparser.o
#include "picohttpparser.h"
struct C.phr_header {
pub:
name charptr
name_len int
value charptr
value_len int
}
struct C.phr_header_t {}
fn phr_parse_request() int
fn phr_parse_response() int
fn phr_parse_headers() int
fn phr_parse_request_path() int
fn phr_parse_request_path_pipeline() int
fn C.get_date() byteptr
fn C.u64toa() int
fn C.memcmp() int

View File

@@ -0,0 +1,67 @@
module picohttpparser
pub struct Request {
mut:
method string
path string
headers *C.phr_header_t
num_headers u64
}
[inline]
pub fn (r mut Request) parse_request(s string, headers *C.phr_header_t, max_headers int) int {
method_len := u64(0)
path_len := u64(0)
minor_version := 0
num_headers := u64(max_headers)
pret := C.phr_parse_request(
s.str, s.len,
&r.method, &method_len,
&r.path, &path_len,
&minor_version,
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
}
[inline]
pub fn (r mut Request) parse_request_path(s string) int {
method_len := u64(0)
path_len := u64(0)
pret := C.phr_parse_request_path(
s.str, s.len,
&r.method, &method_len,
&r.path, &path_len
)
if pret > 0 {
r.method = tos(r.method.str, int(method_len))
r.path = tos(r.path.str, int(path_len))
}
return pret
}
[inline]
pub fn (r mut Request) parse_request_path_pipeline(s string) int {
method_len := u64(0)
path_len := u64(0)
pret := C.phr_parse_request_path_pipeline(
s.str, s.len,
&r.method, &method_len,
&r.path, &path_len
)
if pret > 0 {
r.method = tos(r.method.str, int(method_len))
r.path = tos(r.path.str, int(path_len))
}
return pret
}

View File

@@ -0,0 +1,96 @@
module picohttpparser
pub struct Response {
fd int
pub:
date byteptr
buf_start byteptr
mut:
buf byteptr
}
[inline]
pub fn (r mut Response) http_ok() &Response {
r.buf += cpy_str(r.buf, "HTTP/1.1 200 OK\r\n")
return r
}
[inline]
pub fn (r mut Response) header(k, v string) &Response {
r.buf += cpy_str(r.buf, k)
r.buf += cpy_str(r.buf, ": ")
r.buf += cpy_str(r.buf, v)
r.buf += cpy_str(r.buf, "\r\n")
return r
}
[inline]
pub fn (r mut Response) header_date() &Response {
r.buf += cpy_str(r.buf, "Date: ")
r.buf += cpy(r.buf, r.date, 29)
r.buf += cpy_str(r.buf, "\r\n")
return r
}
[inline]
pub fn (r mut Response) header_server() &Response {
r.buf += cpy_str(r.buf, "Server: V\r\n")
return r
}
[inline]
pub fn (r mut Response) content_type(s string) &Response {
r.buf += cpy_str(r.buf, "Content-Type: ")
r.buf += cpy_str(r.buf, s)
r.buf += cpy_str(r.buf, "\r\n")
return r
}
[inline]
pub fn (r mut Response) plain() &Response {
r.buf += cpy_str(r.buf, "Content-Type: text/plain\r\n")
return r
}
[inline]
pub fn (r mut Response) json() &Response {
r.buf += cpy_str(r.buf, "Content-Type: application/json\r\n")
return r
}
[inline]
pub fn (r mut Response) body(body string) {
r.buf += cpy_str(r.buf, "Content-Length: ")
r.buf += C.u64toa(r.buf, body.len)
r.buf += cpy_str(r.buf, "\r\n\r\n")
r.buf += cpy_str(r.buf, body)
}
[inline]
pub fn (r mut Response) http_404() {
r.buf += cpy_str(r.buf, 'HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n')
}
[inline]
pub fn (r mut Response) http_405() {
r.buf += cpy_str(r.buf, 'HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n')
}
[inline]
pub fn (r mut Response) http_500() {
r.buf += cpy_str(r.buf, 'HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n')
}
[inline]
pub fn (r mut Response) raw(response string) {
r.buf += cpy_str(r.buf, response)
}
[inline]
pub fn (r mut Response) end() int {
n := int(r.buf - r.buf_start)
if C.write(r.fd, r.buf_start, n) != n {
return -1
}
return n
}