2020-01-23 03:26:30 +01:00
|
|
|
module picohttpparser
|
|
|
|
|
|
|
|
pub struct Response {
|
|
|
|
fd int
|
|
|
|
pub:
|
|
|
|
date byteptr
|
|
|
|
buf_start byteptr
|
2020-05-20 05:36:46 +02:00
|
|
|
pub mut:
|
2020-01-23 03:26:30 +01:00
|
|
|
buf byteptr
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) http_ok() &Response {
|
2020-01-23 03:26:30 +01:00
|
|
|
r.buf += cpy_str(r.buf, "HTTP/1.1 200 OK\r\n")
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) header(k, v string) &Response {
|
2020-01-23 03:26:30 +01:00
|
|
|
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]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) header_date() &Response {
|
2020-01-23 03:26:30 +01:00
|
|
|
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]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) header_server() &Response {
|
2020-01-23 03:26:30 +01:00
|
|
|
r.buf += cpy_str(r.buf, "Server: V\r\n")
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) content_type(s string) &Response {
|
2020-01-23 03:26:30 +01:00
|
|
|
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]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) plain() &Response {
|
2020-01-23 03:26:30 +01:00
|
|
|
r.buf += cpy_str(r.buf, "Content-Type: text/plain\r\n")
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) json() &Response {
|
2020-01-23 03:26:30 +01:00
|
|
|
r.buf += cpy_str(r.buf, "Content-Type: application/json\r\n")
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) body(body string) {
|
2020-01-23 03:26:30 +01:00
|
|
|
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]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) http_404() {
|
2020-01-23 03:26:30 +01:00
|
|
|
r.buf += cpy_str(r.buf, 'HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) http_405() {
|
2020-01-23 03:26:30 +01:00
|
|
|
r.buf += cpy_str(r.buf, 'HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) http_500() {
|
2020-01-23 03:26:30 +01:00
|
|
|
r.buf += cpy_str(r.buf, 'HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) raw(response string) {
|
2020-01-23 03:26:30 +01:00
|
|
|
r.buf += cpy_str(r.buf, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) end() int {
|
2020-06-07 01:23:30 +02:00
|
|
|
n := int(r.buf) - int(r.buf_start)
|
2020-01-23 03:26:30 +01:00
|
|
|
if C.write(r.fd, r.buf_start, n) != n {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|