1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

net.http: add a close method to immediatly close the listener of a started http.Server, add more tests (#11248)

This commit is contained in:
Fabricio Pashaj 2021-08-24 19:21:24 +03:00 committed by GitHub
parent 3c85a03b8a
commit 0bf9197f41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 145 additions and 16 deletions

28
examples/http_server.v Normal file
View File

@ -0,0 +1,28 @@
module main
import net.http { CommonHeader, Request, Response, Server }
struct ExampleHandler {}
fn (h ExampleHandler) handle(req Request) Response {
mut res := Response{
header: http.new_header_from_map({
CommonHeader.content_type: 'text/plain'
})
}
res.text = match req.url {
'/foo' { 'bar\n' }
'/hello' { 'world\n' }
'/' { 'foo\nhello\n' }
else { 'Not found\n' }
}
res.status_code = if res.text == 'Not found' { 404 } else { 200 }
return res
}
fn main() {
mut server := Server{
handler: ExampleHandler{}
}
server.listen_and_serve() ?
}

View File

@ -7,12 +7,24 @@ import io
import net import net
import time import time
// ServerStatus is the current status of the server.
// .running means that the server is active and serving.
// .stopped means that the server is not active but still listening.
// .closed means that the server is completely inactive.
pub enum ServerStatus {
running
stopped
closed
}
interface Handler { interface Handler {
handle(Request) Response handle(Request) Response
} }
pub struct Server { pub struct Server {
stop_signal chan bool = chan bool{cap: 1} mut:
state ServerStatus = .closed
listener net.TcpListener
pub mut: pub mut:
port int = 8080 port int = 8080
handler Handler = DebugHandler{} handler Handler = DebugHandler{}
@ -21,22 +33,20 @@ pub mut:
accept_timeout time.Duration = 30 * time.second accept_timeout time.Duration = 30 * time.second
} }
pub fn (s &Server) listen_and_serve() ? { pub fn (mut s Server) listen_and_serve() ? {
if s.handler is DebugHandler { if s.handler is DebugHandler {
eprintln('Server handler not set, using debug handler') eprintln('Server handler not set, using debug handler')
} }
mut l := net.listen_tcp(.ip6, ':$s.port') ? s.listener = net.listen_tcp(.ip6, ':$s.port') ?
l.set_accept_timeout(s.accept_timeout) s.listener.set_accept_timeout(s.accept_timeout)
eprintln('Listening on :$s.port') eprintln('Listening on :$s.port')
s.state = .running
for { for {
// break if we have a stop signal (non-blocking check) // break if we have a stop signal
select { if s.state != .running {
_ := <-s.stop_signal {
break break
} }
else {} mut conn := s.listener.accept() or {
}
mut conn := l.accept() or {
if err.msg != 'net: op timed out' { if err.msg != 'net: op timed out' {
eprintln('accept() failed: $err; skipping') eprintln('accept() failed: $err; skipping')
} }
@ -47,10 +57,27 @@ pub fn (s &Server) listen_and_serve() ? {
// TODO: make concurrent // TODO: make concurrent
s.parse_and_respond(mut conn) s.parse_and_respond(mut conn)
} }
if s.state == .stopped {
s.close()
}
} }
pub fn (s Server) stop() { // stop signals the server that it should not respond anymore
s.stop_signal <- true [inline]
pub fn (mut s Server) stop() {
s.state = .stopped
}
// close immediatly closes the port and signals the server that it has been closed
[inline]
pub fn (mut s Server) close() {
s.state = .closed
s.listener.close() or { return }
}
[inline]
pub fn (s &Server) status() ServerStatus {
return s.state
} }
fn (s &Server) parse_and_respond(mut conn net.TcpConn) { fn (s &Server) parse_and_respond(mut conn net.TcpConn) {

View File

@ -1,16 +1,90 @@
module http import net.http
import time import time
fn test_server_stop() ? { fn test_server_stop() ? {
server := &Server{ mut server := &http.Server{
accept_timeout: 1 * time.second accept_timeout: 1 * time.second
} }
t := go server.listen_and_serve() t := go server.listen_and_serve()
time.sleep(250 * time.millisecond) time.sleep(250 * time.millisecond)
mut watch := time.new_stopwatch() mut watch := time.new_stopwatch()
server.stop() server.stop()
assert server.status() == .stopped
assert watch.elapsed() < 100 * time.millisecond assert watch.elapsed() < 100 * time.millisecond
t.wait() ? t.wait() ?
assert watch.elapsed() < 999 * time.millisecond assert watch.elapsed() < 999 * time.millisecond
} }
fn test_server_close() ? {
mut server := &http.Server{
accept_timeout: 1 * time.second
handler: MyHttpHandler{}
}
t := go server.listen_and_serve()
time.sleep(250 * time.millisecond)
mut watch := time.new_stopwatch()
server.close()
assert server.status() == .closed
assert watch.elapsed() < 100 * time.millisecond
t.wait() ?
assert watch.elapsed() < 999 * time.millisecond
}
struct MyHttpHandler {
mut:
counter int
oks int
not_founds int
}
fn (mut handler MyHttpHandler) handle(req http.Request) http.Response {
handler.counter++
// eprintln('$time.now() | counter: $handler.counter | $req.method $req.url\n$req.header\n$req.data - 200 OK\n')
mut r := http.Response{
text: req.data + ', $req.url'
header: req.header
}
match req.url.all_before('?') {
'/endpoint', '/another/endpoint' {
r.set_status(.ok)
handler.oks++
}
else {
r.set_status(.not_found)
handler.not_founds++
}
}
r.set_version(req.version)
return r
}
const cport = 8198
fn test_server_custom_handler() ? {
mut handler := MyHttpHandler{}
mut server := &http.Server{
accept_timeout: 1 * time.second
handler: handler
port: cport
}
t := go server.listen_and_serve()
for server.status() != .running {
time.sleep(10 * time.millisecond)
}
x := http.fetch(url: 'http://localhost:$cport/endpoint?abc=xyz', data: 'my data') ?
assert x.text == 'my data, /endpoint?abc=xyz'
assert x.status_code == 200
assert x.http_version == '1.1'
y := http.fetch(url: 'http://localhost:$cport/another/endpoint', data: 'abcde') ?
assert y.text == 'abcde, /another/endpoint'
assert y.status_code == 200
assert y.status() == .ok
assert y.http_version == '1.1'
//
http.fetch(url: 'http://localhost:$cport/something/else') ?
server.stop()
t.wait() ?
assert handler.counter == 3
assert handler.oks == 2
assert handler.not_founds == 1
}