2021-06-05 23:43:14 +02:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module http
|
|
|
|
|
|
|
|
import io
|
|
|
|
import net
|
|
|
|
import time
|
|
|
|
|
2021-08-24 18:21:24 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-08-01 14:50:11 +02:00
|
|
|
interface Handler {
|
2021-10-11 14:41:31 +02:00
|
|
|
mut:
|
2021-08-01 14:50:11 +02:00
|
|
|
handle(Request) Response
|
|
|
|
}
|
|
|
|
|
2021-06-05 23:43:14 +02:00
|
|
|
pub struct Server {
|
2021-08-24 18:21:24 +02:00
|
|
|
mut:
|
|
|
|
state ServerStatus = .closed
|
|
|
|
listener net.TcpListener
|
2021-06-05 23:43:14 +02:00
|
|
|
pub mut:
|
2021-08-19 10:19:49 +02:00
|
|
|
port int = 8080
|
|
|
|
handler Handler = DebugHandler{}
|
|
|
|
read_timeout time.Duration = 30 * time.second
|
|
|
|
write_timeout time.Duration = 30 * time.second
|
|
|
|
accept_timeout time.Duration = 30 * time.second
|
2021-06-05 23:43:14 +02:00
|
|
|
}
|
|
|
|
|
2021-08-24 18:21:24 +02:00
|
|
|
pub fn (mut s Server) listen_and_serve() ? {
|
2021-08-01 14:50:11 +02:00
|
|
|
if s.handler is DebugHandler {
|
2021-06-05 23:43:14 +02:00
|
|
|
eprintln('Server handler not set, using debug handler')
|
|
|
|
}
|
2021-08-24 18:21:24 +02:00
|
|
|
s.listener = net.listen_tcp(.ip6, ':$s.port') ?
|
|
|
|
s.listener.set_accept_timeout(s.accept_timeout)
|
2021-06-05 23:43:14 +02:00
|
|
|
eprintln('Listening on :$s.port')
|
2021-08-24 18:21:24 +02:00
|
|
|
s.state = .running
|
2021-06-05 23:43:14 +02:00
|
|
|
for {
|
2021-08-24 18:21:24 +02:00
|
|
|
// break if we have a stop signal
|
|
|
|
if s.state != .running {
|
|
|
|
break
|
2021-08-19 10:19:49 +02:00
|
|
|
}
|
2021-08-24 18:21:24 +02:00
|
|
|
mut conn := s.listener.accept() or {
|
2021-08-19 10:19:49 +02:00
|
|
|
if err.msg != 'net: op timed out' {
|
|
|
|
eprintln('accept() failed: $err; skipping')
|
|
|
|
}
|
2021-06-05 23:43:14 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
conn.set_read_timeout(s.read_timeout)
|
|
|
|
conn.set_write_timeout(s.write_timeout)
|
|
|
|
// TODO: make concurrent
|
|
|
|
s.parse_and_respond(mut conn)
|
|
|
|
}
|
2021-08-24 18:21:24 +02:00
|
|
|
if s.state == .stopped {
|
|
|
|
s.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop signals the server that it should not respond anymore
|
|
|
|
[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 }
|
2021-06-05 23:43:14 +02:00
|
|
|
}
|
|
|
|
|
2021-08-24 18:21:24 +02:00
|
|
|
[inline]
|
|
|
|
pub fn (s &Server) status() ServerStatus {
|
|
|
|
return s.state
|
2021-08-19 10:19:49 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 14:41:31 +02:00
|
|
|
fn (mut s Server) parse_and_respond(mut conn net.TcpConn) {
|
2021-06-05 23:43:14 +02:00
|
|
|
defer {
|
|
|
|
conn.close() or { eprintln('close() failed: $err') }
|
|
|
|
}
|
|
|
|
|
|
|
|
mut reader := io.new_buffered_reader(reader: conn)
|
|
|
|
defer {
|
|
|
|
reader.free()
|
|
|
|
}
|
|
|
|
req := parse_request(mut reader) or {
|
|
|
|
$if debug {
|
|
|
|
// only show in debug mode to prevent abuse
|
|
|
|
eprintln('error parsing request: $err')
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-08-01 14:50:11 +02:00
|
|
|
mut resp := s.handler.handle(req)
|
2021-07-24 19:47:45 +02:00
|
|
|
if resp.version() == .unknown {
|
|
|
|
resp.set_version(req.version)
|
2021-06-05 23:43:14 +02:00
|
|
|
}
|
2021-06-13 22:53:38 +02:00
|
|
|
conn.write(resp.bytes()) or { eprintln('error sending response: $err') }
|
2021-06-05 23:43:14 +02:00
|
|
|
}
|
2021-08-01 14:50:11 +02:00
|
|
|
|
|
|
|
// DebugHandler implements the Handler interface by echoing the request
|
|
|
|
// in the response
|
|
|
|
struct DebugHandler {}
|
|
|
|
|
|
|
|
fn (d DebugHandler) handle(req Request) Response {
|
|
|
|
$if debug {
|
|
|
|
eprintln('[$time.now()] $req.method $req.url\n\r$req.header\n\r$req.data - 200 OK')
|
|
|
|
} $else {
|
|
|
|
eprintln('[$time.now()] $req.method $req.url - 200')
|
|
|
|
}
|
|
|
|
mut r := Response{
|
|
|
|
text: req.data
|
|
|
|
header: req.header
|
|
|
|
}
|
|
|
|
r.set_status(.ok)
|
|
|
|
r.set_version(req.version)
|
|
|
|
return r
|
|
|
|
}
|