aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/picohttpparser
diff options
context:
space:
mode:
Diffstat (limited to 'v_windows/v/old/vlib/picohttpparser')
-rw-r--r--v_windows/v/old/vlib/picohttpparser/misc.v20
-rw-r--r--v_windows/v/old/vlib/picohttpparser/picohttpparser.v35
-rw-r--r--v_windows/v/old/vlib/picohttpparser/request.v65
-rw-r--r--v_windows/v/old/vlib/picohttpparser/response.v114
4 files changed, 234 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/picohttpparser/misc.v b/v_windows/v/old/vlib/picohttpparser/misc.v
new file mode 100644
index 0000000..6c8e1e7
--- /dev/null
+++ b/v_windows/v/old/vlib/picohttpparser/misc.v
@@ -0,0 +1,20 @@
+module picohttpparser
+
+[inline; unsafe]
+fn cpy(dst &byte, src &byte, len int) int {
+ unsafe { C.memcpy(dst, src, len) }
+ return len
+}
+
+[inline]
+pub fn cmp(dst string, src string) bool {
+ if dst.len != src.len {
+ return false
+ }
+ return unsafe { C.memcmp(dst.str, src.str, src.len) == 0 }
+}
+
+[inline]
+pub fn cmpn(dst string, src string, n int) bool {
+ return unsafe { C.memcmp(dst.str, src.str, n) == 0 }
+}
diff --git a/v_windows/v/old/vlib/picohttpparser/picohttpparser.v b/v_windows/v/old/vlib/picohttpparser/picohttpparser.v
new file mode 100644
index 0000000..a8c93e6
--- /dev/null
+++ b/v_windows/v/old/vlib/picohttpparser/picohttpparser.v
@@ -0,0 +1,35 @@
+// Copyright (c) 2019-2021 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 @VEXEROOT/thirdparty/picohttpparser
+#flag -L @VEXEROOT/thirdparty/picohttpparser
+#flag @VEXEROOT/thirdparty/picohttpparser/picohttpparser.o
+
+#include "picohttpparser.h"
+
+struct C.phr_header {
+pub:
+ name &char
+ name_len int
+ value &char
+ value_len int
+}
+
+type PPchar = &&char
+
+struct C.phr_header_t {}
+
+fn C.phr_parse_request(buf &char, len size_t, method PPchar, method_len &size_t, path PPchar, path_len &size_t, minor_version &int, headers &C.phr_header, num_headers &size_t, last_len size_t) int
+
+fn C.phr_parse_response(buf &char, len size_t, minor_version &int, status &int, msg PPchar, msg_len &size_t, headers &C.phr_header, num_headers &size_t, last_len size_t) int
+
+fn C.phr_parse_headers(buf &char, len size_t, headers &C.phr_header, num_headers &size_t, last_len size_t) int
+
+fn C.phr_parse_request_path(buf_start &char, len size_t, method PPchar, method_len &size_t, path PPchar, path_len &size_t) int
+fn C.phr_parse_request_path_pipeline(buf_start &char, len size_t, method PPchar, method_len &size_t, path PPchar, path_len &size_t) int
+fn C.get_date() &byte
+
+// static inline int u64toa(char* buf, uint64_t value) {
+fn C.u64toa(buffer &char, value u64) int
diff --git a/v_windows/v/old/vlib/picohttpparser/request.v b/v_windows/v/old/vlib/picohttpparser/request.v
new file mode 100644
index 0000000..98667be
--- /dev/null
+++ b/v_windows/v/old/vlib/picohttpparser/request.v
@@ -0,0 +1,65 @@
+module picohttpparser
+
+pub struct Request {
+mut:
+ prev_len int
+pub mut:
+ method string
+ path string
+ headers [100]C.phr_header
+ num_headers u64
+ body string
+}
+
+[inline]
+pub fn (mut r Request) parse_request(s string, max_headers int) int {
+ method_len := size_t(0)
+ path_len := size_t(0)
+ minor_version := 0
+ num_headers := size_t(max_headers)
+
+ pret := C.phr_parse_request(s.str, s.len, PPchar(&r.method.str), &method_len, PPchar(&r.path.str),
+ &path_len, &minor_version, &r.headers[0], &num_headers, r.prev_len)
+ if pret > 0 {
+ unsafe {
+ r.method = tos(r.method.str, int(method_len))
+ r.path = tos(r.path.str, int(path_len))
+ }
+ r.num_headers = u64(num_headers)
+ }
+ r.body = unsafe { (&s.str[pret]).vstring_literal_with_len(s.len - pret) }
+ r.prev_len = s.len
+ return pret
+}
+
+[inline]
+pub fn (mut r Request) parse_request_path(s string) int {
+ method_len := size_t(0)
+ path_len := size_t(0)
+
+ pret := C.phr_parse_request_path(s.str, s.len, PPchar(&r.method.str), &method_len,
+ PPchar(&r.path.str), &path_len)
+ if pret > 0 {
+ unsafe {
+ r.method = tos(r.method.str, int(method_len))
+ r.path = tos(r.path.str, int(path_len))
+ }
+ }
+ return pret
+}
+
+[inline]
+pub fn (mut r Request) parse_request_path_pipeline(s string) int {
+ method_len := size_t(0)
+ path_len := size_t(0)
+
+ pret := C.phr_parse_request_path_pipeline(s.str, s.len, PPchar(&r.method.str), &method_len,
+ PPchar(&r.path.str), &path_len)
+ if pret > 0 {
+ unsafe {
+ r.method = tos(r.method.str, int(method_len))
+ r.path = tos(r.path.str, int(path_len))
+ }
+ }
+ return pret
+}
diff --git a/v_windows/v/old/vlib/picohttpparser/response.v b/v_windows/v/old/vlib/picohttpparser/response.v
new file mode 100644
index 0000000..5c490ea
--- /dev/null
+++ b/v_windows/v/old/vlib/picohttpparser/response.v
@@ -0,0 +1,114 @@
+module picohttpparser
+
+pub struct Response {
+ fd int
+pub:
+ date &byte = 0
+ buf_start &byte = 0
+pub mut:
+ buf &byte = 0
+}
+
+[inline]
+pub fn (mut r Response) write_string(s string) {
+ unsafe {
+ C.memcpy(r.buf, s.str, s.len)
+ r.buf += s.len
+ }
+}
+
+[inline]
+pub fn (mut r Response) http_ok() &Response {
+ r.write_string('HTTP/1.1 200 OK\r\n')
+ return unsafe { r }
+}
+
+[inline]
+pub fn (mut r Response) header(k string, v string) &Response {
+ r.write_string(k)
+ r.write_string(': ')
+ r.write_string(v)
+ r.write_string('\r\n')
+ return unsafe { r }
+}
+
+[inline]
+pub fn (mut r Response) header_date() &Response {
+ r.write_string('Date: ')
+ unsafe {
+ r.buf += cpy(r.buf, r.date, 29)
+ }
+ r.write_string('\r\n')
+ return unsafe { r }
+}
+
+[inline]
+pub fn (mut r Response) header_server() &Response {
+ r.write_string('Server: V\r\n')
+ return unsafe { r }
+}
+
+[inline]
+pub fn (mut r Response) content_type(s string) &Response {
+ r.write_string('Content-Type: ')
+ r.write_string(s)
+ r.write_string('\r\n')
+ return unsafe { r }
+}
+
+[inline]
+pub fn (mut r Response) html() &Response {
+ r.write_string('Content-Type: text/html\r\n')
+ return unsafe { r }
+}
+
+[inline]
+pub fn (mut r Response) plain() &Response {
+ r.write_string('Content-Type: text/plain\r\n')
+ return unsafe { r }
+}
+
+[inline]
+pub fn (mut r Response) json() &Response {
+ r.write_string('Content-Type: application/json\r\n')
+ return unsafe { r }
+}
+
+[inline]
+pub fn (mut r Response) body(body string) {
+ r.write_string('Content-Length: ')
+ unsafe {
+ r.buf += C.u64toa(r.buf, body.len)
+ }
+ r.write_string('\r\n\r\n')
+ r.write_string(body)
+}
+
+[inline]
+pub fn (mut r Response) http_404() {
+ r.write_string('HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n')
+}
+
+[inline]
+pub fn (mut r Response) http_405() {
+ r.write_string('HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n')
+}
+
+[inline]
+pub fn (mut r Response) http_500() {
+ r.write_string('HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n')
+}
+
+[inline]
+pub fn (mut r Response) raw(response string) {
+ r.write_string(response)
+}
+
+[inline]
+pub fn (mut r Response) end() int {
+ n := int(r.buf) - int(r.buf_start)
+ if C.write(r.fd, r.buf_start, n) != n {
+ return -1
+ }
+ return n
+}