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 print STDERR "Accepted a new connection\n" if $log;
23 while (my $req = $con->get_request) {
24 my $url_path = $req->url->path;
25 if ($url_path =~ m{/$}) {
26 $url_path .= 'index.html';
29 print STDERR "Method: ", $req->method, "\n";
30 print STDERR "Path: ", $url_path, "\n";
31 print STDERR "Available URLs: ", "\n";
32 foreach my $key (keys %$urls) {
33 print STDERR $key, "\n";
36 if (exists($urls->{$url_path})) {
37 print STDERR "Serving requested URL: ", $url_path, "\n" if $log;
38 next unless ($req->method eq "HEAD" || $req->method eq "GET");
41 my $tmp = $urls->{$url_path};
42 my $resp = HTTP::Response->new ($tmp->{code}, $tmp->{msg});
43 print STDERR "HTTP::Response: \n", $resp->as_string if $log;
45 #if (is_dynamic_url) { # dynamic resource
46 #} else { # static resource
48 while (my ($name, $value) = each %{$tmp->{headers}}) {
49 # print STDERR "setting header: $name = $value\n";
50 $resp->header($name => $value);
52 print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
54 if ($req->method eq "GET") {
55 if (exists($tmp->{headers}{"Content-Length"})) {
56 # Content-Length and length($tmp->{content}) don't match
57 # manually prepare the HTTP response
58 $con->send_basic_header($tmp->{code}, $resp->message, $resp->protocol);
59 print $con $resp->headers_as_string($CRLF);
61 print $con $tmp->{content};
65 $resp->content($tmp->{content});
66 print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
70 $con->send_response($resp);
71 print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
73 print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
74 $con->send_error($HTTP::Status::RC_FORBIDDEN);
78 print STDERR "Closing connection\n" if $log;