123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657letsrc=Logs.Src.create"http-miou-unix"moduleLog=(valLogs.src_logsrc:Logs.LOG)externalreraise:exn->'a="%reraise"(* NOTE(dinosaure): [Unix.read_bigarray] and [Unix.single_write_bigarray] exist
since OCaml 5.02 and let us read into (and write from) the connection's
bigstring without any intermediate [bytes]/[string]: the kernel writes
exactly where [h1]/[h2] parse from, and [Faraday]'s iovecs go to [write(2)]
as they are (no slicing either).
[Miou_unix] does not expose these, but it does expose {!Miou_unix.blocking_read}
and {!Miou_unix.blocking_write}, which is all we need to reimplement its
[EAGAIN] loop here. This assumes the file descriptor is in non-blocking
mode, which is what {!Miou_unix.accept}, {!Miou_unix.tcpv4} and
{!Miou_unix.tcpv6} always give us. *)moduleTCP=structtypet=Miou_unix.file_descrletreadfdbstr~off~len=matchMiou_unix.read_bigstringfd~off~lenbstrwith|exceptionUnix.(Unix_error(ECONNRESET,_,_))->0|len->lenletwritevfdbstrs=letfn{Faraday.buffer;off;len}=matchMiou_unix.write_bigstringfdbuffer~off~lenwith|exceptionUnix.(Unix_error(EPIPE,_,_))->reraiseRuntime.Flow.Closed_by_peer|()->()inList.iterfnbstrsletclose=Miou_unix.closeletshutdownflowcmd=trymatchcmdwith|`read->Unix.shutdown(Miou_unix.to_file_descrflow)Unix.SHUTDOWN_RECEIVE|`write->Unix.shutdown(Miou_unix.to_file_descrflow)Unix.SHUTDOWN_SEND|`read_write->Unix.close(Miou_unix.to_file_descrflow)withUnix.Unix_error(Unix.ENOTCONN,_,_)->()[@@ocamlformat"disable"]end(* NOTE(dinosaure): [ocaml-tls] decrypts into a [string], so a TLS flow cannot
avoid one copy into the connection's bigstring. {!Runtime.Flow.Of_bytes}
owns the scratch buffer that copy needs - it used to be allocated by the
[Runtime] for every flow, TLS or not. *)moduleTLS=Runtime.Flow.Of_bytes(structincludeTls_miou_unixletwritefd?off?lenstr=trywritefd?off?lenstrwith|Tls_miou_unix.Closed_by_peer->reraiseRuntime.Flow.Closed_by_peer|exn->reraiseexnend)