-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRequestResponse.pony
73 lines (57 loc) · 2.08 KB
/
RequestResponse.pony
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
use "collections"
use "frames"
type Status is U16
class box Request
let headers: List[(String, String)] val
let body: Array[U8] val
new val create(headers': List[(String, String)] val, body': Array[U8] val) =>
headers = headers'
body = body'
fun header(name: String): (String | None) =>
for hf in headers.values() do
if hf._1.eq(name) then return hf._2 end
end
class box Response
var headers: List[(String, Stringable)] val = recover List[(String, Stringable)] end
var body: Array[U8] val = recover val Array[U8] end
let _stream: U32
new create(stream: U32) =>
_stream = stream
fun box apply(status: Status): Array[Frame] val =>
let ext_headers = recover val
List[(String, Stringable)]
.> push((":status", status))
.> push(("content-length", body.size()))
.> append(headers)
end
let headerFrame = FrameBuilder.header_list(ext_headers, true, _stream)
let dataFrame = FrameBuilder.data(body, _stream)
recover val [as Frame: headerFrame; dataFrame] end
interface val RequestHandler
fun on_request(req: Request val, res: Response trn): (Response val, Status)
/*
interface Middleware
fun on_request(req: Request iso, res: Response iso): (Request iso^, Response iso^)
actor SomewhereOverTheRainbow
let _writer: FrameWriter
let _stream: U32
let _handler: RequestHandler iso
let _middleware: Seq[Middleware] val
new create(handler: RequestHandler iso, middleware: Seq[Middleware] val, writer: FrameWriter tag, stream: U32) =>
_writer = writer
_stream = stream
_handler = consume handler
_middleware = middleware
be handle(req': Request iso) =>
var req: Request iso = consume req'
var res: Response iso = recover Response end
for mw in _middleware.values() do
(let treq: Request iso, let tres: Response iso) = mw.on_request(consume req, consume res)
req = consume treq
res = consume tres
end
_send(_handler.on_request(consume req, consume res))
fun _send(res: Response iso) =>
// TODO build frames for headers and data and enqueue those
None
*/