]> git.mxchange.org Git - friendica.git/commitdiff
Fix IHTTPResult::getHeader()
authorPhilipp <admin@philipp.info>
Fri, 20 Aug 2021 17:48:20 +0000 (19:48 +0200)
committerPhilipp <admin@philipp.info>
Fri, 20 Aug 2021 17:58:42 +0000 (19:58 +0200)
- Now returns a string array, like expected
- Fix usages
- Fix dataset

src/Content/Text/BBCode.php
src/Model/Photo.php
src/Model/User.php
src/Network/CurlResult.php
src/Network/IHTTPResult.php
src/Network/Probe.php
src/Protocol/OStatus.php
src/Util/Images.php
src/Util/ParseUrl.php
tests/datasets/curl/about.head.php
tests/datasets/curl/about.redirect.php

index cd433bdbb8e66191809a5dceb074112e06c651c2..ce6b360817db3c0e9d80faf2d7c9d34b0207074c 100644 (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);
index a59c30aca2d854d2e8765bdc82f1aaa1a05ab30c..20d2c8a7aa3d28df4858b1d96097ede13ed9d467 100644 (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()) {
index 49423ce9ef74792b17423aabd52380ef7ec5006e..48a13e6ddbcca627a1bcdad2fec38758b09d6fa8 100644 (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()) {
index 65c110338f7abdd2f8c0e538dbc5b7024c0c53af..1c74634db48d2b67a55bf286b82160360af206be 100644 (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;
index 77ee86976bd044bc672fca9be1bdd5f78db511cb..acee2dde983b6437754c0a044a5373d4e005a482 100644 (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);
 
index 1115808007a99c420a8f4fadb01fd1924a868a30..ef899b6c1a2b4727d66265c6f85d01ecc7896732 100644 (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;
                }
 
index 8d818471bfbbc23fafba7cf619f0af4a1de22b68..215c08fa08dca9d1749b37e73f8cbf15e474e4fe 100644 (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();
                }
index bf84ee6c22552bad74d3db1eb83272a5bc52f0d3..c1ac731b6cf9b6af18c8af606c62029ad45cbd50 100644 (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'];
                }
 
index d95897b1d01c8f7d3a5843d6da166e5359779981..79c3928d7de5703e749f7691912b2f8b1dcda5fa 100644 (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
index d0be0feb4f6030e7c944daa0c46dfa3f3ffc8a84..b7773b81af3d29fa472d5ad981cd83934cdadbda 100644 (file)
@@ -1,20 +1,20 @@
 <?php
 
 return [
-       'http/2 200' => '',
-    'date' => 'Thu, 11 Oct 2018 18:43:54 GMT',
-    'content-type' => 'text/html; charset=utf-8',
-    'vary' => 'Accept-Encoding',
-    'server' => 'Mastodon',
-    'x-frame-options' => 'SAMEORIGIN',
-    'x-content-type-options' => 'nosniff',
-    'x-xss-protection' => '1; mode=block',
-    'etag' => 'W/"706e6c48957e1d46ecf9d7597a7880af"',
-    'cache-control' => 'max-age=0, private, must-revalidate',
-    'set-cookie' => '_mastodon_session=v3kcy%2FW3aZYBBvZUohuwksEKwzYIyEUlEuJ1KqTAfWPKvVQq%2F4UuJ39zp621VyfpQNlvY46TL%2FYutzXowSLYQBNFCJcrEiF04aU0TdtHls9zynMiyeHhoVgCijOXWXNt9%2FCmpQ49RkNEujkv9NaJ0cum32MCVZKjE9%2BMKmLM%2F8ZygZeLBGJ7sg%3D%3D--QGIiU0%2FpXc3Aym8F--he2iRRPePOdtEs3z%2BufSXg%3D%3D; path=/; secure; HttpOnly',
-    'x-request-id' => 'a0c0b8e7-cd60-4efa-b79b-cf1b0d5a0784',
-    'x-runtime' => '0.049566',
-    'strict-transport-security' => 'max-age=31536000; includeSubDomains; preload',
-    'referrer-policy' => 'same-origin',
-    'content-security-policy' => "frame-ancestors 'none'; script-src 'self'; object-src 'self'; img-src * data: blob:; media-src 'self' data:; font-src 'self' data: https://fonts.gstatic.com/; connect-src 'self' blob: wss://mastodonten.de",
+       'http/2 200' => [''],
+    'date' => ['Thu, 11 Oct 2018 18:43:54 GMT'],
+    'content-type' => ['text/html; charset=utf-8'],
+    'vary' => ['Accept-Encoding'],
+    'server' => ['Mastodon'],
+    'x-frame-options' => ['DENY', 'SAMEORIGIN'],
+    'x-content-type-options' => ['nosniff'],
+    'x-xss-protection' => ['1; mode=block'],
+    'etag' => ['W/"706e6c48957e1d46ecf9d7597a7880af"'],
+    'cache-control' => ['max-age=0, private, must-revalidate'],
+    'set-cookie' => ['_mastodon_session=v3kcy%2FW3aZYBBvZUohuwksEKwzYIyEUlEuJ1KqTAfWPKvVQq%2F4UuJ39zp621VyfpQNlvY46TL%2FYutzXowSLYQBNFCJcrEiF04aU0TdtHls9zynMiyeHhoVgCijOXWXNt9%2FCmpQ49RkNEujkv9NaJ0cum32MCVZKjE9%2BMKmLM%2F8ZygZeLBGJ7sg%3D%3D--QGIiU0%2FpXc3Aym8F--he2iRRPePOdtEs3z%2BufSXg%3D%3D; path=/; secure; HttpOnly'],
+    'x-request-id' => ['a0c0b8e7-cd60-4efa-b79b-cf1b0d5a0784'],
+    'x-runtime' => ['0.049566'],
+    'strict-transport-security' => ['max-age=31536000; includeSubDomains; preload'],
+    'referrer-policy' => ['same-origin'],
+    'content-security-policy' => ["frame-ancestors 'none'; script-src 'self'; object-src 'self'; img-src * data: blob:; media-src 'self' data:; font-src 'self' data: https://fonts.gstatic.com/; connect-src 'self' blob: wss://mastodonten.de"],
 ];
index 5ae3fd88f7c0526e429355bd0183477629793541..f01689aadcb2e70ee14578ecd244cb4a940b4b4d 100644 (file)
@@ -1,21 +1,21 @@
 <?php
 
 return [
-       'http/2 301' => '',
-    'date' => 'Thu, 11 Oct 2018 18:43:54 GMT',
-    'content-type' => 'text/html; charset=utf-8',
-    'vary' => 'Accept-Encoding',
-    'server' => 'Mastodon',
-    'location' => 'https://test.other/some/',
-    'x-frame-options' => 'SAMEORIGIN',
-    'x-content-type-options' => 'nosniff',
-    'x-xss-protection' => '1; mode=block',
-    'etag' => 'W/"706e6c48957e1d46ecf9d7597a7880af"',
-    'cache-control' => 'max-age=0, private, must-revalidate',
-    'set-cookie' => '_mastodon_session=v3kcy%2FW3aZYBBvZUohuwksEKwzYIyEUlEuJ1KqTAfWPKvVQq%2F4UuJ39zp621VyfpQNlvY46TL%2FYutzXowSLYQBNFCJcrEiF04aU0TdtHls9zynMiyeHhoVgCijOXWXNt9%2FCmpQ49RkNEujkv9NaJ0cum32MCVZKjE9%2BMKmLM%2F8ZygZeLBGJ7sg%3D%3D--QGIiU0%2FpXc3Aym8F--he2iRRPePOdtEs3z%2BufSXg%3D%3D; path=/; secure; HttpOnly',
-    'x-request-id' => 'a0c0b8e7-cd60-4efa-b79b-cf1b0d5a0784',
-    'x-runtime' => '0.049566',
-    'strict-transport-security' => 'max-age=31536000; includeSubDomains; preload',
-    'referrer-policy' => 'same-origin',
-    'content-security-policy' => "frame-ancestors 'none'; script-src 'self'; object-src 'self'; img-src * data: blob:; media-src 'self' data:; font-src 'self' data: https://fonts.gstatic.com/; connect-src 'self' blob: wss://mastodonten.de",
+       'http/2 301' => [''],
+    'date' => ['Thu, 11 Oct 2018 18:43:54 GMT'],
+    'content-type' => ['text/html; charset=utf-8'],
+    'vary' => ['Accept-Encoding'],
+    'server' => ['Mastodon'],
+    'location' => ['https://test.other/some/'],
+    'x-frame-options' => ['DENY', 'SAMEORIGIN'],
+    'x-content-type-options' => ['nosniff'],
+    'x-xss-protection' => ['1; mode=block'],
+    'etag' => ['W/"706e6c48957e1d46ecf9d7597a7880af"'],
+    'cache-control' => ['max-age=0, private, must-revalidate'],
+    'set-cookie' => ['_mastodon_session=v3kcy%2FW3aZYBBvZUohuwksEKwzYIyEUlEuJ1KqTAfWPKvVQq%2F4UuJ39zp621VyfpQNlvY46TL%2FYutzXowSLYQBNFCJcrEiF04aU0TdtHls9zynMiyeHhoVgCijOXWXNt9%2FCmpQ49RkNEujkv9NaJ0cum32MCVZKjE9%2BMKmLM%2F8ZygZeLBGJ7sg%3D%3D--QGIiU0%2FpXc3Aym8F--he2iRRPePOdtEs3z%2BufSXg%3D%3D; path=/; secure; HttpOnly'],
+    'x-request-id' => ['a0c0b8e7-cd60-4efa-b79b-cf1b0d5a0784'],
+    'x-runtime' => ['0.049566'],
+    'strict-transport-security' => ['max-age=31536000; includeSubDomains; preload'],
+    'referrer-policy' => ['same-origin'],
+    'content-security-policy' => ["frame-ancestors 'none'; script-src 'self'; object-src 'self'; img-src * data: blob:; media-src 'self' data:; font-src 'self' data: https://fonts.gstatic.com/; connect-src 'self' blob: wss://mastodonten.de"],
 ];