summaryrefslogtreecommitdiff
path: root/it_url.class
diff options
context:
space:
mode:
authorChristian Schneider2020-05-18 09:54:20 +0200
committerChristian Schneider2020-05-18 09:54:20 +0200
commit2393e0b28699675b5c6755a615e439af6ac31b37 (patch)
treeb85fe5d26d7ceea318615fc3af19a3139e9f6f3e /it_url.class
parent78ae466036ed812c298e01dccc6fdffa78e4cfba (diff)
downloaditools-2393e0b28699675b5c6755a615e439af6ac31b37.tar.gz
itools-2393e0b28699675b5c6755a615e439af6ac31b37.tar.bz2
itools-2393e0b28699675b5c6755a615e439af6ac31b37.zip
Add 'assoc' => true mode to it_url::get() and prepare to mark function static
Diffstat (limited to 'it_url.class')
-rw-r--r--it_url.class73
1 files changed, 49 insertions, 24 deletions
diff --git a/it_url.class b/it_url.class
index 5bd796c..85471d4 100644
--- a/it_url.class
+++ b/it_url.class
@@ -72,7 +72,7 @@ function __construct($url = null)
function is_reachable($timeout = 5)
{
$url = new it_url($this->url);
- @$url->get(['method' => 'HEAD', 'totaltimeout' => $timeout]);
+ @$url->_get(['method' => 'HEAD', 'totaltimeout' => $timeout]);
return $url->result >= 200 && $url->result < 400;
}
@@ -93,6 +93,7 @@ static function _postprocess($data, $p)
*
* @param $p parameter array with the following keys
* @param $p['url'] url to get, defaults to constructor URL
+ * @param $p['assoc'] Return [ 'data' => string, 'status' => int, 'cookies' => array, 'headers' => array, 'errstr' => string ] instead of just data
* @param $p['headers'] optional associative array of HTTP headers to send
* @param $p['safety'] DEPRECATED. 0 = ignore errors, 1 = errors, 2 = fatals
* @param $p['it_error'] extra arguments for it_error or false to ignore errors
@@ -108,35 +109,39 @@ static function _postprocess($data, $p)
* @param $p['retrysleep'] Number of seconds to wait before retry (additional to fetchsleep), fractions ok
* @param $p['compression'] use compression (uses curl to do that)
* @param $p['postprocess'] function called with content and $p which has it_error. returns content or null
- * @return contents of resulting page, considering redirects, excluding headers, or false on error
+ * @return Content of resulting page (considering redirects, excluding headers or false on error) or array (empty on error) if 'assoc' => true
*/
-function get($p=null, $timeout=5)
+function get($p = [], $timeout = null)
{
- if (!is_int($timeout))
- it::error("Wrong value for second argument of it_url::get()!");
- if (!is_array($p))
- $p = array('url' => $p, 'timeout' => $timeout);
+ if (isset($this) && $this instanceof it_url) # FIXME 202006 cschneid Compatibility until all application switched to 'assoc' => true or it_ur::get()
+ return $this->_get($p, $timeout);
+ return (new static)->_get($p, $timeout);
+}
+
+/**
+ * Non-static alias for get so we can make get() static
+ */
+function _get($p = [], $timeout = null)
+{
+ if (isset($timeout))
+ it::error("Deprecated second argument of it_url::get()!");
+ if (is_string($p))
+ $p = array('url' => $p, 'timeout' => 5);
$p += array('retries' => 1);
if (($filter = EDC('req')) && ($filter == 1 || strstr($p['url'], "/$filter.")))
ED($p['url'] ?: $this->url);
- if (isset($this) && $this instanceof it_url)
- {
- $url = $this;
- if ($p['url'])
- $this->__construct($p['url']);
- }
- else # called statically
- $url = new it_url($p['url']);
+ if ($p['url'])
+ $this->__construct($p['url']);
- $result = $url->request($p + ['followlocation' => true]);
+ $result = $this->request($p + ['followlocation' => true]);
$result = self::_postprocess($result, $p);
- if (!$result && $p['retries'] > 0 && !it::match('^(4..|204)$', $url->result))
+ if (!$result && $p['retries'] > 0 && !it::match('^(4..|204)$', $this->result))
{
usleep($p['retrysleep']*1000000);
- $result = $url->get(array('retries' => $p['retries'] - 1) + $p);
+ $result = $this->_get(array('retries' => $p['retries'] - 1) + $p);
}
if (($filter = EDC('res')) && strstr($p['url'], it::replace(array('1' => ":"), $filter)))
@@ -144,6 +149,9 @@ function get($p=null, $timeout=5)
usleep($p['fetchsleep'] * 1000000);
+ if ($p['assoc'])
+ $result = $result !== false ? [ 'status' => $this->result, 'data' => $this->data, 'headers' => $this->headers, 'cookies' => $this->cookies, 'errstr' => $this->errstr ] : [];
+
return $result;
}
@@ -524,9 +532,9 @@ static function get_cache($p = array())
EDC('getcache', "new", $filemtime, $p['url'], $path);
$url = new it_url;
- if ($success = $url->get($p + ['checkonly' => true, 'filemtime' => EDC('nocache') ? null : $filemtime])) # => true means not modified (no new data fetched)
+ if ($success = $url->_get($p + ['checkonly' => true, 'filemtime' => EDC('nocache') ? null : $filemtime])) # => true means not modified (no new data fetched)
{
- $newfile = it_url::_atomicwrite($path, $success);
+ $newfile = it_url::_atomicwrite($path, $p['assoc'] ? serialize($success) : $success);
if ($p['returnheaders'])
it::file_put_contents("$path.headers", '<?php return ' . var_export($url->headers, true) . ";\n");
}
@@ -618,11 +626,28 @@ static function get_cache($p = array())
/**
* Fetch a file, cache it and return contents
- * @param see it_url::get_cache()
+ * @param @see it_url::get_cache()
+ * @param $p['assoc'] Return [ 'data' => string, 'status' => int, 'cookies' => array, 'headers' => array, 'errstr' => string ] instead of just data
+ * @return @see it_url::get()
*/
-function get_cache_contents($p)
+static function get_cache_contents($p)
{
- return ($fn = self::get_cache($p)) ? self::_postprocess(it::file_get_contents($fn), $p) : it::error((array)$p['it_error'] + ['title' => $p['safety'] == 0 ? false : "failed getting " . it_url::absolute($p['url']), 'body' => $p]);
+ if ($fn = self::get_cache($p))
+ {
+ $result = it::file_get_contents($fn);
+ if ($p['assoc'])
+ {
+ $response = unserialize($result);
+ $response['data'] = self::_postprocess($response['data'], $p);
+ $result = $response;
+ }
+ else
+ $result = self::_postprocess($result, $p);
+ }
+ else
+ $result = it::error((array)$p['it_error'] + ['title' => $p['safety'] == 0 ? false : "failed getting " . it_url::absolute($p['url']), 'body' => $p]);
+
+ return $result;
}
/**
@@ -671,7 +696,7 @@ static function _unlock($path, $lock)
/**
* Wait for file which is currently locked
* @param $path File to wait for
- * @param $p Wait parameters, see @get_cache
+ * @param $p Wait parameters, @see get_cache()
* @return Whether lock was released within timeout and file is still there
*/
static function _waitforlockedfile($path, $p)