Skip to content

Commit

Permalink
Merge pull request #4960 from Gustry/http-request
Browse files Browse the repository at this point in the history
Admin - See if an HTTP error occurred
  • Loading branch information
Gustry authored Nov 21, 2024
2 parents cf41756 + 07c8c1c commit c320a73
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lizmap/modules/lizmap/lib/Request/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,22 @@ protected static function fileProxy($url, $options)
return array($data, $mime, $http_code);
}

/**
* Log if the HTTP code is a 4XX or 5XX error code.
*
* @param int $httpCode The HTTP code of the request
* @param string $url The URL of the request, for logging
*/
public static function logRequestIfError($httpCode, $url)
{
$httpCodeClass = substr($httpCode, 0, 1);
// Change to str_starts_with when PHP 8.1 will be minimum version for all maintained version
if ($httpCodeClass == '4' || $httpCodeClass == '5') {
\jLog::log('An HTTP request ended with an error, please check the main error log. Code '.$httpCode, 'lizmapadmin');
\jLog::log('The HTTP request below ended with an error. Code '.$httpCode.''.$url, 'error');
}
}

/**
* Get remote data from URL, with curl or internal php functions.
*
Expand Down Expand Up @@ -526,10 +542,16 @@ public static function getRemoteData($url, $options = null, $debug = null, $meth
// Proxy http backend : use curl or file_get_contents
if (extension_loaded('curl') && $options['proxyHttpBackend'] != 'php') {
// With curl
return self::curlProxy($url, $options);
$curlRequest = self::curlProxy($url, $options);
self::logRequestIfError($curlRequest[2], $url);

return $curlRequest;
}
// With file_get_contents
return self::fileProxy($url, $options);
$request = self::fileProxy($url, $options);
self::logRequestIfError($request[2], $url);

return $request;
}

/**
Expand Down Expand Up @@ -610,6 +632,7 @@ public static function getRemoteDataAsStream($url, $options = null)
}

$response = $client->send($request, $reqOptions);
self::logRequestIfError($response->getStatusCode(), $url);

return new ProxyResponse(
$response->getStatusCode(),
Expand Down

0 comments on commit c320a73

Please sign in to comment.