From 4d3956ddba5aeb99655abfca0b1eba020afbefdd Mon Sep 17 00:00:00 2001 From: Christian Helbling Date: Mon, 3 Jul 2017 14:53:34 +0200 Subject: add filemtime / If-Modified-Since functionality to request_curl --- it_url.class | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/it_url.class b/it_url.class index 3525117..c32eb91 100644 --- a/it_url.class +++ b/it_url.class @@ -166,13 +166,16 @@ function parse_http_header($header) static function _default_headers($url, $p) { - return [ + $headers = [ 'Host' => $url->realhostname . $url->explicitport, 'User-Agent' => "Mozilla/5.0 (compatible; MSIE 9.0; ITools)", 'Accept-Language' => $p['headers']['Accept-Language'] ?: T_lang(), # can prevent loading of it_text 'Referer' => it::match('([-\w]+\.\w+)$', $url->hostname) == it::match('([-\w]+\.\w+)$', $_SERVER['HTTP_HOST']) ? it_url::absolute(U($_GET)) : null, 'X-Ultra-Https' => $_SERVER['HTTPS'], ]; + if (is_int($p['filemtime'])) + $headers['If-Modified-Since'] = date("r", $p['filemtime']); + return $headers; } function request($p=array()) @@ -196,9 +199,6 @@ function request($p=array()) $p['headers'] = (array)$p['headers'] + self::_default_headers($url, $p); - if (is_int($p['filemtime'])) - $p['headers']['If-Modified-Since'] = date("r", $p['filemtime']); - if ($datalen = strlen($data)) { $method = $p['method'] ?: "POST"; @@ -324,8 +324,6 @@ static function curl_opts($p=array()) /* * drop in replacement for request using curl * - * todo: - * @param $p['filemtime'] Add HTTP header to only fetch when newer than this, otherwise return true instead of data * @param $p['data'] POST data array with key-value pairs * @param $p['files'] [fieldname => filename] of files to upload * @param $p['method'] different HTTP method @@ -359,8 +357,11 @@ function request_curl($p=array()) if ($p['maxlength'] && (strlen($this->data) > $p['maxlength'])) { $result = $this->result = false; $errstr = $this->errstr = "maxlength reached"; - } else + } else if ($p['filemtime'] && ($url->result == 304)) { + $result = true; # Not modified, success but no data + } else { $result =& $url->data; + } } else { $result = $this->result = false; $errstr = $this->errstr = curl_error($curl); -- cgit v1.2.3