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); }