Simplification in html/js/css code and redirecting if /fhem not specified

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@1417 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2012-04-06 08:27:32 +00:00
parent 74aad48c0a
commit 326cd00c6a

View File

@ -55,7 +55,8 @@ my $try_zlib = 1;
######################### #########################
# As we are _not_ multithreaded, it is safe to use global variables. # As we are _not_ multithreaded, it is safe to use global variables.
# Note: for delivering SVG plots we fork # Note: for delivering SVG plots we fork
my %FW_webArgs; # all arguments specifie in the GET my @FW_httpheader; # HTTP header, line by line
my %FW_webArgs; # all arguments specified in the GET
my $FW_cmdret; # Returned data by the fhem call my $FW_cmdret; # Returned data by the fhem call
my $FW_data; # Filecontent from browser when editing a file my $FW_data; # Filecontent from browser when editing a file
my $FW_detail; # currently selected device for detail view my $FW_detail; # currently selected device for detail view
@ -214,7 +215,8 @@ FW_Read($)
$selectlist{$nhash{NAME}} = \%nhash; $selectlist{$nhash{NAME}} = \%nhash;
if($hash->{SSL}) { if($hash->{SSL}) {
# Certs directory must be in the modpath, i.e. at the same level as the FHEM directory # Certs directory must be in the modpath, i.e. at the same level as the
# FHEM directory
my $mp = AttrVal("global", "modpath", "."); my $mp = AttrVal("global", "modpath", ".");
my $ret = IO::Socket::SSL->start_SSL($nhash{CD}, { my $ret = IO::Socket::SSL->start_SSL($nhash{CD}, {
SSL_server => 1, SSL_server => 1,
@ -265,13 +267,13 @@ FW_Read($)
return if($hash->{BUF} !~ m/\n\n$/ && $hash->{BUF} !~ m/\r\n\r\n$/); return if($hash->{BUF} !~ m/\n\n$/ && $hash->{BUF} !~ m/\r\n\r\n$/);
#Log 0, "Got: >$hash->{BUF}<"; #Log 0, "Got: >$hash->{BUF}<";
my @lines = split("[\r\n]", $hash->{BUF}); @FW_httpheader = split("[\r\n]", $hash->{BUF});
############################# #############################
# BASIC HTTP AUTH # BASIC HTTP AUTH
my $basicAuth = AttrVal($FW_wname, "basicAuth", undef); my $basicAuth = AttrVal($FW_wname, "basicAuth", undef);
if($basicAuth) { if($basicAuth) {
my @auth = grep /^Authorization: Basic $basicAuth/, @lines; my @auth = grep /^Authorization: Basic $basicAuth/, @FW_httpheader;
if(!@auth) { if(!@auth) {
my $msg = AttrVal($FW_wname, "basicAuthMsg", "Fhem: login required"); my $msg = AttrVal($FW_wname, "basicAuthMsg", "Fhem: login required");
print $c "HTTP/1.1 401 Authorization Required\r\n", print $c "HTTP/1.1 401 Authorization Required\r\n",
@ -282,8 +284,8 @@ FW_Read($)
} }
############################# #############################
my @enc = grep /Accept-Encoding/, @lines; my @enc = grep /Accept-Encoding/, @FW_httpheader;
my ($mode, $arg, $method) = split(" ", $lines[0]); my ($mode, $arg, $method) = split(" ", $FW_httpheader[0]);
$hash->{BUF} = ""; $hash->{BUF} = "";
Log $ll, "HTTP $name GET $arg"; Log $ll, "HTTP $name GET $arg";
@ -304,9 +306,9 @@ FW_Read($)
} }
my $compressed = ""; my $compressed = "";
if(($FW_RETTYPE=~m/text/i || if(($FW_RETTYPE =~ m/text/i ||
$FW_RETTYPE=~m/svg/i || $FW_RETTYPE =~ m/svg/i ||
$FW_RETTYPE=~m/script/i) && $FW_RETTYPE =~ m/script/i) &&
(int(@enc) == 1 && $enc[0] =~ m/gzip/) && (int(@enc) == 1 && $enc[0] =~ m/gzip/) &&
$try_zlib && $try_zlib &&
AttrVal($FW_wname, "fwcompress", 1)) { AttrVal($FW_wname, "fwcompress", 1)) {
@ -342,28 +344,21 @@ FW_AnswerCall($)
$FW_tp = AttrVal($FW_wname, "touchpad", $FW_ss); $FW_tp = AttrVal($FW_wname, "touchpad", $FW_ss);
# Lets go: # Lets go:
if($arg =~ m,^${FW_ME}/(example.*|.*html)$,) { if($arg =~ m,^${FW_ME}/(.*)\.(css|html|js)$,) {
my $f = $1; my ($file, $ext) = ($1, $2);
$f =~ s,/,,g; # little bit of security $file =~ s,/,,g; # little bit of security
open(FH, "$FW_dir/$f") || return 0; open(FH, "$FW_dir/$file.$ext") || return 0;
FW_pO join("", <FH>); FW_pO join("", <FH>);
close(FH); close(FH);
$FW_RETTYPE = "text/plain; charset=$FW_encoding" if($f !~ m/\.*html$/); $FW_RETTYPE = "text/css" if($ext eq "css");
return 1; $FW_RETTYPE = "application/javascript" if($ext eq "js");
} elsif($arg =~ m,^$FW_ME/(.*).css,) {
my $cssName = $1;
return 0 if(!open(FH, "$FW_dir/$cssName.css"));
FW_pO join("", <FH>);
close(FH);
$FW_RETTYPE = "text/css";
return 1; return 1;
} elsif($arg =~ m,^$FW_ME/icons/(.*)$, || } elsif($arg =~ m,^$FW_ME/icons/(.*)$, ||
$arg =~ m,^$FW_ME/(.*.png)$,i) { $arg =~ m,^$FW_ME/(.*.png)$,i) {
my $img = $1; my $img = $1;
my $cachable = 1; my $cachable = 1;
if(!open(FH, "$FW_dir/$img")) { if(!open(FH, "$FW_dir/$img")) { # Hack: convert device state to icon name
FW_ReadIcons(); FW_ReadIcons();
$img = FW_dev2image($img); $img = FW_dev2image($img);
$cachable = 0; $cachable = 0;
@ -376,16 +371,13 @@ FW_AnswerCall($)
$FW_RETTYPE = "image/$f_ext[-1]"; $FW_RETTYPE = "image/$f_ext[-1]";
return $cachable; return $cachable;
} elsif($arg =~ m,^$FW_ME/(.*).js,) { #kpb java include
open(FH, "$FW_dir/$1.js") || return 0;
FW_pO join("", <FH>);
close(FH);
$FW_RETTYPE = "application/javascript";
return 1;
} elsif($arg !~ m/^$FW_ME(.*)/) { } elsif($arg !~ m/^$FW_ME(.*)/) {
Log(5, "Unknown document $arg requested"); my $c = $me->{CD};
return 0; Log 2, "$FW_wname: redirecting $arg to $FW_ME";
print $c "HTTP/1.1 302 Found\r\n",
"Content-Length: 0\r\n",
"Location: $FW_ME\r\n\r\n";
return -1;
} }
@ -826,10 +818,10 @@ FW_roomOverview($)
"Howto", "$FW_ME/HOWTO.html", "Howto", "$FW_ME/HOWTO.html",
"Wiki", "http://fhemwiki.de", "Wiki", "http://fhemwiki.de",
"Details", "$FW_ME/commandref.html", "Details", "$FW_ME/commandref.html",
"Definition...", "$FW_ME/cmd=style%20addDef", "Definition...", "$FW_ME?cmd=style%20addDef",
"Edit files", "$FW_ME/cmd=style%20list", "Edit files", "$FW_ME?cmd=style%20list",
"Select style", "$FW_ME/cmd=style%20select", "Select style", "$FW_ME?cmd=style%20select",
"Event monitor", "$FW_ME/cmd=style%20eventMonitor", "Event monitor", "$FW_ME?cmd=style%20eventMonitor",
"", ""); "", "");
my $lastname = ","; # Avoid double "". my $lastname = ","; # Avoid double "".
for(my $idx = 0; $idx < @list; $idx+= 2) { for(my $idx = 0; $idx < @list; $idx+= 2) {