Merge pull request #594 from flightphp/maintain-headers

Maintains headers on redirect, error, and halt. Added jsonHalt
pull/597/head v3.10.0
n0nag0n 8 months ago committed by GitHub
commit c8a1c88899
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -57,6 +57,8 @@ use flight\net\Route;
* @method void redirect(string $url, int $code = 303) Redirects the current request to another URL. * @method void redirect(string $url, int $code = 303) Redirects the current request to another URL.
* @method void json(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0) * @method void json(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
* Sends a JSON response. * Sends a JSON response.
* @method void jsonHalt(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
* Sends a JSON response and immediately halts the request.
* @method void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0) * @method void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
* Sends a JSONP response. * Sends a JSONP response.
* *
@ -73,7 +75,7 @@ class Engine
*/ */
private const MAPPABLE_METHODS = [ private const MAPPABLE_METHODS = [
'start', 'stop', 'route', 'halt', 'error', 'notFound', 'start', 'stop', 'route', 'halt', 'error', 'notFound',
'render', 'redirect', 'etag', 'lastModified', 'json', 'jsonp', 'render', 'redirect', 'etag', 'lastModified', 'json', 'jsonHalt', 'jsonp',
'post', 'put', 'patch', 'delete', 'group', 'getUrl' 'post', 'put', 'patch', 'delete', 'group', 'getUrl'
]; ];
@ -608,7 +610,7 @@ class Engine
try { try {
$this->response() $this->response()
->clear() ->clearBody()
->status(500) ->status(500)
->write($msg) ->write($msg)
->send(); ->send();
@ -735,7 +737,7 @@ class Engine
public function _halt(int $code = 200, string $message = '', bool $actuallyExit = true): void public function _halt(int $code = 200, string $message = '', bool $actuallyExit = true): void
{ {
$this->response() $this->response()
->clear() ->clearBody()
->status($code) ->status($code)
->write($message) ->write($message)
->send(); ->send();
@ -750,7 +752,7 @@ class Engine
$output = '<h1>404 Not Found</h1><h3>The page you have requested could not be found.</h3>'; $output = '<h1>404 Not Found</h1><h3>The page you have requested could not be found.</h3>';
$this->response() $this->response()
->clear() ->clearBody()
->status(404) ->status(404)
->write($output) ->write($output)
->send(); ->send();
@ -775,7 +777,7 @@ class Engine
} }
$this->response() $this->response()
->clear() ->clearBody()
->status($code) ->status($code)
->header('Location', $url) ->header('Location', $url)
->send(); ->send();
@ -829,6 +831,33 @@ class Engine
} }
} }
/**
* Sends a JSON response and halts execution immediately.
*
* @param mixed $data JSON data
* @param int $code HTTP status code
* @param bool $encode Whether to perform JSON encoding
* @param string $charset Charset
* @param int $option Bitmask Json constant such as JSON_HEX_QUOT
*
* @throws Exception
*/
public function _jsonHalt(
$data,
int $code = 200,
bool $encode = true,
string $charset = 'utf-8',
int $option = 0
): void {
$this->json($data, $code, $encode, $charset, $option);
$jsonBody = $this->response()->getBody();
if ($this->response()->v2_output_buffering === false) {
$this->response()->clearBody();
$this->response()->send();
}
$this->halt($code, $jsonBody, empty(getenv('PHPUNIT_TEST')));
}
/** /**
* Sends a JSONP response. * Sends a JSONP response.
* *
@ -877,6 +906,7 @@ class Engine
isset($_SERVER['HTTP_IF_NONE_MATCH']) && isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
$_SERVER['HTTP_IF_NONE_MATCH'] === $id $_SERVER['HTTP_IF_NONE_MATCH'] === $id
) { ) {
$this->response()->clear();
$this->halt(304, '', empty(getenv('PHPUNIT_TEST'))); $this->halt(304, '', empty(getenv('PHPUNIT_TEST')));
} }
} }
@ -894,6 +924,7 @@ class Engine
isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $time strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $time
) { ) {
$this->response()->clear();
$this->halt(304, '', empty(getenv('PHPUNIT_TEST'))); $this->halt(304, '', empty(getenv('PHPUNIT_TEST')));
} }
} }

@ -68,6 +68,8 @@ require_once __DIR__ . '/autoload.php';
* @method static void redirect(string $url, int $code = 303) Redirects to another URL. * @method static void redirect(string $url, int $code = 303) Redirects to another URL.
* @method static void json(mixed $data, int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512) * @method static void json(mixed $data, int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
* Sends a JSON response. * Sends a JSON response.
* @method void jsonHalt(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
* Sends a JSON response and immediately halts the request.
* @method static void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512) * @method static void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
* Sends a JSONP response. * Sends a JSONP response.
* @method static void error(Throwable $exception) Sends an HTTP 500 response. * @method static void error(Throwable $exception) Sends an HTTP 500 response.

@ -370,6 +370,16 @@ class EngineTest extends TestCase
$this->assertEquals(200, $engine->response()->status()); $this->assertEquals(200, $engine->response()->status());
} }
public function testJsonHalt()
{
$engine = new Engine();
$this->expectOutputString('{"key1":"value1","key2":"value2"}');
$engine->jsonHalt(['key1' => 'value1', 'key2' => 'value2']);
$this->assertEquals('application/json; charset=utf-8', $engine->response()->headers()['Content-Type']);
$this->assertEquals(200, $engine->response()->status());
$this->assertEquals('{"key1":"value1","key2":"value2"}', $engine->response()->getBody());
}
public function testJsonP() public function testJsonP()
{ {
$engine = new Engine(); $engine = new Engine();

@ -631,6 +631,10 @@ class RouterTest extends TestCase
$this->router->rewind(); $this->router->rewind();
$result = $this->router->valid(); $result = $this->router->valid();
$this->assertTrue($result); $this->assertTrue($result);
$this->router->previous();
$result = $this->router->valid();
$this->assertFalse($result);
} }
public function testGetRootUrlByAlias() public function testGetRootUrlByAlias()

@ -116,6 +116,10 @@ Flight::route('/jsonp', function () {
echo "\n\n\n\n\n"; echo "\n\n\n\n\n";
}); });
Flight::route('/json-halt', function () {
Flight::jsonHalt(['message' => 'JSON rendered and halted successfully with no other body content!']);
});
// Test 10: Halt // Test 10: Halt
Flight::route('/halt', function () { Flight::route('/halt', function () {
Flight::halt(400, 'Halt worked successfully'); Flight::halt(400, 'Halt worked successfully');
@ -200,6 +204,7 @@ echo '
<li><a href="/error">Error</a></li> <li><a href="/error">Error</a></li>
<li><a href="/json">JSON</a></li> <li><a href="/json">JSON</a></li>
<li><a href="/jsonp?jsonp=myjson">JSONP</a></li> <li><a href="/jsonp?jsonp=myjson">JSONP</a></li>
<li><a href="/json-halt">JSON Halt</a></li>
<li><a href="/halt">Halt</a></li> <li><a href="/halt">Halt</a></li>
<li><a href="/redirect">Redirect</a></li> <li><a href="/redirect">Redirect</a></li>
</ul>'; </ul>';

@ -74,6 +74,7 @@ class LayoutMiddleware
<li><a href="/error">Error</a></li> <li><a href="/error">Error</a></li>
<li><a href="/json">JSON</a></li> <li><a href="/json">JSON</a></li>
<li><a href="/jsonp?jsonp=myjson">JSONP</a></li> <li><a href="/jsonp?jsonp=myjson">JSONP</a></li>
<li><a href="/json-halt">JSON Halt</a></li>
<li><a href="/halt">Halt</a></li> <li><a href="/halt">Halt</a></li>
<li><a href="/redirect">Redirect</a></li> <li><a href="/redirect">Redirect</a></li>
<li><a href="/streamResponse">Stream Plain</a></li> <li><a href="/streamResponse">Stream Plain</a></li>

@ -171,6 +171,10 @@ Flight::route('/jsonp', function () {
Flight::jsonp(['message' => 'JSONP renders successfully!'], 'jsonp'); Flight::jsonp(['message' => 'JSONP renders successfully!'], 'jsonp');
}); });
Flight::route('/json-halt', function () {
Flight::jsonHalt(['message' => 'JSON rendered and halted successfully with no other body content!']);
});
Flight::map('error', function (Throwable $e) { Flight::map('error', function (Throwable $e) {
echo sprintf( echo sprintf(
<<<HTML <<<HTML

Loading…
Cancel
Save