Skip to content

Commit d79bacd

Browse files
committed
fix: change status code from 307 to 302 by default for GET request
1 parent f5bbe1a commit d79bacd

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

system/HTTP/ResponseTrait.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,14 @@ public function redirect(string $uri, string $method = 'auto', ?int $code = null
516516
isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD'])
517517
&& $this->getProtocolVersion() >= 1.1
518518
) {
519-
$code = (in_array($_SERVER['REQUEST_METHOD'], ['POST', 'PUT', 'DELETE'], true))
520-
? 303 // reference: https://en.wikipedia.org/wiki/Post/Redirect/Get
521-
: 307;
519+
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
520+
$code = 302;
521+
} elseif (in_array($_SERVER['REQUEST_METHOD'], ['POST', 'PUT', 'DELETE'], true)) {
522+
// reference: https://en.wikipedia.org/wiki/Post/Redirect/Get
523+
$code = 303;
524+
} else {
525+
$code = 307;
526+
}
522527
}
523528
}
524529

tests/system/HTTP/ResponseTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public function testRedirect(
294294
public function provideForRedirect()
295295
{
296296
yield from [
297-
['Apache/2.4.17', 'HTTP/1.1', 'GET', null, 307],
297+
['Apache/2.4.17', 'HTTP/1.1', 'GET', null, 302],
298298
['Apache/2.4.17', 'HTTP/1.1', 'GET', 307, 307],
299299
['Apache/2.4.17', 'HTTP/1.1', 'GET', 302, 302],
300300
['Apache/2.4.17', 'HTTP/1.1', 'POST', null, 303],
@@ -519,7 +519,7 @@ public function testMisbehaving()
519519
$response->getStatusCode();
520520
}
521521

522-
public function testTemporaryRedirect11()
522+
public function testTemporaryRedirectHTTP11()
523523
{
524524
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
525525
$_SERVER['REQUEST_METHOD'] = 'POST';
@@ -531,7 +531,7 @@ public function testTemporaryRedirect11()
531531
$this->assertSame(303, $response->getStatusCode());
532532
}
533533

534-
public function testTemporaryRedirectGet11()
534+
public function testTemporaryRedirectGetHTTP11()
535535
{
536536
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
537537
$_SERVER['REQUEST_METHOD'] = 'GET';
@@ -540,7 +540,7 @@ public function testTemporaryRedirectGet11()
540540
$response->setProtocolVersion('HTTP/1.1');
541541
$response->redirect('/foo');
542542

543-
$this->assertSame(307, $response->getStatusCode());
543+
$this->assertSame(302, $response->getStatusCode());
544544
}
545545

546546
// Make sure cookies are set by RedirectResponse this way

0 commit comments

Comments
 (0)