diff options
-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)) |