-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.pm
61 lines (50 loc) · 1.93 KB
/
server.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package API::server;
use strict; use warnings; use utf8; use feature ':5.10';
### Load HTTP::Server::Simple::CGI::PreFork which use Net::Server::PreFork (requires IPv6 on host)
use HTTP::Server::Simple::CGI::PreFork;
use base qw(HTTP::Server::Simple::CGI::PreFork);
### Load our json module
use json;
### Load our html-site modules. Note: Modules must have a print() function!
use html::dhcp;
use html::readme;
### Overwrite cgi_init function of HTTP::Server::Simple::CGI to load our own CGI module and initialize it
sub cgi_init {
my $self = shift;
$self->{cgi_init} = shift if(@_);
return $self->{cgi_init} || sub { require modules::CGI; CGI::initialize_globals(); };
}
### Serve the requests... (Generate JSON, or check if we have a html module loaded for this URI and serve it :)
sub handle_request {
my %dispatch = (
'/' => \&API::json::print
);
my $self = shift;
my $cgi = shift;
my $path = $cgi->path_info();
my @loaded_html_modules = map /html\/(\w+)\.pm/, sort keys %INC;
my ($uri) = grep { $path =~ /^\/$_\/?$/ } @loaded_html_modules;
my $handler;
if( defined $uri ) {
{
no strict 'refs';
$dispatch{$uri} = \&{"API::html::${uri}::print"};
}
$handler = $dispatch{$uri}; # automatical load SITENAME html-page if a html::SITENAME module is loaded
} else {
$handler = $dispatch{$path}; # select from pre-initialized %dispatch
}
if( ref($handler) eq "CODE") {
print "HTTP/1.0 200 OK\r\n";
&$handler($cgi);
} else {
print "HTTP/1.0 404 Not found\r\n";
print $cgi->header, $cgi->start_html('404 Not found'), $cgi->h1("404 Not found");
for ( @loaded_html_modules ) {
print "\n<p>Goto:<a style='margin-left:10px;' href='/$_'>".uc($_)."</a></p>";
}
print "\n<p>Goto:<a style='margin-left:10px;' href='/'>JSON API</a></p>";
print $cgi->end_html;
}
}
1;