2022-01-04 10:21:08 +01:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-01-23 03:26:30 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module picoev
|
|
|
|
|
2021-04-24 12:21:30 +02:00
|
|
|
import net
|
2020-01-23 03:26:30 +01:00
|
|
|
import picohttpparser
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <signal.h>
|
2021-04-19 18:01:47 +02:00
|
|
|
#flag -I @VEXEROOT/thirdparty/picoev
|
|
|
|
#flag @VEXEROOT/thirdparty/picoev/picoev.o
|
2020-01-23 03:26:30 +01:00
|
|
|
#include "src/picoev.h"
|
|
|
|
|
2021-10-01 12:49:38 +02:00
|
|
|
[typedef]
|
2021-04-24 12:21:30 +02:00
|
|
|
struct C.picoev_loop {}
|
2020-01-23 03:26:30 +01:00
|
|
|
|
2020-05-20 05:36:46 +02:00
|
|
|
fn C.picoev_del(&C.picoev_loop, int) int
|
2020-10-21 11:23:03 +02:00
|
|
|
|
2020-05-20 05:36:46 +02:00
|
|
|
fn C.picoev_set_timeout(&C.picoev_loop, int, int)
|
2020-10-21 11:23:03 +02:00
|
|
|
|
2021-03-05 15:41:11 +01:00
|
|
|
// fn C.picoev_handler(loop &C.picoev_loop, fd int, revents int, cb_arg voidptr)
|
|
|
|
// TODO: (sponge) update to C.picoev_handler with C type def update
|
2021-11-30 10:15:59 +01:00
|
|
|
type PicoevHandler = fn (loop &C.picoev_loop, fd int, revents int, context voidptr)
|
2021-03-05 15:41:11 +01:00
|
|
|
|
2021-11-30 10:15:59 +01:00
|
|
|
fn C.picoev_add(&C.picoev_loop, int, int, int, &PicoevHandler, voidptr) int
|
2020-10-21 11:23:03 +02:00
|
|
|
|
2020-05-20 05:36:46 +02:00
|
|
|
fn C.picoev_init(int) int
|
2020-10-21 11:23:03 +02:00
|
|
|
|
2020-05-20 05:36:46 +02:00
|
|
|
fn C.picoev_create_loop(int) &C.picoev_loop
|
2020-10-21 11:23:03 +02:00
|
|
|
|
2020-05-20 05:36:46 +02:00
|
|
|
fn C.picoev_loop_once(&C.picoev_loop, int) int
|
2020-10-21 11:23:03 +02:00
|
|
|
|
2020-05-20 05:36:46 +02:00
|
|
|
fn C.picoev_destroy_loop(&C.picoev_loop) int
|
2020-10-21 11:23:03 +02:00
|
|
|
|
2020-05-20 05:36:46 +02:00
|
|
|
fn C.picoev_deinit() int
|
2020-10-21 11:23:03 +02:00
|
|
|
|
2021-05-01 13:20:10 +02:00
|
|
|
const (
|
|
|
|
max_fds = 1024
|
|
|
|
max_timeout = 10
|
|
|
|
max_read = 4096
|
|
|
|
max_write = 8192
|
|
|
|
)
|
|
|
|
|
|
|
|
enum Event {
|
|
|
|
read = C.PICOEV_READ
|
|
|
|
write = C.PICOEV_WRITE
|
|
|
|
timeout = C.PICOEV_TIMEOUT
|
|
|
|
add = C.PICOEV_ADD
|
|
|
|
del = C.PICOEV_DEL
|
|
|
|
readwrite = C.PICOEV_READWRITE
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Config {
|
|
|
|
pub:
|
|
|
|
port int = 8080
|
|
|
|
cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response)
|
|
|
|
err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_err_cb
|
|
|
|
user_data voidptr = voidptr(0)
|
|
|
|
timeout_secs int = 8
|
|
|
|
max_headers int = 100
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Picoev {
|
|
|
|
loop &C.picoev_loop
|
|
|
|
cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response)
|
|
|
|
err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError)
|
|
|
|
user_data voidptr
|
|
|
|
timeout_secs int
|
|
|
|
max_headers int
|
|
|
|
mut:
|
|
|
|
date &byte
|
|
|
|
buf &byte
|
|
|
|
idx [1024]int
|
|
|
|
out &byte
|
|
|
|
}
|
|
|
|
|
2020-01-23 03:26:30 +01:00
|
|
|
[inline]
|
2021-05-01 13:20:10 +02:00
|
|
|
fn setup_sock(fd int) ? {
|
|
|
|
flag := 1
|
|
|
|
if C.setsockopt(fd, C.IPPROTO_TCP, C.TCP_NODELAY, &flag, sizeof(int)) < 0 {
|
|
|
|
return error('setup_sock.setup_sock failed')
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
if C.fcntl(fd, C.F_SETFL, C.O_NONBLOCK) != 0 {
|
2021-05-01 13:20:10 +02:00
|
|
|
return error('fcntl failed')
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-05-20 05:36:46 +02:00
|
|
|
fn close_conn(loop &C.picoev_loop, fd int) {
|
2021-10-01 12:49:38 +02:00
|
|
|
C.picoev_del(voidptr(loop), fd)
|
2020-01-23 03:26:30 +01:00
|
|
|
C.close(fd)
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2021-05-01 13:20:10 +02:00
|
|
|
fn req_read(fd int, b &byte, max_len int, idx int) int {
|
2020-07-03 18:10:10 +02:00
|
|
|
unsafe {
|
|
|
|
return C.read(fd, b + idx, max_len - idx)
|
|
|
|
}
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 13:20:10 +02:00
|
|
|
fn rw_callback(loop &C.picoev_loop, fd int, events int, context voidptr) {
|
|
|
|
mut p := unsafe { &Picoev(context) }
|
|
|
|
defer {
|
2020-01-23 03:26:30 +01:00
|
|
|
p.idx[fd] = 0
|
2021-05-01 13:20:10 +02:00
|
|
|
}
|
|
|
|
if (events & int(Event.timeout)) != 0 {
|
|
|
|
close_conn(loop, fd)
|
2020-01-23 03:26:30 +01:00
|
|
|
return
|
2021-05-01 13:20:10 +02:00
|
|
|
} else if (events & int(Event.read)) != 0 {
|
2021-10-01 12:49:38 +02:00
|
|
|
C.picoev_set_timeout(voidptr(loop), fd, p.timeout_secs)
|
2021-04-24 12:21:30 +02:00
|
|
|
|
|
|
|
// Request init
|
2020-07-03 18:10:10 +02:00
|
|
|
mut buf := p.buf
|
|
|
|
unsafe {
|
2021-05-01 13:20:10 +02:00
|
|
|
buf += fd * picoev.max_read // pointer magic
|
2020-07-03 18:10:10 +02:00
|
|
|
}
|
2021-04-24 12:21:30 +02:00
|
|
|
mut req := picohttpparser.Request{}
|
2021-05-01 13:20:10 +02:00
|
|
|
|
|
|
|
// Response init
|
|
|
|
mut out := p.out
|
|
|
|
unsafe {
|
|
|
|
out += fd * picoev.max_write // pointer magic
|
|
|
|
}
|
|
|
|
mut res := picohttpparser.Response{
|
|
|
|
fd: fd
|
|
|
|
date: p.date
|
|
|
|
buf_start: out
|
|
|
|
buf: out
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Request parsing loop
|
|
|
|
r := req_read(fd, buf, picoev.max_read, p.idx[fd]) // Get data from socket
|
|
|
|
if r == 0 {
|
|
|
|
// connection closed by peer
|
2020-01-23 03:26:30 +01:00
|
|
|
close_conn(loop, fd)
|
|
|
|
return
|
2021-05-01 13:20:10 +02:00
|
|
|
} else if r == -1 {
|
|
|
|
// error
|
2021-10-01 12:49:38 +02:00
|
|
|
if C.errno == C.EAGAIN {
|
2021-05-01 13:20:10 +02:00
|
|
|
// try again later
|
2021-04-24 12:21:30 +02:00
|
|
|
return
|
2021-10-01 12:49:38 +02:00
|
|
|
}
|
|
|
|
if C.errno == C.EWOULDBLOCK {
|
|
|
|
// try again later
|
2021-04-24 12:21:30 +02:00
|
|
|
return
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
2021-10-01 12:49:38 +02:00
|
|
|
// fatal error
|
|
|
|
close_conn(loop, fd)
|
|
|
|
return
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
2021-04-24 12:21:30 +02:00
|
|
|
p.idx[fd] += r
|
|
|
|
|
|
|
|
mut s := unsafe { tos(buf, p.idx[fd]) }
|
|
|
|
pret := req.parse_request(s, p.max_headers) // Parse request via picohttpparser
|
|
|
|
if pret > 0 { // Success
|
|
|
|
break
|
|
|
|
} else if pret == -1 { // Parse error
|
2021-05-01 13:20:10 +02:00
|
|
|
p.err_cb(mut p.user_data, req, mut &res, error('ParseError'))
|
2021-04-24 12:21:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-01 13:20:10 +02:00
|
|
|
assert pret == -2
|
|
|
|
// request is incomplete, continue the loop
|
2021-04-24 12:21:30 +02:00
|
|
|
if p.idx[fd] == sizeof(buf) {
|
2021-05-01 13:20:10 +02:00
|
|
|
p.err_cb(mut p.user_data, req, mut &res, error('RequestIsTooLongError'))
|
2021-04-24 12:21:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback (should call .end() itself)
|
|
|
|
p.cb(mut p.user_data, req, mut &res)
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 11:23:03 +02:00
|
|
|
fn accept_callback(loop &C.picoev_loop, fd int, events int, cb_arg voidptr) {
|
2021-05-01 13:20:10 +02:00
|
|
|
mut p := unsafe { &Picoev(cb_arg) }
|
2020-01-23 03:26:30 +01:00
|
|
|
newfd := C.accept(fd, 0, 0)
|
|
|
|
if newfd != -1 {
|
2021-05-01 13:20:10 +02:00
|
|
|
setup_sock(newfd) or {
|
|
|
|
p.err_cb(mut p.user_data, picohttpparser.Request{}, mut &picohttpparser.Response{},
|
|
|
|
err)
|
|
|
|
}
|
2021-10-01 12:49:38 +02:00
|
|
|
C.picoev_add(voidptr(loop), newfd, int(Event.read), p.timeout_secs, rw_callback,
|
|
|
|
cb_arg)
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 13:20:10 +02:00
|
|
|
fn default_err_cb(data voidptr, req picohttpparser.Request, mut res picohttpparser.Response, error IError) {
|
|
|
|
eprintln('picoev: $error')
|
|
|
|
res.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(config Config) &Picoev {
|
2021-06-13 22:53:38 +02:00
|
|
|
fd := C.socket(net.AddrFamily.ip, net.SocketType.tcp, 0)
|
2020-01-23 03:26:30 +01:00
|
|
|
assert fd != -1
|
2021-11-30 10:15:59 +01:00
|
|
|
|
|
|
|
// Setting flags for socket
|
2020-01-23 03:26:30 +01:00
|
|
|
flag := 1
|
|
|
|
assert C.setsockopt(fd, C.SOL_SOCKET, C.SO_REUSEADDR, &flag, sizeof(int)) == 0
|
|
|
|
$if linux {
|
2021-11-30 10:15:59 +01:00
|
|
|
assert C.setsockopt(fd, C.SOL_SOCKET, C.SO_REUSEPORT, &flag, sizeof(int)) == 0
|
2020-01-23 03:26:30 +01:00
|
|
|
assert C.setsockopt(fd, C.IPPROTO_TCP, C.TCP_QUICKACK, &flag, sizeof(int)) == 0
|
|
|
|
timeout := 10
|
|
|
|
assert C.setsockopt(fd, C.IPPROTO_TCP, C.TCP_DEFER_ACCEPT, &timeout, sizeof(int)) == 0
|
|
|
|
queue_len := 4096
|
|
|
|
assert C.setsockopt(fd, C.IPPROTO_TCP, C.TCP_FASTOPEN, &queue_len, sizeof(int)) == 0
|
|
|
|
}
|
2021-11-30 10:15:59 +01:00
|
|
|
|
|
|
|
// Setting addr
|
|
|
|
mut addr := C.sockaddr_in{
|
2022-04-15 13:58:56 +02:00
|
|
|
sin_family: u8(C.AF_INET)
|
2021-11-30 10:15:59 +01:00
|
|
|
sin_port: C.htons(config.port)
|
|
|
|
sin_addr: C.htonl(C.INADDR_ANY)
|
|
|
|
}
|
|
|
|
size := sizeof(C.sockaddr_in)
|
2021-10-01 12:49:38 +02:00
|
|
|
bind_res := C.bind(fd, voidptr(unsafe { &net.Addr(&addr) }), size)
|
2020-01-23 03:26:30 +01:00
|
|
|
assert bind_res == 0
|
|
|
|
listen_res := C.listen(fd, C.SOMAXCONN)
|
|
|
|
assert listen_res == 0
|
2021-05-01 13:20:10 +02:00
|
|
|
setup_sock(fd) or {
|
|
|
|
config.err_cb(mut config.user_data, picohttpparser.Request{}, mut &picohttpparser.Response{},
|
|
|
|
err)
|
|
|
|
}
|
2021-11-30 10:15:59 +01:00
|
|
|
|
2021-05-01 13:20:10 +02:00
|
|
|
C.picoev_init(picoev.max_fds)
|
|
|
|
loop := C.picoev_create_loop(picoev.max_timeout)
|
2020-07-24 12:29:47 +02:00
|
|
|
mut pv := &Picoev{
|
2020-01-23 03:26:30 +01:00
|
|
|
loop: loop
|
2021-05-01 13:20:10 +02:00
|
|
|
cb: config.cb
|
|
|
|
err_cb: config.err_cb
|
|
|
|
user_data: config.user_data
|
|
|
|
timeout_secs: config.timeout_secs
|
|
|
|
max_headers: config.max_headers
|
2022-04-15 13:58:56 +02:00
|
|
|
date: &u8(C.get_date())
|
2021-06-15 13:47:11 +02:00
|
|
|
buf: unsafe { malloc_noscan(picoev.max_fds * picoev.max_read + 1) }
|
|
|
|
out: unsafe { malloc_noscan(picoev.max_fds * picoev.max_write + 1) }
|
2020-01-23 03:26:30 +01:00
|
|
|
}
|
2021-11-30 10:15:59 +01:00
|
|
|
|
2021-10-01 12:49:38 +02:00
|
|
|
C.picoev_add(voidptr(loop), fd, int(Event.read), 0, accept_callback, pv)
|
2020-06-07 01:23:30 +02:00
|
|
|
go update_date(mut pv)
|
2020-01-23 03:26:30 +01:00
|
|
|
return pv
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (p Picoev) serve() {
|
|
|
|
for {
|
|
|
|
C.picoev_loop_once(p.loop, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:35:40 +02:00
|
|
|
fn update_date(mut p Picoev) {
|
2020-01-23 03:26:30 +01:00
|
|
|
for {
|
2022-04-15 13:58:56 +02:00
|
|
|
p.date = &u8(C.get_date())
|
2020-01-23 03:26:30 +01:00
|
|
|
C.usleep(1000000)
|
|
|
|
}
|
|
|
|
}
|