12 our @ISA=qw(HTTP::Daemon);
15 my $CRLF = "\015\012"; # "\r\n" is not portable
19 my ($self, $urls) = @_;
21 while (my $con = $self->accept) {
22 while (my $req = $con->get_request) {
23 my $url_path = $req->url->path;
24 if ($url_path =~ m{/$}) {
25 $url_path .= 'index.html';
28 print STDERR "Method: ", $req->method, "\n";
29 print STDERR "Path: ", $url_path, "\n";
30 print STDERR "Available URLs: ", "\n";
31 foreach my $key (keys %$urls) {
32 print STDERR $key, "\n";
35 if (exists($urls->{$url_path})) {
36 print STDERR "Serving requested URL: ", $url_path, "\n" if $log;
37 next unless ($req->method eq "HEAD" || $req->method eq "GET");
40 my $tmp = $urls->{$url_path};
41 my $resp = HTTP::Response->new ($tmp->{code}, $tmp->{msg});
42 print STDERR "HTTP::Response: \n", $resp->as_string if $log;
44 #if (is_dynamic_url) { # dynamic resource
45 #} else { # static resource
47 while (my ($name, $value) = each %{$tmp->{headers}}) {
48 # print STDERR "setting header: $name = $value\n";
49 $resp->header($name => $value);
51 print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
53 if ($req->method eq "GET") {
54 if (exists($tmp->{headers}{"Content-Length"})) {
55 # Content-Length and length($tmp->{content}) don't match
56 # manually prepare the HTTP response
57 $con->send_basic_header($tmp->{code}, $resp->message, $resp->protocol);
58 print $con $resp->headers_as_string($CRLF);
60 print $con $tmp->{content};
64 $resp->content($tmp->{content});
65 print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
69 $con->send_response($resp);
70 print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
72 print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
73 $con->send_error($HTTP::Status::RC_FORBIDDEN);