diff options
author | Christian Schneider | 2021-01-13 16:13:25 +0100 |
---|---|---|
committer | Christian Schneider | 2021-01-13 16:13:25 +0100 |
commit | 81e5fda87626dfe94d46be511ebc546076b1975b (patch) | |
tree | 9669ecb252500e514ff932df2e1036ab13cf8008 /it_url.class | |
parent | 1f57cc663c83ec44ec292256147f9811945e260a (diff) | |
download | itools-81e5fda87626dfe94d46be511ebc546076b1975b.tar.gz itools-81e5fda87626dfe94d46be511ebc546076b1975b.tar.bz2 itools-81e5fda87626dfe94d46be511ebc546076b1975b.zip |
Unified brace and else if style
Diffstat (limited to 'it_url.class')
-rw-r--r-- | it_url.class | 67 |
1 files changed, 46 insertions, 21 deletions
diff --git a/it_url.class b/it_url.class index dbd39e4..bddbdb8 100644 --- a/it_url.class +++ b/it_url.class @@ -157,11 +157,12 @@ function _get($p = [], $timeout = null) function parse_http_header($header) { - foreach (explode("\n", trim($header)) as $line) { + foreach (explode("\n", trim($header)) as $line) + { $line = trim($line); if (preg_match('#^(HTTP)\S+\s(\d+)#', $line, $parts)) # Parse result code $this->headers[$parts[1]] = $this->result = $parts[2]; - elseif (preg_match('#^([^:]+): (.*)$#', $line, $parts)) + else if (preg_match('#^([^:]+): (.*)$#', $line, $parts)) $this->headers[ucwords($parts[1], '-')] = $parts[2]; if (strtolower($parts[1]) == 'set-cookie' && preg_match('/^([^=]+)=([^;]*)/', $parts[2], $cookie)) $this->cookies[$cookie[1]] = $cookie[2]; @@ -207,7 +208,8 @@ static function curl_opts($p=array()) if ($p['pass'] || $p['user']) $add += [ CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => $p['user'] . ':' . $p['pass'] ]; - if ($p['writefunction']) { + if ($p['writefunction']) + { $add += [ CURLOPT_RETURNTRANSFER => false, CURLOPT_WRITEFUNCTION => $p['writefunction'], @@ -261,13 +263,15 @@ function request($p=array()) $url->headers = array(); $p['headers'] = (array)$p['headers'] + self::_default_headers($url, $p); $opts = self::curl_opts($p + array('user' => $this->user, 'pass' => $this->pass, 'followlocation' => false)); - if ($p['verbose']) { + if ($p['verbose']) + { $stderr = it::fopen("php://memory", "r+"); $opts += [CURLOPT_STDERR => $stderr, CURLOPT_VERBOSE => 1]; } $curl = curl_init($url->url); - if ($p['maxlength'] && !$p['writefunction']) { + if ($p['maxlength'] && !$p['writefunction']) + { $content = ""; $opts[CURLOPT_WRITEFUNCTION] = function ($dummy, $data) use ($p, &$content) { static $total = 0; @@ -294,18 +298,24 @@ function request($p=array()) EDC('curlinfo', curl_getinfo($curl)); - if ($got !== false) { + if ($got !== false) + { $url->header = array_slice(explode("\r\n\r\n", trim($header)), -1)[0] . "\r\n\r\n"; $url->data = $got; $url->parse_http_header($url->header); - if ($p['filemtime'] && ($url->result == 304)) { + if ($p['filemtime'] && ($url->result == 304)) + { $result = true; # Not modified, success but no data - } else if ($url->result == 414) { + } + else if ($url->result == 414) + { it::error((array)$p['it_error'] + ['title' => "Request-URI Too Long: " . substr($url->url, 0, 100) . "...(truncated " . (strlen($url->url) - 100) . " bytes)", 'body' => curl_getinfo($curl) + ($p['verbose'] ? ['verbose' => $this->verbose] : [])]); $this->errstr = "HTTP Status " . $url->result; - } else { + } + else + { $result =& $url->data; $this->errstr = "HTTP Status " . $url->result; } @@ -315,13 +325,15 @@ function request($p=array()) $this->curlinfo = curl_getinfo($curl); } - if ($p['verbose']) { + if ($p['verbose']) + { rewind($stderr); $this->verbose = stream_get_contents($stderr); fclose($stderr); } - if ($got === false && $p['retries'] <= 0) { + if ($got === false && $p['retries'] <= 0) + { it::error((array)$p['it_error'] + ['title' => "problem " . ($p['method'] ?: "gett") . "ing $url->url with curl: (" . curl_errno($curl) . ") " . curl_error($curl), 'body' => curl_getinfo($curl) + ($p['verbose'] ? ['verbose' => $this->verbose] : [])]); } @@ -382,7 +394,8 @@ static function get_multi($p=null) $iterator = $p['urls']; $parallel = $p['parallel'] ?: PHP_INT_MAX; - while (count($handles) < $parallel && $iterator->valid()) { + while (count($handles) < $parallel && $iterator->valid()) + { $addhandle($iterator->key(), $iterator->current()); $iterator->next(); } @@ -414,28 +427,37 @@ static function get_multi($p=null) $content = $p['postprocess']($content, ['it_error' => $retries[$key] < $p['retries'] ? false : (array)$p['it_error'] + ['title' => "invalid content from " . $urls[$key]]]); EDC('reqtimings', $key, $info['result'], (gettimeofday(true) - $start) * 1000); - if ($info['result'] == CURLE_OK && $content !== null) { + if ($info['result'] == CURLE_OK && $content !== null) + { if (!$p['noresults']) $results_unordered[$key] = $content; - if (it::match(self::$retryable, curl_getinfo($handles[$key], CURLINFO_RESPONSE_CODE)) && $retries[$key]++ < $p['retries']) { + if (it::match(self::$retryable, curl_getinfo($handles[$key], CURLINFO_RESPONSE_CODE)) && $retries[$key]++ < $p['retries']) + { $sleepuntils[$key] = microtime(true) + $p['retrysleep']; - } else { + } + else + { if (is_array($urls[$key]) && ($handler = $urls[$key]['handler'])) $abort = $handler($info['handle'], $content); unset($urls[$key]); } $closehandle($key); - } else if($retries[$key]++ < $p['retries']) { + } + else if($retries[$key]++ < $p['retries']) + { $closehandle($key); # closehandle must be called before addhandle as we use the same key $sleepuntils[$key] = microtime(true) + $p['retrysleep']; - } else { + } + else + { $results_unordered[$key] = false; unset($urls[$key]); $closehandle($key); } - if (!$abort && count($handles) < $parallel && $iterator->valid()) { + if (!$abort && count($handles) < $parallel && $iterator->valid()) + { $addhandle($iterator->key(), $iterator->current()); $iterator->next(); } @@ -443,8 +465,10 @@ static function get_multi($p=null) } } while ($mrc == CURLM_CALL_MULTI_PERFORM); - foreach ((array)$sleepuntils as $key => $time) { - if (microtime(true) >= $time && count($handles) < $parallel) { + foreach ((array)$sleepuntils as $key => $time) + { + if (microtime(true) >= $time && count($handles) < $parallel) + { $addhandle($key, $urls[$key]); unset($sleepuntils[$key]); } @@ -800,7 +824,8 @@ static function redirect($url = null, $type = "temporary") $url = preg_replace("/[\r\n].*/", '', it_url::absolute($url)); # Security: cut after CR/LF - if (EDC('noredir')) { + if (EDC('noredir')) + { if (!function_exists('a')) new it_html(); echo a(array('href' => $url), Q($url)) . Q(" (HTTP/1.1 $code, $type redirect)") . br() . Q("Trace: " . it_debug::backtrace()); |