2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 06:39:11 +00:00

new module 02_HTTPSRV.pm

git-svn-id: https://svn.fhem.de/fhem/trunk@1815 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
borisneubert 2012-08-27 17:52:27 +00:00
parent c0aa71ae70
commit 9516b9f95e
2 changed files with 153 additions and 0 deletions

102
fhem/FHEM/02_HTTPSRV.pm Normal file
View File

@ -0,0 +1,102 @@
#
#
# 02_HTTPSRV.pm
# written by Dr. Boris Neubert 2012-08-27
# e-mail: omega at online dot de
#
##############################################
# $Id$
package main;
use strict;
use warnings;
use vars qw(%data);
#########################
sub
HTTPSRV_addExtension($$$) {
my ($func,$link,$friendlyname)= @_;
my $url = "/" . $link;
$data{FWEXT}{$url}{FUNC} = $func;
$data{FWEXT}{$url}{LINK} = $link;
$data{FWEXT}{$url}{NAME} = $friendlyname;
}
##################
sub
HTTPSRV_Initialize($) {
my ($hash) = @_;
$hash->{DefFn} = "HTTPSRV_Define";
#$hash->{AttrFn} = "HTTPSRV_Attr";
$hash->{AttrList}= "loglevel:0,1,2,3,4,5";
#$hash->{SetFn} = "HTTPSRV_Set";
return undef;
}
##################
sub
HTTPSRV_Define($$) {
my ($hash, $def) = @_;
my @a = split("[ \t]+", $def, 5);
return "Usage: define <name> HTTPSRV <infix> <directory> <friendlyname>" if(int(@a) != 5);
my $name= $a[0];
my $infix= $a[2];
my $directory= $a[3];
my $friendlyname= $a[4];
$hash->{fhem}{infix}= $infix;
$hash->{fhem}{directory}= $directory;
$hash->{fhem}{friendlyname}= $friendlyname;
HTTPSRV_addExtension("HTTPSRV_CGI", $infix, $friendlyname);
$hash->{STATE} = $name;
return undef;
}
##################
#
# here we answer any request to http://host:port/fhem/$infix and below
sub
HTTPSRV_CGI(){
my ($request) = @_; # /$infix/filename
if($request !~ m,^/.+/.*,) {
$request= "$request/index.html";
}
if($request =~ m,^\/(.*)/(.*)$,) {
my $name= $1;
my $filename= $2;
if(!defined($defs{$name})) {
return("text/plain; charset=utf-8", "Unknown HTTPSRV device: $name");
}
my $directory= $defs{$name}{fhem}{directory};
$filename= "$directory/$filename";
my @contents;
if(open(INPUTFILE, $filename)) {
binmode(INPUTFILE);
@contents= <INPUTFILE>;
close(INPUTFILE);
return("", join("", @contents));
} else {
return("text/plain; charset=utf-8", "File not found: $filename");
}
} else {
return("text/plain; charset=utf-8", "Illegal request: $request");
}
}
####
1;

View File

@ -96,6 +96,7 @@
<a href="#FS20">FS20</a> &nbsp;
<a href="#HMS">HMS</a> &nbsp;
<a href="#HMLAN">HMLAN</a> &nbsp;
<a href="#HTTPSRV">HTTPSRV</a> &nbsp;
<a href="#IPCAM">IPCAM</a> &nbsp;
<a href="#IPWE">IPWE</a> &nbsp;
<a href="#IT">IT</a> &nbsp;
@ -5296,6 +5297,56 @@ To send the data, both send or write could be used.<br>
<br>
</ul>
<a name="HTTPSRV"></a>
<h3>HTTPSRV</h3>
<ul>
Provides a mini HTTP server plugin for FHEMWEB. It serves files from a given directory.<p>
HTTPSRV is an extension to <a href="HTTPSRV">FHEMWEB</a>. You must install FHEMWEB to use HTTPSRV.</p>
<a name="HTTPSRVdefine"></a>
<b>Define</b>
<ul>
<code>define &lt;name&gt; &lt;infix&gt; &lt;directory&gt; &lt;friendlyname&gt;</code><br><br>
Defines the HTTP server. <code>&lt;infix&gt;</code> is the portion behind the FHEMWEB base URL (usually
<code>http://hostname:8083/fhem</code>), <code>&lt;directory&gt;</code> is the absolute path the
files are served from, and <code>&lt;friendlyname&gt;</code> is the name displayed in the side menu of FHEMWEB.<p><p>
Example:
<ul>
<code>define myJSFrontend HTTPSRV jsf /usr/share/jsfrontend My little frontend</code><br>
</ul>
<br>
</ul>
<a name="HTTPSRVset"></a>
<b>Set</b>
<ul>
n/a
</ul>
<br><br>
<a name="HTTPSRVattr"></a>
<b>Attributes</b>
<br><br>
<ul>
n/a
</ul>
<br><br>
<b>Usage information</b>
<br><br>
<ul>
The above example on <code>http://hostname:8083/fhem</code> will return the file
<code>/usr/share/jsfrontend/foo.html</code> for <code>http://hostname:8083/fhem/jsf/foo.html</code>.
If no filename is given, <code>index.html</code> is returned.<p>
Notice: All links are relative to <code>http://hostname:8083/fhem</code>.
</ul>
<br><br>
<a name="USF1000"></a>
<h3>USF1000</h3>
<ul>