Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package App::TCC::WWWserver;
- use 5.012001;
- use strict;
- use warnings;
- use Data::Dumper;
- use URI;
- use URI::QueryParam;
- use CGI qw/:standard -no_xhtml/;
- use CGI::Cookie;
- use POE qw(Component::Server::TCP Filter::HTTPD);
- require Exporter;
- use AutoLoader qw(AUTOLOAD);
- our @ISA = qw(Exporter);
- our @EXPORT = qw();
- our $VERSION = '0.01';
- my $css=<<END;
- html { width:900px; }
- body { width:900px; }
- .loginbox {
- margin-left: auto;
- margin-right: auto;
- background-color: #defbfa;
- width: 204px;
- }
- .loginbox input { margin: 0.1em 2em; }
- .smallheader { font-size: 12pt; color: black; }
- .submitbox {
- margin-left: auto;
- margin-right: auto;
- background-color: #defbfa;
- width: 180px;
- }
- .submitbtn {
- margin-top: 6px;
- width: 180px;
- height: 30px;
- }
- END
- 1;
- sub new {
- my $package = shift;
- my %opts = %{$_[0]} if ($_[0]);
- $opts{ lc $_ } = delete $opts{$_} for keys %opts; # convert opts to lower case
- my $self = bless \%opts, $package;
- $self->{start} = time;
- $self->{cycles} = 0;
- # die "username: $self->{user}, password: $self->{pass}";
- $self->{me} = POE::Component::Server::TCP->new(
- Alias => "webserver",
- Port => 8088,
- ClientFilter => 'POE::Filter::HTTPD',
- ClientInput => sub {
- my ($kernel, $heap, $request) = @_[KERNEL, HEAP, ARG0];
- if ($request->isa("HTTP::Response")) {
- $heap->{client}->put($request);
- $kernel->yield("shutdown");
- return;
- }
- my $headers;
- my $uri = URI->new($request->uri());
- my $cgi = CGI->new();
- my $authed = 0;
- my %c = CGI::Cookie->parse($request->header('Cookie')) if ($request->header('Cookie'));
- # Record wether its a post, get etc..
- $headers->{target}->{method} = $request->method();
- $headers->{param}->{compat} = "";
- $request->headers()->scan(
- sub {
- my ($header, $value) = @_;
- $headers->{request}->{$header} = $value
- }
- );
- if ($headers->{target}->{method} eq 'POST') {
- foreach my $pair (split(m#&#,$request->content())) {
- my ($key,$value) = $pair =~ m#(.*?)=(.*)$#;
- $headers->{param}->{$key} = $value;
- }
- }
- # Find any values passed to the script
- foreach my $key ($uri->query_param) {
- $headers->{param}->{compat} .= "$key=".$uri->query_param($key)."\n";
- $headers->{param}->{$key} = $uri->query_param($key);
- }
- # Find The target refference
- if ($request->uri() =~ m#\?#) {
- ($headers->{target}->{url}) = $request->uri() =~ m#^(.*?)\?#;
- } else {
- $headers->{target}->{url} = $request->uri()
- }
- # Send a 200 (TODO)
- print STDERR "Serving [$headers->{target}->{method}]: $headers->{target}->{url}\n";
- # Generate a response object
- my $response;
- given($headers->{target}->{url}) {
- when(m#^/$#) {
- $response = HTTP::Response->new(200);
- $response->push_header('Content-type', 'text/html');
- my $username = "";
- my $emsg = "";
- if ($headers->{target}->{method} eq 'POST') {
- warn "Its a post!";
- if (($headers->{param}->{username}) && ($headers->{param}->{password})) {
- if (($headers->{param}->{username} eq $self->{user}) && ($headers->{param}->{password} eq $self->{pass})) { $authed = 1 }
- else { $emsg = "Incorrect login credentials" }
- }
- }
- my $cookie;
- if ($authed) {
- $cookie = CGI::Cookie->new(-name=>'Auth',-value=>join(':',$self->{user},$self->{pass}))
- } else {
- if ($c{Auth}->value eq join(':',$self->{user},$self->{pass})) { $cookie = CGI::Cookie->new(-name=>'Auth',-value=>join(':',$self->{user},$self->{pass})); $aut
- else { $cookie = CGI::Cookie->new(-name=>'Auth',-value=>"") }
- }
- $response->push_header('Set-Cookie',$cookie);
- $response->content(
- $cgi->start_html(
- -title=>'TCC Management Panel',
- -author=>'opensource@interflecive.com',
- -style=>{'code'=>$css},
- ).
- $cgi->h1('Please login to continue').
- $cgi->p('If this is the first run on this node then please refer to the console where you launched TCC for the username/password').
- $cgi->p({'style'=>'color:red'},$emsg).
- $cgi->start_form(-method=>'POST',-action=>'/',-enctype=>'application/x-www-form-urlencoded').
- $cgi->div({'class'=>'loginbox'},
- $cgi->p({'class'=>'smallheader'},'Username').
- $cgi->textfield(-name=>'username',-value=>$username,-size=>'20')
- ).
- $cgi->div({'class'=>'loginbox'},
- $cgi->p({'class'=>'smallheader'},'Password').
- $cgi->password_field(-name=>'password',-value=>'',-size=>'20')
- ).
- $cgi->div({'class'=>'submitbox'},
- $cgi->submit({'class'=>'submitbtn'},'Action','Submit')
- ).
- $cgi->end_form().
- $cgi->end_html
- );
- }
- default {
- $response = HTTP::Response->new(404);
- $response->push_header('Content-type', 'text/html');
- $response->content('<html><head><title>TCC: 404 not found</title></head><body><h1>404 - not found</h1><p>The resource you have requested is not availible at present.</
- }
- }
- $heap->{client}->put($response);
- $kernel->yield("shutdown");
- }
- );
- }
- __END__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement