2005-11-02 09:27:23 -05:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
2005-12-05 08:35:07 -05:00
|
|
|
package HTTPServer;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
2005-11-02 09:27:23 -05:00
|
|
|
use HTTP::Daemon;
|
|
|
|
use HTTP::Status;
|
|
|
|
use HTTP::Headers;
|
|
|
|
use HTTP::Response;
|
|
|
|
|
2005-12-05 08:35:07 -05:00
|
|
|
our @ISA=qw(HTTP::Daemon);
|
2006-01-24 07:15:58 -05:00
|
|
|
my $VERSION = 0.01;
|
2005-11-02 09:27:23 -05:00
|
|
|
|
2005-12-05 08:35:07 -05:00
|
|
|
my $CRLF = "\015\012"; # "\r\n" is not portable
|
2006-01-24 07:15:58 -05:00
|
|
|
my $log = undef;
|
2005-11-02 09:27:23 -05:00
|
|
|
|
2005-12-05 08:35:07 -05:00
|
|
|
sub run {
|
2006-05-29 05:15:06 -04:00
|
|
|
my ($self, $urls, $synch_callback) = @_;
|
|
|
|
my $initialized = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (!$initialized) {
|
|
|
|
$synch_callback->();
|
|
|
|
$initialized = 1;
|
|
|
|
}
|
2005-11-02 09:27:23 -05:00
|
|
|
|
2006-05-29 05:15:06 -04:00
|
|
|
my $con = $self->accept();
|
2006-05-26 08:55:34 -04:00
|
|
|
print STDERR "Accepted a new connection\n" if $log;
|
2005-11-02 09:27:23 -05:00
|
|
|
while (my $req = $con->get_request) {
|
2006-04-27 05:33:36 -04:00
|
|
|
my $url_path = $req->url->path;
|
|
|
|
if ($url_path =~ m{/$}) {
|
|
|
|
$url_path .= 'index.html';
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
2006-04-27 05:33:36 -04:00
|
|
|
if ($log) {
|
|
|
|
print STDERR "Method: ", $req->method, "\n";
|
|
|
|
print STDERR "Path: ", $url_path, "\n";
|
|
|
|
print STDERR "Available URLs: ", "\n";
|
|
|
|
foreach my $key (keys %$urls) {
|
|
|
|
print STDERR $key, "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (exists($urls->{$url_path})) {
|
|
|
|
print STDERR "Serving requested URL: ", $url_path, "\n" if $log;
|
2005-12-05 08:35:07 -05:00
|
|
|
next unless ($req->method eq "HEAD" || $req->method eq "GET");
|
2006-01-24 07:15:58 -05:00
|
|
|
|
2005-11-02 09:27:23 -05:00
|
|
|
# create response
|
2006-04-27 05:33:36 -04:00
|
|
|
my $tmp = $urls->{$url_path};
|
|
|
|
my $resp = HTTP::Response->new ($tmp->{code}, $tmp->{msg});
|
2006-01-24 07:15:58 -05:00
|
|
|
print STDERR "HTTP::Response: \n", $resp->as_string if $log;
|
2005-11-02 09:27:23 -05:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
#if (is_dynamic_url) { # dynamic resource
|
|
|
|
#} else { # static resource
|
|
|
|
# fill in headers
|
|
|
|
while (my ($name, $value) = each %{$tmp->{headers}}) {
|
|
|
|
# print STDERR "setting header: $name = $value\n";
|
|
|
|
$resp->header($name => $value);
|
|
|
|
}
|
|
|
|
print STDERR "HTTP::Response with headers: \n", $resp->as_string if $log;
|
|
|
|
|
|
|
|
if ($req->method eq "GET") {
|
|
|
|
if (exists($tmp->{headers}{"Content-Length"})) {
|
|
|
|
# Content-Length and length($tmp->{content}) don't match
|
|
|
|
# manually prepare the HTTP response
|
|
|
|
$con->send_basic_header($tmp->{code}, $resp->message, $resp->protocol);
|
|
|
|
print $con $resp->headers_as_string($CRLF);
|
|
|
|
print $con $CRLF;
|
2006-08-17 05:15:00 -04:00
|
|
|
print $con $tmp->{content};
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ($req->header("Range")) {
|
|
|
|
$req->header("Range") =~ m/bytes=(\d*)-(\d*)/;
|
|
|
|
my $content_len = length($tmp->{content});
|
|
|
|
my $start = $1 ? $1 : 0;
|
|
|
|
my $end = $2 ? $2 : ($content_len - 1);
|
|
|
|
my $len = $2 ? ($2 - $start) : ($content_len - $start);
|
|
|
|
$resp->header("Accept-Ranges" => "bytes");
|
|
|
|
$resp->header("Content-Length" => $len);
|
|
|
|
$resp->header("Content-Range" => "bytes $start-$end/$content_len");
|
|
|
|
$resp->header("Keep-Alive" => "timeout=15, max=100");
|
|
|
|
$resp->header("Connection" => "Keep-Alive");
|
|
|
|
$con->send_basic_header(206, "Partial Content", $resp->protocol);
|
|
|
|
print $con $resp->headers_as_string($CRLF);
|
|
|
|
print $con $CRLF;
|
|
|
|
print $con substr($tmp->{content}, $start, $len);
|
2006-01-24 07:15:58 -05:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
# fill in content
|
|
|
|
$resp->content($tmp->{content});
|
|
|
|
print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
|
2005-12-05 08:35:07 -05:00
|
|
|
}
|
2006-01-24 07:15:58 -05:00
|
|
|
#}
|
2005-11-02 09:27:23 -05:00
|
|
|
|
|
|
|
$con->send_response($resp);
|
2006-01-24 07:15:58 -05:00
|
|
|
print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
|
2005-11-02 09:27:23 -05:00
|
|
|
} else {
|
2006-04-27 05:33:36 -04:00
|
|
|
print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
|
2005-11-02 09:27:23 -05:00
|
|
|
$con->send_error($HTTP::Status::RC_FORBIDDEN);
|
2006-05-26 08:55:34 -04:00
|
|
|
last;
|
|
|
|
}
|
2005-11-02 09:27:23 -05:00
|
|
|
}
|
2006-05-26 08:55:34 -04:00
|
|
|
print STDERR "Closing connection\n" if $log;
|
2005-11-02 09:27:23 -05:00
|
|
|
$con->close;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
# vim: et ts=4 sw=4
|
|
|
|
|