Fix IHTTPResult::getHeader()

- Now returns a string array, like expected
- Fix usages
- Fix dataset
This commit is contained in:
Philipp
2021-08-20 19:48:20 +02:00
parent 3c074ab315
commit 803c1d71de
11 changed files with 67 additions and 58 deletions

View File

@@ -499,8 +499,8 @@ class BBCode
}
$i = $curlResult->getBody();
$type = $curlResult->getContentType();
$type = Images::getMimeTypeByData($i, $mtch[1], $type);
$contType = $curlResult->getContentType();
$type = Images::getMimeTypeByData($i, $mtch[1], $contType);
if ($i) {
$Image = new Image($i, $type);

View File

@@ -492,16 +492,17 @@ class Photo
if (!empty($image_url)) {
$ret = DI::httpRequest()->get($image_url);
$img_str = $ret->getBody();
$type = $ret->getContentType();
$contType = $ret->getContentType();
} else {
$img_str = '';
$contType = [];
}
if ($quit_on_error && ($img_str == "")) {
return false;
}
$type = Images::getMimeTypeByData($img_str, $image_url, $type);
$type = Images::getMimeTypeByData($img_str, $image_url, $contType);
$Image = new Image($img_str, $type);
if ($Image->isValid()) {

View File

@@ -1095,13 +1095,13 @@ class User
$curlResult = DI::httpRequest()->get($photo);
if ($curlResult->isSuccess()) {
$img_str = $curlResult->getBody();
$type = $curlResult->getContentType();
$contType = $curlResult->getContentType();
} else {
$img_str = '';
$type = '';
$contType = [];
}
$type = Images::getMimeTypeByData($img_str, $photo, $type);
$type = Images::getMimeTypeByData($img_str, $photo, $contType);
$Image = new Image($img_str, $type);
if ($Image->isValid()) {

View File

@@ -245,7 +245,7 @@ class CurlResult implements IHTTPResult
public function getHeader($header)
{
if (empty($header)) {
return '';
return [];
}
$header = strtolower(trim($header));
@@ -256,7 +256,7 @@ class CurlResult implements IHTTPResult
return $headers[$header];
}
return '';
return [];
}
/** {@inheritDoc} */
@@ -289,7 +289,11 @@ class CurlResult implements IHTTPResult
$parts = explode(':', $line);
$headerfield = strtolower(trim(array_shift($parts)));
$headerdata = trim(implode(':', $parts));
$this->header_fields[$headerfield] = $headerdata;
if (empty($this->header_fields[$headerfield])) {
$this->header_fields[$headerfield] = [$headerdata];
} elseif (!in_array($headerdata, $this->header_fields[$headerfield])) {
$this->header_fields[$headerfield][] = $headerdata;
}
}
return $this->header_fields;

View File

@@ -19,7 +19,7 @@ interface IHTTPResult
/**
* Returns the Content Type
*
* @return string the Content Type
* @return string[] the Content Types
*/
public function getContentType();
@@ -29,7 +29,7 @@ interface IHTTPResult
*
* @param string $header optional header field. Return all fields if empty
*
* @return string the headers or the specified content of the header variable
* @return string[] the headers or the specified content of the header variable
*/
public function getHeader($header);

View File

@@ -430,7 +430,7 @@ class Probe
}
// If it isn't a HTML file then exit
if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) {
if (!in_array('html', $curlResult->getContentType())) {
return false;
}

View File

@@ -736,7 +736,7 @@ class OStatus
$xml = '';
if ($curlResult->inHeader('Content-Type') &&
stristr($curlResult->getHeader('Content-Type'), 'application/atom+xml')) {
in_array('application/atom+xml', $curlResult->getHeader('Content-Type'))) {
$xml = $curlResult->getBody();
}
@@ -930,7 +930,7 @@ class OStatus
$xml = '';
if ($curlResult->inHeader('Content-Type') &&
stristr($curlResult->getHeader('Content-Type'), 'application/atom+xml')) {
in_array('application/atom+xml', $curlResult->getHeader('Content-Type'))) {
Logger::log('Directly fetched XML for URI ' . $related_uri, Logger::DEBUG);
$xml = $curlResult->getBody();
}

View File

@@ -75,23 +75,25 @@ class Images
/**
* Fetch image mimetype from the image data or guessing from the file name
*
* @param string $image_data Image data
* @param string $filename File name (for guessing the type via the extension)
* @param string $mime default mime type
* @param string $image_data Image data
* @param string $filename File name (for guessing the type via the extension)
* @param string[] $mimeTypes possible mime types
*
* @return string
* @throws \Exception
*/
public static function getMimeTypeByData(string $image_data, string $filename = '', string $mime = '')
public static function getMimeTypeByData(string $image_data, string $filename = '', array $mimeTypes = [])
{
if (substr($mime, 0, 6) == 'image/') {
Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mime]);
return $mime;
foreach ($mimeTypes as $mimeType) {
if (substr($mimeType, 0, 6) == 'image/') {
Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mimeTypes]);
return $mimeType;
}
}
$image = @getimagesizefromstring($image_data);
if (!empty($image['mime'])) {
Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $mime, 'mime' => $image['mime']]);
Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $mimeTypes, 'mime' => $image['mime']]);
return $image['mime'];
}

View File

@@ -272,8 +272,10 @@ class ParseUrl
$charset = '';
// Look for a charset, first in headers
// Expected form: Content-Type: text/html; charset=ISO-8859-4
if (preg_match('/charset=([a-z0-9-_.\/]+)/i', $curlResult->getContentType(), $matches)) {
$charset = trim(trim(trim(array_pop($matches)), ';,'));
foreach ($curlResult->getContentType() as $type) {
if (preg_match('/charset=([a-z0-9-_.\/]+)/i', $type, $matches)) {
$charset = trim(trim(trim(array_pop($matches)), ';,'));
}
}
// Then in body that gets precedence