From 069753416d75fa63d6e476ae5706f30b43c9730f Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 11 Oct 2020 23:26:03 +0200 Subject: [PATCH] Revert "Fix IHTTPResult::getHeader/s() - Split functionality "getHeader()" and "getHeaders()" analog to IMessageInterface::getHeader/s() - Fix functionality at various places - Adapt CurlResultTest" This reverts commit 933ea7c9 --- mod/parse_url.php | 11 +++++------ src/Network/CurlResult.php | 18 ++++++------------ src/Network/HTTPRequest.php | 3 ++- src/Network/IHTTPResult.php | 22 +++------------------- src/Protocol/DFRN.php | 4 ++-- src/Protocol/OStatus.php | 6 ++---- src/Protocol/Salmon.php | 2 +- src/Util/ParseUrl.php | 3 ++- tests/datasets/curl/about.head.php | 20 -------------------- tests/datasets/curl/about.redirect.php | 21 --------------------- tests/src/Network/CurlResultTest.php | 14 +++++--------- 11 files changed, 28 insertions(+), 96 deletions(-) delete mode 100644 tests/datasets/curl/about.head.php delete mode 100644 tests/datasets/curl/about.redirect.php diff --git a/mod/parse_url.php b/mod/parse_url.php index 39aae4a5a0..a1faab6efb 100644 --- a/mod/parse_url.php +++ b/mod/parse_url.php @@ -90,13 +90,12 @@ function parse_url_content(App $a) if ($curlResponse->isSuccess()) { // Convert the header fields into an array $hdrs = []; - $h = $curlResponse->getHeaders(); + $h = explode("\n", $curlResponse->getHeader()); foreach ($h as $l) { - foreach ($l as $k => $v) { - if (empty($hdrs[$k])) { - $hdrs[$k] = $v; - } - $hdrs[$k] .= " " . $v; + $header = array_map('trim', explode(':', trim($l), 2)); + if (count($header) == 2) { + list($k, $v) = $header; + $hdrs[$k] = $v; } } $type = null; diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php index b0e801010d..1187e45eb6 100644 --- a/src/Network/CurlResult.php +++ b/src/Network/CurlResult.php @@ -242,29 +242,23 @@ class CurlResult implements IHTTPResult } /** {@inheritDoc} */ - public function getHeader($header) + public function getHeader(string $field = '') { - if (empty($header)) { - return ''; + if (empty($field)) { + return $this->header; } - $header = strtolower(trim($header)); + $field = strtolower(trim($field)); $headers = $this->getHeaderArray(); - if (isset($headers[$header])) { - return $headers[$header]; + if (isset($headers[$field])) { + return $headers[$field]; } return ''; } - /** {@inheritDoc} */ - public function getHeaders() - { - return $this->getHeaderArray(); - } - /** {@inheritDoc} */ public function inHeader(string $field) { diff --git a/src/Network/HTTPRequest.php b/src/Network/HTTPRequest.php index 1ced5fca76..37d268f615 100644 --- a/src/Network/HTTPRequest.php +++ b/src/Network/HTTPRequest.php @@ -465,7 +465,8 @@ class HTTPRequest implements IHTTPRequest 'timeout' => $timeout, 'accept_content' => $accept_content, 'cookiejar' => $cookiejar - ] + ], + $redirects ); } diff --git a/src/Network/IHTTPResult.php b/src/Network/IHTTPResult.php index 5904bcfa38..be190c80c1 100644 --- a/src/Network/IHTTPResult.php +++ b/src/Network/IHTTPResult.php @@ -2,8 +2,6 @@ namespace Friendica\Network; -use Psr\Http\Message\MessageInterface; - /** * Temporary class to map Friendica used variables based on PSR-7 HTTPResponse */ @@ -25,25 +23,15 @@ interface IHTTPResult /** * Returns the headers - * @see MessageInterface::getHeader() * - * @param string $header optional header field. Return all fields if empty + * @param string $field optional header field. Return all fields if empty * * @return string the headers or the specified content of the header variable */ - public function getHeader($header); - - /** - * Returns all headers - * @see MessageInterface::getHeaders() - * - * @return string[][] - */ - public function getHeaders(); + public function getHeader(string $field = ''); /** * Check if a specified header exists - * @see MessageInterface::hasHeader() * * @param string $field header field * @@ -53,10 +41,8 @@ interface IHTTPResult /** * Returns the headers as an associated array - * @see MessageInterface::getHeaders() - * @deprecated * - * @return string[][] associated header array + * @return array associated header array */ public function getHeaderArray(); @@ -76,8 +62,6 @@ interface IHTTPResult public function getRedirectUrl(); /** - * @see MessageInterface::getBody() - * * @return string */ public function getBody(); diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index f718e0a741..d20864cf7f 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -1358,7 +1358,7 @@ class DFRN return -9; // timed out } - if (($curl_stat == 503) && $postResult->inHeader('retry-after')) { + if (($curl_stat == 503) && stristr($postResult->getHeader(), 'retry-after')) { return -10; } @@ -1453,7 +1453,7 @@ class DFRN return -9; // timed out } - if (($curl_stat == 503) && $postResult->inHeader('retry-after')) { + if (($curl_stat == 503) && (stristr($postResult->getHeader(), 'retry-after'))) { return -10; } diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index ef38aaebe3..0635be87d1 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -746,8 +746,7 @@ class OStatus $xml = ''; - if ($curlResult->inHeader('Content-Type') && - stristr($curlResult->getHeader('Content-Type'), 'application/atom+xml')) { + if (stristr($curlResult->getHeader(), 'Content-Type: application/atom+xml')) { $xml = $curlResult->getBody(); } @@ -940,8 +939,7 @@ class OStatus $xml = ''; - if ($curlResult->inHeader('Content-Type') && - stristr($curlResult->getHeader('Content-Type'), 'application/atom+xml')) { + if (stristr($curlResult->getHeader(), 'Content-Type: application/atom+xml')) { Logger::log('Directly fetched XML for URI ' . $related_uri, Logger::DEBUG); $xml = $curlResult->getBody(); } diff --git a/src/Protocol/Salmon.php b/src/Protocol/Salmon.php index 8084625925..169a4d0cbd 100644 --- a/src/Protocol/Salmon.php +++ b/src/Protocol/Salmon.php @@ -215,7 +215,7 @@ class Salmon return -1; } - if (($return_code == 503) && $postResult->inHeader('retry-after')) { + if (($return_code == 503) && (stristr($postResult->getHeader(), 'retry-after'))) { return -1; } diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index c3cbda70e3..1596e015be 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -175,6 +175,7 @@ class ParseUrl return $siteinfo; } + $header = $curlResult->getHeader(); $body = $curlResult->getBody(); if ($do_oembed) { @@ -203,7 +204,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', $curlResult->getContentType(), $matches)) { + if (preg_match('/charset=([a-z0-9-_.\/]+)/i', $header, $matches)) { $charset = trim(trim(trim(array_pop($matches)), ';,')); } diff --git a/tests/datasets/curl/about.head.php b/tests/datasets/curl/about.head.php deleted file mode 100644 index d0be0feb4f..0000000000 --- a/tests/datasets/curl/about.head.php +++ /dev/null @@ -1,20 +0,0 @@ - '', - '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 deleted file mode 100644 index 5ae3fd88f7..0000000000 --- a/tests/datasets/curl/about.redirect.php +++ /dev/null @@ -1,21 +0,0 @@ - '', - '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", -]; diff --git a/tests/src/Network/CurlResultTest.php b/tests/src/Network/CurlResultTest.php index 03a5288124..e066fb89b0 100644 --- a/tests/src/Network/CurlResultTest.php +++ b/tests/src/Network/CurlResultTest.php @@ -53,7 +53,6 @@ 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'); @@ -66,7 +65,7 @@ class CurlResultTest extends TestCase $this->assertTrue($curlResult->isSuccess()); $this->assertFalse($curlResult->isTimeout()); $this->assertFalse($curlResult->isRedirectUrl()); - $this->assertSame($headerArray, $curlResult->getHeaders()); + $this->assertSame($header, $curlResult->getHeader()); $this->assertSame($body, $curlResult->getBody()); $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType()); $this->assertSame('https://test.local', $curlResult->getUrl()); @@ -81,7 +80,6 @@ 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'); @@ -95,7 +93,7 @@ class CurlResultTest extends TestCase $this->assertTrue($curlResult->isSuccess()); $this->assertFalse($curlResult->isTimeout()); $this->assertTrue($curlResult->isRedirectUrl()); - $this->assertSame($headerArray, $curlResult->getHeaders()); + $this->assertSame($header, $curlResult->getHeader()); $this->assertSame($body, $curlResult->getBody()); $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType()); $this->assertSame('https://test.local/test/it', $curlResult->getUrl()); @@ -108,7 +106,6 @@ 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'); @@ -122,7 +119,7 @@ class CurlResultTest extends TestCase $this->assertFalse($curlResult->isSuccess()); $this->assertTrue($curlResult->isTimeout()); $this->assertFalse($curlResult->isRedirectUrl()); - $this->assertSame($headerArray, $curlResult->getHeaders()); + $this->assertSame($header, $curlResult->getHeader()); $this->assertSame($body, $curlResult->getBody()); $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType()); $this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl()); @@ -137,7 +134,6 @@ 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'); @@ -150,7 +146,7 @@ class CurlResultTest extends TestCase $this->assertTrue($curlResult->isSuccess()); $this->assertFalse($curlResult->isTimeout()); $this->assertTrue($curlResult->isRedirectUrl()); - $this->assertSame($headerArray, $curlResult->getHeaders()); + $this->assertSame($header, $curlResult->getHeader()); $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()); @@ -208,7 +204,7 @@ class CurlResultTest extends TestCase 'url' => 'https://test.local' ]); - $this->assertNotEmpty($curlResult->getHeaders()); + $this->assertNotEmpty($curlResult->getHeader()); $this->assertEmpty($curlResult->getHeader('wrongHeader')); } }