fix: connectivityaction (#2725)

This commit is contained in:
Dag 2022-05-17 23:46:37 +02:00 committed by GitHub
parent 7a87a09fc5
commit 0212c4790f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 10 deletions

View File

@ -83,12 +83,10 @@ class ConnectivityAction extends ActionAbstract {
try { try {
$reply = getContents($bridge::URI, array(), $curl_opts, true); $reply = getContents($bridge::URI, array(), $curl_opts, true);
if($reply) { if($reply['code'] === 200) {
$retVal['successful'] = true; $retVal['successful'] = true;
if (isset($reply['header'])) { if (strpos(implode('', $reply['status_lines']), '301 Moved Permanently')) {
if (strpos($reply['header'], 'HTTP/1.1 301 Moved Permanently') !== false) { $retVal['http_code'] = 301;
$retVal['http_code'] = 301;
}
} }
} }
} catch(Exception $e) { } catch(Exception $e) {

View File

@ -49,14 +49,14 @@ const RSSBRIDGE_HTTP_STATUS_CODES = [
* *
* @param array $httpHeaders E.g. ['Content-type: text/plain'] * @param array $httpHeaders E.g. ['Content-type: text/plain']
* @param array $curlOptions Associative array e.g. [CURLOPT_MAXREDIRS => 3] * @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 * @return string|array
*/ */
function getContents( function getContents(
string $url, string $url,
array $httpHeaders = [], array $httpHeaders = [],
array $curlOptions = [], array $curlOptions = [],
bool $returnHeader = false bool $returnFull = false
) { ) {
$cacheFactory = new CacheFactory(); $cacheFactory = new CacheFactory();
$cacheFactory->setWorkingDir(PATH_LIB_CACHES); $cacheFactory->setWorkingDir(PATH_LIB_CACHES);
@ -78,6 +78,8 @@ function getContents(
$result = _http_request($url, $config); $result = _http_request($url, $config);
$response = [ $response = [
'code' => $result['code'],
'status_lines' => $result['status_lines'],
'header' => $result['headers'], 'header' => $result['headers'],
'content' => $result['body'], 'content' => $result['body'],
]; ];
@ -110,7 +112,7 @@ function getContents(
) )
); );
} }
if ($returnHeader === true) { if ($returnFull === true) {
return $response; return $response;
} }
return $response['content']; return $response['content'];
@ -158,10 +160,15 @@ function _http_request(string $url, array $config = []): array
curl_setopt($ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); curl_setopt($ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
} }
$responseStatusLines = [];
$responseHeaders = []; $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); $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; return $len;
} }
$header = explode(':', $rawHeader); $header = explode(':', $rawHeader);
@ -195,6 +202,7 @@ function _http_request(string $url, array $config = []): array
curl_close($ch); curl_close($ch);
return [ return [
'code' => $statusCode, 'code' => $statusCode,
'status_lines' => $responseStatusLines,
'headers' => $responseHeaders, 'headers' => $responseHeaders,
'body' => $data, 'body' => $data,
]; ];