From f5c4671bfbad96bf346bd7e9a21fc4317b4959df Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sat, 3 Dec 2022 17:00:20 +0530 Subject: Adds most of the tools --- v_windows/v/old/vlib/picohttpparser/response.v | 114 +++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 v_windows/v/old/vlib/picohttpparser/response.v (limited to 'v_windows/v/old/vlib/picohttpparser/response.v') 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 +} -- cgit v1.2.3