Merge pull request #9405 from nupplaphil/task/psr7_remove_binary

Remove $binary flag for HTTPRequest
This commit is contained in:
Michael Vogel 2020-10-18 21:19:27 +02:00 committed by GitHub
commit bfbf8816e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 31 additions and 40 deletions

View file

@ -85,7 +85,7 @@ function parse_url_content(App $a)
// Check if the URL is an image, video or audio file. If so format // Check if the URL is an image, video or audio file. If so format
// the URL with the corresponding BBCode media tag // the URL with the corresponding BBCode media tag
// Fetch the header of the URL // Fetch the header of the URL
$curlResponse = DI::httpRequest()->get($url, false, ['novalidate' => true, 'nobody' => true]); $curlResponse = DI::httpRequest()->get($url, ['novalidate' => true, 'nobody' => true]);
if ($curlResponse->isSuccess()) { if ($curlResponse->isSuccess()) {
// Convert the header fields into an array // Convert the header fields into an array

View file

@ -95,7 +95,7 @@ class OEmbed
if (!in_array($ext, $noexts)) { if (!in_array($ext, $noexts)) {
// try oembed autodiscovery // try oembed autodiscovery
$html_text = DI::httpRequest()->fetch($embedurl, false, 15, 'text/*'); $html_text = DI::httpRequest()->fetch($embedurl, 15, 'text/*');
if ($html_text) { if ($html_text) {
$dom = @DOMDocument::loadHTML($html_text); $dom = @DOMDocument::loadHTML($html_text);
if ($dom) { if ($dom) {

View file

@ -485,7 +485,7 @@ class BBCode
continue; continue;
} }
$curlResult = DI::httpRequest()->get($mtch[1], true); $curlResult = DI::httpRequest()->get($mtch[1]);
if (!$curlResult->isSuccess()) { if (!$curlResult->isSuccess()) {
continue; continue;
} }

View file

@ -122,7 +122,7 @@ class Search
$searchUrl .= '&page=' . $page; $searchUrl .= '&page=' . $page;
} }
$resultJson = DI::httpRequest()->fetch($searchUrl, false, 0, 'application/json'); $resultJson = DI::httpRequest()->fetch($searchUrl, 0, 'application/json');
$results = json_decode($resultJson, true); $results = json_decode($resultJson, true);
@ -264,7 +264,7 @@ class Search
$return = Contact::searchByName($search, $mode); $return = Contact::searchByName($search, $mode);
} else { } else {
$p = $page > 1 ? 'p=' . $page : ''; $p = $page > 1 ? 'p=' . $page : '';
$curlResult = DI::httpRequest()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), false, ['accept_content' => 'application/json']); $curlResult = DI::httpRequest()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), ['accept_content' => 'application/json']);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$searchResult = json_decode($curlResult->getBody(), true); $searchResult = json_decode($curlResult->getBody(), true);
if (!empty($searchResult['profiles'])) { if (!empty($searchResult['profiles'])) {

View file

@ -1064,7 +1064,7 @@ class Worker
} }
$url = DI::baseUrl() . '/worker'; $url = DI::baseUrl() . '/worker';
DI::httpRequest()->fetch($url, false, 1); DI::httpRequest()->fetch($url, 1);
} }
/** /**

View file

@ -311,7 +311,7 @@ class GServer
// When a nodeinfo is present, we don't need to dig further // When a nodeinfo is present, we don't need to dig further
$xrd_timeout = DI::config()->get('system', 'xrd_timeout'); $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
$curlResult = DI::httpRequest()->get($url . '/.well-known/nodeinfo', false, ['timeout' => $xrd_timeout]); $curlResult = DI::httpRequest()->get($url . '/.well-known/nodeinfo', ['timeout' => $xrd_timeout]);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
self::setFailure($url); self::setFailure($url);
return false; return false;
@ -344,7 +344,7 @@ class GServer
$basedata = ['detection-method' => self::DETECT_MANUAL]; $basedata = ['detection-method' => self::DETECT_MANUAL];
} }
$curlResult = DI::httpRequest()->get($baseurl, false, ['timeout' => $xrd_timeout]); $curlResult = DI::httpRequest()->get($baseurl, ['timeout' => $xrd_timeout]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$basedata = self::analyseRootHeader($curlResult, $basedata); $basedata = self::analyseRootHeader($curlResult, $basedata);
$basedata = self::analyseRootBody($curlResult, $basedata, $baseurl); $basedata = self::analyseRootBody($curlResult, $basedata, $baseurl);
@ -361,7 +361,7 @@ class GServer
// When the base path doesn't seem to contain a social network we try the complete path. // When the base path doesn't seem to contain a social network we try the complete path.
// Most detectable system have to be installed in the root directory. // Most detectable system have to be installed in the root directory.
// We checked the base to avoid false positives. // We checked the base to avoid false positives.
$curlResult = DI::httpRequest()->get($url, false, ['timeout' => $xrd_timeout]); $curlResult = DI::httpRequest()->get($url, ['timeout' => $xrd_timeout]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$urldata = self::analyseRootHeader($curlResult, $serverdata); $urldata = self::analyseRootHeader($curlResult, $serverdata);
$urldata = self::analyseRootBody($curlResult, $urldata, $url); $urldata = self::analyseRootBody($curlResult, $urldata, $url);
@ -913,7 +913,7 @@ class GServer
private static function validHostMeta(string $url) private static function validHostMeta(string $url)
{ {
$xrd_timeout = DI::config()->get('system', 'xrd_timeout'); $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
$curlResult = DI::httpRequest()->get($url . '/.well-known/host-meta', false, ['timeout' => $xrd_timeout]); $curlResult = DI::httpRequest()->get($url . '/.well-known/host-meta', ['timeout' => $xrd_timeout]);
if (!$curlResult->isSuccess()) { if (!$curlResult->isSuccess()) {
return false; return false;
} }
@ -1639,7 +1639,7 @@ class GServer
if (!empty($accesstoken)) { if (!empty($accesstoken)) {
$api = 'https://instances.social/api/1.0/instances/list?count=0'; $api = 'https://instances.social/api/1.0/instances/list?count=0';
$header = ['Authorization: Bearer '.$accesstoken]; $header = ['Authorization: Bearer '.$accesstoken];
$curlResult = DI::httpRequest()->get($api, false, ['headers' => $header]); $curlResult = DI::httpRequest()->get($api, ['headers' => $header]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$servers = json_decode($curlResult->getBody(), true); $servers = json_decode($curlResult->getBody(), true);

View file

@ -422,7 +422,7 @@ class Photo
$filename = basename($image_url); $filename = basename($image_url);
if (!empty($image_url)) { if (!empty($image_url)) {
$ret = DI::httpRequest()->get($image_url, true); $ret = DI::httpRequest()->get($image_url);
$img_str = $ret->getBody(); $img_str = $ret->getBody();
$type = $ret->getContentType(); $type = $ret->getContentType();
} else { } else {

View file

@ -1002,7 +1002,7 @@ class User
$photo_failure = false; $photo_failure = false;
$filename = basename($photo); $filename = basename($photo);
$curlResult = DI::httpRequest()->get($photo, true); $curlResult = DI::httpRequest()->get($photo);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$img_str = $curlResult->getBody(); $img_str = $curlResult->getBody();
$type = $curlResult->getContentType(); $type = $curlResult->getContentType();

View file

@ -100,7 +100,7 @@ class Magic extends BaseModule
); );
// Try to get an authentication token from the other instance. // Try to get an authentication token from the other instance.
$curlResult = DI::httpRequest()->get($basepath . '/owa', false, ['headers' => $headers]); $curlResult = DI::httpRequest()->get($basepath . '/owa', ['headers' => $headers]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$j = json_decode($curlResult->getBody(), true); $j = json_decode($curlResult->getBody(), true);

View file

@ -59,7 +59,7 @@ class HTTPRequest implements IHTTPRequest
* *
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public function get(string $url, bool $binary = false, array $opts = [], int &$redirects = 0) public function get(string $url, array $opts = [], int &$redirects = 0)
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
@ -172,12 +172,7 @@ class HTTPRequest implements IHTTPRequest
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
} }
if ($binary) { $logger = $this->logger;
@curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
}
// don't let curl abort the entire application
// if it throws any errors.
$s = @curl_exec($ch); $s = @curl_exec($ch);
$curl_info = @curl_getinfo($ch); $curl_info = @curl_getinfo($ch);
@ -196,7 +191,7 @@ class HTTPRequest implements IHTTPRequest
$redirects++; $redirects++;
$this->logger->notice('Curl redirect.', ['url' => $url, 'to' => $curlResponse->getRedirectUrl()]); $this->logger->notice('Curl redirect.', ['url' => $url, 'to' => $curlResponse->getRedirectUrl()]);
@curl_close($ch); @curl_close($ch);
return $this->get($curlResponse->getRedirectUrl(), $binary, $opts, $redirects); return $this->get($curlResponse->getRedirectUrl(), $opts, $redirects);
} }
@curl_close($ch); @curl_close($ch);
@ -435,9 +430,9 @@ class HTTPRequest implements IHTTPRequest
* *
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public function fetch(string $url, bool $binary = false, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0) public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0)
{ {
$ret = $this->fetchFull($url, $binary, $timeout, $accept_content, $cookiejar, $redirects); $ret = $this->fetchFull($url, $timeout, $accept_content, $cookiejar, $redirects);
return $ret->getBody(); return $ret->getBody();
} }
@ -449,11 +444,10 @@ class HTTPRequest implements IHTTPRequest
* *
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public function fetchFull(string $url, bool $binary = false, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0) public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0)
{ {
return $this->get( return $this->get(
$url, $url,
$binary,
[ [
'timeout' => $timeout, 'timeout' => $timeout,
'accept_content' => $accept_content, 'accept_content' => $accept_content,

View file

@ -34,7 +34,6 @@ interface IHTTPRequest
* to preserve cookies from one request to the next. * to preserve cookies from one request to the next.
* *
* @param string $url URL to fetch * @param string $url URL to fetch
* @param bool $binary default false
* TRUE if asked to return binary results (file download) * TRUE if asked to return binary results (file download)
* @param int $timeout Timeout in seconds, default system config value or 60 seconds * @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 string $accept_content supply Accept: header with 'accept_content' as the value
@ -42,7 +41,7 @@ interface IHTTPRequest
* *
* @return string The fetched content * @return string The fetched content
*/ */
public function fetch(string $url, bool $binary = false, int $timeout = 0, string $accept_content = '', string $cookiejar = ''); public function fetch(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '');
/** /**
* Fetches the whole response of an URL. * Fetches the whole response of an URL.
@ -51,7 +50,6 @@ interface IHTTPRequest
* all the information collected during the fetch. * all the information collected during the fetch.
* *
* @param string $url URL to fetch * @param string $url URL to fetch
* @param bool $binary default false
* TRUE if asked to return binary results (file download) * TRUE if asked to return binary results (file download)
* @param int $timeout Timeout in seconds, default system config value or 60 seconds * @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 string $accept_content supply Accept: header with 'accept_content' as the value
@ -59,13 +57,12 @@ interface IHTTPRequest
* *
* @return CurlResult With all relevant information, 'body' contains the actual fetched content. * @return CurlResult With all relevant information, 'body' contains the actual fetched content.
*/ */
public function fetchFull(string $url, bool $binary = false, int $timeout = 0, string $accept_content = '', string $cookiejar = ''); public function fetchFull(string $url, int $timeout = 0, string $accept_content = '', string $cookiejar = '');
/** /**
* Send a GET to an URL. * Send a GET to an URL.
* *
* @param string $url URL to fetch * @param string $url URL to fetch
* @param bool $binary default false
* TRUE if asked to return binary results (file download) * TRUE if asked to return binary results (file download)
* @param array $opts (optional parameters) assoziative array with: * @param array $opts (optional parameters) assoziative array with:
* 'accept_content' => supply Accept: header with 'accept_content' as the value * 'accept_content' => supply Accept: header with 'accept_content' as the value
@ -78,7 +75,7 @@ interface IHTTPRequest
* *
* @return CurlResult * @return CurlResult
*/ */
public function get(string $url, bool $binary = false, array $opts = []); public function get(string $url, array $opts = []);
/** /**
* Send POST request to an URL * Send POST request to an URL

View file

@ -169,7 +169,7 @@ class Probe
Logger::info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url, 'callstack' => System::callstack(20)]); Logger::info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url, 'callstack' => System::callstack(20)]);
$xrd = null; $xrd = null;
$curlResult = DI::httpRequest()->get($ssl_url, false, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']); $curlResult = DI::httpRequest()->get($ssl_url, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
$ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0); $ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$xml = $curlResult->getBody(); $xml = $curlResult->getBody();
@ -186,7 +186,7 @@ class Probe
} }
if (!is_object($xrd) && !empty($url)) { if (!is_object($xrd) && !empty($url)) {
$curlResult = DI::httpRequest()->get($url, false, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']); $curlResult = DI::httpRequest()->get($url, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
$connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0); $connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
Logger::info('Probing timeout', ['url' => $url]); Logger::info('Probing timeout', ['url' => $url]);
@ -941,7 +941,7 @@ class Probe
{ {
$xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20); $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
$curlResult = DI::httpRequest()->get($url, false, ['timeout' => $xrd_timeout, 'accept_content' => $type]); $curlResult = DI::httpRequest()->get($url, ['timeout' => $xrd_timeout, 'accept_content' => $type]);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
self::$istimeout = true; self::$istimeout = true;
return []; return [];

View file

@ -738,7 +738,7 @@ class OStatus
self::$conv_list[$conversation] = true; self::$conv_list[$conversation] = true;
$curlResult = DI::httpRequest()->get($conversation, false, ['accept_content' => 'application/atom+xml, text/html']); $curlResult = DI::httpRequest()->get($conversation, ['accept_content' => 'application/atom+xml, text/html']);
if (!$curlResult->isSuccess()) { if (!$curlResult->isSuccess()) {
return; return;
@ -931,7 +931,7 @@ class OStatus
} }
$stored = false; $stored = false;
$curlResult = DI::httpRequest()->get($related, false, ['accept_content' => 'application/atom+xml, text/html']); $curlResult = DI::httpRequest()->get($related, ['accept_content' => 'application/atom+xml, text/html']);
if (!$curlResult->isSuccess()) { if (!$curlResult->isSuccess()) {
return; return;

View file

@ -449,7 +449,7 @@ class HTTPSignature
$curl_opts = $opts; $curl_opts = $opts;
$curl_opts['header'] = $headers; $curl_opts['header'] = $headers;
$curlResult = DI::httpRequest()->get($request, false, $curl_opts); $curlResult = DI::httpRequest()->get($request, $curl_opts);
$return_code = $curlResult->getReturnCode(); $return_code = $curlResult->getReturnCode();
Logger::log('Fetched for user ' . $uid . ' from ' . $request . ' returned ' . $return_code, Logger::DEBUG); Logger::log('Fetched for user ' . $uid . ' from ' . $request . ' returned ' . $return_code, Logger::DEBUG);

View file

@ -184,7 +184,7 @@ class Images
return $data; return $data;
} }
$img_str = DI::httpRequest()->fetch($url, true, 4); $img_str = DI::httpRequest()->fetch($url, 4);
if (!$img_str) { if (!$img_str) {
return []; return [];

View file

@ -444,7 +444,7 @@ class OnePoll
} }
$cookiejar = tempnam(get_temppath(), 'cookiejar-onepoll-'); $cookiejar = tempnam(get_temppath(), 'cookiejar-onepoll-');
$curlResult = DI::httpRequest()->get($contact['poll'], false, ['cookiejar' => $cookiejar]); $curlResult = DI::httpRequest()->get($contact['poll'], ['cookiejar' => $cookiejar]);
unlink($cookiejar); unlink($cookiejar);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {