-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.pony
More file actions
79 lines (67 loc) · 2.17 KB
/
Copy pathmain.pony
File metadata and controls
79 lines (67 loc) · 2.17 KB
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
use stallion = "../../stallion"
use lori = "lori"
actor Main
new create(env: Env) =>
let auth = lori.TCPListenAuth(env.root)
Listener(auth, "0.0.0.0", "8080", env.out)
actor Listener is lori.TCPListenerActor
"""
TCP listener that creates `HeadServer` actors for each connection.
"""
var _tcp_listener: lori.TCPListener = lori.TCPListener.none()
let _out: OutStream
let _config: stallion.ServerConfig
let _server_auth: lori.TCPServerAuth
new create(
auth: lori.TCPListenAuth,
host: String,
port: String,
out: OutStream)
=>
_out = out
_server_auth = lori.TCPServerAuth(auth)
_config = stallion.ServerConfig(host, port)
_tcp_listener = lori.TCPListener(auth, host, port, this)
fun ref _listener(): lori.TCPListener => _tcp_listener
fun ref _on_accept(fd: U32): lori.TCPConnectionActor =>
HeadServer(_server_auth, fd, _config)
fun ref _on_listening() =>
try
(let host, let port) = _tcp_listener.local_address().name()?
_out.print("Server listening on " + host + ":" + port)
else
_out.print("Server listening")
end
fun ref _on_listen_failure() =>
_out.print("Failed to start server")
fun ref _on_closed() =>
_out.print("Server closed")
actor HeadServer is stallion.HTTPServerActor
"""
Responds to GET with a body and to HEAD with headers only.
"""
var _http: stallion.HTTPServer = stallion.HTTPServer.none()
new create(
auth: lori.TCPServerAuth,
fd: U32,
config: stallion.ServerConfig)
=>
_http = stallion.HTTPServer(auth, fd, this, config)
fun ref _http_connection(): stallion.HTTPServer => _http
fun ref on_request_complete(
request': stallion.Request val,
responder: stallion.Responder)
=>
"""
Builds a response with Content-Length, adding the body only for non-HEAD
requests.
"""
let body: String val = "Hello, World!"
let response = stallion.ResponseBuilder(stallion.StatusOK)
.add_header("Content-Type", "text/plain")
.add_header("Content-Length", body.size().string())
.finish_headers()
if not (request'.method is stallion.HEAD) then
response.add_chunk(body)
end
responder.respond(response.build())