def head.loop

bend-kit-websocket@0.4.0.0 · websocket.bend:803

import bend-kit-websocket@0.4.0.0/websocket.bend as Websocket
import Base
import bend-kit-bytes@0.3.3.0/bytes.bend as Bytes

803def head.loop(n: Nat, h: Head, +tls: Bool, +ms: U32) -> IO(Socket & Result<&1, &1, U32 & String, String & Bytes.Bytes>):
804  match n:
805    case 0n:
806      match h:
807        case HRead{r, acc}:
808          (sock, x) = r
809          do IO<Socket & Result<&1, &1, U32 & String, String & Bytes.Bytes>>:
810            return (sock, Fail{(1002, "HTTP head took too many reads")})
811        case HSplit{sock, s}:
812          do IO<Socket & Result<&1, &1, U32 & String, String & Bytes.Bytes>>:
813            return (sock, Fail{(1002, "HTTP head took too many reads")})
814    case 1n+p:
815      match h:
816        case HRead{r, acc}:
817          match r:
818            case (sock, Fail{e}):
819              do IO<Socket & Result<&1, &1, U32 & String, String & Bytes.Bytes>>:
820                return (sock, Fail{e})
821            case (sock, Done{(0, buf)}):
822              do IO<Socket & Result<&1, &1, U32 & String, String & Bytes.Bytes>>:
823                return (sock, Fail{(1006, "connection closed before the HTTP head ended")})
824            case (sock, Done{(+len, buf)}):
825              head.loop(p, HSplit{sock, split(Bytes.append(acc, Bytes.Bytes{len, buf}))}, tls, ms)
826        case HSplit{sock, s}:
827          match s:
828            case Found{head, rest}:
829              do IO<Socket & Result<&1, &1, U32 & String, String & Bytes.Bytes>>:
830                return (sock, Done{(head, rest)})
831            case Long{}:
832              do IO<Socket & Result<&1, &1, U32 & String, String & Bytes.Bytes>>:
833                return (sock, Fail{(1002, "HTTP head over 16 KiB")})
834            case Short{acc}:
835              do IO<Socket & Result<&1, &1, U32 & String, String & Bytes.Bytes>>:
836                r : Socket & Result<&1, &1, U32 & String, U32 & Array<U32>> <- io.recv(tls, sock, ms)
837                head.loop(p, HRead{r, acc}, tls, ms)

Uses (20)

Used by (1)