Skip to content

[4.x] Keep the response status code in sync with the Status header - #161

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-response-status-code
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-response-status-code

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

redirect() and respond() carry the HTTP status in a pseudo Status header instead of the PSR-7 status code. After redirect($url, 303), getResponse()->getStatusCode() is still 200, so PSR-7 consumers, emitters and middleware see the wrong status, and a later $app->setResponse($response->withStatus(404)) is ignored once a Status header exists. respond() also adds its Cache-Control header with $replace = false, so an application-set Cache-Control is duplicated instead of replaced (two Cache-Control headers after respond() runs twice).

This PR:

  • makes setHeader('Status', $code) (any case of the name) also call withStatus() on the response for a numeric code between 100 and 599, so the header and the PSR-7 status code agree. This covers redirect() and every existing setHeader('Status', …) call in applications,
  • makes respond() send the status line from getStatusCode(): if the Status header differs (which can now only happen when the status was changed directly on the PSR-7 response), the header is replaced,
  • sets Cache-Control with $replace = true for uncacheable responses, like Expires and Last-Modified in the same branch.

The finding also suggests deprecating the Status header as a way to set the status. That is not done here: the docs and many applications (including the CMS) use setHeader('Status', …), and with the synchronisation it no longer causes wrong status codes. Whether to deprecate it for 5.0 is open.

Behaviour change in 4.x: with caching disabled, a Cache-Control header set by the application is now replaced instead of being sent alongside the package's header. A status set on the PSR-7 response after a Status header now wins.

Testing Instructions

New tests in AbstractWebApplicationTest: testRespondUsesResponseStatusCode, testRespondReplacesCacheControlHeader, testSetHeaderStatusSetsResponseStatusCode, and testRedirectWithMoved now also asserts that the response status code is 301. All four fail without the change in src/; the existing redirect tests (which check the exact header order) still pass.

Documentation Changes Required

docs/web-application.md: the Status header paragraph and the note under "Caching", which said that Cache-Control was appended to. docs/api-reference.md: setHeader().

The "Caching" note is a few lines below the Cache-Control block that #154 updates; they should merge cleanly, but whichever is merged second should be checked.

🤖 Generated with Claude Code

redirect() and respond() carry the HTTP status in a pseudo "Status"
header, while the PSR-7 response kept status code 200. After
redirect($url, 303) getResponse()->getStatusCode() still returned 200,
and a status set later with withStatus() was ignored once a Status header
existed. respond() also appended its Cache-Control header to one set by
the application instead of replacing it.

setHeader('Status', ...) now also sets the status code of the response,
respond() sends the status line from the response status code, and the
Cache-Control header for uncacheable responses replaces an existing one,
like Expires and Last-Modified already do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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