Repository navigation
Allow middleware to set headers on the request - #22
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Requestexposed 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 sameRequestinstance the controller gets, so the only missing piece was a way to write headers.This adds four methods to
Framework\Http\Request:set_header($name, $value)42→'42'), an array of scalars as multiple values, andnullremoves the header. Any other value throwsInvalidArgumentExceptionand leaves the header unchanged.with_headers(array)set_header()to each entry. Headers not named in the array are kept.remove_header($name)has_header($name)Names match the way
get_header()reads them, soX-Tenant,x-tenantandx_tenantare the same header.Security
Only the request's header bag changes. Server parameters aren't touched, so setting
X-Forwarded-FororCF-Connecting-IPdoes not changeip(), even when the connecting address is a trusted proxy. Tests cover this.Breaking for contract implementers
Framework\Contracts\Requestnow declares the four methods. A class that implements the contract directly, instead of extendingHttp\Request, must add them. In this repo onlyHttp\Requestimplements it. Callers and subclasses ofHttp\Requestare unaffected. This is noted in the3.3.4CHANGELOG entry.Other changes
$headersnow defaults to[], so a freshly constructedRequestaccepts headers.docs/routing-examples.md§13.openspec/specs/http-request-headers/spec.md, plus the archived change inopenspec/changes/archive/2026-11-16-add-request-header-mutation/.Testing
tests/Unit/Http/RequestHeaderTest.php(21 tests). It covers:nullremoving a headerwith_headers(), plusremove_header()andhas_header()new Request()ip()staying unchanged when forwarded-address headers are settests/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 onmain.🤖 Generated with Claude Code