]> git.mxchange.org Git - friendica.git/commitdiff
Fix IHTTPResult::getHeader/s()
authorPhilipp <admin@philipp.info>
Fri, 9 Oct 2020 17:33:19 +0000 (19:33 +0200)
committerPhilipp <admin@philipp.info>
Sat, 10 Oct 2020 21:11:30 +0000 (23:11 +0200)
- Split functionality "getHeader()" and "getHeaders()" analog to IMessageInterface::getHeader/s()
- Fix functionality at various places
- Adapt CurlResultTest

mod/parse_url.php
src/Network/CurlResult.php
src/Network/HTTPRequest.php
src/Network/IHTTPResult.php
src/Protocol/DFRN.php
src/Protocol/OStatus.php
src/Protocol/Salmon.php
src/Util/ParseUrl.php
tests/datasets/curl/about.head.php [new file with mode: 0644]
tests/datasets/curl/about.redirect.php [new file with mode: 0644]
tests/src/Network/CurlResultTest.php

index a1faab6efb6d07c82bc5e49d5f3bdc8bfbfbc910..39aae4a5a02a7dc594e397927d357f6d037d04e0 100644 (file)
@@ -90,12 +90,13 @@ function parse_url_content(App $a)
        if ($curlResponse->isSuccess()) {
                // Convert the header fields into an array
                $hdrs = [];
-               $h = explode("\n", $curlResponse->getHeader());
+               $h = $curlResponse->getHeaders();
                foreach ($h as $l) {
-                       $header = array_map('trim', explode(':', trim($l), 2));
-                       if (count($header) == 2) {
-                               list($k, $v) = $header;
-                               $hdrs[$k] = $v;
+                       foreach ($l as $k => $v) {
+                               if (empty($hdrs[$k])) {
+                                       $hdrs[$k] = $v;
+                               }
+                               $hdrs[$k] .= " " . $v;
                        }
                }
                $type = null;
index 1187e45eb6f903fc7b73cbb423d446dbe5e64214..b0e801010d4ea0615f749feb3f50f355cee5d653 100644 (file)
@@ -242,23 +242,29 @@ class CurlResult implements IHTTPResult
        }
 
        /** {@inheritDoc} */
-       public function getHeader(string $field = '')
+       public function getHeader($header)
        {
-               if (empty($field)) {
-                       return $this->header;
+               if (empty($header)) {
+                       return '';
                }
 
-               $field = strtolower(trim($field));
+               $header = strtolower(trim($header));
 
                $headers = $this->getHeaderArray();
 
-               if (isset($headers[$field])) {
-                       return $headers[$field];
+               if (isset($headers[$header])) {
+                       return $headers[$header];
                }
 
                return '';
        }
 
+       /** {@inheritDoc} */
+       public function getHeaders()
+       {
+               return $this->getHeaderArray();
+       }
+
        /** {@inheritDoc} */
        public function inHeader(string $field)
        {
index 6341c0729732ef23567be6d3b208a781ac3ef70a..798b0cdea8ab4472f1f6359b71e088ad2fad451a 100644 (file)
@@ -454,8 +454,7 @@ class HTTPRequest implements IHTTPRequest
                                'timeout'        => $timeout,
                                'accept_content' => $accept_content,
                                'cookiejar'      => $cookiejar
-                       ],
-                       $redirects
+                       ]
                );
        }
 
index be190c80c16469bc0eec1274507dbea25c285e94..5904bcfa386c8df8ab221645d07a46967c210a81 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace Friendica\Network;
 
+use Psr\Http\Message\MessageInterface;
+
 /**
  * Temporary class to map Friendica used variables based on PSR-7 HTTPResponse
  */
@@ -23,15 +25,25 @@ interface IHTTPResult
 
        /**
         * Returns the headers
+        * @see MessageInterface::getHeader()
         *
-        * @param string $field optional header field. Return all fields if empty
+        * @param string $header optional header field. Return all fields if empty
         *
         * @return string the headers or the specified content of the header variable
         */
-       public function getHeader(string $field = '');
+       public function getHeader($header);
+
+       /**
+        * Returns all headers
+        * @see MessageInterface::getHeaders()
+        *
+        * @return string[][]
+        */
+       public function getHeaders();
 
        /**
         * Check if a specified header exists
+        * @see MessageInterface::hasHeader()
         *
         * @param string $field header field
         *
@@ -41,8 +53,10 @@ interface IHTTPResult
 
        /**
         * Returns the headers as an associated array
+        * @see MessageInterface::getHeaders()
+        * @deprecated
         *
-        * @return array associated header array
+        * @return string[][] associated header array
         */
        public function getHeaderArray();
 
@@ -62,6 +76,8 @@ interface IHTTPResult
        public function getRedirectUrl();
 
        /**
+        * @see MessageInterface::getBody()
+        *
         * @return string
         */
        public function getBody();
index d20864cf7f8515e55555adf83e8f10445d0e9b72..f718e0a741e8ad87f2dbda9516dcf94647d9005a 100644 (file)
@@ -1358,7 +1358,7 @@ class DFRN
                        return -9; // timed out
                }
 
-               if (($curl_stat == 503) && stristr($postResult->getHeader(), 'retry-after')) {
+               if (($curl_stat == 503) && $postResult->inHeader('retry-after')) {
                        return -10;
                }
 
@@ -1453,7 +1453,7 @@ class DFRN
                        return -9; // timed out
                }
 
-               if (($curl_stat == 503) && (stristr($postResult->getHeader(), 'retry-after'))) {
+               if (($curl_stat == 503) && $postResult->inHeader('retry-after')) {
                        return -10;
                }
 
index 0635be87d1c9a83191d80ac0bed7d349b06ed577..ef38aaebe3853bb17f7a4aa6a375cdc112dcd66f 100644 (file)
@@ -746,7 +746,8 @@ class OStatus
 
                $xml = '';
 
-               if (stristr($curlResult->getHeader(), 'Content-Type: application/atom+xml')) {
+               if ($curlResult->inHeader('Content-Type') &&
+                       stristr($curlResult->getHeader('Content-Type'), 'application/atom+xml')) {
                        $xml = $curlResult->getBody();
                }
 
@@ -939,7 +940,8 @@ class OStatus
 
                $xml = '';
 
-               if (stristr($curlResult->getHeader(), 'Content-Type: application/atom+xml')) {
+               if ($curlResult->inHeader('Content-Type') &&
+                       stristr($curlResult->getHeader('Content-Type'), 'application/atom+xml')) {
                        Logger::log('Directly fetched XML for URI ' . $related_uri, Logger::DEBUG);
                        $xml = $curlResult->getBody();
                }
index 169a4d0cbd8ec541fd39518d8d1534e84a887548..80846259257ba4b9bd46bd84c3af1971f5f23e7c 100644 (file)
@@ -215,7 +215,7 @@ class Salmon
                        return -1;
                }
 
-               if (($return_code == 503) && (stristr($postResult->getHeader(), 'retry-after'))) {
+               if (($return_code == 503) && $postResult->inHeader('retry-after')) {
                        return -1;
                }
 
index 1596e015bef1415fb1858cd3d45565eaf0dbf1ed..c3cbda70e3f2f483522ba43fd11de251f8f10648 100644 (file)
@@ -175,7 +175,6 @@ class ParseUrl
                        return $siteinfo;
                }
 
-               $header = $curlResult->getHeader();
                $body = $curlResult->getBody();
 
                if ($do_oembed) {
@@ -204,7 +203,7 @@ 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', $header, $matches)) {
+               if (preg_match('/charset=([a-z0-9-_.\/]+)/i', $curlResult->getContentType(), $matches)) {
                        $charset = trim(trim(trim(array_pop($matches)), ';,'));
                }
 
diff --git a/tests/datasets/curl/about.head.php b/tests/datasets/curl/about.head.php
new file mode 100644 (file)
index 0000000..d0be0fe
--- /dev/null
@@ -0,0 +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",
+];
diff --git a/tests/datasets/curl/about.redirect.php b/tests/datasets/curl/about.redirect.php
new file mode 100644 (file)
index 0000000..5ae3fd8
--- /dev/null
@@ -0,0 +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",
+];
index e066fb89b052a0b0c65c9a0d1a41e7c57f5c5b6e..03a5288124bb3a77707e5f129db23c1133e59e27 100644 (file)
@@ -53,6 +53,7 @@ class CurlResultTest extends TestCase
        public function testNormal()
        {
                $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
+               $headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php');
                $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
 
 
@@ -65,7 +66,7 @@ class CurlResultTest extends TestCase
                $this->assertTrue($curlResult->isSuccess());
                $this->assertFalse($curlResult->isTimeout());
                $this->assertFalse($curlResult->isRedirectUrl());
-               $this->assertSame($header, $curlResult->getHeader());
+               $this->assertSame($headerArray, $curlResult->getHeaders());
                $this->assertSame($body, $curlResult->getBody());
                $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
                $this->assertSame('https://test.local', $curlResult->getUrl());
@@ -80,6 +81,7 @@ class CurlResultTest extends TestCase
        public function testRedirect()
        {
                $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
+               $headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php');
                $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
 
 
@@ -93,7 +95,7 @@ class CurlResultTest extends TestCase
                $this->assertTrue($curlResult->isSuccess());
                $this->assertFalse($curlResult->isTimeout());
                $this->assertTrue($curlResult->isRedirectUrl());
-               $this->assertSame($header, $curlResult->getHeader());
+               $this->assertSame($headerArray, $curlResult->getHeaders());
                $this->assertSame($body, $curlResult->getBody());
                $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
                $this->assertSame('https://test.local/test/it', $curlResult->getUrl());
@@ -106,6 +108,7 @@ class CurlResultTest extends TestCase
        public function testTimeout()
        {
                $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
+               $headerArray = include(__DIR__ . '/../../datasets/curl/about.head.php');
                $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
 
 
@@ -119,7 +122,7 @@ class CurlResultTest extends TestCase
                $this->assertFalse($curlResult->isSuccess());
                $this->assertTrue($curlResult->isTimeout());
                $this->assertFalse($curlResult->isRedirectUrl());
-               $this->assertSame($header, $curlResult->getHeader());
+               $this->assertSame($headerArray, $curlResult->getHeaders());
                $this->assertSame($body, $curlResult->getBody());
                $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
                $this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
@@ -134,6 +137,7 @@ class CurlResultTest extends TestCase
        public function testRedirectHeader()
        {
                $header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
+               $headerArray = include(__DIR__ . '/../../datasets/curl/about.redirect.php');
                $body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
 
 
@@ -146,7 +150,7 @@ class CurlResultTest extends TestCase
                $this->assertTrue($curlResult->isSuccess());
                $this->assertFalse($curlResult->isTimeout());
                $this->assertTrue($curlResult->isRedirectUrl());
-               $this->assertSame($header, $curlResult->getHeader());
+               $this->assertSame($headerArray, $curlResult->getHeaders());
                $this->assertSame($body, $curlResult->getBody());
                $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
                $this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
@@ -204,7 +208,7 @@ class CurlResultTest extends TestCase
                        'url' => 'https://test.local'
                ]);
 
-               $this->assertNotEmpty($curlResult->getHeader());
+               $this->assertNotEmpty($curlResult->getHeaders());
                $this->assertEmpty($curlResult->getHeader('wrongHeader'));
        }
 }