From 1c657c2264c17dc2980bbce2f63ddad7546e96d5 Mon Sep 17 00:00:00 2001 From: Nathan Gass Date: Mon, 21 Oct 2024 19:12:19 +0200 Subject: use CURLOPT_MAXFILESIZE now that we have current curl --- it_url.class | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) (limited to 'it_url.class') diff --git a/it_url.class b/it_url.class index 8333f76..772b7d1 100644 --- a/it_url.class +++ b/it_url.class @@ -261,6 +261,9 @@ 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, @@ -314,20 +317,6 @@ 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); @@ -335,13 +324,12 @@ function request($p=array()) curl_setopt_array($curl, $opts); - $got = curl_exec($curl); - $body = $origbody = $p['maxlength'] && $got ? $content : $got; + $body = $origbody = curl_exec($curl); $this->curlinfo = curl_getinfo($curl); EDC('curlinfo', $this->curlinfo); - if ($body !== false || curl_errno($curl) == 23) + if ($body !== false || curl_errno($curl) == 63) { $url->header = array_slice(explode("\r\n\r\n", trim($header)), -1)[0] . "\r\n\r\n"; $url->data = $body; @@ -349,7 +337,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 && strlen($content) && !$body) + if ($p['maxlength'] && $url->status == 200 && !$body) $url->status = $this->result = 204; if ($p['filemtime'] && ($url->status == 304)) -- cgit v1.2.3