Skip to content

Commit

Permalink
Better trim base_url (#4423)
Browse files Browse the repository at this point in the history
Avoid usual errors for instance with quotes, especially when provided through Docker / CLI
  • Loading branch information
Alkarex committed Jun 21, 2022
1 parent 5b31c82 commit 47ab9d5
Showing 1 changed file with 9 additions and 28 deletions.
37 changes: 9 additions & 28 deletions lib/Minz/Request.php
Expand Up @@ -133,10 +133,8 @@ public static function is($controller_name, $action_name) {

/**
* Return true if the request is over HTTPS, false otherwise (HTTP)
*
* @return boolean
*/
public static function isHttps() {
public static function isHttps(): bool {
$header = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '';
if ('' != $header) {
return 'https' === strtolower($header);
Expand All @@ -159,10 +157,7 @@ public static function guessBaseUrl(): string {
return filter_var("{$protocol}://{$host}{$port}{$prefix}{$path}", FILTER_SANITIZE_URL);
}

/**
* @return string
*/
private static function extractProtocol() {
private static function extractProtocol(): string {
if (self::isHttps()) {
return 'https';
}
Expand Down Expand Up @@ -202,10 +197,7 @@ private static function extractPort() {
return self::isHttps() ? 443 : 80;
}

/**
* @return string
*/
private static function extractPortForUrl() {
private static function extractPortForUrl(): string {
if (self::isHttps() && 443 !== $port = self::extractPort()) {
return ":{$port}";
}
Expand All @@ -215,10 +207,7 @@ private static function extractPortForUrl() {
return '';
}

/**
* @return string
*/
private static function extractPrefix() {
private static function extractPrefix(): string {
if ('' != $prefix = ($_SERVER['HTTP_X_FORWARDED_PREFIX'] ?? '')) {
return rtrim($prefix, '/ ');
}
Expand All @@ -235,13 +224,11 @@ private static function extractPath(): string {
}

/**
* Return the base_url from configuration and add a suffix if given.
*
* @return string base_url with a suffix.
* Return the base_url from configuration
*/
public static function getBaseUrl() {
public static function getBaseUrl(): string {
$conf = Minz_Configuration::get('system');
$url = rtrim($conf->base_url, '/\\');
$url = trim($conf->base_url, ' /\\"');
return filter_var($url, FILTER_SANITIZE_URL);
}

Expand Down Expand Up @@ -397,17 +384,11 @@ private static function initJSON() {
}
}

/**
* @return string
*/
private static function extractContentType() {
private static function extractContentType(): string {
return strtolower(trim($_SERVER['CONTENT_TYPE'] ?? ''));
}

/**
* @return boolean
*/
public static function isPost() {
public static function isPost(): bool {
return 'POST' === ($_SERVER['REQUEST_METHOD'] ?? '');
}

Expand Down

0 comments on commit 47ab9d5

Please sign in to comment.