From 3be8c88c08d68924489f82d4944c8c593a35bb85 Mon Sep 17 00:00:00 2001 From: rudolfkoenig Date: Sun, 10 Feb 2013 19:22:55 +0000 Subject: [PATCH] Serve arbitrary files from the www directory (FHEMWEB) git-svn-id: https://fhem.svn.sourceforge.net/svnroot/fhem/trunk/fhem@2684 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- CHANGED | 1 + FHEM/01_FHEMWEB.pm | 18 ++++++++++++++++-- FHEM/HttpUtils.pm | 24 +++++++++++++----------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/CHANGED b/CHANGED index 3bab4acd2..94e6358fd 100644 --- a/CHANGED +++ b/CHANGED @@ -70,6 +70,7 @@ - feature: added new set commands and basicauth to 49_IPCAM.pm (M. Fischer) - feature: userReadings - feature: average supports more than one value in combined readings (T:x H:y) + - feature: FHEMWEB serves arbitrary files from the www directory - 2012-10-28 (5.3) diff --git a/FHEM/01_FHEMWEB.pm b/FHEM/01_FHEMWEB.pm index 666134b7f..95a0ead12 100755 --- a/FHEM/01_FHEMWEB.pm +++ b/FHEM/01_FHEMWEB.pm @@ -351,7 +351,7 @@ FW_serveSpecial($$$$) } $FW_RETTYPE = ext2MIMEType($ext); - #Log 1, "We serve $dir/$file.$ext, $FW_RETTYPE"; + #Log 1, "Serving $dir/$file.$ext as $FW_RETTYPE, cacheable:$cacheable"; return FW_returnFileAsStream("$dir/$file.$ext", "", $FW_RETTYPE, 0, $cacheable); } @@ -366,6 +366,7 @@ FW_setDirs() } else { $FW_dir = AttrVal($FW_wname, "fwmodpath", "$attr{global}{modpath}/FHEM"); } + # icon dir if(-d "$FW_dir/images") { $FW_icondir = "$FW_dir/images"; @@ -374,6 +375,7 @@ FW_setDirs() } else { $FW_icondir = $FW_dir; } + # doc dir if(-d "$FW_dir/docs") { $FW_docdir = "$FW_dir/docs"; @@ -384,12 +386,14 @@ FW_setDirs() } else { $FW_docdir = $FW_dir; } + # css dir if(-d "$FW_dir/pgm2") { $FW_cssdir = "$FW_dir/pgm2"; } else { $FW_cssdir = $FW_dir; } + # gplot dir if(-d "$FW_dir/gplot") { $FW_gplotdir = "$FW_dir/gplot"; @@ -398,6 +402,7 @@ FW_setDirs() } else { $FW_gplotdir = $FW_dir; } + # javascript dir if(-d "$FW_dir/pgm2") { $FW_jsdir = "$FW_dir/pgm2"; @@ -460,6 +465,15 @@ FW_answerCall($) $FW_icons{$icon} =~ m/(.*)\.($ICONEXTENSION)/; return FW_serveSpecial($1, $2, $FW_icondir, $cacheable); + } elsif($arg =~ m,^$FW_ME/(.*/)([^/]*),) { + my ($dir, $file, $ext) = ($1, $2, ""); + $dir =~ s/\.\.//g; + if($file =~ m/^(.*)\.([^.]*)$/) { + $file = $1; $ext = $2; + } + return FW_serveSpecial($file, $ext, "$FW_dir/$dir", + ($arg =~ m/nocache/) ? 0 : 1); + } elsif($arg !~ m/^$FW_ME(.*)/) { my $c = $me->{CD}; Log 4, "$FW_wname: redirecting $arg to $FW_ME"; @@ -1309,7 +1323,7 @@ FW_returnFileAsStream($$$$$) binmode(FH) if($type !~ m/text/); # necessary for Windows $etag = defined($etag) ? "ETag: \"$etag\"\r\n" : ""; - my $expires = $cacheable ? ("Expires: ".localtime(time()+900)." GMT\r\n"): ""; + my $expires = $cacheable ? ("Expires: ".gmtime(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", diff --git a/FHEM/HttpUtils.pm b/FHEM/HttpUtils.pm index 77c708e0a..908c0fd2e 100644 --- a/FHEM/HttpUtils.pm +++ b/FHEM/HttpUtils.pm @@ -8,30 +8,32 @@ use IO::Socket::INET; use MIME::Base64; my %ext2MIMEType= qw{ - txt text/plain - html text/html - pdf application/pdf css text/css - jpg image/jpeg - png image/png gif image/gif + html text/html ico image/x-icon -}; + jpg image/jpeg + js text/javascript + pdf application/pdf + png image/png + svg image/svg+xml + txt text/plain -my $KNOWNEXTENSIONS= 'txt|html|pdf|css|jpg|png|gif|ico'; +}; sub ext2MIMEType($) { my ($ext)= @_; - my $MIMEType= $ext ? $ext2MIMEType{$ext} : ""; - return $MIMEType ? $MIMEType : ""; + return "text/plain" if(!$ext); + my $MIMEType = $ext2MIMEType{lc($ext)}; + return ($MIMEType ? $MIMEType : "text/$ext"); } sub filename2MIMEType($) { my ($filename)= @_; - $filename =~ m/^(.*)\.($KNOWNEXTENSIONS)$/; - return ext2MIMEType($2); + $filename =~ m/^.*\.([^\.]*)$/; + return ext2MIMEType($1); }