blob: 7a9660da7a1ae895b32cb7b69fcbed5e918ef24d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
// 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
// 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 {
handle(Request) Response
}
pub struct Server {
mut:
state ServerStatus = .closed
listener net.TcpListener
pub mut:
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
}
pub fn (mut s Server) listen_and_serve() ? {
if s.handler is DebugHandler {
eprintln('Server handler not set, using debug handler')
}
s.listener = net.listen_tcp(.ip6, ':$s.port') ?
s.listener.set_accept_timeout(s.accept_timeout)
eprintln('Listening on :$s.port')
s.state = .running
for {
// break if we have a stop signal
if s.state != .running {
break
}
mut conn := s.listener.accept() or {
if err.msg != 'net: op timed out' {
eprintln('accept() failed: $err; skipping')
}
continue
}
conn.set_read_timeout(s.read_timeout)
conn.set_write_timeout(s.write_timeout)
// TODO: make concurrent
s.parse_and_respond(mut conn)
}
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 }
}
[inline]
pub fn (s &Server) status() ServerStatus {
return s.state
}
fn (s &Server) parse_and_respond(mut conn net.TcpConn) {
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
}
mut resp := s.handler.handle(req)
if resp.version() == .unknown {
resp.set_version(req.version)
}
conn.write(resp.bytes()) or { eprintln('error sending response: $err') }
}
// 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
}
|