Skip to content

[4.x] Look up request headers case-insensitively and accept list values - #86

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-header-normalisation
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-header-normalisation

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

Two problems with request headers. They are in one PR because both change the same lines of the cURL transport.

Case-sensitive header lookups. HTTP header names are case-insensitive, but the transports checked isset($headers['Content-Type']), isset($headers['Connection']) and isset($headers['Host']) on the exact spelling, and Http merged its headers option the same way. Passing content-type: application/json with a body added a second Content-Type: application/x-www-form-urlencoded; a lower-case host gave two Host headers in the socket transport; accept in a request did not override Accept from the options. (PHP 8.3's built-in web server even crashed with zend_mm_heap corrupted on the duplicate Content-Type while reproducing this.)

List values from sendRequest(). PSR-7 getHeaders() returns a list per header. The cURL transport put the raw value into CURLOPT_ENCODING ("Array to string conversion", encoding Array, so cURL no longer decompressed responses) and passed it to strpos() for the multipart check, a TypeError when the data is an array.

AbstractTransport gets two helpers, getHeaderValue() (case-insensitive lookup, list values joined with , ) and setHeader() (replaces a header in any case). The transports use them for Content-Type, Content-Length, Connection, Host and Accept-Encoding; Http::makeTransportRequest() compares the option header names case-insensitively.

Testing Instructions

Tests/Transport/HeaderNormalisationTest.php (new):

  • unit tests of the two helpers;
  • for each transport, a POST with content-type: application/json reaches the stub with that content type (before: overridden by the form-encoded one);
  • for each transport, Accept-Encoding as a list is sent without PHP warnings (before: "Array to string conversion" with cURL);
  • cURL with a list-valued multipart/form-data content type and array data (before: TypeError).

Tests/HttpOptionHeadersTest.php (new): accept in a request overrides Accept from the headers option.

jhttp_stub.php now also echoes the request headers (getallheaders()).

Documentation Changes Required

docs/overview.md, "Working with options": header names are compared case-insensitively and values may be lists.

🤖 Generated with Claude Code

The transports checked for Content-Type, Content-Length, Connection and
Host with isset() on the exact spelling, so a "content-type" passed by the
caller got a second, form-encoded Content-Type added, and a lower-case
"host" produced two Host headers in the socket transport. The headers
option of Http was merged the same way.

Header values from sendRequest() are lists. The cURL transport used the raw
value for CURLOPT_ENCODING ("Array to string conversion", encoding "Array")
and passed it to strpos() for the multipart check (TypeError).

Look up headers case-insensitively, replace Content-Length in any case, and
join list values before using them for cURL options.

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