From: Stefan Schantl Date: Mon, 11 Apr 2022 03:48:17 +0000 (+0200) Subject: ids-functions.pl: Add support for Etags. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=990d111d70b7f5276b5ff3b6729773f1066fcee7;p=people%2Fstevee%2Fipfire-2.x.git ids-functions.pl: Add support for Etags. Etags are used to itentify if an ressource has been changed by sending a special request and an Etag value to the server. If the ressource has changed the server will serve the new content otherwise it will return the 304 (Not-Modified) code. Signed-off-by: Stefan Schantl --- diff --git a/config/cfgroot/ids-functions.pl b/config/cfgroot/ids-functions.pl index cf896f3402..b81c63b675 100644 --- a/config/cfgroot/ids-functions.pl +++ b/config/cfgroot/ids-functions.pl @@ -83,6 +83,10 @@ our $providers_settings_file = "$settingsdir/providers-settings"; # File which stores the configured settings for whitelisted addresses. our $ignored_file = "$settingsdir/ignored"; +# File which stores HTTP Etags for providers which supports them +# for cache management. +our $etags_file = "$settingsdir/etags"; + # Location where the downloaded rulesets are stored. our $dl_rules_path = "/var/tmp"; @@ -394,6 +398,20 @@ sub downloadruleset ($) { $request->header( 'If-Modified-Since' => "$http_date" ); } + # Read-in Etags file for known Etags if the file is present. + my %etags = (); + &General::readhash("$etags_file", \%etags) if (-f $etags_file); + + # Check if an Etag for the current provider is stored. + if ($etags{$provider}) { + # Grab the stored tag. + my $etag = $etags{$provider}; + + # Add an "If-None-Match header to the request to ask the server if the + # file has been modified. + $request->header( 'If-None-Match' => $etag ); + } + my $dl_attempt = 1; my $response; @@ -438,6 +456,15 @@ sub downloadruleset ($) { # Get the remote size of the downloaded file. my $remote_filesize = $headers->content_length; + # Grab the Etag from response it the server provides one. + if ($response->header('Etag')) { + # Add the Etag to the etags hash. + $etags{$provider} = $response->header('Etag'); + + # Write the etags file. + &General::writehash($etags_file, \%etags); + } + # Perform stat on the tmpfile. my $stat = stat($tmpfile);