]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
ids-functions.pl: Add support for Etags.
authorStefan Schantl <stefan.schantl@ipfire.org>
Mon, 11 Apr 2022 03:48:17 +0000 (05:48 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Mon, 11 Apr 2022 03:48:17 +0000 (05:48 +0200)
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 <stefan.schantl@ipfire.org>
config/cfgroot/ids-functions.pl

index cf896f340213b2a946660936726d86152c802fa0..b81c63b6750a67a69db169cc94acc1f25866e1fa 100644 (file)
@@ -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);