From 0188f31272910cec24607d45e3e321b532b728ae Mon Sep 17 00:00:00 2001 From: Nathan Gass Date: Tue, 29 Oct 2019 11:56:27 +0100 Subject: implementation of maxlength using decompressed data size --- it_url.class | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/it_url.class b/it_url.class index 4afccb8..626311c 100644 --- a/it_url.class +++ b/it_url.class @@ -171,15 +171,6 @@ static function curl_opts($p=array()) foreach ($p['headers'] as $header => $value) $headers[] = "$header: $value"; - if ($p['maxlength']) { - $maxlength = $p['maxlength']; - $add += [ - #CURLOPT_BUFFERSIZE => 1024 * 1024 * 10, - CURLOPT_NOPROGRESS => false, - CURLOPT_PROGRESSFUNCTION => function ($dummy0, $dummy1, $size, $dummy2, $dummy3) use ($maxlength) { return $size < $maxlength ? 0 : 1; }, - ]; - } - # file upload foreach ((array)$p['files'] as $field => $filename) $p['data'][$field] = new CURLFile($filename, mime_content_type($filename)); @@ -246,29 +237,47 @@ function request($p=array()) $url->headers = array(); $p['headers'] = (array)$p['headers'] + self::_default_headers($url, $p); - $opts = array(CURLOPT_HEADER => !$p['writefunction']) + self::curl_opts($p + array('user' => $this->user, 'pass' => $this->pass, 'followlocation' => false)); + $opts = self::curl_opts($p + array('user' => $this->user, 'pass' => $this->pass, 'followlocation' => false)); 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']) { + $content = ""; + $opts[CURLOPT_WRITEFUNCTION] = function ($dummy, $data) use ($p, &$content) { + static $total = 0; + $size = strlen($data); + $total += $size; + if ($total > $p['maxlength']) + return 0; + $content .= $data; + return $size; + }; + } + + $opts[CURLOPT_HEADERFUNCTION] = function ($dummy, $data) use (&$header) { + $header .= $data; + return strlen($data); + }; + curl_setopt_array($curl, $opts); $got = curl_exec($curl); + if ($p['maxlength'] && $got) + $got = $content; + EDC('curlinfo', curl_getinfo($curl)); if ($got !== false) { - $headersize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); - $url->header = array_slice(explode("\r\n\r\n", trim(substr($got, 0, $headersize))), -1)[0] . "\r\n\r\n"; - $url->data = substr($got, $headersize); + $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['maxlength'] && (strlen($this->data) > $p['maxlength'])) { - $result = $this->result = false; - $this->errstr = "maxlength reached"; - } else 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) { 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] : [])]); -- cgit v1.2.3