// Streamed responses over loopback sockets (native only: the interpreter // has no sockets). They run when LOUISE_NET_TESTS is set, and are skipped // otherwise: // // LOUISE_NET_TESTS=1 louise test --native // // The servers are raw TCP: they write a response in steps the test // controls, so a test can see a piece arrive before the rest is sent. import fetch. { type BodyStream , type Options , type Piece , AnyAddress , Data , End , Malformed , Options , PublicOnly , Refused , Streamed , TimedOut } import sse.{Event} import std/bytes import std/ffi.{type Resource} import std/net.{type TcpListener, type TcpStream} import std/option import std/os import std/process.{type Subject, type proc} import std/result import std/string import std/test.{assert, assert_equal} import std/time.{type clock} import std/tls.{type TlsConfig} import url.{type Url} fn enabled :: -> Bool / { option.is_some (os.env "LOUISE_NET_TESTS") } fn local :: deadline_ms: Int -> Options / { // Plain http only: the TLS configuration is never used. Options( ..fetch.options (tls_defaults ()) , policy: AnyAddress , deadline: fetch.deadline_in deadline_ms ) } fn tls_defaults :: -> Resource / { result.expect (tls.config (tls.defaults ())) "a TLS configuration" } fn at :: port: Int -> Url { result.expect (url.parse "http://127.0.0.1:{port}/events") "url" } fn now_ms :: -> Int / { time.in_milliseconds (time.monotonic ()) } // ---- a server in steps --------------------------------------------------------- /// What the server does, in order: write some text, wait for the test's /// go-ahead, or read until the client closes and report it. type Step { Write(String), AwaitGo, ReportClose } /// A server for one connection that reads the request head, then runs /// `steps` and closes. Its port, and where to send go-aheads. fn stepped_server :: steps: List closed: Subject -> (Int, Subject>) / { let listener = result.expect (net.listen "127.0.0.1" 0) "listen" let port = net.port listener // The server's go-ahead subject must be its own: it sends it back. let handoff = process.subject () let _ = process.spawn fn { let go = process.subject () process.send handoff go serve listener steps go closed } (port, handoff) } fn serve :: listener: Resource steps: List go: Subject<()> closed: Subject -> () / { match net.accept listener { Ok(conn) -> { let _ = head_from conn (bytes.empty ()) run conn steps go closed net.close conn } , Error(_) -> () } } fn run :: conn: Resource steps: List go: Subject<()> closed: Subject -> () / { match steps { [] -> () , [Write(text), ..rest] -> { let _ = net.write conn (bytes.from_string text) run conn rest go closed } , [AwaitGo, ..rest] -> { let _ = process.receive go 10000 run conn rest go closed } , [ReportClose, ..rest] -> { process.send closed (until_closed conn) run conn rest go closed } } } /// Reads until the peer closes (True), or gives up after a while (False). fn until_closed :: conn: Resource -> Bool / { match net.read conn 4096 { Ok(more) if bytes.length more > 0 -> until_closed conn , Ok(_) -> True , Error(_) -> True } } fn head_from :: conn: Resource have: Bytes -> Bytes / { if string.contains (bytes.to_string_lossy have) "\r\n\r\n" { have } else { match net.read conn 4096 { Ok(more) if bytes.length more > 0 -> head_from conn (bytes.concat have more) , _ -> have } } } fn go_ahead :: handoff: Subject> -> () / { match process.receive handoff 5000 { Some(go) -> process.send go () , None -> panic "the server never sent its go-ahead subject" } } fn start :: port: Int opts: Options -> BodyStream / { match fetch.request_streaming "GET" (at port) [] (bytes.empty ()) opts { Ok(Streamed(200, _, stream)) -> stream , Ok(Streamed(status, _, _)) -> panic "status {status}" , Error(e) -> panic (show e) } } /// Every piece until the end: the text and whether it was cut. fn rest_of :: stream: BodyStream text: String -> (String, Bool) / { match fetch.next stream { Ok(Data(b)) -> rest_of stream (text <> bytes.to_string_lossy b) , Ok(End(cut)) -> (text, cut) , Error(e) -> panic (show e) } } // ---- tests --------------------------------------------------------------------- @test fn a_chunked_body_arrives_before_it_is_complete :: -> () / { if enabled () { let closed = process.subject () let (port, handoff) = stepped_server [ Write( "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nTransfer-Encoding: chunked\r\n\r\n" <> "15\r\nevent: delta\ndata: Bo\r\n" ) , AwaitGo , Write("5\r\nn\n\nda\r\n") , Write("a\r\nta: jour\n\n\r\n0\r\n\r\n") ] closed let stream = start port (local 5000) // The server waits for the go-ahead: this piece came on its own. assert_equal (fetch.next stream) Ok(Data(bytes.from_string "event: delta\ndata: Bo")) go_ahead handoff let (text, cut) = rest_of stream "" assert (!cut) "not cut" let all = "event: delta\ndata: Bo" <> text assert_equal (sse.feed (sse.parser ()) (bytes.from_string all)).1 [Event("delta", "Bon", ""), Event("message", "jour", "")] } } @test fn bodies_by_length_and_until_close_are_capped :: -> () / { if enabled () { let closed = process.subject () let (port, _) = stepped_server [Write("HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\n0123456789")] closed let opts = Options(..local 5000, max_body: 4) assert_equal (rest_of (start port opts) "") ("0123", True) let (port, _) = stepped_server [Write("HTTP/1.1 200 OK\r\n\r\nabc"), Write("def")] closed assert_equal (rest_of (start port (local 5000)) "") ("abcdef", False) let (port, _) = stepped_server [Write("HTTP/1.1 200 OK\r\n\r\nabcdef")] closed assert_equal (rest_of (start port opts) "") ("abcd", True) } } @test fn the_deadline_stops_a_stalled_body :: -> () / { if enabled () { let closed = process.subject () let (port, _) = stepped_server [Write("HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nabc"), AwaitGo] closed let started = now_ms () let stream = start port (local 300) assert_equal (fetch.next stream) Ok(Data(bytes.from_string "abc")) assert_equal (fetch.next stream) Error(TimedOut) assert (now_ms () - started < 3000) "stopped at the deadline" } } @test fn cancel_closes_the_connection :: -> () / { if enabled () { let closed = process.subject () let (port, _) = stepped_server [ Write( "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nabc\r\n" ) , ReportClose ] closed let stream = start port (local 5000) assert_equal (fetch.next stream) Ok(Data(bytes.from_string "abc")) fetch.cancel stream assert_equal (process.receive closed 3000) Some(True) // Harmless twice. fetch.cancel stream } } @test fn failures_before_the_head_are_errors :: -> () / { if enabled () { let closed = process.subject () let opts = Options(..local 5000, policy: PublicOnly) match fetch.request_streaming "GET" (at 9) [] (bytes.empty ()) opts { Error(Refused(_, ip)) -> assert_equal ip "127.0.0.1" , _ -> panic "a private address was allowed" } let (port, _) = stepped_server [Write("SPDY/9 nope\r\n\r\n")] closed match fetch.request_streaming "GET" (at port) [] (bytes.empty ()) (local 5000) { Error(Malformed(_)) -> (), _ -> panic "a malformed head was accepted" } let (port, _) = stepped_server [Write("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\nzz\r\n")] closed let stream = start port (local 5000) match fetch.next stream { Error(Malformed(_)) -> () , other -> panic "a bad chunk was accepted: {show other}" } } }