Skip to content

phoenix-wasm: add Conn.to_raw_response/1 for HTTP response serialization#5

Open
yevbar wants to merge 1 commit intomasterfrom
phoenix-wasm-conn-to-raw-response
Open

phoenix-wasm: add Conn.to_raw_response/1 for HTTP response serialization#5
yevbar wants to merge 1 commit intomasterfrom
phoenix-wasm-conn-to-raw-response

Conversation

@yevbar
Copy link
Copy Markdown
Contributor

@yevbar yevbar commented Feb 28, 2026

By Sneezy

Summary

Adds Conn.to_raw_response/1 — the response-side counterpart of to_raw_request/1 — completing the full HTTP request/response lifecycle for the WASM pipeline:

Raw HTTP request → Conn (parse) → middleware/routing → Conn → Raw HTTP response

Changes

Conn.to_raw_response/1

Converts a Conn's response state (status, resp_headers, resp_body) into a complete HTTP/1.1 response string:

conn = Conn.new("GET", "/api/users")
|> Conn.json(200, %{users: []})

Conn.to_raw_response(conn)
# => "HTTP/1.1 200 OK\r\ncontent-type: application/json; charset=utf-8\r\ncontent-length: 12\r\n\r\n{\"users\":[]}"
  • Auto-adds Content-Length header when not already set
  • Uses IO lists internally for zero intermediate string allocations (~1.5µs/op)
  • Handles all response types: JSON, HTML, text, redirects, error pages

Conn.status_reason/1

Maps HTTP status codes to standard reason phrases:

Conn.status_reason(200)  # => "OK"
Conn.status_reason(404)  # => "Not Found"
Conn.status_reason(302)  # => "Found"

Covers all common HTTP/1.1 status codes (1xx through 5xx).

Tests

23 new tests covering:

  • All common status codes (200, 201, 204, 301, 302, 303, 404, 500)
  • Content types (JSON, HTML, text/plain)
  • Redirect responses with location headers
  • Content-Length auto-generation and override behavior
  • Edge cases (nil status, nil body, unknown status codes)
  • Round-trip parsing (response → parse headers/body → verify)
  • Full request+response lifecycle integration
  • Performance benchmark (~1.5µs/op)

Add the response-side counterpart of to_raw_request/1, completing
the full HTTP request/response lifecycle for the WASM pipeline:

  Raw HTTP request → Conn → middleware/routing → Conn → Raw HTTP response

- Conn.to_raw_response/1: converts a Conn's response state (status,
  resp_headers, resp_body) into a complete HTTP/1.1 response string.
  Auto-adds Content-Length header. Uses IO lists for zero intermediate
  string allocations (~1.5µs/op).

- Conn.status_reason/1: maps HTTP status codes to standard reason
  phrases (200 → OK, 404 → Not Found, etc.).

Includes 23 tests covering all status codes, content types (JSON,
HTML, text), redirects, edge cases, round-trip parsing, and a
performance benchmark.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant