From 0212c4790f8bd2da3b6638756f55b65fffe5a6bb Mon Sep 17 00:00:00 2001 From: Dag Date: Tue, 17 May 2022 23:46:37 +0200 Subject: [PATCH] fix: connectivityaction (#2725) --- actions/ConnectivityAction.php | 8 +++----- lib/contents.php | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/actions/ConnectivityAction.php b/actions/ConnectivityAction.php index a24b4901..43abdd34 100644 --- a/actions/ConnectivityAction.php +++ b/actions/ConnectivityAction.php @@ -83,12 +83,10 @@ class ConnectivityAction extends ActionAbstract { try { $reply = getContents($bridge::URI, array(), $curl_opts, true); - if($reply) { + if($reply['code'] === 200) { $retVal['successful'] = true; - if (isset($reply['header'])) { - if (strpos($reply['header'], 'HTTP/1.1 301 Moved Permanently') !== false) { - $retVal['http_code'] = 301; - } + if (strpos(implode('', $reply['status_lines']), '301 Moved Permanently')) { + $retVal['http_code'] = 301; } } } catch(Exception $e) { diff --git a/lib/contents.php b/lib/contents.php index 012c1c26..e67382c8 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -49,14 +49,14 @@ const RSSBRIDGE_HTTP_STATUS_CODES = [ * * @param array $httpHeaders E.g. ['Content-type: text/plain'] * @param array $curlOptions Associative array e.g. [CURLOPT_MAXREDIRS => 3] - * @param bool $returnHeader Whether to include headers in the returned value + * @param bool $returnFull Whether to return an array ['header' => [...], 'content' => '...'] * @return string|array */ function getContents( string $url, array $httpHeaders = [], array $curlOptions = [], - bool $returnHeader = false + bool $returnFull = false ) { $cacheFactory = new CacheFactory(); $cacheFactory->setWorkingDir(PATH_LIB_CACHES); @@ -78,6 +78,8 @@ function getContents( $result = _http_request($url, $config); $response = [ + 'code' => $result['code'], + 'status_lines' => $result['status_lines'], 'header' => $result['headers'], 'content' => $result['body'], ]; @@ -110,7 +112,7 @@ function getContents( ) ); } - if ($returnHeader === true) { + if ($returnFull === true) { return $response; } return $response['content']; @@ -158,10 +160,15 @@ function _http_request(string $url, array $config = []): array curl_setopt($ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); } + $responseStatusLines = []; $responseHeaders = []; - curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $rawHeader) use (&$responseHeaders) { + curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $rawHeader) use (&$responseHeaders, &$responseStatusLines) { $len = strlen($rawHeader); - if (preg_match('#^HTTP/(2|1.1|1.0)#', $rawHeader) || $rawHeader === "\r\n") { + if ($rawHeader === "\r\n") { + return $len; + } + if (preg_match('#^HTTP/(2|1.1|1.0)#', $rawHeader)) { + $responseStatusLines[] = $rawHeader; return $len; } $header = explode(':', $rawHeader); @@ -195,6 +202,7 @@ function _http_request(string $url, array $config = []): array curl_close($ch); return [ 'code' => $statusCode, + 'status_lines' => $responseStatusLines, 'headers' => $responseHeaders, 'body' => $data, ];