Documentation

Std.Http.Protocol.H1

HTTP/1.1 Protocol State Machine #

This module implements the core HTTP/1.1 protocol state machine that handles parsing requests/responses and generating output. The machine is direction-aware, supporting both server mode (receiving requests) and client mode (receiving responses).

Results from a single step of the state machine.

  • events : Array (Event dir)

    Events that occurred during this step (e.g., headers received, control flow, errors).

  • Output data ready to be sent to the socket.

Instances For

    A single body chunk produced by a pull-driven read.

    • final : Bool

      Whether this chunk finishes the current body stream.

    • incomplete : Bool

      Whether the chunk data is partial for the current frame/body slice (more bytes are still required to complete it).

    • chunk : Chunk

      The pulled body chunk. Chunked framing is removed; other transfer codings, such as the gzip of Transfer-Encoding: gzip, are not decoded. A server rejects a request with a coding that is not in Config.acceptedTransferCodings.

    Instances For

      The HTTP/1.1 protocol state machine.

      • reader : Reader dir

        The state of the reader.

      • writer : Writer dir

        The state of the writer.

      • config : Config

        The configuration.

      • events : Array (Event dir)

        Events that happened during reading and writing.

      • error : Option Error

        Error thrown by the machine.

      • The Date field value a server writes in each final response whose head has none, including the error responses the machine writes itself (RFC 9110 §6.6.1). The caller keeps it current; with none, no Date is added.

      • keepAlive : Bool

        If the connection will be kept alive after the message.

      • pullBodyStalled : Bool

        Set when a previous pullBody call could not make progress in .readBody. Cleared on new input or state reset.

      Instances For
        @[inline]

        Returns true if the machine failed. error holds the cause.

        Equations
        Instances For
          @[inline]

          Returns true if the reader has completed successfully.

          Equations
          Instances For
            @[inline]

            Returns true if the reader is closed.

            Equations
            Instances For
              @[inline]

              Returns true when the reader is paused waiting for a canContinue decision (server-side only; always false on the client side).

              Equations
              Instances For
                @[inline]

                Returns true if the machine should flush buffered output.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  @[inline]

                  Returns true if the writer is waiting for headers of a new message.

                  Equations
                  Instances For
                    @[inline]

                    Returns true if both reader and writer are closed and no output remains.

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      @[inline]

                      Returns true when pullBody can attempt to produce body data immediately.

                      Equations
                      Instances For
                        @[inline]

                        Returns true when pullBody is currently expected to make progress.

                        Equations
                        Instances For
                          @[inline]
                          def Std.Http.Protocol.H1.Machine.feed {dir : Direction} (machine : Machine dir) (data : ByteArray) :

                          Feeds input bytes into the reader side of the machine.

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            @[inline]

                            Stops taking input without changing the reader state: input already buffered is still parsed, then the reader ends as at EOF. Used internally for flow control (e.g., after rejecting Expect: 100-continue). To also transition the reader to .closed, follow this with setReaderState .closed.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              @[inline]

                              Closes the writer side, preventing any further message output.

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For
                                @[inline]

                                Signals that the user is not sending data anymore.

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For
                                  @[inline]

                                  Signals that the peer closed its side of the connection, so no more input will arrive.

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For
                                    @[inline]

                                    Set a known size for the message body, replacing any previous value.

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      @[inline]
                                      def Std.Http.Protocol.H1.Machine.suppressOutgoingBody {dir : Direction} (machine : Machine dir) (forceZero : Bool := false) :

                                      Suppresses writing body bytes for the current outgoing message while keeping header generation active. Used for responses that must not carry payload bytes.

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For
                                        @[inline]
                                        def Std.Http.Protocol.H1.Machine.send {dir : Direction} (machine : Machine dir) (message : Message.Head dir.swap) :

                                        Send the head of a message to the machine.

                                        Must only be called when machine.isWaitingMessage is true. If called in any other state (e.g. called twice), the machine panics.

                                        Informational (1xx) responses are written directly to the output buffer, or dropped when the request is HTTP/1.0; the writer stays in waitingHeaders so the application can send additional interim responses or the final response. The reader state is not affected — use canContinue to resolve an Expect: 100-continue decision separately.

                                        A Content-Length in the head gives the length of a body set as chunked, whose length is unknown. A head whose Content-Length or Transfer-Encoding otherwise contradicts the length set with setKnownSize, or describes no length, is not written: a server answers 500 Internal Server Error and closes the connection, and a client ends the exchange. Both emit .failed with an .other error, set error and close the reader, so the machine halts. A response without content (to HEAD, or a 204, 205 or 304) cannot contradict it, and a HEAD response advertises the Content-Length of its head.

                                        A message HTTP/1.0 cannot frame (see unframeableForHttp10) is rejected the same way. A 2xx to CONNECT is written without content or framing fields, and the connection then closes.

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For
                                          @[inline]
                                          def Std.Http.Protocol.H1.Machine.sendData {dir : Direction} (machine : Machine dir) (data : Array Chunk) :

                                          Enqueues body chunks into the writer buffer for encoding and sending.

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For
                                            def Std.Http.Protocol.H1.Machine.canContinue {dir : Direction} (machine : Machine dir) (status : Status) :

                                            Resolves an Expect: 100-continue decision.

                                            When status is 100 Continue, sends the interim response and advances to body reading. For any other status, sends the rejection response (typically 417) with a short text/plain explanation, unless a final response was already sent, disables keep-alive, and closes the reader — the body will not be received.

                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            • machine_3.canContinue status = machine_3
                                            • machine_3.canContinue status = machine_3
                                            Instances For

                                              Sends an error response with Connection: close and shuts down input.

                                              If the machine is waiting to send a message, sends status with a Connection: close header and a short text/plain explanation (letting the step function encode and flush the response), then stops accepting further input. Otherwise, closes the writer directly, ends keep-alive and stops accepting further input.

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                @[inline]

                                                Takes and clears all accumulated events, returning the drained array.

                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                Instances For
                                                  @[inline]

                                                  Takes and clears accumulated output bytes, returning them as a buffer.

                                                  Equations
                                                  • One or more equations did not get rendered due to their size.
                                                  Instances For
                                                    partial def Std.Http.Protocol.H1.Machine.processWrite {dir : Direction} (machine : Machine dir) :

                                                    Advances writer-side state machine by one logical transition.

                                                    Depending on writer state, this may:

                                                    • request an application answer,
                                                    • serialize headers,
                                                    • flush fixed/chunked body bytes,
                                                    • finalize/close after completion.
                                                    partial def Std.Http.Protocol.H1.Machine.processRead {dir : Direction} (machine : Machine dir) :

                                                    Advances reader-side state machine by one logical transition.

                                                    Dispatches parsing work according to current reader state (start-line, headers, body, completion, or failure handling).

                                                    Execute one step of the state machine.

                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For
                                                      @[inline]

                                                      Pulls at most one body chunk from the reader.

                                                      It advances body parsing until it either:

                                                      • produces a chunk (some PulledChunk), or
                                                      • cannot continue for now (none).
                                                      Equations
                                                      • One or more equations did not get rendered due to their size.
                                                      Instances For