Skip to content

Commit 66682d3

Browse files
committed
feat: add connection data to the connection
1 parent 0c289d9 commit 66682d3

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

Http/IO/Server.lean

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ open Http.Data.Headers
1515
open Http.Classes
1616
open Http.Data
1717

18-
def simpleStatusResponse (status: Status) (conn: Connection) := do
18+
def simpleStatusResponse (status: Status) (conn: Connection α) := do
1919
conn.response.modify λres => res
2020
|>.withHeaderStd .connection ConnectionHeader.close
2121
|>.withStatus status
2222
conn.end
2323

24-
def handleError (conn: Connection) : ParsingError → IO Unit
24+
def handleError (conn: Connection α) : ParsingError → IO Unit
2525
| .invalidMessage _ => simpleStatusResponse .badRequest conn
2626
| .uriTooLong => simpleStatusResponse .uriTooLong conn
2727
| .bodyTooLong => simpleStatusResponse .payloadTooLarge conn
@@ -31,7 +31,7 @@ def handleError (conn: Connection) : ParsingError → IO Unit
3131
def keepAlive (config: Config) : KeepAlive :=
3232
KeepAlive.new (some $ config.idleTimeout / 1000) config.maxKeepAliveRequests
3333

34-
def onRequest (config: Config) (connRef: IO.Ref Connection) (onReq: Connection → IO Unit) (request: Request) : IO Bool := do
34+
def onRequest (config: Config) (connRef: IO.Ref (Connection α)) (onReq: Connection α → IO Unit) (request: Request) : IO Bool := do
3535
let conn ← connRef.get
3636
conn.requests.modify (· + 1)
3737

@@ -64,17 +64,18 @@ def onRequest (config: Config) (connRef: IO.Ref Connection) (onReq: Connection
6464

6565
return ¬isClosing
6666

67-
def closeConnectionTimeout (conn: IO.Ref Connection) : IO Unit := do
67+
def closeConnectionTimeout (conn: IO.Ref (Connection α)) : IO Unit := do
6868
let connection ← conn.get
6969
connection.close
7070

7171
def readSocket
7272
(loop: UV.Loop)
7373
(config: Config)
7474
(socket: UV.TCP)
75-
(onReq: Connection → IO Unit)
76-
(onData: Connection → Chunk → IO Unit)
77-
(onTrailer: Connection → Trailers → IO Unit)
75+
(onReq: Connection α → IO Unit)
76+
(onData: Connection α → Chunk → IO Unit)
77+
(onTrailer: Connection α → Trailers → IO Unit)
78+
(data : α)
7879
: UV.IO Unit
7980
:= do
8081
let timer ← loop.mkTimer
@@ -84,10 +85,10 @@ def readSocket
8485
socket.read_stop
8586
socket.stop
8687

87-
let conn ← Connection.new socket (λ_ => UV.IO.run $ onEnd ())
88+
let conn ← Connection.new socket (λ_ => UV.IO.run $ onEnd ()) data
8889
let connRef ← IO.toUVIO (IO.mkRef conn)
8990

90-
let readRef : {x y: Type} → (Connection → y → IO x) → y → IO x := λfunc y => do
91+
let readRef : {x y: Type} → (Connection α → y → IO x) → y → IO x := λfunc y => do
9192
let conn ← connRef.get
9293
func conn y
9394

@@ -119,12 +120,14 @@ def readSocket
119120
IO.toUVIO $ handleError conn err
120121

121122
def server
123+
[Inhabited α]
122124
(host: String)
123125
(port: UInt16)
124126
(config: Config := Inhabited.default)
125-
(onReq: Connection → IO Unit := (λ_ => pure ()))
126-
(onData: Connection → Chunk → IO Unit := (λ_ _ => pure ()))
127-
(onEnd: Connection → Trailers → IO Unit := (λ_ _ => pure ()))
127+
(onReq: Connection α → IO Unit := (λ_ => pure ()))
128+
(onData: Connection α → Chunk → IO Unit := (λ_ _ => pure ()))
129+
(onEnd: Connection α → Trailers → IO Unit := (λ_ _ => pure ()))
130+
(data : α := Inhabited.default)
128131
: IO Unit := do
129132
let go : UV.IO Unit := do
130133
let loop ← UV.mkLoop
@@ -134,6 +137,6 @@ def server
134137
server.listen 128 do
135138
let client ← loop.mkTCP
136139
server.accept client
137-
readSocket loop config client onReq onData onEnd
140+
readSocket loop config client onReq onData onEnd data
138141
let _ ← loop.run
139142
UV.IO.run go

Http/IO/Server/Connection.lean

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ open Http.Data.Headers
1111
open Http.Data
1212
open Http.Classes
1313

14-
structure Connection where
14+
structure Connection (α: Type) where
1515
isClosing: IO.Ref Bool
1616
sentHeaders: IO.Ref Bool
1717
isChunked: IO.Ref Bool
@@ -26,7 +26,9 @@ structure Connection where
2626
socket: UV.TCP
2727
onEnd: Unit → IO Unit
2828

29-
def Connection.new (socket: UV.TCP) (onEnd: Unit → IO Unit) : UV.IO Connection := do
29+
data: IO.Ref α
30+
31+
def Connection.new (socket: UV.TCP) (onEnd: Unit → IO Unit) (data: α) : UV.IO (Connection α) := do
3032
return { isClosing := ← IO.mkRef false
3133
, isHead := ← IO.mkRef false
3234
, requests := ← IO.mkRef 0
@@ -37,33 +39,34 @@ def Connection.new (socket: UV.TCP) (onEnd: Unit → IO Unit) : UV.IO Connection
3739
, request := Data.Request.empty
3840
, buffer := ← IO.mkRef #[ByteArray.empty]
3941
, response := ← IO.mkRef Data.Response.empty
42+
, data := ← IO.mkRef data
4043
, socket
4144
, onEnd }
4245

43-
def Connection.guard (connection: Connection) (func: IO Unit) : IO Unit := do
46+
def Connection.guard (connection: Connection α) (func: IO Unit) : IO Unit := do
4447
let isClosing ← connection.isClosing.get
4548
if ¬isClosing then func
4649

47-
def Connection.withHeader (connection: Connection) (name: String) (value: String) : IO Unit := connection.guard do
50+
def Connection.withHeader (connection: Connection α) (name: String) (value: String) : IO Unit := connection.guard do
4851
let sentHeaders ← connection.sentHeaders.get
4952
if sentHeaders then
5053
throw (IO.userError (toString "cannot write headers after they were sent"))
5154
connection.response.modify (·.withHeader name value)
5255

53-
def Connection.setStatus (connection: Connection) (status: Status) : IO Unit := connection.guard do
56+
def Connection.setStatus (connection: Connection α) (status: Status) : IO Unit := connection.guard do
5457
connection.response.modify ({· with status := status})
5558

56-
def Connection.withHeaderStd (connection: Connection) [Canonical .text α] (name: Headers.HeaderName.Standard) (value: α) [Headers.Header name α] : IO Unit := connection.guard do
59+
def Connection.withHeaderStd (connection: Connection β) [Canonical .text α] (name: Headers.HeaderName.Standard) (value: α) [Headers.Header name α] : IO Unit := connection.guard do
5760
let sentHeaders ← connection.sentHeaders.get
5861
if sentHeaders then
5962
throw (IO.userError (toString "cannot write headers after they were sent"))
6063
connection.response.modify (·.withHeaderStd name value)
6164

62-
def Connection.close (connection: Connection) : IO Unit := connection.guard do
65+
def Connection.close (connection: Connection α) : IO Unit := connection.guard do
6366
connection.isClosing.set true
6467
connection.onEnd ()
6568

66-
def Connection.writeByteArray (connection: Connection) (data: ByteArray) : IO Unit := connection.guard do
69+
def Connection.writeByteArray (connection: Connection α) (data: ByteArray) : IO Unit := connection.guard do
6770
let isChunked ← connection.isChunked.get
6871
let data :=
6972
if isChunked
@@ -72,19 +75,19 @@ def Connection.writeByteArray (connection: Connection) (data: ByteArray) : IO Un
7275
connection.size.modify (· + data.size)
7376
connection.buffer.modify (λx => x.push data)
7477

75-
def Connection.write (connection: Connection) (data: String) : IO Unit := connection.guard do
78+
def Connection.write (connection: Connection α) (data: String) : IO Unit := connection.guard do
7679
connection.writeByteArray (String.toUTF8 data)
7780

78-
def Connection.rawWrite (connection: Connection) (buffer: Array ByteArray) : IO Unit := connection.guard do
81+
def Connection.rawWrite (connection: Connection α) (buffer: Array ByteArray) : IO Unit := connection.guard do
7982
if buffer.size > 0 then
8083
UV.IO.run do let _ ← connection.socket.write buffer (λ_ => pure ())
8184

82-
def Connection.sendLastChunk (connection: Connection) : IO Unit := connection.guard do
85+
def Connection.sendLastChunk (connection: Connection α) : IO Unit := connection.guard do
8386
let isChunked ← connection.isChunked.get
8487
if isChunked then
8588
connection.rawWrite #[Canonical.binary Chunk.zeroed]
8689

87-
def Connection.sendHeaders (connection: Connection) : IO Unit := connection.guard do
90+
def Connection.sendHeaders (connection: Connection α) : IO Unit := connection.guard do
8891
let sent ← connection.sentHeaders.get
8992

9093
if ¬sent then
@@ -102,12 +105,12 @@ def Connection.sendHeaders (connection: Connection) : IO Unit := connection.guar
102105
connection.rawWrite #[String.toUTF8 $ Canonical.text response]
103106
connection.sentHeaders.set true
104107

105-
def Connection.flush (connection: Connection) : IO Unit := connection.guard do
108+
def Connection.flush (connection: Connection α) : IO Unit := connection.guard do
106109
connection.sendHeaders
107110
let buffer ← connection.buffer.swap #[]
108111
connection.rawWrite buffer
109112

110-
def Connection.end (connection: Connection) : IO Unit := connection.guard do
113+
def Connection.end (connection: Connection α) : IO Unit := connection.guard do
111114
connection.flush
112115
connection.sendLastChunk
113116

0 commit comments

Comments
 (0)