@@ -11,7 +11,7 @@ open Http.Data.Headers
1111open Http.Data
1212open 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