Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Tests/Transport/StreamRedirectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* @copyright Copyright (C) 2005 - 2026 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Http\Tests\Transport;

use Joomla\Http\Transport\Stream;
use Joomla\Uri\Uri;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

/**
* Test class for redirects followed by Joomla\Http\Transport\Stream.
*/
#[CoversClass(Stream::class)]
class StreamRedirectTest extends TestCase
{
/**
* The URL of the directory holding the HTTP stubs.
*
* @var string
*/
private $stubDirectory;

/**
* Sets up the fixture.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();

if (!Stream::isSupported()) {
$this->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'));
}
}
31 changes: 31 additions & 0 deletions Tests/stubs/redirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* @copyright Copyright (C) 2005 - 2026 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

/**
* Redirect stub for the transport tests. It redirects, with a relative Location unless `to` is
* given, until jhttp_stub.php is reached.
*
* Query parameters:
* hops Number of redirects before jhttp_stub.php is reached (default 1).
* code The redirect status code (default 302).
* to Base URL (ending in a slash) to redirect to, e.g. the stub directory on another host.
* loop When set, redirect to this script again, forever.
*/

$hops = (int) ($_GET['hops'] ?? 1);
$code = (int) ($_GET['code'] ?? 302);
$base = (string) ($_GET['to'] ?? '');

if (isset($_GET['loop'])) {
$location = 'redirect.php?' . http_build_query(['loop' => 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);
2 changes: 1 addition & 1 deletion docs/classes/TransportInterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
12 changes: 11 additions & 1 deletion src/AbstractTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];
}
Expand Down
9 changes: 9 additions & 0 deletions src/Transport/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Loading