diff --git a/resources/lib/UnityHTTPD.php b/resources/lib/UnityHTTPD.php index 7a43f87ed..9bfaf5be0 100644 --- a/resources/lib/UnityHTTPD.php +++ b/resources/lib/UnityHTTPD.php @@ -18,15 +18,15 @@ enum UnityHTTPDMessageLevel: string class UnityHTTPD { - public static function die(?string $x = null): never + public static function die(?string $msg = null, int $return_code = 0): never { - if ($x !== null) { - echo $x; + if ($msg !== null) { + echo $msg; } if (CONFIG["site"]["allow_die"]) { - die(); + exit($return_code); } else { - throw new NoDieException(); + throw new NoDieException(strval($return_code)); } } @@ -44,7 +44,7 @@ public static function redirect(?string $dest = null): never echo "If you're reading this message, then your browser has failed to redirect you " . "to the proper destination. click here to continue."; } - self::die(); + self::die(return_code: 0); } /* @@ -64,6 +64,7 @@ public static function gracefulDie( string $user_message_body, ?\Throwable $error = null, int $http_response_code = 200, + int $return_code = 0, mixed $data = null, ): never { $errorid = uniqid(); @@ -81,7 +82,7 @@ public static function gracefulDie( } self::errorLog($log_title, $log_message, data: $data, error: $error, errorid: $errorid); if (ini_get("html_errors") !== "1") { - self::die("$user_message_title -- $user_message_body"); + self::die("$user_message_title -- $user_message_body", $return_code); } elseif (($_SERVER["REQUEST_METHOD"] ?? "") == "POST") { self::messageError($user_message_title, $user_message_body); self::redirect(); @@ -102,7 +103,7 @@ public static function gracefulDie( echo $error->xdebug_message; echo ""; } - self::die(); + self::die(return_code: $return_code); } } @@ -171,6 +172,7 @@ public static function badRequest( $user_message, error: $error, http_response_code: 400, + return_code: 1, data: $data, ); } @@ -188,6 +190,7 @@ public static function forbidden( $user_message, error: $error, http_response_code: 403, + return_code: 1, data: $data, ); } @@ -205,6 +208,7 @@ public static function internalServerError( $user_message, error: $error, http_response_code: 500, + return_code: 1, data: $data, ); } diff --git a/test/phpunit-bootstrap.php b/test/phpunit-bootstrap.php index ce7dcb297..48c860cef 100644 --- a/test/phpunit-bootstrap.php +++ b/test/phpunit-bootstrap.php @@ -83,19 +83,33 @@ function http_post( } $_POST = $post_data; ob_start(); - $post_did_redirect_or_die = false; + $died_gracefully = false; try { include $phpfile; } catch (UnityWebPortal\lib\exceptions\NoDieException $e) { - $post_did_redirect_or_die = true; + if (($rc_str = $e->getMessage()) === "0") { + $died_gracefully = true; + } else { + $output = ob_get_clean(); + throw new Exception( + sprintf( + "%s tried to exit with nonzero return code %s. output: %s", + $phpfile, + $rc_str, + $output, + ), + ); + } } finally { - ob_get_clean(); // discard output + if (ob_get_level() > 1) { + ob_get_clean(); // discard output + } unset($_POST); $_SERVER = $_PREVIOUS_SERVER; } if ($enforce_PRG) { // https://en.wikipedia.org/wiki/Post/Redirect/Get - ensure($post_did_redirect_or_die, "post did not redirect or die!"); + ensure($died_gracefully, "post did not redirect or die!"); } }