/[mws]/trunk/lib/HTTP/Daemon/Simple.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/HTTP/Daemon/Simple.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 52 - (show annotations)
Tue May 25 18:55:46 2004 UTC (20 years ago) by dpavlin
File size: 3641 byte(s)
move perl modules into lib directory, extracted HTTP daemon into new module
HTTP::Daemon::Simple

1 package HTTP::Daemon::Simple;
2
3 =head1 NAME
4
5 HTTP::Daemon::Simple - simple implementation of http server
6
7 =head1 DESCRIPTION
8
9 This class is abstracted from C<httpd.pl> to encapsulate different HTTP server
10 related tasks (single or multi user model, encryption etc.)
11
12 This is small http server, based on C<HTTP::Daemon> which is designed
13 for single-user use (on laptop for example) via loopback.
14
15 It doesn't provide any authentification or authorisation, and it can handle
16 just one request at the time, so it's not suted for public-facing sites,
17 even if you don't care about security of your mailboxes.
18
19 It's very much based on based on post available at:
20 L<http://www.mail-archive.com/libwww@perl.org/msg04750.html>
21
22 =head1 SEE ALSO
23
24 C<httpd.pl> script which uses this module
25
26 =cut
27
28 use strict;
29 use warnings;
30 use HTTP::Daemon;
31 use HTTP::Status;
32 use IO::String;
33 use CGI::Lite;
34
35 my ($local_addr,$local_port) = ('127.0.0.1',6969);
36
37 sub new {
38 my $class = shift;
39 my $self = {@_};
40 bless($self, $class);
41
42 if ($self->{'listen'} && $self->{'listen'} =~ m/:/) {
43 ($local_addr,$local_port) = split(/:/,$self->{'listen'},2);
44 } elsif ($self->{'listen'}) {
45 $local_addr = $self->{'listen'};
46 }
47
48 print STDERR "using listen ",$self->{'listen'},"\n" if ($self->{'listen'} && $self->{'debug'});
49
50 $self->{daemon} = HTTP::Daemon->new(
51 Reuse => 1,
52 LocalAddr => $local_addr,
53 LocalPort => $local_port,
54 ) || die "can't create HTTP::Daemon on $local_addr:$local_port: $!";
55
56 $self->{cgi} = new CGI::Lite;
57
58 $self ? return $self : return undef;
59 }
60
61 sub run_server {
62 my $self = shift;
63
64 my $coderef = shift || die "run_server needs coderef to process requests";
65 my $d = $self->{daemon} || die "BUG: HTTP::Daemon object not created in constructior";
66 my $cgi = $self->{cgi} || die "BUG: CGI::Lite object not created in constructor";
67
68 while ( my $c = $d->accept ) {
69 while ( my $r = $c->get_request ) {
70
71 # environs that a webserver should set.
72 $ENV{'REQUEST_METHOD'} = $r->method;
73 $ENV{'GATEWAY_INTERFACE'} = "CGI/1.0";
74 $ENV{'SERVER_PROTOCOL'} = $r->protocol;
75 $ENV{'CONTENT_TYPE'} = $r->content_type;
76
77 # this part is based on CGI::Lite
78
79 $cgi->close_all_files();
80 $cgi->{web_data} = {};
81 $cgi->{ordered_keys} = [];
82 $cgi->{all_handles} = [];
83 $cgi->{error_status} = 0;
84 $cgi->{error_message} = undef;
85
86 if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) {
87 my $query_string = $r->uri;
88 $query_string =~ s/[^\?]+\?(.*)/$1/;
89 $cgi->_decode_url_encoded_data (\$query_string, 'form');
90
91 } elsif ( $r->method eq 'POST' ) {
92
93 if ($r->content_type eq 'application/x-www-form-urlencoded') {
94 # local $^W = 0;
95 $cgi->_decode_url_encoded_data (\$r->content, 'form');
96 } elsif ($r->content_type =~ /multipart\/form-data/) {
97 my ($boundary) = $r->content_type =~ /boundary=(\S+)$/;
98 $cgi->_parse_multipart_data ($r->content_length, $boundary);
99 }
100 } else {
101 $c->send_error(RC_FORBIDDEN);
102 }
103
104 #my $param = $cgi->{web_data};
105 my $url = $r->url->path;
106
107 # XXX LOG
108 print $r->method," ",$url," " if ($self->{debug});
109
110 # is this static page?
111 my $static_html = $self->{'static_html'};
112 if ($static_html && -f "$static_html/$url") {
113 $c->send_file_response("$static_html/$url");
114 $c->close;
115 next;
116 }
117
118 my $html = &$coderef($url,$cgi->{web_data});
119
120 #
121 # send HTMLto client
122 #
123
124 my $res = HTTP::Response->new(RC_OK);
125 $res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' );
126 $res->content($html);
127 $c->send_response($res);
128
129 $c->close;
130 }
131 undef($c);
132 }
133 }
134
135 sub url {
136 my $self = shift;
137
138 return $self->{daemon}->url;
139 }

  ViewVC Help
Powered by ViewVC 1.1.26