Files
el/examples/http-status-envelope.el
T
Will Anderson 742bd0b4f9 fix: three foundation/el root-cause bugs (no more bandaids)
1. Parser+codegen: bare reassignment `x = expr` inside an if-body
   was compiling to three orphan expressions with no store. Now
   emits a real assignment.

2. Runtime json_get: dot-path segments that are all digits now
   correctly traverse array indices. `json_get(s, "0.field")` works.

3. Runtime HTTP writer: response bodies starting with
   `{"__status__":<int>,...}` now set the HTTP status header to
   that value and strip the marker from the served body. Existing
   404/401/503 paths in product code now produce real status codes
   instead of HTTP 200 with the status hidden in the body.

Self-host fixed point holds: gen2 == gen3 byte-identical.
Snapshot tagged at dist/platform/elc.20260502-1231-self-host.

Backlog: bl-c121edda
2026-05-02 12:32:23 -05:00

34 lines
1.2 KiB
EmacsLisp

// http-status-envelope.el acceptance test for the __status__ HTTP envelope.
//
// Before fix: a handler returning {"__status__":401,"error":"unauthorized"}
// went out as an HTTP 200 with the JSON body verbatim, so Cloud Run logs were
// full of 200s for what should have been 4xx/5xx.
//
// After fix: when the response body's FIRST key is __status__, the runtime
// reads the integer value as the HTTP status code and strips the marker from
// the body before sending it to the client.
//
// Verify with curl:
// curl -i http://localhost:8081/auth -> HTTP/1.1 401 Unauthorized
// curl -i http://localhost:8081/health -> HTTP/1.1 200 OK
// curl -i http://localhost:8081/oops -> HTTP/1.1 503 Service Unavailable
fn handle(method: String, path: String, body: String) -> String {
if path == "/auth" {
return "{\"__status__\":401,\"error\":\"unauthorized\"}"
}
if path == "/oops" {
return "{\"__status__\":503,\"error\":\"degraded\"}"
}
if path == "/health" {
return "{\"ok\":true}"
}
return "{\"__status__\":404,\"error\":\"not found\"}"
}
fn main() -> Int {
http_set_handler("handle")
http_serve(8081, "handle")
return 0
}