Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions resources/lib/UnityHTTPD.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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 <a href='$dest'>here</a> to continue.";
}
self::die();
self::die(return_code: 0);
}

/*
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -102,7 +103,7 @@ public static function gracefulDie(
echo $error->xdebug_message;
echo "</table>";
}
self::die();
self::die(return_code: $return_code);
}
}

Expand Down Expand Up @@ -171,6 +172,7 @@ public static function badRequest(
$user_message,
error: $error,
http_response_code: 400,
return_code: 1,
data: $data,
);
}
Expand All @@ -188,6 +190,7 @@ public static function forbidden(
$user_message,
error: $error,
http_response_code: 403,
return_code: 1,
data: $data,
);
}
Expand All @@ -205,6 +208,7 @@ public static function internalServerError(
$user_message,
error: $error,
http_response_code: 500,
return_code: 1,
data: $data,
);
}
Expand Down
22 changes: 18 additions & 4 deletions test/phpunit-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}

Expand Down
Loading