From dbe1601223b45cdbf3cc8d4c0e4976982273bb1b Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Thu, 12 Nov 2026 23:50:34 +0100 Subject: [PATCH] Return the final response when the stream transport follows redirects With follow_location enabled, which is the default, PHP's http wrapper lists the headers of every response in the redirect chain. The transport took the status code of the first response and turned each further status line into a header without a name, which Diactoros rejects, so every redirect ended in an InvalidArgumentException. Use only the headers of the last response and skip lines that are not headers. Co-Authored-By: Claude Opus 5.5 --- Tests/Transport/StreamRedirectTest.php | 69 ++++++++++++++++++++++++++ Tests/stubs/redirect.php | 31 ++++++++++++ docs/classes/TransportInterface.md | 2 +- src/AbstractTransport.php | 12 ++++- src/Transport/Stream.php | 9 ++++ 5 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 Tests/Transport/StreamRedirectTest.php create mode 100644 Tests/stubs/redirect.php diff --git a/Tests/Transport/StreamRedirectTest.php b/Tests/Transport/StreamRedirectTest.php new file mode 100644 index 00000000..35512305 --- /dev/null +++ b/Tests/Transport/StreamRedirectTest.php @@ -0,0 +1,69 @@ +markTestSkipped('The stream transport is not supported in this environment.'); + } + + if (!\defined('JTEST_HTTP_STUB') && getenv('JTEST_HTTP_STUB') == '') { + $this->markTestSkipped('The Transport test stub has not been configured'); + } + + $stubUrl = \defined('JTEST_HTTP_STUB') ? JTEST_HTTP_STUB : getenv('JTEST_HTTP_STUB'); + $this->stubDirectory = substr($stubUrl, 0, strrpos($stubUrl, '/') + 1); + } + + public function testRedirectsAreFollowedAndTheLastResponseIsReturned(): void + { + $transport = new Stream([]); + + $response = $transport->request('GET', new Uri($this->stubDirectory . 'redirect.php?hops=2')); + + $this->assertSame(200, $response->getStatusCode()); + $this->assertFalse($response->hasHeader('Location'), 'The headers of the redirect responses must not be merged in.'); + $this->assertSame('GET', json_decode((string) $response->getBody())->method); + } + + public function testRedirectIsReturnedWhenFollowingIsDisabled(): void + { + $transport = new Stream(['follow_location' => false]); + + $response = $transport->request('GET', new Uri($this->stubDirectory . 'redirect.php')); + + $this->assertSame(302, $response->getStatusCode()); + $this->assertSame('jhttp_stub.php', $response->getHeaderLine('Location')); + } +} diff --git a/Tests/stubs/redirect.php b/Tests/stubs/redirect.php new file mode 100644 index 00000000..50e42847 --- /dev/null +++ b/Tests/stubs/redirect.php @@ -0,0 +1,31 @@ + 1, 'code' => $code]); +} elseif ($hops > 1) { + $location = 'redirect.php?' . http_build_query(['hops' => $hops - 1, 'code' => $code, 'to' => $base]); +} else { + $location = 'jhttp_stub.php'; +} + +header('Location: ' . $base . $location, true, $code); diff --git a/docs/classes/TransportInterface.md b/docs/classes/TransportInterface.md index ffb364a5..eb4caa79 100644 --- a/docs/classes/TransportInterface.md +++ b/docs/classes/TransportInterface.md @@ -105,7 +105,7 @@ with `getOption($key, $default)` and are transport-specific; the commonly used o | Option | Used by | Meaning | |---|---|---| | `userauth` / `passwordauth` | Curl | HTTP Basic credentials | -| `follow_location` | Curl | Follow redirects, default `true` | +| `follow_location` | Curl, Stream | Follow redirects, default `true`; the response at the end of the chain is returned | | `transport.curl` | Curl | Raw `CURLOPT_*` overrides, applied last | | `stream.certpath` | Stream | CA bundle file or directory | | `protocolVersion` | Curl | `1.0`, `1.1` or `2.0` | diff --git a/src/AbstractTransport.php b/src/AbstractTransport.php index 2dcde7ac..55a0df16 100644 --- a/src/AbstractTransport.php +++ b/src/AbstractTransport.php @@ -77,9 +77,19 @@ protected function processHeaders(array $headers): array // Add the response headers to the response object. foreach ($headers as $header) { - $pos = strpos($header, ':'); + $pos = strpos($header, ':'); + + // Skip lines that are not a header, such as a further status line. + if ($pos === false) { + continue; + } + $keyName = trim(substr($header, 0, $pos)); + if ($keyName === '') { + continue; + } + if (!isset($verifiedHeaders[$keyName])) { $verifiedHeaders[$keyName] = []; } diff --git a/src/Transport/Stream.php b/src/Transport/Stream.php index ef468ba0..ffc1227c 100644 --- a/src/Transport/Stream.php +++ b/src/Transport/Stream.php @@ -215,6 +215,15 @@ public function request( $headers = $metadata['wrapper_data']; } + // After followed redirects the headers of every response are listed, so keep only those of the last one. + for ($i = \count($headers) - 1; $i > 0; $i--) { + if (preg_match('#^HTTP/\d#i', $headers[$i])) { + $headers = \array_slice($headers, $i); + + break; + } + } + return $this->getResponse($headers, $content); }