Skip to content

Allow middleware to set headers on the request - #22

Merged
ahamed merged 1 commit into
mainfrom
feature/request-header-mutation
Nov 16, 2026
Merged

ahamed merged 1 commit into
mainfrom
feature/request-header-mutation

Conversation

@ahamed

@ahamed ahamed commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

Request exposed headers for reading only (get_header, header, get_headers), so middleware had no way to attach a header such as a tenant or trace id for the controller to read. Middleware already receives the same Request instance the controller gets, so the only missing piece was a way to write headers.

This adds four methods to Framework\Http\Request:

Method Behaviour
set_header($name, $value) Replaces any existing values. Scalars are stored as strings (42 → '42'), an array of scalars as multiple values, and null removes the header. Any other value throws InvalidArgumentException and leaves the header unchanged.
with_headers(array) Applies set_header() to each entry. Headers not named in the array are kept.
remove_header($name) Removes the header. Does nothing if it's absent.
has_header($name) Reports whether the header is present.

Names match the way get_header() reads them, so X-Tenant, x-tenant and x_tenant are the same header.

class TenantMiddleware implements Middleware
{
    public function handle(RequestContract $request, callable $next)
    {
        $request->set_header('X-Tenant', resolve_tenant_slug());

        return $next($request);
    }
}

Security

Only the request's header bag changes. Server parameters aren't touched, so setting X-Forwarded-For or CF-Connecting-IP does not change ip(), even when the connecting address is a trusted proxy. Tests cover this.

Breaking for contract implementers

Framework\Contracts\Request now declares the four methods. A class that implements the contract directly, instead of extending Http\Request, must add them. In this repo only Http\Request implements it. Callers and subclasses of Http\Request are unaffected. This is noted in the 3.3.4 CHANGELOG entry.

Other changes

  • $headers now defaults to [], so a freshly constructed Request accepts headers.
  • Docs: a new "Setting request headers from middleware" subsection in docs/routing-examples.md §13.
  • OpenSpec: a new openspec/specs/http-request-headers/spec.md, plus the archived change in openspec/changes/archive/2026-11-16-add-request-header-mutation/.

Testing

  • New tests/Unit/Http/RequestHeaderTest.php (21 tests). It covers:
    • matching names regardless of case or separator
    • replacing, casting and array values
    • null removing a header
    • rejecting invalid values without changing the header
    • merging with with_headers(), plus remove_header() and has_header()
    • requests built from REST input, raw HTTP input and new Request()
    • ip() staying unchanged when forwarded-address headers are set
  • tests/Unit/RouteMiddlewareTest.php: a header set by middleware reaches both a REST controller and a closure route.
  • composer test:unit: 1003 tests pass.
  • composer phpcs: no errors. Its existing line-length warnings in other files are unchanged and also appear on main.

🤖 Generated with Claude Code

Request exposed headers for reading only, so middleware had no way to
attach a tenant, trace id or similar header for the controller to read.

Request gains set_header(), with_headers(), remove_header() and
has_header(). Names are normalised the same way get_header() reads them.
Scalars are stored as strings, arrays of scalars as multiple values, null
removes a header, and any other value throws InvalidArgumentException
without touching the existing header. with_headers() merges and keeps
headers it does not name.

Only the header bag changes: server parameters and ip() are untouched, so
setting X-Forwarded-For or CF-Connecting-IP cannot spoof the client
address. $headers now defaults to [] so a fresh Request works.

Framework\Contracts\Request declares the four methods. A class that
implements the contract directly, rather than extending Http\Request,
must add them.

Includes the http-request-headers spec, the archived OpenSpec change,
docs in routing-examples.md section 13 and a 3.3.4 CHANGELOG entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ahamed
ahamed merged commit d806493 into main Nov 16, 2026
1 check passed
@ahamed
ahamed deleted the feature/request-header-mutation branch November 16, 2026 12:17
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