From c68b5fc6cf0888c635f2dbaea622d64205ef32f5 Mon Sep 17 00:00:00 2001 From: rudolfkoenig Date: Wed, 21 Nov 2012 19:47:19 +0000 Subject: [PATCH] ETags by Matthias git-svn-id: https://svn.fhem.de/fhem/trunk@2150 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 3 ++- fhem/FHEM/01_FHEMWEB.pm | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 0e343cbdf..e3e5251ec 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -16,7 +16,8 @@ - bugfix: FHEMWEB slider with min > 0 - change: FHEMWEB CORS moved to options - change: FHEMWEB closing old TCP connections - - change: FHEMWEB added "Associated with" to detail-screen + - change: FHEMWEB added "Associated with" to detail-screen (Uli) + - change: FHEMWEB added ETag headers (Matthias) - 2012-10-28 (5.3) - feature: added functions trim, ltrim, rtrim, UntoggleDirect, diff --git a/fhem/FHEM/01_FHEMWEB.pm b/fhem/FHEM/01_FHEMWEB.pm index 6610af2ba..26f3bfbd0 100755 --- a/fhem/FHEM/01_FHEMWEB.pm +++ b/fhem/FHEM/01_FHEMWEB.pm @@ -1236,18 +1236,36 @@ sub FW_returnFileAsStream($$$$$) { my ($path, $suffix, $type, $doEsc, $cacheable) = @_; + + #Check for If-None-Match header (ETag) + my @if_none_match_lines = grep /If-None-Match/, @FW_httpheader; + my $if_none_match = undef; + if(@if_none_match_lines) { + $if_none_match = $if_none_match_lines[0]; + $if_none_match =~ s/If-None-Match: \"(.*)\"/$1/; + } + + my $c = $FW_chash->{CD}; + my $etag = (stat($path))[9]; #mtime + + if(defined($etag) and defined($if_none_match) and $etag eq $if_none_match) { + print $c "HTTP/1.1 304 Not Modified\r\n", + $FW_headercors, "\r\n"; + return -1; + } + if(!open(FH, $path)) { FW_pO "
$path: $!
"; return 0; } binmode(FH) if($type !~ m/text/); # necessary for Windows - my $c = $FW_chash->{CD}; + $etag = defined($etag) ? "ETag: \"$etag\"\r\n" : ""; my $expires = $cacheable ? ("Expires: ".localtime(time()+900)." GMT\r\n"): ""; my $compr = ((int(@FW_enc) == 1 && $FW_enc[0] =~ m/gzip/) && $FW_use_zlib) ? "Content-Encoding: gzip\r\n" : ""; print $c "HTTP/1.1 200 OK\r\n", - $compr, $expires, $FW_headercors, + $compr, $expires, $FW_headercors, $etag, "Transfer-Encoding: chunked\r\n", "Content-Type: $type; charset=$FW_encoding\r\n\r\n";