diff options
author | Christian Schneider | 2024-12-02 12:29:58 +0100 |
---|---|---|
committer | Christian Schneider | 2024-12-02 12:29:58 +0100 |
commit | 15b450b9405529ae943bb7761e76b7adc5cda8e6 (patch) | |
tree | 171b9ba95f7de7f14d28c52e2bf3371bc03eae94 /it_url.class | |
parent | 8b913996bbeff055585beb6b4307b5b33082c919 (diff) | |
download | itools-15b450b9405529ae943bb7761e76b7adc5cda8e6.tar.gz itools-15b450b9405529ae943bb7761e76b7adc5cda8e6.tar.bz2 itools-15b450b9405529ae943bb7761e76b7adc5cda8e6.zip |
Revert "use CURLOPT_MAXFILESIZE now that we have current curl"
This reverts commit 1c657c2264c17dc2980bbce2f63ddad7546e96d5.
Diffstat (limited to 'it_url.class')
-rw-r--r-- | it_url.class | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/it_url.class b/it_url.class index cfeaa1d..8ac71ae 100644 --- a/it_url.class +++ b/it_url.class @@ -261,9 +261,6 @@ static function curl_opts($p=array()) if (isset($p['accept_encoding'])) $add += [CURLOPT_ENCODING => $p['accept_encoding']]; # NOTE: the curl library renamed the option to CURLOPT_ACCEPT_ENCODING, in php both are possible, CURLOPT_ENCODING is documented - if (isset($p['maxlength'])) - $add += [CURLOPT_MAXFILESIZE => $p['maxlength']]; - return $add + [ CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, @@ -317,6 +314,20 @@ function request($p=array()) curl_setopt($curl, CURLOPT_URL, $url->url); } + + // FIXME 2025-01 NG just use CURLOPT_MAXFILESIZE if we have curl 8.4 + $content = ""; + if ($p['maxlength'] && !$p['writefunction']) + { + $opts[CURLOPT_WRITEFUNCTION] = function ($dummy, $data) use ($p, &$content) { + static $space; + $write = min($space ??= $p['maxlength'], strlen($data)); + $content .= substr($data, 0, $write); + $space -= $write; + return $write; + }; + } + $opts[CURLOPT_HEADERFUNCTION] = function ($dummy, $data) use (&$header) { $header .= $data; return strlen($data); @@ -324,12 +335,13 @@ function request($p=array()) curl_setopt_array($curl, $opts); - $body = $origbody = curl_exec($curl); + $got = curl_exec($curl); + $body = $origbody = $p['maxlength'] && $got ? $content : $got; $this->curlinfo = curl_getinfo($curl); EDC('curlinfo', $this->curlinfo); - if ($body !== false || curl_errno($curl) == 63) + if ($body !== false || curl_errno($curl) == 23) { $url->header = array_slice(explode("\r\n\r\n", trim($header)), -1)[0] . "\r\n\r\n"; $url->data = $body; @@ -337,7 +349,7 @@ function request($p=array()) $url->parse_http_header($url->header); # Change result status for content longer than maxlength to 204 as we do not return partial data but still want to indicate success e.g. for is_reachable - if ($p['maxlength'] && $url->status == 200 && !$body) + if ($p['maxlength'] && $url->status == 200 && strlen($content) && !$body) $url->status = $this->result = 204; if ($p['filemtime'] && ($url->status == 304)) |