Skip to content

[4.x] Send the whole PSR-7 body and throw PSR-18 network exceptions - #85

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-sendrequest-body-psr18-exceptions
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-sendrequest-body-psr18-exceptions

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

Two problems in Http::sendRequest(), the PSR-18 entry point. They are in one PR because both change the same few lines.

The body was read from the current stream position. $request->getBody()->getContents() returns what follows the stream pointer, so a body stream that had been written to (Diactoros leaves the pointer at the end) or already read was sent empty. empty($data) additionally treated the body "0" as "no body". The body is now read with (string) $request->getBody(), which rewinds first, and only an empty string counts as no data.

Transport failures did not implement the PSR-18 exception interfaces. Connection failures came out of sendRequest() as plain \RuntimeException, so code typed against PSR-18 could not catch them with NetworkExceptionInterface/ClientExceptionInterface. They are now wrapped in the new Joomla\Http\Exception\NetworkException, which implements NetworkExceptionInterface (with getRequest()), keeps message, code and the original as getPrevious(), and still extends \RuntimeException, so existing catch (\RuntimeException $e) blocks are unaffected. Exceptions that already implement ClientExceptionInterface (InvalidResponseCodeException, UnexpectedResponseException) pass through unchanged. The native API (get(), post(), …) is not changed.

Not done: passing the request's protocol version to the transport. Transports read protocolVersion from their own options, set at construction, and TransportInterface::request() has no parameter for it, so this needs an interface change (5.x). The docs now say that the transport's option decides.

Testing Instructions

Tests/HttpSendRequestTest.php (new, with a mocked transport, no server needed):

  • a freshly written body and a body that was already read are sent completely (both sent '' before);
  • the body "0" is sent (was dropped);
  • an empty body is passed as null;
  • a RuntimeException from the transport becomes a NetworkException with the request, message, code and previous exception;
  • an InvalidResponseCodeException is rethrown as is.

Documentation Changes Required

docs/classes/Http.md: new section "Sending a PSR-7 request" on the body handling, the protocol version and the exceptions.

🤖 Generated with Claude Code

sendRequest() read the body with getContents(), which starts at the current
position of the stream, so a body that had been written or read without
rewinding was sent empty. empty() also dropped the body "0".

Transport failures were thrown as plain RuntimeExceptions, which PSR-18
consumers cannot catch through the PSR interfaces. Wrap them in a new
NetworkException that implements NetworkExceptionInterface and still
extends RuntimeException.

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