Sfoglia il codice sorgente

Return more info and status from `httpGet()` (#8700)

Useful for extensions and core code needing HTTP status information.
E.g. https://github.com/FreshRSS/Extensions/pull/458
Alexandre Alapetite 1 mese fa
parent
commit
7a9b023dfd
1 ha cambiato i file con 10 aggiunte e 5 eliminazioni
  1. 10 5
      app/Utils/httpUtil.php

+ 10 - 5
app/Utils/httpUtil.php

@@ -261,7 +261,11 @@ final class FreshRSS_http_Util {
 	 * @param string $type {html,ico,json,opml,xml}
 	 * @param array<string,mixed> $attributes
 	 * @param array<int,mixed> $curl_options
-	 * @return array{body:string,effective_url:string,redirect_count:int,fail:bool}
+	 * @return array{body:string,effective_url:string,redirect_count:int,fail:bool,status:int,error:string}
+	 *   `status` is the HTTP response code (e.g. 200, 404), or a custom negative value:
+	 *   * `-200` served from local cache;
+	 *   * `-429` blocked by active `Retry-After` period;
+	 *   * `-500` `curl_init()` failure.
 	 */
 	public static function httpGet(string $url, string $cachePath, string $type = 'html', array $attributes = [], array $curl_options = []): array {
 		$limits = FreshRSS_Context::systemConf()->limits;
@@ -272,7 +276,7 @@ final class FreshRSS_http_Util {
 			$body = @file_get_contents($cachePath);
 			if ($body != false) {
 				syslog(LOG_DEBUG, 'FreshRSS uses cache for ' . \SimplePie\Misc::url_remove_credentials($url));
-				return ['body' => $body, 'effective_url' => $url, 'redirect_count' => 0, 'fail' => false];
+				return ['body' => $body, 'effective_url' => $url, 'redirect_count' => 0, 'fail' => false, 'status' => -200, 'error' => ''];
 			}
 		}
 
@@ -299,7 +303,7 @@ final class FreshRSS_http_Util {
 
 		if (($retryAfter = FreshRSS_http_Util::getRetryAfter($url, $proxy)) > 0) {
 			Minz_Log::warning('For that domain, will first retry after ' . date('c', $retryAfter) . '. ' . \SimplePie\Misc::url_remove_credentials($url));
-			return ['body' => '', 'effective_url' => $url, 'redirect_count' => 0, 'fail' => true];
+			return ['body' => '', 'effective_url' => $url, 'redirect_count' => 0, 'fail' => true, 'status' => -429, 'error' => ''];
 		}
 
 		if (FreshRSS_Context::systemConf()->simplepie_syslog_enabled) {
@@ -328,7 +332,7 @@ final class FreshRSS_http_Util {
 		// TODO: Implement HTTP 1.1 conditional GET If-Modified-Since
 		$ch = curl_init();
 		if ($ch === false) {
-			return ['body' => '', 'effective_url' => '', 'redirect_count' => 0, 'fail' => true];
+			return ['body' => '', 'effective_url' => '', 'redirect_count' => 0, 'fail' => true, 'status' => -500, 'error' => ''];
 		}
 		curl_setopt_array($ch, [
 			CURLOPT_URL => $url,
@@ -420,7 +424,8 @@ final class FreshRSS_http_Util {
 			Minz_Log::warning("Error saving cache $cachePath for $url");
 		}
 
-		return ['body' => $body, 'effective_url' => $c_effective_url, 'redirect_count' => $c_redirect_count, 'fail' => $fail];
+		return ['body' => $body, 'effective_url' => $c_effective_url, 'redirect_count' => $c_redirect_count,
+			'fail' => $fail, 'status' => $c_status, 'error' => $c_error];
 	}
 
 	/**