/
webserver_native.fz
82 lines (66 loc) · 2.24 KB
/
webserver_native.fz
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
# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation. If not, see <https://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------
#
# Tokiwa Software GmbH, Germany
#
# Source code of Fuzion example webserver
#
# -----------------------------------------------------------------------
# webserver is a minimalistic webserver example using the networking
# facilities provided by Fuzion itself.
#
# To start this webserver in the interpreter, use:
#
# fz webserver_native.fz
#
# Alternatively, compile it using the C backend and run the resulting
# executable:
#
# fz -c webserver_native.fz
# ./webserver
#
webserver_native is
port u16 := 8080
match net.server net.family.ipv6 net.protocol.tcp port
e error => say e
unit =>
say "started server on port: $port"
say "you may now send a request, e.g.:"
say "echo 'Hello' | curl 'http://[::1]:8080'"
# definition of a request handler
#
rh(n i32) : net.Request_Handler unit is
handle_request unit =>
say "accepted connection:"
match io.buffered.read_line
str String => say str
* =>
send200(data String) outcome unit =>
net.channel.env
.write """
HTTP/1.1 200 OK
Connection: close
Server: Fuzion demo WebServer v0.02
Content-Length: {data.byte_length}
Content-Type: text/html
{data}
""".utf8
_ := send200 "<html>Hello Fuzion $n!</html>"
say "closed connection"
# accept connections while server is active
#
for n in 1..
while net.server.is_active
net.server.accept (rh n)