From 4bfd85aa05e269ad1cb36705a4e7880f0d679ee9 Mon Sep 17 00:00:00 2001 From: borisneubert Date: Sat, 1 Sep 2012 17:09:33 +0000 Subject: [PATCH] - new helper functions ext2MIMEType(), filename2MIMEType() - 02_HTTPSRV also serves files from subdirectories - 02_HTTPSRV sends MIME types git-svn-id: https://fhem.svn.sourceforge.net/svnroot/fhem/trunk/fhem@1825 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- FHEM/02_HTTPSRV.pm | 8 +++++--- FHEM/HttpUtils.pm | 28 ++++++++++++++++++++++++++++ webfrontend/pgm2/01_FHEMWEB.pm | 9 +-------- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/FHEM/02_HTTPSRV.pm b/FHEM/02_HTTPSRV.pm index 960b57dd1..84a46fe7a 100644 --- a/FHEM/02_HTTPSRV.pm +++ b/FHEM/02_HTTPSRV.pm @@ -11,7 +11,7 @@ package main; use strict; use warnings; use vars qw(%data); - +use HttpUtils; ######################### sub @@ -71,11 +71,13 @@ HTTPSRV_CGI(){ if($request !~ m,^/.+/.*,) { $request= "$request/index.html"; } - if($request =~ m,^\/(.*)/(.*)$,) { + if($request =~ m,^/([^/]+)/(.*)$,) { my $name= $1; my $filename= $2; + my $MIMEtype= filename2MIMEType($filename); + #return("text/plain; charset=utf-8", "HTTPSRV device: $name, filename: $filename, MIME type: $MIMEtype"); if(!defined($defs{$name})) { - return("text/plain; charset=utf-8", "Unknown HTTPSRV device: $name"); + return("$MIMEtype; charset=utf-8", "Unknown HTTPSRV device: $name"); } my $directory= $defs{$name}{fhem}{directory}; $filename= "$directory/$filename"; diff --git a/FHEM/HttpUtils.pm b/FHEM/HttpUtils.pm index 902c0fadf..913ca6794 100644 --- a/FHEM/HttpUtils.pm +++ b/FHEM/HttpUtils.pm @@ -6,6 +6,34 @@ use strict; use warnings; use IO::Socket::INET; +my %ext2MIMEType= qw{ + txt text/plain + html text/html + pdf application/pdf + css text/css + jpg image/jpeg + png image/png + gif image/gif + ico image/x-icon +}; + +my $KNOWNEXTENSIONS= 'txt|html|pdf|css|jpg|png|gif|ico'; + +sub +ext2MIMEType($) { + my ($ext)= @_; + my $MIMEType= $ext ? $ext2MIMEType{$ext} : ""; + return $MIMEType ? $MIMEType : ""; +} + +sub +filename2MIMEType($) { + my ($filename)= @_; + $filename =~ m/^(.*)\.($KNOWNEXTENSIONS)$/; + return ext2MIMEType($2); +} + + ################## sub urlEncode($) { diff --git a/webfrontend/pgm2/01_FHEMWEB.pm b/webfrontend/pgm2/01_FHEMWEB.pm index 66c654b6f..ef01df685 100755 --- a/webfrontend/pgm2/01_FHEMWEB.pm +++ b/webfrontend/pgm2/01_FHEMWEB.pm @@ -313,14 +313,7 @@ FW_ServeSpecial($$$) { binmode(FH) if($ext =~ m/$ICONEXTENSION/); # necessary for Windows FW_pO join("", ); close(FH); - $FW_RETTYPE = "text/plain" if($ext eq "txt"); - $FW_RETTYPE = "text/html" if($ext eq "html"); - $FW_RETTYPE = "application/pdf" if($ext eq "pdf"); - $FW_RETTYPE = "text/css" if($ext eq "css"); - $FW_RETTYPE = "image/jpeg" if($ext eq "jpg"); - $FW_RETTYPE = "image/png" if($ext eq "png"); - $FW_RETTYPE = "image/gif" if($ext eq "gif"); - $FW_RETTYPE = "image/x-icon" if($ext eq "ico"); + $FW_RETTYPE = ext2MIMEType($ext); return 1; }