Skip to content

Commit

Permalink
Fix redirect for 307 and 308 http status
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas LEGER authored and ElGigi committed Jan 15, 2024
1 parent 835d7e0 commit bc951a4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Berlioz\Http\Client\Adapter\AdapterInterface;
use Berlioz\Http\Client\Adapter\CurlAdapter;
use Berlioz\Http\Client\Adapter\StreamAdapter;
use Berlioz\Http\Client\Components;
use Berlioz\Http\Client\Cookies\CookiesManager;
use Berlioz\Http\Client\Exception\HttpClientException;
use Berlioz\Http\Client\Exception\HttpException;
Expand Down Expand Up @@ -266,8 +265,8 @@ public function sendRequest(RequestInterface $request, Options|array $options =
Response::HTTP_STATUS_MOVED_PERMANENTLY,
Response::HTTP_STATUS_MOVED_TEMPORARILY,
Response::HTTP_STATUS_SEE_OTHER,
307,
308
Response::HTTP_STATUS_TEMPORARY_REDIRECT,
Response::HTTP_STATUS_PERMANENT_REDIRECT,
]
)) {
continue;
Expand Down Expand Up @@ -300,9 +299,23 @@ public function sendRequest(RequestInterface $request, Options|array $options =
}
}

$redirectMethod = Request::HTTP_METHOD_GET;
$redirectBody = null;
// For 307 and 308 redirect cases, method and body not changed.
if (in_array(
$response->getStatusCode(),
[
Response::HTTP_STATUS_TEMPORARY_REDIRECT,
Response::HTTP_STATUS_PERMANENT_REDIRECT,
]
)) {
$redirectMethod = $request->getMethod();
$redirectBody = $request->getBody();
}

// Create request for redirection
$request = $this->prepareRequest(
new Request(Request::HTTP_METHOD_GET, Uri::create($redirectUri, $request->getUri())),
new Request($redirectMethod, Uri::create($redirectUri, $request->getUri()), $redirectBody),
$cookies,
Options::make([
'headers' => [
Expand Down
13 changes: 13 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ public function testGet_redirection()
$this->assertEquals('GET', $bodyExploded[0]);
}

public function testPost_307Redirection()
{
$uri = new Uri('http', 'localhost', 8080, '/request.php?redirect=1&response_code=307');
$client = new Client();
$response = $client->post($uri, http_build_query(['body' => 'test']));

$this->assertEquals(200, $response->getStatusCode());

$bodyExploded = preg_split('/\r?\n/', (string)$response->getBody());
$this->assertEquals('POST', $bodyExploded[0]);
$this->assertEquals('test', end($bodyExploded));
}

public function testGet_encodedHttpReason()
{
$uri = new Uri('http', 'localhost', 8080, '/request.php?test=encoded_http_reason');
Expand Down
3 changes: 2 additions & 1 deletion tests/server/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
print PHP_EOL;
print file_get_contents('php://stdin');
print_r(getallheaders());
array_map(fn($postValue) => print $postValue, $_POST);
$contents = ob_get_clean();

setcookie('test', 'value');
Expand All @@ -23,7 +24,7 @@
header(
'Location: /request.php?encoding=' . ($_GET['encoding'] ?? null) . '&redirect=' . ($redirect - 1),
true,
301
$_GET['response_code'] ?? 301
);
exit;
}
Expand Down

0 comments on commit bc951a4

Please sign in to comment.