| /- | |
| Copyright (c) 2025 Lean FRO, LLC. All rights reserved. | |
| Released under Apache 2.0 license as described in the file LICENSE. | |
| Authors: Sofia Rodrigues | |
| -/ | |
| module | |
| prelude | |
| public import Std.Http.Protocol.H1 | |
| public section | |
| /-! | |
| # Transport | |
| This module exposes a `Transport` type class that is used to represent different transport mechanisms | |
| that can be used with an HTTP connection. | |
| -/ | |
| namespace Std.Http | |
| open Std Async TCP | |
| set_option linter.all true | |
| /-- | |
| Generic HTTP interface that abstracts over different transport mechanisms. | |
| -/ | |
| class Transport (α : Type) where | |
| /-- | |
| Receive data from the client connection, up to the expected size. | |
| Returns None if the connection is closed or no data is available. | |
| -/ | |
| recv : α → UInt64 → Async (Option ByteArray) | |
| /-- | |
| Send all data through the client connection. | |
| -/ | |
| sendAll : α → Array ByteArray → Async Unit | |
| /-- | |
| Get a selector for receiving data asynchronously. | |
| -/ | |
| recvSelector : α → UInt64 → Selector (Option ByteArray) | |
| /-- | |
| Close the transport connection, cancelling any write still in flight. | |
| The default implementation is a no-op; override this for transports that require explicit teardown. | |
| -/ | |
| close : α → IO Unit := fun _ => pure () | |
| /-- | |
| Shut down the write side of the connection: the peer reads end-of-stream after everything already | |
| written, while this side can still read. Returns `false` when the transport cannot, which is what | |
| the default implementation does. | |
| -/ | |
| shutdown : α → Async Bool := fun _ => pure false | |
| instance : Transport Socket.Client where | |
| recv client expect := client.recv? expect | |
| sendAll client data := do | |
| let promise ← client.native.send data | |
| -- A finished send needs no await, and a pending one is mapped on the thread that completes it | |
| -- instead of in a task of its own. | |
| if ← IO.hasFinished promise.result? then | |
| match promise.result?.get with | |
| | some (.ok ()) => pure () | |
| | some (.error e) => throw e | |
| | none => throw (IO.userError "the promise linked to the Async was dropped") | |
| else | |
| Async.ofAsyncTask <| promise.result?.map (sync := true) fun | |
| | none => .error (IO.userError "the promise linked to the Async was dropped") | |
| | some res => res | |
| recvSelector client expect := client.recvSelector expect | |
| close client := client.close | |
| shutdown client := do | |
| client.shutdown | |
| return true | |
| namespace Internal | |
| open Async in | |
| /-- | |
| Shared state for a bidirectional mock connection. | |
| -/ | |
| private structure Mock.SharedState where | |
| /-- | |
| Client to server direction. | |
| -/ | |
| clientToServer : Std.CloseableChannel ByteArray | |
| /-- | |
| Server to client direction. | |
| -/ | |
| serverToClient : Std.CloseableChannel ByteArray | |
| /-- | |
| Mock client endpoint for testing. | |
| -/ | |
| structure Mock.Client where | |
| private shared : Mock.SharedState | |
| /-- | |
| Mock server endpoint for testing. | |
| -/ | |
| structure Mock.Server where | |
| private shared : Mock.SharedState | |
| namespace Mock | |
| /-- | |
| Creates a mock server and client that are connected to each other and share the | |
| same underlying state, enabling bidirectional communication. | |
| -/ | |
| def new : BaseIO (Mock.Client × Mock.Server) := do | |
| let first ← Std.CloseableChannel.new | |
| let second ← Std.CloseableChannel.new | |
| return (⟨⟨first, second⟩⟩, ⟨⟨first, second⟩⟩) | |
| /-- | |
| Receives data from a channel, joining all available data up to the expected size. First does a | |
| blocking recv, then greedily consumes available data with tryRecv until `expect` bytes are reached. | |
| -/ | |
| def recvJoined (recvChan : Std.CloseableChannel ByteArray) (expect : Option UInt64) : Async (Option ByteArray) := do | |
| match ← await (← recvChan.recv) with | |
| | none => return none | |
| | some first => | |
| let mut result := first | |
| repeat | |
| if let some expect := expect then | |
| if result.size.toUInt64 ≥ expect then break | |
| match ← recvChan.tryRecv with | |
| | none => break | |
| | some chunk => result := result ++ chunk | |
| return some result | |
| /-- | |
| Sends a single ByteArray through a channel. | |
| -/ | |
| def send (sendChan : Std.CloseableChannel ByteArray) (data : ByteArray) : Async Unit := do | |
| Async.ofAsyncTask ((← sendChan.send data) |>.map (Except.mapError (IO.userError ∘ toString))) | |
| /-- | |
| Sends ByteArrays through a channel. | |
| -/ | |
| def sendAll (sendChan : Std.CloseableChannel ByteArray) (data : Array ByteArray) : Async Unit := do | |
| for chunk in data do | |
| send sendChan chunk | |
| /-- | |
| Creates a selector for receiving from a channel. | |
| -/ | |
| def recvSelector (recvChan : Std.CloseableChannel ByteArray) : Selector (Option ByteArray) := | |
| recvChan.recvSelector | |
| end Mock | |
| namespace Mock.Client | |
| /-- | |
| Gets the receive channel for a client (server to client direction). | |
| -/ | |
| def getRecvChan (client : Mock.Client) : Std.CloseableChannel ByteArray := | |
| client.shared.serverToClient | |
| /-- | |
| Gets the send channel for a client (client to server direction). | |
| -/ | |
| def getSendChan (client : Mock.Client) : Std.CloseableChannel ByteArray := | |
| client.shared.clientToServer | |
| /-- | |
| Sends a single ByteArray. | |
| -/ | |
| def send (client : Mock.Client) (data : ByteArray) : Async Unit := | |
| Mock.send (getSendChan client) data | |
| /-- | |
| Receives data, joining all available chunks. | |
| -/ | |
| def recv? (client : Mock.Client) (expect : Option UInt64 := none) : Async (Option ByteArray) := | |
| Mock.recvJoined (getRecvChan client) expect | |
| /-- | |
| Tries to receive data without blocking, joining all immediately available chunks. | |
| Returns `none` if no data is available. | |
| -/ | |
| def tryRecv? (client : Mock.Client) (_expect : UInt64 := 0) : BaseIO (Option ByteArray) := do | |
| match ← (getRecvChan client).tryRecv with | |
| | none => return none | |
| | some first => | |
| let mut result := first | |
| repeat | |
| match ← (getRecvChan client).tryRecv with | |
| | none => break | |
| | some chunk => result := result ++ chunk | |
| return some result | |
| /-- | |
| Closes the mock server and client. | |
| -/ | |
| def close (client : Mock.Client) : IO Unit := do | |
| if !(← client.shared.clientToServer.isClosed) then client.shared.clientToServer.close | |
| if !(← client.shared.serverToClient.isClosed) then client.shared.serverToClient.close | |
| end Mock.Client | |
| namespace Mock.Server | |
| /-- | |
| Gets the receive channel for a server (client to server direction). | |
| -/ | |
| def getRecvChan (server : Mock.Server) : Std.CloseableChannel ByteArray := | |
| server.shared.clientToServer | |
| /-- | |
| Gets the send channel for a server (server to client direction). | |
| -/ | |
| def getSendChan (server : Mock.Server) : Std.CloseableChannel ByteArray := | |
| server.shared.serverToClient | |
| /-- | |
| Sends a single ByteArray. | |
| -/ | |
| def send (server : Mock.Server) (data : ByteArray) : Async Unit := | |
| Mock.send (getSendChan server) data | |
| /-- | |
| Receives data, joining all available chunks. | |
| -/ | |
| def recv? (server : Mock.Server) (expect : Option UInt64 := none) : Async (Option ByteArray) := | |
| Mock.recvJoined (getRecvChan server) expect | |
| /-- | |
| Tries to receive data without blocking, joining all immediately available chunks. Returns `none` if no | |
| data is available. | |
| -/ | |
| def tryRecv? (server : Mock.Server) (_expect : UInt64 := 0) : BaseIO (Option ByteArray) := do | |
| match ← (getRecvChan server).tryRecv with | |
| | none => return none | |
| | some first => | |
| let mut result := first | |
| repeat | |
| match ← (getRecvChan server).tryRecv with | |
| | none => break | |
| | some chunk => result := result ++ chunk | |
| return some result | |
| /-- | |
| Closes the mock server and client. | |
| -/ | |
| def close (server : Mock.Server) : IO Unit := do | |
| if !(← server.shared.clientToServer.isClosed) then server.shared.clientToServer.close | |
| if !(← server.shared.serverToClient.isClosed) then server.shared.serverToClient.close | |
| end Mock.Server | |
| instance : Transport Mock.Client where | |
| recv client expect := Mock.recvJoined (Mock.Client.getRecvChan client) (some expect) | |
| sendAll client data := Mock.sendAll (Mock.Client.getSendChan client) data | |
| recvSelector client _ := Mock.recvSelector (Mock.Client.getRecvChan client) | |
| close client := client.close | |
| instance : Transport Mock.Server where | |
| recv server expect := Mock.recvJoined (Mock.Server.getRecvChan server) (some expect) | |
| sendAll server data := Mock.sendAll (Mock.Server.getSendChan server) data | |
| recvSelector server _ := Mock.recvSelector (Mock.Server.getRecvChan server) | |
| close server := server.close | |
| end Internal | |
| end Std.Http |