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 {
$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) {

View File

@ -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,
];