2020-01-23 05:26:30 +03:00
|
|
|
module picohttpparser
|
|
|
|
|
|
|
|
pub struct Response {
|
|
|
|
pub:
|
2023-06-08 01:54:15 +03:00
|
|
|
fd int
|
2022-09-15 07:59:31 +03:00
|
|
|
date &u8 = unsafe { nil }
|
|
|
|
buf_start &u8 = unsafe { nil }
|
2020-05-20 06:36:46 +03:00
|
|
|
pub mut:
|
2022-09-15 07:59:31 +03:00
|
|
|
buf &u8 = unsafe { nil }
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
2020-07-24 13:29:47 +03:00
|
|
|
[inline]
|
2021-04-24 13:21:30 +03:00
|
|
|
pub fn (mut r Response) write_string(s string) {
|
2020-07-01 15:50:17 +03:00
|
|
|
unsafe {
|
|
|
|
C.memcpy(r.buf, s.str, s.len)
|
|
|
|
r.buf += s.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 05:26:30 +03:00
|
|
|
[inline]
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut r Response) http_ok() &Response {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('HTTP/1.1 200 OK\r\n')
|
2021-05-07 15:58:48 +03:00
|
|
|
return unsafe { r }
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-10-21 12:23:03 +03:00
|
|
|
pub fn (mut r Response) header(k string, v string) &Response {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string(k)
|
|
|
|
r.write_string(': ')
|
|
|
|
r.write_string(v)
|
|
|
|
r.write_string('\r\n')
|
2021-05-07 15:58:48 +03:00
|
|
|
return unsafe { r }
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut r Response) header_date() &Response {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('Date: ')
|
2020-07-01 15:50:17 +03:00
|
|
|
unsafe {
|
2023-07-12 09:40:16 +03:00
|
|
|
C.memcpy(r.buf, r.date, 29)
|
|
|
|
r.buf += 29
|
2020-07-01 15:50:17 +03:00
|
|
|
}
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('\r\n')
|
2021-05-07 15:58:48 +03:00
|
|
|
return unsafe { r }
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut r Response) header_server() &Response {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('Server: V\r\n')
|
2021-05-07 15:58:48 +03:00
|
|
|
return unsafe { r }
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut r Response) content_type(s string) &Response {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('Content-Type: ')
|
|
|
|
r.write_string(s)
|
|
|
|
r.write_string('\r\n')
|
2021-05-07 15:58:48 +03:00
|
|
|
return unsafe { r }
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
2020-06-29 21:25:16 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (mut r Response) html() &Response {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('Content-Type: text/html\r\n')
|
2021-05-07 15:58:48 +03:00
|
|
|
return unsafe { r }
|
2020-06-29 21:25:16 +03:00
|
|
|
}
|
|
|
|
|
2020-01-23 05:26:30 +03:00
|
|
|
[inline]
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut r Response) plain() &Response {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('Content-Type: text/plain\r\n')
|
2021-05-07 15:58:48 +03:00
|
|
|
return unsafe { r }
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut r Response) json() &Response {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('Content-Type: application/json\r\n')
|
2021-05-07 15:58:48 +03:00
|
|
|
return unsafe { r }
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut r Response) body(body string) {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('Content-Length: ')
|
2020-07-01 15:50:17 +03:00
|
|
|
unsafe {
|
2023-07-12 09:40:16 +03:00
|
|
|
r.buf += u64toa(r.buf, u64(body.len)) or { panic(err) }
|
2020-07-01 15:50:17 +03:00
|
|
|
}
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('\r\n\r\n')
|
|
|
|
r.write_string(body)
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut r Response) http_404() {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n')
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut r Response) http_405() {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n')
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut r Response) http_500() {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string('HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n')
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut r Response) raw(response string) {
|
2021-03-20 19:25:51 +03:00
|
|
|
r.write_string(response)
|
2020-01-23 05:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut r Response) end() int {
|
2021-10-01 13:49:38 +03:00
|
|
|
n := int(i64(r.buf) - i64(r.buf_start))
|
2023-07-12 09:40:16 +03:00
|
|
|
// use send instead of write for windows compatibility
|
|
|
|
if C.send(r.fd, r.buf_start, n, 0) != n {
|
2020-01-23 05:26:30 +03:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|