Skip to content

Commit 48d1b80

Browse files
committed
fix: specified code may not used in redirect
1 parent 52bc1a1 commit 48d1b80

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

system/HTTP/ResponseTrait.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -503,28 +503,25 @@ public function sendBody()
503503
*/
504504
public function redirect(string $uri, string $method = 'auto', ?int $code = null)
505505
{
506-
// Assume 302 status code response; override if needed
507-
if (empty($code)) {
508-
$code = 302;
509-
}
510-
511506
// IIS environment likely? Use 'refresh' for better compatibility
512507
if (
513508
$method === 'auto'
514509
&& isset($_SERVER['SERVER_SOFTWARE'])
515510
&& strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false
516511
) {
517512
$method = 'refresh';
518-
}
519-
520-
// override status code for HTTP/1.1 & higher
521-
// reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
522-
if (
523-
isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD'])
524-
&& $this->getProtocolVersion() >= 1.1
525-
&& $method !== 'refresh'
526-
) {
527-
$code = ($_SERVER['REQUEST_METHOD'] !== 'GET') ? 303 : ($code === 302 ? 307 : $code);
513+
} elseif ($method !== 'refresh' && $code === null) {
514+
// override status code for HTTP/1.1 & higher
515+
if (
516+
isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD'])
517+
&& $this->getProtocolVersion() >= 1.1
518+
) {
519+
$code = ($_SERVER['REQUEST_METHOD'] !== 'GET')
520+
? 303 // reference: https://en.wikipedia.org/wiki/Post/Redirect/Get
521+
: 307;
522+
} else {
523+
$code = 302;
524+
}
528525
}
529526

530527
switch ($method) {

tests/system/CodeIgniterTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ public function testRunRedirectionWithGET()
457457

458458
// Inject mock router.
459459
$routes = Services::routes();
460+
// addRedirect() sets status code 302 by default.
460461
$routes->addRedirect('example', 'pages/notset');
461462

462463
$router = Services::router($routes, Services::incomingrequest());
@@ -468,7 +469,7 @@ public function testRunRedirectionWithGET()
468469

469470
$response = $this->getPrivateProperty($this->codeigniter, 'response');
470471
$this->assertSame('http://example.com/pages/notset', $response->header('Location')->getValue());
471-
$this->assertSame(307, $response->getStatusCode());
472+
$this->assertSame(302, $response->getStatusCode());
472473
}
473474

474475
public function testRunRedirectionWithGETAndHTTPCode301()
@@ -516,7 +517,7 @@ public function testRunRedirectionWithPOSTAndHTTPCode301()
516517
ob_get_clean();
517518

518519
$response = $this->getPrivateProperty($this->codeigniter, 'response');
519-
$this->assertSame(303, $response->getStatusCode());
520+
$this->assertSame(301, $response->getStatusCode());
520521
}
521522

522523
public function testStoresPreviousURL()

tests/system/HTTP/ResponseTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ public function provideForRedirect()
296296
yield from [
297297
['Apache/2.4.17', 'HTTP/1.1', 'GET', null, 307],
298298
['Apache/2.4.17', 'HTTP/1.1', 'GET', 307, 307],
299-
['Apache/2.4.17', 'HTTP/1.1', 'GET', 302, 307],
299+
['Apache/2.4.17', 'HTTP/1.1', 'GET', 302, 302],
300300
['Apache/2.4.17', 'HTTP/1.1', 'POST', null, 303],
301-
['Apache/2.4.17', 'HTTP/1.1', 'POST', 307, 303],
302-
['Apache/2.4.17', 'HTTP/1.1', 'POST', 302, 303],
301+
['Apache/2.4.17', 'HTTP/1.1', 'POST', 307, 307],
302+
['Apache/2.4.17', 'HTTP/1.1', 'POST', 302, 302],
303303
];
304304
}
305305

0 commit comments

Comments
 (0)