Move internal recursion-counter to the end of all cur/fetchUrl parameters

This commit is contained in:
Philipp Holzer 2019-06-10 14:34:54 +02:00
parent 10683a43c8
commit cc7444d84d
No known key found for this signature in database
GPG Key ID: D8365C3D36B77D90
15 changed files with 34 additions and 38 deletions

View File

@ -209,7 +209,7 @@ function dfrn_confirm_post(App $a, $handsfree = null)
* *
*/ */
$res = Network::post($dfrn_confirm, $params, null, $redirects, 120)->getBody(); $res = Network::post($dfrn_confirm, $params, null, 120)->getBody();
Logger::log(' Confirm: received data: ' . $res, Logger::DATA); Logger::log(' Confirm: received data: ' . $res, Logger::DATA);

View File

@ -70,9 +70,8 @@ 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
$redirects = 0;
// Fetch the header of the URL // Fetch the header of the URL
$curlResponse = Network::curl($url, false, $redirects, ['novalidate' => true, 'nobody' => true]); $curlResponse = Network::curl($url, false, ['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

@ -83,8 +83,7 @@ class OEmbed
if (!in_array($ext, $noexts)) { if (!in_array($ext, $noexts)) {
// try oembed autodiscovery // try oembed autodiscovery
$redirects = 0; $html_text = Network::fetchUrl($embedurl, false, 15, 'text/*');
$html_text = Network::fetchUrl($embedurl, false, $redirects, 15, 'text/*');
if ($html_text) { if ($html_text) {
$dom = @DOMDocument::loadHTML($html_text); $dom = @DOMDocument::loadHTML($html_text);
if ($dom) { if ($dom) {

View File

@ -107,8 +107,7 @@ class Search extends BaseObject
$searchUrl .= '&page=' . $page; $searchUrl .= '&page=' . $page;
} }
$red = 0; $resultJson = Network::fetchUrl($searchUrl, false, 0, 'application/json');
$resultJson = Network::fetchUrl($searchUrl, false, $red, 0, 'application/json');
$results = json_decode($resultJson, true); $results = json_decode($resultJson, true);

View File

@ -983,7 +983,7 @@ class Worker
} }
$url = System::baseUrl()."/worker"; $url = System::baseUrl()."/worker";
Network::fetchUrl($url, false, $redirects, 1); Network::fetchUrl($url, false, 1);
} }
/** /**

View File

@ -34,7 +34,7 @@ class APContact extends BaseObject
$webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr); $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
$curlResult = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']); $curlResult = Network::curl($webfinger, false, ['accept_content' => 'application/jrd+json,application/json']);
if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
return false; return false;
} }

View File

@ -85,7 +85,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 = Network::curl($basepath . '/owa', false, $redirects, ['headers' => $headers]); $curlResult = Network::curl($basepath . '/owa', false, ['headers' => $headers]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$j = json_decode($curlResult->getBody(), true); $j = json_decode($curlResult->getBody(), true);

View File

@ -109,12 +109,11 @@ class Probe
$url = "http://".$host."/.well-known/host-meta"; $url = "http://".$host."/.well-known/host-meta";
$xrd_timeout = Config::get('system', 'xrd_timeout', 20); $xrd_timeout = Config::get('system', 'xrd_timeout', 20);
$redirects = 0;
Logger::log("Probing for ".$host, Logger::DEBUG); Logger::log("Probing for ".$host, Logger::DEBUG);
$xrd = null; $xrd = null;
$curlResult = Network::curl($ssl_url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']); $curlResult = Network::curl($ssl_url, false, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$xml = $curlResult->getBody(); $xml = $curlResult->getBody();
$xrd = XML::parseString($xml, false); $xrd = XML::parseString($xml, false);
@ -122,7 +121,7 @@ class Probe
} }
if (!is_object($xrd)) { if (!is_object($xrd)) {
$curlResult = Network::curl($url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']); $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
Logger::log("Probing timeout for " . $url, Logger::DEBUG); Logger::log("Probing timeout for " . $url, Logger::DEBUG);
self::$istimeout = true; self::$istimeout = true;
@ -738,9 +737,8 @@ class Probe
private static function webfinger($url, $type) private static function webfinger($url, $type)
{ {
$xrd_timeout = Config::get('system', 'xrd_timeout', 20); $xrd_timeout = Config::get('system', 'xrd_timeout', 20);
$redirects = 0;
$curlResult = Network::curl($url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => $type]); $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout, 'accept_content' => $type]);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
self::$istimeout = true; self::$istimeout = true;
return false; return false;

View File

@ -781,7 +781,7 @@ class Image
$data = Cache::get($url); $data = Cache::get($url);
if (is_null($data) || !$data || !is_array($data)) { if (is_null($data) || !$data || !is_array($data)) {
$img_str = Network::fetchUrl($url, true, $redirects, 4); $img_str = Network::fetchUrl($url, true, 4);
if (!$img_str) { if (!$img_str) {
return false; return false;

View File

@ -74,7 +74,7 @@ class ActivityPub
return HTTPSignature::fetch($url, $uid); return HTTPSignature::fetch($url, $uid);
} }
$curlResult = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']); $curlResult = Network::curl($url, false, ['accept_content' => 'application/activity+json, application/ld+json']);
if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
return false; return false;
} }

View File

@ -738,7 +738,7 @@ class OStatus
self::$conv_list[$conversation] = true; self::$conv_list[$conversation] = true;
$curlResult = Network::curl($conversation, false, $redirects, ['accept_content' => 'application/atom+xml, text/html']); $curlResult = Network::curl($conversation, false, ['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 = Network::curl($related, false, $redirects, ['accept_content' => 'application/atom+xml, text/html']); $curlResult = Network::curl($related, false, ['accept_content' => 'application/atom+xml, text/html']);
if (!$curlResult->isSuccess()) { if (!$curlResult->isSuccess()) {
return; return;

View File

@ -1004,7 +1004,7 @@ class PortableContact
$server_url = str_replace("http://", "https://", $server_url); $server_url = str_replace("http://", "https://", $server_url);
// We set the timeout to 20 seconds since this operation should be done in no time if the server was vital // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
$curlResult = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]); $curlResult = Network::curl($server_url."/.well-known/host-meta", false, ['timeout' => 20]);
// Quit if there is a timeout. // Quit if there is a timeout.
// But we want to make sure to only quit if we are mostly sure that this server url fits. // But we want to make sure to only quit if we are mostly sure that this server url fits.
@ -1021,7 +1021,7 @@ class PortableContact
$server_url = str_replace("https://", "http://", $server_url); $server_url = str_replace("https://", "http://", $server_url);
// We set the timeout to 20 seconds since this operation should be done in no time if the server was vital // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
$curlResult = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]); $curlResult = Network::curl($server_url."/.well-known/host-meta", false, ['timeout' => 20]);
// Quit if there is a timeout // Quit if there is a timeout
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
@ -1624,7 +1624,7 @@ class PortableContact
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 = Network::curl($api, false, $redirects, ['headers' => $header]); $curlResult = Network::curl($api, false, ['headers' => $header]);
if ($curlResult->isSuccess()) { if ($curlResult->isSuccess()) {
$servers = json_decode($curlResult->getBody(), true); $servers = json_decode($curlResult->getBody(), true);

View File

@ -455,7 +455,7 @@ class HTTPSignature
$curl_opts = $opts; $curl_opts = $opts;
$curl_opts['header'] = $headers; $curl_opts['header'] = $headers;
$curlResult = Network::curl($request, false, $redirects, $curl_opts); $curlResult = Network::curl($request, false, $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

@ -25,17 +25,17 @@ class Network
* @param string $url URL to fetch * @param string $url URL to fetch
* @param bool $binary default false * @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 $redirects The recursion counter for internal use - default 0
* @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
* @param string $cookiejar Path to cookie jar file * @param string $cookiejar Path to cookie jar file
* @param int $redirects The recursion counter for internal use - default 0
* *
* @return string The fetched content * @return string The fetched content
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function fetchUrl(string $url, bool $binary = false, int &$redirects = 0, int $timeout = 0, string $accept_content = null, string $cookiejar = '') public static function fetchUrl(string $url, bool $binary = false, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0)
{ {
$ret = self::fetchUrlFull($url, $binary, $redirects, $timeout, $accept_content, $cookiejar); $ret = self::fetchUrlFull($url, $binary, $timeout, $accept_content, $cookiejar, $redirects);
return $ret->getBody(); return $ret->getBody();
} }
@ -50,24 +50,25 @@ class Network
* @param string $url URL to fetch * @param string $url URL to fetch
* @param bool $binary default false * @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 $redirects The recursion counter for internal use - default 0
* @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
* @param string $cookiejar Path to cookie jar file * @param string $cookiejar Path to cookie jar file
* @param int $redirects The recursion counter for internal use - default 0
* *
* @return CurlResult With all relevant information, 'body' contains the actual fetched content. * @return CurlResult With all relevant information, 'body' contains the actual fetched content.
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function fetchUrlFull(string $url, bool $binary = false, int &$redirects = 0, int $timeout = 0, string $accept_content = null, string $cookiejar = '') public static function fetchUrlFull(string $url, bool $binary = false, int $timeout = 0, string $accept_content = '', string $cookiejar = '', int &$redirects = 0)
{ {
return self::curl( return self::curl(
$url, $url,
$binary, $binary,
$redirects, [
['timeout'=>$timeout, 'timeout' => $timeout,
'accept_content' => $accept_content, 'accept_content' => $accept_content,
'cookiejar' => $cookiejar 'cookiejar' => $cookiejar
] ],
$redirects
); );
} }
@ -77,7 +78,6 @@ class Network
* @param string $url URL to fetch * @param string $url URL to fetch
* @param bool $binary default false * @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 $redirects The recursion counter for internal use - default 0
* @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
* 'timeout' => int Timeout in seconds, default system config value or 60 seconds * 'timeout' => int Timeout in seconds, default system config value or 60 seconds
@ -86,11 +86,12 @@ class Network
* 'nobody' => only return the header * 'nobody' => only return the header
* 'cookiejar' => path to cookie jar file * 'cookiejar' => path to cookie jar file
* 'header' => header array * 'header' => header array
* @param int $redirects The recursion counter for internal use - default 0
* *
* @return CurlResult * @return CurlResult
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function curl(string $url, bool $binary = false, int &$redirects = 0, array $opts = []) public static function curl(string $url, bool $binary = false, array $opts = [], int &$redirects = 0)
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
@ -227,7 +228,7 @@ class Network
$redirects++; $redirects++;
Logger::log('curl: redirect ' . $url . ' to ' . $curlResponse->getRedirectUrl()); Logger::log('curl: redirect ' . $url . ' to ' . $curlResponse->getRedirectUrl());
@curl_close($ch); @curl_close($ch);
return self::curl($curlResponse->getRedirectUrl(), $binary, $redirects, $opts); return self::curl($curlResponse->getRedirectUrl(), $binary, $opts, $redirects);
} }
@curl_close($ch); @curl_close($ch);
@ -249,7 +250,7 @@ class Network
* @return CurlResult The content * @return CurlResult The content
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function post(string $url, $params, string $headers = null, int &$redirects = 0, int $timeout = 0) public static function post(string $url, $params, string $headers = null, int $timeout = 0, int &$redirects = 0)
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);

View File

@ -371,7 +371,7 @@ class OnePoll
} }
$cookiejar = tempnam(get_temppath(), 'cookiejar-onepoll-'); $cookiejar = tempnam(get_temppath(), 'cookiejar-onepoll-');
$curlResult = Network::curl($contact['poll'], false, $redirects, ['cookiejar' => $cookiejar]); $curlResult = Network::curl($contact['poll'], false, ['cookiejar' => $cookiejar]);
unlink($cookiejar); unlink($cookiejar);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {