2020-01-23 03:26:30 +01:00
|
|
|
module picohttpparser
|
|
|
|
|
|
|
|
pub struct Response {
|
2020-10-21 11:23:03 +02:00
|
|
|
fd int
|
2020-01-23 03:26:30 +01:00
|
|
|
pub:
|
2020-10-21 11:23:03 +02:00
|
|
|
date byteptr
|
2020-01-23 03:26:30 +01:00
|
|
|
buf_start byteptr
|
2020-05-20 05:36:46 +02:00
|
|
|
pub mut:
|
2020-10-21 11:23:03 +02:00
|
|
|
buf byteptr
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
|
2020-07-24 12:29:47 +02:00
|
|
|
[inline]
|
2020-07-01 14:50:17 +02:00
|
|
|
fn (mut r Response) write_str(s string) {
|
|
|
|
unsafe {
|
|
|
|
C.memcpy(r.buf, s.str, s.len)
|
|
|
|
r.buf += s.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 03:26:30 +01:00
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) http_ok() &Response {
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str('HTTP/1.1 200 OK\r\n')
|
2020-01-23 03:26:30 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-10-21 11:23:03 +02:00
|
|
|
pub fn (mut r Response) header(k string, v string) &Response {
|
2020-07-01 14:50:17 +02:00
|
|
|
r.write_str(k)
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str(': ')
|
2020-07-01 14:50:17 +02:00
|
|
|
r.write_str(v)
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str('\r\n')
|
2020-01-23 03:26:30 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) header_date() &Response {
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str('Date: ')
|
2020-07-01 14:50:17 +02:00
|
|
|
unsafe {
|
|
|
|
r.buf += cpy(r.buf, r.date, 29)
|
|
|
|
}
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str('\r\n')
|
2020-01-23 03:26:30 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) header_server() &Response {
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str('Server: V\r\n')
|
2020-01-23 03:26:30 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) content_type(s string) &Response {
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str('Content-Type: ')
|
2020-07-01 14:50:17 +02:00
|
|
|
r.write_str(s)
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str('\r\n')
|
2020-01-23 03:26:30 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-06-29 20:25:16 +02:00
|
|
|
[inline]
|
|
|
|
pub fn (mut r Response) html() &Response {
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str('Content-Type: text/html\r\n')
|
2020-06-29 20:25:16 +02:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-01-23 03:26:30 +01:00
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) plain() &Response {
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str('Content-Type: text/plain\r\n')
|
2020-01-23 03:26:30 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) json() &Response {
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str('Content-Type: application/json\r\n')
|
2020-01-23 03:26:30 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) body(body string) {
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str('Content-Length: ')
|
2020-07-01 14:50:17 +02:00
|
|
|
unsafe {
|
|
|
|
r.buf += C.u64toa(r.buf, body.len)
|
|
|
|
}
|
2020-10-21 11:23:03 +02:00
|
|
|
r.write_str('\r\n\r\n')
|
2020-07-01 14:50:17 +02:00
|
|
|
r.write_str(body)
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) http_404() {
|
2020-07-01 14:50:17 +02:00
|
|
|
r.write_str('HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n')
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) http_405() {
|
2020-07-01 14:50:17 +02:00
|
|
|
r.write_str('HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n')
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) http_500() {
|
2020-07-01 14:50:17 +02:00
|
|
|
r.write_str('HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n')
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Response) raw(response string) {
|
2020-07-01 14:50:17 +02:00
|
|
|
r.write_str(response)
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[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
|
|
|
|
}
|