mirror of
https://github.com/friendica/friendica
synced 2024-11-09 23:42:53 +00:00
Fix IHTTPResult::getHeader/s()
- Split functionality "getHeader()" and "getHeaders()" analog to IMessageInterface::getHeader/s() - Fix functionality at various places - Adapt CurlResultTest
This commit is contained in:
parent
a60ca4a1cf
commit
dee1899628
10 changed files with 89 additions and 22 deletions
|
@ -242,23 +242,29 @@ class CurlResult implements IHTTPResult
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public function getHeader(string $field = '')
|
public function getHeader($header)
|
||||||
{
|
{
|
||||||
if (empty($field)) {
|
if (empty($header)) {
|
||||||
return $this->header;
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$field = strtolower(trim($field));
|
$header = strtolower(trim($header));
|
||||||
|
|
||||||
$headers = $this->getHeaderArray();
|
$headers = $this->getHeaderArray();
|
||||||
|
|
||||||
if (isset($headers[$field])) {
|
if (isset($headers[$header])) {
|
||||||
return $headers[$field];
|
return $headers[$header];
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
public function getHeaders()
|
||||||
|
{
|
||||||
|
return $this->getHeaderArray();
|
||||||
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public function inHeader(string $field)
|
public function inHeader(string $field)
|
||||||
{
|
{
|
||||||
|
|
|
@ -493,8 +493,7 @@ class HTTPRequest implements IHTTPRequest
|
||||||
'timeout' => $timeout,
|
'timeout' => $timeout,
|
||||||
'accept_content' => $accept_content,
|
'accept_content' => $accept_content,
|
||||||
'cookiejar' => $cookiejar
|
'cookiejar' => $cookiejar
|
||||||
],
|
]
|
||||||
$redirects
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace Friendica\Network;
|
namespace Friendica\Network;
|
||||||
|
|
||||||
|
use Psr\Http\Message\MessageInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Temporary class to map Friendica used variables based on PSR-7 HTTPResponse
|
* Temporary class to map Friendica used variables based on PSR-7 HTTPResponse
|
||||||
*/
|
*/
|
||||||
|
@ -23,15 +25,25 @@ interface IHTTPResult
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the headers
|
* 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
|
* @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
|
* Check if a specified header exists
|
||||||
|
* @see MessageInterface::hasHeader()
|
||||||
*
|
*
|
||||||
* @param string $field header field
|
* @param string $field header field
|
||||||
*
|
*
|
||||||
|
@ -41,8 +53,10 @@ interface IHTTPResult
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the headers as an associated array
|
* Returns the headers as an associated array
|
||||||
|
* @see MessageInterface::getHeaders()
|
||||||
|
* @deprecated
|
||||||
*
|
*
|
||||||
* @return array associated header array
|
* @return string[][] associated header array
|
||||||
*/
|
*/
|
||||||
public function getHeaderArray();
|
public function getHeaderArray();
|
||||||
|
|
||||||
|
@ -62,6 +76,8 @@ interface IHTTPResult
|
||||||
public function getRedirectUrl();
|
public function getRedirectUrl();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @see MessageInterface::getBody()
|
||||||
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getBody();
|
public function getBody();
|
||||||
|
|
|
@ -985,7 +985,7 @@ class DFRN
|
||||||
return -9; // timed out
|
return -9; // timed out
|
||||||
}
|
}
|
||||||
|
|
||||||
if (($curl_stat == 503) && (stristr($postResult->getHeader(), 'retry-after'))) {
|
if (($curl_stat == 503) && $postResult->inHeader('retry-after')) {
|
||||||
return -10;
|
return -10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -735,7 +735,8 @@ class OStatus
|
||||||
|
|
||||||
$xml = '';
|
$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();
|
$xml = $curlResult->getBody();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -928,7 +929,8 @@ class OStatus
|
||||||
|
|
||||||
$xml = '';
|
$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);
|
Logger::log('Directly fetched XML for URI ' . $related_uri, Logger::DEBUG);
|
||||||
$xml = $curlResult->getBody();
|
$xml = $curlResult->getBody();
|
||||||
}
|
}
|
||||||
|
|
|
@ -215,7 +215,7 @@ class Salmon
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (($return_code == 503) && (stristr($postResult->getHeader(), 'retry-after'))) {
|
if (($return_code == 503) && $postResult->inHeader('retry-after')) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -232,7 +232,6 @@ class ParseUrl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$header = $curlResult->getHeader();
|
|
||||||
$body = $curlResult->getBody();
|
$body = $curlResult->getBody();
|
||||||
|
|
||||||
if ($do_oembed) {
|
if ($do_oembed) {
|
||||||
|
@ -273,7 +272,7 @@ class ParseUrl
|
||||||
$charset = '';
|
$charset = '';
|
||||||
// Look for a charset, first in headers
|
// Look for a charset, first in headers
|
||||||
// Expected form: Content-Type: text/html; charset=ISO-8859-4
|
// 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)), ';,'));
|
$charset = trim(trim(trim(array_pop($matches)), ';,'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
20
tests/datasets/curl/about.head.php
Normal file
20
tests/datasets/curl/about.head.php
Normal file
|
@ -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",
|
||||||
|
];
|
21
tests/datasets/curl/about.redirect.php
Normal file
21
tests/datasets/curl/about.redirect.php
Normal file
|
@ -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",
|
||||||
|
];
|
|
@ -53,6 +53,7 @@ class CurlResultTest extends TestCase
|
||||||
public function testNormal()
|
public function testNormal()
|
||||||
{
|
{
|
||||||
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
$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');
|
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,7 +66,7 @@ class CurlResultTest extends TestCase
|
||||||
self::assertTrue($curlResult->isSuccess());
|
self::assertTrue($curlResult->isSuccess());
|
||||||
self::assertFalse($curlResult->isTimeout());
|
self::assertFalse($curlResult->isTimeout());
|
||||||
self::assertFalse($curlResult->isRedirectUrl());
|
self::assertFalse($curlResult->isRedirectUrl());
|
||||||
self::assertSame($header, $curlResult->getHeader());
|
self::assertSame($headerArray, $curlResult->getHeaders());
|
||||||
self::assertSame($body, $curlResult->getBody());
|
self::assertSame($body, $curlResult->getBody());
|
||||||
self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
|
self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
|
||||||
self::assertSame('https://test.local', $curlResult->getUrl());
|
self::assertSame('https://test.local', $curlResult->getUrl());
|
||||||
|
@ -80,6 +81,7 @@ class CurlResultTest extends TestCase
|
||||||
public function testRedirect()
|
public function testRedirect()
|
||||||
{
|
{
|
||||||
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
$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');
|
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
||||||
|
|
||||||
|
|
||||||
|
@ -93,7 +95,7 @@ class CurlResultTest extends TestCase
|
||||||
self::assertTrue($curlResult->isSuccess());
|
self::assertTrue($curlResult->isSuccess());
|
||||||
self::assertFalse($curlResult->isTimeout());
|
self::assertFalse($curlResult->isTimeout());
|
||||||
self::assertTrue($curlResult->isRedirectUrl());
|
self::assertTrue($curlResult->isRedirectUrl());
|
||||||
self::assertSame($header, $curlResult->getHeader());
|
self::assertSame($headerArray, $curlResult->getHeaders());
|
||||||
self::assertSame($body, $curlResult->getBody());
|
self::assertSame($body, $curlResult->getBody());
|
||||||
self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
|
self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
|
||||||
self::assertSame('https://test.local/test/it', $curlResult->getUrl());
|
self::assertSame('https://test.local/test/it', $curlResult->getUrl());
|
||||||
|
@ -106,6 +108,7 @@ class CurlResultTest extends TestCase
|
||||||
public function testTimeout()
|
public function testTimeout()
|
||||||
{
|
{
|
||||||
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.head');
|
$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');
|
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
||||||
|
|
||||||
|
|
||||||
|
@ -119,7 +122,7 @@ class CurlResultTest extends TestCase
|
||||||
self::assertFalse($curlResult->isSuccess());
|
self::assertFalse($curlResult->isSuccess());
|
||||||
self::assertTrue($curlResult->isTimeout());
|
self::assertTrue($curlResult->isTimeout());
|
||||||
self::assertFalse($curlResult->isRedirectUrl());
|
self::assertFalse($curlResult->isRedirectUrl());
|
||||||
self::assertSame($header, $curlResult->getHeader());
|
self::assertSame($headerArray, $curlResult->getHeaders());
|
||||||
self::assertSame($body, $curlResult->getBody());
|
self::assertSame($body, $curlResult->getBody());
|
||||||
self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
|
self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
|
||||||
self::assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
|
self::assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
|
||||||
|
@ -134,6 +137,7 @@ class CurlResultTest extends TestCase
|
||||||
public function testRedirectHeader()
|
public function testRedirectHeader()
|
||||||
{
|
{
|
||||||
$header = file_get_contents(__DIR__ . '/../../datasets/curl/about.redirect');
|
$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');
|
$body = file_get_contents(__DIR__ . '/../../datasets/curl/about.body');
|
||||||
|
|
||||||
|
|
||||||
|
@ -146,7 +150,7 @@ class CurlResultTest extends TestCase
|
||||||
self::assertTrue($curlResult->isSuccess());
|
self::assertTrue($curlResult->isSuccess());
|
||||||
self::assertFalse($curlResult->isTimeout());
|
self::assertFalse($curlResult->isTimeout());
|
||||||
self::assertTrue($curlResult->isRedirectUrl());
|
self::assertTrue($curlResult->isRedirectUrl());
|
||||||
self::assertSame($header, $curlResult->getHeader());
|
self::assertSame($headerArray, $curlResult->getHeaders());
|
||||||
self::assertSame($body, $curlResult->getBody());
|
self::assertSame($body, $curlResult->getBody());
|
||||||
self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
|
self::assertSame('text/html; charset=utf-8', $curlResult->getContentType());
|
||||||
self::assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
|
self::assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
|
||||||
|
@ -204,7 +208,7 @@ class CurlResultTest extends TestCase
|
||||||
'url' => 'https://test.local'
|
'url' => 'https://test.local'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
self::assertNotEmpty($curlResult->getHeader());
|
self::assertNotEmpty($curlResult->getHeaders());
|
||||||
self::assertEmpty($curlResult->getHeader('wrongHeader'));
|
self::assertEmpty($curlResult->getHeader('wrongHeader'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue