muro_plug

An Elixir Plug and Phoenix adapter that serves HTTP/1.1 from the muro-http book. The book's step.muro, emitted with muro's default Elixir backend, runs on Thousand Island and decides every protocol fact of a connection. Any Plug, or a Phoenix.Endpoint, runs on it instead of Bandit or Cowboy.

The package will live under doctorcorral until launch, then move to murolang with the rest.

Status

Version 0.1.0. HTTP/1.1 over TCP and over TLS (HTTPS), and WebSockets (RFC 6455, through WebSock) over both: ws:// and wss://, including Phoenix channels and LiveView sockets, with optional RFC 7692 permessage-deflate. Prepared for Hex; this commit does not publish the package. No HTTP/2: over TLS, ALPN advertises http/1.1 only.

Usage

Add the dependency:

def deps do
  [
    {:muro_plug, "~> 0.1.0"}
  ]
end

A Plug in your supervision tree:

children = [
  {MuroPlug, plug: MyApp.Router, port: 4000}
]

HTTPS:

children = [
  {MuroPlug,
   plug: MyApp.Router,
   scheme: :https,
   port: 4040,
   certfile: "/etc/my_app/cert.pem",
   keyfile: "/etc/my_app/key.pem"}
]

Phoenix. In config/config.exs (or config/dev.exs), set the endpoint adapter. Phoenix 1.8 generates adapter: Bandit.PhoenixAdapter; replace that line. http:, https:, or both: each is its own server under the endpoint, child id {endpoint, scheme}, and Endpoint.server_info/1 answers for either scheme. Then mix deps.get and mix phx.server.

config :my_app, MyAppWeb.Endpoint,
  adapter: MuroPlug.PhoenixAdapter,
  http: [ip: {127, 0, 0, 1}, port: 4000],
  https: [
    ip: {127, 0, 0, 1},
    port: 4040,
    certfile: "priv/cert/selfsigned.pem",
    keyfile: "priv/cert/selfsigned_key.pem"
  ]

Options (MuroPlug.child_spec/1, and the endpoint's :http and :https lists): :plug, :scheme (:http default, or :https), :port (default 4000 for :http, 4040 for :https), :ip, :max_body (the book's max, default 8_000_000), :idle_timeout (default 60_000 ms), and :thousand_island_options.

With scheme: :https the server runs on ThousandIsland.Transports.SSL, and these TLS options go through Plug.SSL.configure/1 (the checks Plug.Cowboy and Bandit run) to :ssl: :keyfile, :certfile, :cacertfile, :dhfile, :key, :cert, :cacerts, :certs_keys, :sni_hosts, :sni_fun, :password, :otp_app (relative paths resolve against its app dir; a Phoenix endpoint passes its own), :cipher_suite (:strong or :compatible), :ciphers, :versions, :eccs, :verify, :fail_if_no_peer_cert, and :alpn_preferred_protocols (only ["http/1.1"], the default). TLS options inside thousand_island_options: [transport_options: ...] are used too, and top-level ones override them. :ssl is started before the listener. A bad :scheme, a missing key or certificate, a missing file, other ALPN protocols, or TLS options with scheme: :http (top-level or in :transport_options) raise ArgumentError.

The handler and adapter are the same on both transports: Thousand Island finishes the handshake, and every read and write is ThousandIsland.Socket. conn.scheme is :https, get_ssl_data/1 is the :ssl connection information, and get_peer_data/1 carries a client certificate (DER) when one was sent. send_file over TLS is Thousand Island's SSL sendfile/4: :file.pread of at most 1 MiB at a time, written with :ssl.send (TCP uses the kernel's :file.sendfile).

WebSockets

A Plug upgrades with WebSockAdapter.upgrade/4 (what Phoenix.Endpoint sockets and LiveView do) or directly with Plug.Conn.upgrade_adapter(conn, :websocket, {MyWebSock, state, opts}):

defmodule MyApp.Router do
  use Plug.Router
  plug :match
  plug :dispatch

  get "/ws" do
    WebSockAdapter.upgrade(conn, MyApp.EchoSocket, %{}, timeout: 60_000)
  end
end

muro_plug depends on websock only; websock_adapter (a Phoenix dependency already) is what applications call.

How it fits together

socket event ──fact──▶ MuroPlug.Step.stepFact (emitted step.muro)
                              │ Out
                              ▼
MuroPlug.Handler: read more │ owe 400/413/414/417/431/501 and close
                  │ write 100 Continue │ serve the request │ close
                              │
                              ▼ Plug.Conn on MuroPlug.Adapter
          send_resp / send_file / send_chunked ──▶ Step.hand ──▶ span + keep
                              │
                              ▼ wrote fact
        book closes, idles, or classifies the pipelined tail

upgrade_adapter(:websocket) ──▶ Adapter.upgrade/3 checks the handshake
                              │ Plug returns
                              ▼
        101 Switching Protocols ──▶ MuroPlug.WebSocket.Connection
        (frames + WebSock callbacks on the same ThousandIsland.Socket)

Octets stay binaries

muro-http #24 requires that Bytes and Slice lower to BEAM binaries and that no HTTP body or buffer is a list of octets. test/binary_test.exs checks this on the emitted module and on the request and response path:

Tests

mix test runs the book's own equations against the emitted step, the binary checks, HTTP/1.1 over real TCP sockets (GET, Content-Length and chunked bodies, one-byte packets, keep-alive, pipelining, 100 Continue, 400/413/414/417/431/501, streamed request bodies, chunked and close-delimited responses, send_file, HEAD, 204, Connection: close, HTTP/1.0, inform/3, a raising plug), a Plug.Router app through :httpc, and a Phoenix.Endpoint with a router and controller on MuroPlug.PhoenixAdapter.

test/tls_test.exs runs the same paths over real TLS sockets: a test CA and a localhost certificate are made with openssl in a temp dir when the suite starts and removed after it (no key is committed). It covers conn.scheme == :https, TLS and peer data, ALPN, Content-Length, chunked and streamed bodies, keep-alive, pipelining, 100 Continue, the book's 400/413/414/417/431/501, send_file (with a 2.5 MiB file past the SSL transport's 1 MiB chunk, an offset, past EOF, HEAD, and a closed socket), send_chunked, inform/3, client certificates, DER :cert/:key, option errors, a Plug.Router app through :httpc over HTTPS, and Phoenix endpoints with https: alone and http: + https: together, with server_info/2 for each. openssl must be on the PATH for mix test.

test/websocket_test.exs runs every WebSocket test over real TCP (ws://) and real TLS (wss://) sockets with a raw frame client: the handshake and Sec-WebSocket-Accept (and RFC 6455's example), response headers on the 101, rejected upgrades (400/426), text and binary echo, 16- and 64-bit lengths, fragmentation with pings between fragments, 2000 one-byte fragments (exact reassembly, bounded state), one-byte packets, a first frame pipelined with the upgrade request, an upgrade with an unread streamed or fully-buffered body, option validation (including a max_heap_size map), RFC 7692 permessage-deflate negotiation and compressed echo (zip bombs, corrupt deflate, RSV1 misuse), a client that never answers the server's close (close_timeout), each 1002/1007/1009 failure above, handle_info/2 pushes, handle_control/2, terminate/2 on client close, server stop, timeout, a dropped socket and server shutdown, close codes and reasons, a raising callback, and a Phoenix.Endpoint with both http: and https: serving a Phoenix.Socket.Transport and a Phoenix.Socket channel (join, reply, a 70 KB broadcast, heartbeat) over ws:// and wss://, including with compress: true.

Performance

BENCH.md records two 2026-11-30 oha sittings on this tree (Elixir 1.20.4, OTP 29, oha 1.17.0, governor powersave). The same hello-world Plug runs on muro_plug, Bandit, and Cowboy.

The second sitting inlines the emitted word-sized predicates at compile time. lib/muro_plug/step.ex is still the mix muro.gen output (body sha256 30fccbe9885cf662357fecda7a915474f2fd74e22eb4ef6e068fba3aba4cbc1d). Phoenix keep-alive GET at 64 connections, load about 5–6 in both sittings: muro_plug 25,144 req/s (was 19,006), Bandit 39,099 (was 37,892), Cowboy 30,820 (was 29,043). Bandit is 1.55×. Plug mode at 64 connections in the second sitting: 24,413, 42,777, and 32,445 req/s. The first Plug sitting was a busier box; BENCH.md has both.

A dripped 6,692-byte header block costs muro_plug 199,595 reductions (was 402,862) against 18,429 for Bandit, the same number in both sittings. One GET inside the book fell from about 4,200 reductions to about 2,200. Bandit still decodes the request with :erlang.decode_packet/3, and a partial header is still a byte fold in the generated step. An earlier wrk figure (2026-11-28) was a different tool and is a different measurement.

License

MIT, the same license as muro-http.