Skip to content

Commit

Permalink
vweb2 (#19997)
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper64 authored Dec 9, 2023
1 parent 2768de1 commit 08189d6
Show file tree
Hide file tree
Showing 27 changed files with 4,001 additions and 24 deletions.
1 change: 1 addition & 0 deletions cmd/tools/modules/testing/common.v
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession {
skip_files << 'examples/call_v_from_python/test.v' // the example only makes sense to be compiled, when python is installed
skip_files << 'examples/call_v_from_ruby/test.v' // the example only makes sense to be compiled, when ruby is installed
skip_files << 'vlib/vweb/vweb_app_test.v' // imports the `sqlite` module, which in turn includes sqlite3.h
skip_files << 'vlib/x/vweb/tests/vweb_app_test.v' // imports the `sqlite` module, which in turn includes sqlite3.h
}
$if !macos {
skip_files << 'examples/macos_tray/tray.v'
Expand Down
2 changes: 2 additions & 0 deletions cmd/tools/vtest-self.v
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ const skip_on_ubuntu_musl = [
'vlib/net/smtp/smtp_test.v',
'vlib/v/tests/websocket_logger_interface_should_compile_test.v',
'vlib/v/tests/fn_literal_type_test.v',
'vlib/vweb/x/tests/vweb_test.v',
'vlib/vweb/x/tests/vweb_app_test.v',
]
const skip_on_linux = [
'do_not_remove',
Expand Down
2 changes: 1 addition & 1 deletion examples/pico/pico.v
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn callback(data voidptr, req picohttpparser.Request, mut res picohttpparser.Res
}

fn main() {
println('Starting webserver on http://127.0.0.1:${port}/ ...')
println('Starting webserver on http://localhost:${port}/ ...')
mut server := picoev.new(port: port, cb: callback)
server.serve()
}
3 changes: 2 additions & 1 deletion examples/pico/raw_callback.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net
import picoev

const port = 8080

const http_response = 'HTTP/1.1 200 OK\r\nContent-type: text/html\r\nContent-length: 18\r\n\r\nHello from Picoev!'

fn main() {
Expand All @@ -15,7 +16,7 @@ fn main() {
pico.serve()
}

fn handle_conn(mut pv picoev.Picoev, fd int) {
fn handle_conn(mut pv picoev.Picoev, fd int, events int) {
// setup a nonblocking tcp connection
mut conn := &net.TcpConn{
sock: net.tcp_socket_from_handle_raw(fd)
Expand Down
3 changes: 3 additions & 0 deletions vlib/io/buffered_reader.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mut:
mfails int // maximum fails, after which we can assume that the stream has ended
pub mut:
end_of_stream bool // whether we reached the end of the upstream reader
total_read int // total number of bytes read
}

// BufferedReaderConfig are options that can be given to a buffered reader.
Expand Down Expand Up @@ -55,6 +56,7 @@ pub fn (mut r BufferedReader) read(mut buf []u8) !int {
}
}
r.offset += read
r.total_read += read
return read
}

Expand Down Expand Up @@ -128,6 +130,7 @@ pub fn (mut r BufferedReader) read_line() !string {
// try and find a newline character
mut i := r.offset
for ; i < r.len; i++ {
r.total_read++
c := r.buf[i]
if c == `\n` {
// great, we hit something
Expand Down
35 changes: 35 additions & 0 deletions vlib/io/reader_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,38 @@ fn test_leftover() {
}
assert r.end_of_stream()
}

fn test_totalread_read() {
text := 'Some testing text'
mut s := StringReader{
text: text
}
mut r := new_buffered_reader(reader: s)

mut buf := []u8{len: text.len}
total := r.read(mut buf) or {
assert false
panic('bad')
}

assert r.total_read == total
}

fn test_totalread_readline() {
text := 'Some testing text\nmore_enters'
mut s := StringReader{
text: text
}
mut r := new_buffered_reader(reader: s)

_ := r.read_line() or {
assert false
panic('bad')
}
_ := r.read_line() or {
assert false
panic('bad')
}

assert r.total_read == text.len
}
33 changes: 24 additions & 9 deletions vlib/picoev/picoev.v
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
module picoev

import net
import picohttpparser
import time

pub const max_fds = 1024

pub const max_queue = 4096

// events
pub const picoev_read = 1

pub const picoev_write = 2

pub const picoev_timeout = 4

pub const picoev_add = 0x40000000

pub const picoev_del = 0x20000000

pub const picoev_readwrite = 3

// Target is a data representation of everything that needs to be associated with a single
Expand All @@ -31,19 +38,21 @@ pub:
port int = 8080
cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = unsafe { nil }
err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_err_cb
raw_cb fn (mut Picoev, int) = unsafe { nil }
user_data voidptr = unsafe { nil }
timeout_secs int = 8
max_headers int = 100
max_read int = 4096
max_write int = 8192
raw_cb fn (mut Picoev, int, int) = unsafe { nil }
user_data voidptr = unsafe { nil }
timeout_secs int = 8
max_headers int = 100
max_read int = 4096
max_write int = 8192
family net.AddrFamily = .ip
host string = 'localhost'
}

@[heap]
pub struct Picoev {
cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = unsafe { nil }
err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_err_cb
raw_cb fn (mut Picoev, int) = unsafe { nil }
raw_cb fn (mut Picoev, int, int) = unsafe { nil }

timeout_secs int
max_headers int = 100
Expand Down Expand Up @@ -98,7 +107,7 @@ pub fn (mut pv Picoev) add(fd int, events int, timeout int, cb voidptr) int {

// del removes a file descriptor from the loop
@[direct_array_access]
fn (mut pv Picoev) del(fd int) int {
pub fn (mut pv Picoev) del(fd int) int {
assert fd < picoev.max_fds
mut target := pv.file_descriptors[fd]

Expand Down Expand Up @@ -212,7 +221,7 @@ fn raw_callback(fd int, events int, context voidptr) {
} else if events & picoev.picoev_read != 0 {
pv.set_timeout(fd, pv.timeout_secs)
if !isnil(pv.raw_cb) {
pv.raw_cb(mut pv, fd)
pv.raw_cb(mut pv, fd, events)
return
}

Expand Down Expand Up @@ -272,6 +281,12 @@ fn raw_callback(fd int, events int, context voidptr) {

// Callback (should call .end() itself)
pv.cb(pv.user_data, req, mut &res)
} else if events & picoev.picoev_write != 0 {
pv.set_timeout(fd, pv.timeout_secs)
if !isnil(pv.raw_cb) {
pv.raw_cb(mut pv, fd, events)
return
}
}
}

Expand Down
23 changes: 10 additions & 13 deletions vlib/picoev/socket_util.c.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module picoev

import net
import net.conv
import picohttpparser

#include <errno.h>
Expand Down Expand Up @@ -102,7 +101,7 @@ fn fatal_socket_error(fd int) bool {
// listen creates a listening tcp socket and returns its file descriptor
fn listen(config Config) int {
// not using the `net` modules sockets, because not all socket options are defined
fd := C.socket(net.AddrFamily.ip, net.SocketType.tcp, 0)
fd := C.socket(config.family, net.SocketType.tcp, 0)
assert fd != -1

$if trace_fd ? {
Expand All @@ -124,20 +123,18 @@ fn listen(config Config) int {
}

// addr settings
saddr := '${config.host}:${config.port}'
addrs := net.resolve_addrs(saddr, config.family, .tcp) or { panic(err) }
addr := addrs[0]
alen := addr.len()

sin_port := conv.hton16(u16(config.port))
sin_addr := conv.hton32(u32(C.INADDR_ANY))
mut addr := C.sockaddr_in{
sin_family: u8(C.AF_INET)
sin_port: sin_port
sin_addr: sin_addr
net.socket_error_message(C.bind(fd, voidptr(&addr), alen), 'binding to ${saddr} failed') or {
panic(err)
}
net.socket_error_message(C.listen(fd, C.SOMAXCONN), 'listening on ${saddr} with maximum backlog pending queue of ${C.SOMAXCONN}, failed') or {
panic(err)
}
size := sizeof(C.sockaddr_in)
bind_res := C.bind(fd, voidptr(unsafe { &net.Addr(&addr) }), size)
assert bind_res == 0

listen_res := C.listen(fd, C.SOMAXCONN)
assert listen_res == 0
setup_sock(fd) or {
config.err_cb(config.user_data, picohttpparser.Request{}, mut &picohttpparser.Response{},
err)
Expand Down
Loading

0 comments on commit 08189d6

Please sign in to comment.