mirror of
https://github.com/friendica/friendica
synced 2025-04-30 14:24:22 +02:00
Introduce "accept_header" as specific argument to the http client
This commit is contained in:
parent
73c3b21665
commit
e299fc67c8
35 changed files with 150 additions and 150 deletions
|
@ -21,6 +21,7 @@
|
|||
|
||||
namespace Friendica\Network\HTTPClient\Capability;
|
||||
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
|
||||
use GuzzleHttp\Exception\TransferException;
|
||||
|
||||
/**
|
||||
|
@ -35,13 +36,13 @@ interface ICanSendHttpRequests
|
|||
* to preserve cookies from one request to the next.
|
||||
*
|
||||
* @param string $url URL to fetch
|
||||
* @param int $timeout Timeout in seconds, default system config value or 60 seconds
|
||||
* @param string $accept_content supply Accept: header with 'accept_content' as the value
|
||||
* @param int $timeout Timeout in seconds, default system config value or 60 seconds
|
||||
* @param string $cookiejar Path to cookie jar file
|
||||
*
|
||||
* @return string The fetched content
|
||||
*/
|
||||
public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): string;
|
||||
public function fetch(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): string;
|
||||
|
||||
/**
|
||||
* Fetches the whole response of an URL.
|
||||
|
@ -50,72 +51,75 @@ interface ICanSendHttpRequests
|
|||
* all the information collected during the fetch.
|
||||
*
|
||||
* @param string $url URL to fetch
|
||||
* @param int $timeout Timeout in seconds, default system config value or 60 seconds
|
||||
* @param string $accept_content supply Accept: header with 'accept_content' as the value
|
||||
* @param int $timeout Timeout in seconds, default system config value or 60 seconds
|
||||
* @param string $cookiejar Path to cookie jar file
|
||||
*
|
||||
* @return ICanHandleHttpResponses With all relevant information, 'body' contains the actual fetched content.
|
||||
*/
|
||||
public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): ICanHandleHttpResponses;
|
||||
public function fetchFull(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): ICanHandleHttpResponses;
|
||||
|
||||
/**
|
||||
* Send a HEAD to a URL.
|
||||
*
|
||||
* @param string $url URL to fetch
|
||||
* @param array $opts (optional parameters) associative array with:
|
||||
* 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value
|
||||
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
|
||||
* 'cookiejar' => path to cookie jar file
|
||||
* 'header' => header array
|
||||
* @param string $url URL to fetch
|
||||
* @param string $accept_content supply Accept: header with 'accept_content' as the value
|
||||
* @param array $opts (optional parameters) associative array with:
|
||||
* 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value (overrides default parameter)
|
||||
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
|
||||
* 'cookiejar' => path to cookie jar file
|
||||
* 'header' => header array
|
||||
*
|
||||
* @return ICanHandleHttpResponses
|
||||
*/
|
||||
public function head(string $url, array $opts = []): ICanHandleHttpResponses;
|
||||
public function head(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses;
|
||||
|
||||
/**
|
||||
* Send a GET to an URL.
|
||||
* Send a GET to a URL.
|
||||
*
|
||||
* @param string $url URL to fetch
|
||||
* @param array $opts (optional parameters) associative array with:
|
||||
* 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value
|
||||
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
|
||||
* 'cookiejar' => path to cookie jar file
|
||||
* 'header' => header array
|
||||
* 'content_length' => int maximum File content length
|
||||
* @param string $url URL to get
|
||||
* @param string $accept_content supply Accept: header with 'accept_content' as the value
|
||||
* @param array $opts (optional parameters) associative array with:
|
||||
* 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value (overrides default parameter)
|
||||
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
|
||||
* 'cookiejar' => path to cookie jar file
|
||||
* 'header' => header array
|
||||
*
|
||||
* @return ICanHandleHttpResponses
|
||||
*/
|
||||
public function get(string $url, array $opts = []): ICanHandleHttpResponses;
|
||||
public function get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses;
|
||||
|
||||
/**
|
||||
* Sends a HTTP request to a given url
|
||||
*
|
||||
* @param string $method A HTTP request
|
||||
* @param string $url Url to send to
|
||||
* @param array $opts (optional parameters) associative array with:
|
||||
* 'body' => (mixed) setting the body for sending data
|
||||
* 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value
|
||||
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
|
||||
* 'cookiejar' => path to cookie jar file
|
||||
* 'header' => header array
|
||||
* 'content_length' => int maximum File content length
|
||||
* 'auth' => array authentication settings
|
||||
* @param string $method A HTTP request
|
||||
* @param string $url Url to send to
|
||||
* @param string $accept_content supply Accept: header with 'accept_content' as the value
|
||||
* @param array $opts (optional parameters) associative array with:
|
||||
* 'body' => (mixed) setting the body for sending data
|
||||
* 'accept_content' => (string array) supply Accept: header with 'accept_content' as the value (overrides default parameter)
|
||||
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds
|
||||
* 'cookiejar' => path to cookie jar file
|
||||
* 'header' => header array
|
||||
* 'content_length' => int maximum File content length
|
||||
* 'auth' => array authentication settings
|
||||
*
|
||||
* @return ICanHandleHttpResponses
|
||||
*/
|
||||
public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses;
|
||||
public function request(string $method, string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses;
|
||||
|
||||
/**
|
||||
* Send POST request to an URL
|
||||
*
|
||||
* @param string $url URL to post
|
||||
* @param mixed $params array of POST variables
|
||||
* @param array $headers HTTP headers
|
||||
* @param int $timeout The timeout in seconds, default system config value or 60 seconds
|
||||
* @param string $url URL to post
|
||||
* @param mixed $params array of POST variables
|
||||
* @param string $accept_content supply Accept: header with 'accept_content' as the value
|
||||
* @param array $headers HTTP headers
|
||||
* @param int $timeout The timeout in seconds, default system config value or 60 seconds
|
||||
*
|
||||
* @return ICanHandleHttpResponses The content
|
||||
*/
|
||||
public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses;
|
||||
public function post(string $url, $params, string $accept_content = HttpClientAccept::DEFAULT, array $headers = [], int $timeout = 0): ICanHandleHttpResponses;
|
||||
|
||||
/**
|
||||
* Returns the original URL of the provided URL
|
||||
|
|
|
@ -63,7 +63,7 @@ class HttpClient implements ICanSendHttpRequests
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function request(string $method, string $url, array $opts = []): ICanHandleHttpResponses
|
||||
public function request(string $method, string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses
|
||||
{
|
||||
$this->profiler->startRecording('network');
|
||||
$this->logger->debug('Request start.', ['url' => $url, 'method' => $method]);
|
||||
|
@ -141,7 +141,7 @@ class HttpClient implements ICanSendHttpRequests
|
|||
};
|
||||
|
||||
if (empty($conf[HttpClientOptions::HEADERS]['Accept'])) {
|
||||
$conf[HttpClientOptions::HEADERS]['Accept'] = HttpClientAccept::DEFAULT;
|
||||
$conf[HttpClientOptions::HEADERS]['Accept'] = $accept_content;
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -167,23 +167,23 @@ class HttpClient implements ICanSendHttpRequests
|
|||
|
||||
/** {@inheritDoc}
|
||||
*/
|
||||
public function head(string $url, array $opts = []): ICanHandleHttpResponses
|
||||
public function head(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses
|
||||
{
|
||||
return $this->request('head', $url, $opts);
|
||||
return $this->request('head', $url, $accept_content, $opts);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function get(string $url, array $opts = []): ICanHandleHttpResponses
|
||||
public function get(string $url, string $accept_content = HttpClientAccept::DEFAULT, array $opts = []): ICanHandleHttpResponses
|
||||
{
|
||||
return $this->request('get', $url, $opts);
|
||||
return $this->request('get', $url, $accept_content, $opts);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function post(string $url, $params, array $headers = [], int $timeout = 0): ICanHandleHttpResponses
|
||||
public function post(string $url, $params, string $accept_content = HttpClientAccept::DEFAULT, array $headers = [], int $timeout = 0): ICanHandleHttpResponses
|
||||
{
|
||||
$opts = [];
|
||||
|
||||
|
@ -197,7 +197,7 @@ class HttpClient implements ICanSendHttpRequests
|
|||
$opts[HttpClientOptions::TIMEOUT] = $timeout;
|
||||
}
|
||||
|
||||
return $this->request('post', $url, $opts);
|
||||
return $this->request('post', $url, $accept_content, $opts);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -237,9 +237,9 @@ class HttpClient implements ICanSendHttpRequests
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): string
|
||||
public function fetch(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): string
|
||||
{
|
||||
$ret = $this->fetchFull($url, $timeout, $accept_content, $cookiejar);
|
||||
$ret = $this->fetchFull($url, $accept_content, $timeout, $cookiejar);
|
||||
|
||||
return $ret->getBody();
|
||||
}
|
||||
|
@ -247,14 +247,14 @@ class HttpClient implements ICanSendHttpRequests
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = ''): ICanHandleHttpResponses
|
||||
public function fetchFull(string $url, string $accept_content = HttpClientAccept::DEFAULT, int $timeout = 0, string $cookiejar = ''): ICanHandleHttpResponses
|
||||
{
|
||||
return $this->get(
|
||||
$url,
|
||||
$accept_content,
|
||||
[
|
||||
HttpClientOptions::TIMEOUT => $timeout,
|
||||
HttpClientOptions::ACCEPT_CONTENT => $accept_content,
|
||||
HttpClientOptions::COOKIEJAR => $cookiejar
|
||||
HttpClientOptions::TIMEOUT => $timeout,
|
||||
HttpClientOptions::COOKIEJAR => $cookiejar
|
||||
]
|
||||
);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
namespace Friendica\Network\HTTPClient\Client;
|
||||
|
||||
/**
|
||||
* This class contains a list of possible HTTPClient ACCEPT options.
|
||||
*/
|
||||
class HttpClientAccept
|
||||
{
|
||||
/** @var string Default value for "Accept" header */
|
||||
|
@ -20,4 +23,5 @@ class HttpClientAccept
|
|||
public const TEXT = 'text/plain,text/*;q=0.9,*/*;q=0.8';
|
||||
public const VIDEO = 'video/mp4,video/*;q=0.9,*/*;q=0.8';
|
||||
public const XRD_XML = 'application/xrd+xml,text/xml;q=0.9,*/*;q=0.8';
|
||||
public const XML = 'application/xml,text/xml;q=0.9,*/*;q=0.8';
|
||||
}
|
||||
|
|
|
@ -170,7 +170,7 @@ class Probe
|
|||
Logger::info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url, 'callstack' => System::callstack(20)]);
|
||||
$xrd = null;
|
||||
|
||||
$curlResult = DI::httpClient()->get($ssl_url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::XRD_XML]);
|
||||
$curlResult = DI::httpClient()->get($ssl_url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
|
||||
$ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
|
||||
if ($curlResult->isSuccess()) {
|
||||
$xml = $curlResult->getBody();
|
||||
|
@ -187,7 +187,7 @@ class Probe
|
|||
}
|
||||
|
||||
if (!is_object($xrd) && !empty($url)) {
|
||||
$curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::XRD_XML]);
|
||||
$curlResult = DI::httpClient()->get($url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
|
||||
$connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
|
||||
if ($curlResult->isTimeout()) {
|
||||
Logger::info('Probing timeout', ['url' => $url]);
|
||||
|
@ -429,7 +429,7 @@ class Probe
|
|||
*/
|
||||
private static function getHideStatus($url)
|
||||
{
|
||||
$curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000, HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]);
|
||||
$curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::CONTENT_LENGTH => 1000000]);
|
||||
if (!$curlResult->isSuccess()) {
|
||||
return false;
|
||||
}
|
||||
|
@ -834,7 +834,7 @@ class Probe
|
|||
|
||||
public static function pollZot($url, $data)
|
||||
{
|
||||
$curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]);
|
||||
$curlResult = DI::httpClient()->get($url, HttpClientAccept::JSON);
|
||||
if ($curlResult->isTimeout()) {
|
||||
return $data;
|
||||
}
|
||||
|
@ -931,7 +931,7 @@ class Probe
|
|||
{
|
||||
$xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
|
||||
|
||||
$curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => $type]);
|
||||
$curlResult = DI::httpClient()->get($url, $type, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
|
||||
if ($curlResult->isTimeout()) {
|
||||
self::$istimeout = true;
|
||||
return [];
|
||||
|
@ -1000,7 +1000,7 @@ class Probe
|
|||
*/
|
||||
private static function pollNoscrape($noscrape_url, $data)
|
||||
{
|
||||
$curlResult = DI::httpClient()->get($noscrape_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]);
|
||||
$curlResult = DI::httpClient()->get($noscrape_url, HttpClientAccept::JSON);
|
||||
if ($curlResult->isTimeout()) {
|
||||
self::$istimeout = true;
|
||||
return $data;
|
||||
|
@ -1266,7 +1266,7 @@ class Probe
|
|||
*/
|
||||
private static function pollHcard($hcard_url, $data, $dfrn = false)
|
||||
{
|
||||
$curlResult = DI::httpClient()->get($hcard_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]);
|
||||
$curlResult = DI::httpClient()->get($hcard_url, HttpClientAccept::HTML);
|
||||
if ($curlResult->isTimeout()) {
|
||||
self::$istimeout = true;
|
||||
return [];
|
||||
|
@ -1527,7 +1527,7 @@ class Probe
|
|||
$pubkey = substr($pubkey, 5);
|
||||
}
|
||||
} elseif (Strings::normaliseLink($pubkey) == 'http://') {
|
||||
$curlResult = DI::httpClient()->get($pubkey, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::MAGIC_KEY]);
|
||||
$curlResult = DI::httpClient()->get($pubkey, HttpClientAccept::MAGIC_KEY);
|
||||
if ($curlResult->isTimeout()) {
|
||||
self::$istimeout = true;
|
||||
return $short ? false : [];
|
||||
|
@ -1562,7 +1562,7 @@ class Probe
|
|||
}
|
||||
|
||||
// Fetch all additional data from the feed
|
||||
$curlResult = DI::httpClient()->get($data["poll"], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::FEED_XML]);
|
||||
$curlResult = DI::httpClient()->get($data["poll"], HttpClientAccept::FEED_XML);
|
||||
if ($curlResult->isTimeout()) {
|
||||
self::$istimeout = true;
|
||||
return [];
|
||||
|
@ -1614,7 +1614,7 @@ class Probe
|
|||
*/
|
||||
private static function pumpioProfileData($profile_link)
|
||||
{
|
||||
$curlResult = DI::httpClient()->get($profile_link, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]);
|
||||
$curlResult = DI::httpClient()->get($profile_link, HttpClientAccept::HTML);
|
||||
if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
|
||||
return [];
|
||||
}
|
||||
|
@ -1827,7 +1827,7 @@ class Probe
|
|||
*/
|
||||
private static function feed($url, $probe = true)
|
||||
{
|
||||
$curlResult = DI::httpClient()->get($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::FEED_XML]);
|
||||
$curlResult = DI::httpClient()->get($url, HttpClientAccept::FEED_XML);
|
||||
if ($curlResult->isTimeout()) {
|
||||
self::$istimeout = true;
|
||||
return [];
|
||||
|
@ -2049,7 +2049,7 @@ class Probe
|
|||
return '';
|
||||
}
|
||||
|
||||
$curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON]);
|
||||
$curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick'], HttpClientAccept::JSON);
|
||||
|
||||
if ($curlResult->isSuccess() && !empty($curlResult->getBody())) {
|
||||
$noscrape = json_decode($curlResult->getBody(), true);
|
||||
|
@ -2124,7 +2124,7 @@ class Probe
|
|||
private static function updateFromFeed(array $data)
|
||||
{
|
||||
// Search for the newest entry in the feed
|
||||
$curlResult = DI::httpClient()->get($data['poll'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::ATOM_XML]);
|
||||
$curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::ATOM_XML);
|
||||
if (!$curlResult->isSuccess() || !$curlResult->getBody()) {
|
||||
return '';
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue