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;
|
|
|
|
}
|
|
|
|
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;
|
2006-11-10 06:06:24 -05:00
|
|
|
if ($url_path =~ m{/$}) { # append 'index.html'
|
2006-04-27 05:33:36 -04:00
|
|
|
$url_path .= 'index.html';
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
2006-12-27 04:00:12 -05:00
|
|
|
#if ($url_path =~ m{^/}) { # remove trailing '/'
|
|
|
|
# $url_path = substr ($url_path, 1);
|
|
|
|
#}
|
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
|
|
|
|
2007-07-29 20:29:05 -04:00
|
|
|
my $url_rec = $urls->{$url_path};
|
|
|
|
$self->send_response($req, $url_rec, $con);
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-29 20:29:05 -04:00
|
|
|
sub send_response {
|
|
|
|
my ($self, $req, $url_rec, $con) = @_;
|
|
|
|
|
|
|
|
# create response
|
|
|
|
my ($code, $msg, $headers);
|
|
|
|
my $send_content = ($req->method eq "GET");
|
|
|
|
if (exists $url_rec->{'auth_method'}) {
|
|
|
|
($send_content, $code, $msg, $headers) =
|
|
|
|
$self->handle_auth($req, $url_rec);
|
|
|
|
} else {
|
|
|
|
($code, $msg) = @{$url_rec}{'code', 'msg'};
|
|
|
|
$headers = $url_rec->{headers};
|
|
|
|
}
|
|
|
|
my $resp = HTTP::Response->new ($code, $msg);
|
|
|
|
print STDERR "HTTP::Response: \n", $resp->as_string if $log;
|
|
|
|
|
|
|
|
while (my ($name, $value) = each %{$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 ($send_content) {
|
|
|
|
my $content = $url_rec->{content};
|
|
|
|
if (exists($url_rec->{headers}{"Content-Length"})) {
|
|
|
|
# Content-Length and length($content) don't match
|
|
|
|
# manually prepare the HTTP response
|
|
|
|
$con->send_basic_header($url_rec->{code}, $resp->message, $resp->protocol);
|
|
|
|
print $con $resp->headers_as_string($CRLF);
|
|
|
|
print $con $CRLF;
|
|
|
|
print $con $content;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ($req->header("Range")) {
|
|
|
|
$req->header("Range") =~ m/bytes=(\d*)-(\d*)/;
|
|
|
|
my $content_len = length($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($content, $start, $len);
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
# fill in content
|
|
|
|
$resp->content($content);
|
|
|
|
print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
|
|
|
|
}
|
|
|
|
|
|
|
|
$con->send_response($resp);
|
|
|
|
print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Generates appropriate response content based on the authentication
|
|
|
|
# status of the URL.
|
|
|
|
sub handle_auth {
|
|
|
|
my ($self, $req, $url_rec) = @_;
|
|
|
|
my ($send_content, $code, $msg, $headers);
|
|
|
|
# Catch failure to set code, msg:
|
|
|
|
$code = 500;
|
|
|
|
$msg = "Didn't set response code in handle_auth";
|
|
|
|
# Most cases, we don't want to send content.
|
|
|
|
$send_content = 0;
|
|
|
|
# Initialize headers
|
|
|
|
$headers = {};
|
|
|
|
my $authhdr = $req->header('Authorization');
|
|
|
|
|
|
|
|
# Have we sent the challenge yet?
|
|
|
|
unless (defined $url_rec->{auth_challenged}
|
|
|
|
&& $url_rec->{auth_challenged}) {
|
|
|
|
# Since we haven't challenged yet, we'd better not
|
|
|
|
# have received authentication (for our testing purposes).
|
|
|
|
if ($authhdr) {
|
|
|
|
$code = 400;
|
|
|
|
$msg = "You sent auth before I sent challenge";
|
|
|
|
} else {
|
|
|
|
# Send challenge
|
|
|
|
$code = 401;
|
|
|
|
$msg = "Authorization Required";
|
|
|
|
$headers->{'WWW-Authenticate'} = $url_rec->{'auth_method'}
|
|
|
|
. " realm=\"wget-test\"";
|
|
|
|
$url_rec->{auth_challenged} = 1;
|
|
|
|
}
|
|
|
|
} elsif (!defined($authhdr)) {
|
|
|
|
# We've sent the challenge; we should have received valid
|
|
|
|
# authentication with this one. A normal server would just
|
|
|
|
# resend the challenge; but since this is a test, wget just
|
|
|
|
# failed it.
|
|
|
|
$code = 400;
|
|
|
|
$msg = "You didn't send auth after I sent challenge";
|
|
|
|
} else {
|
|
|
|
my ($sent_method) = ($authhdr =~ /^(\S+)/g);
|
|
|
|
unless ($sent_method eq $url_rec->{'auth_method'}) {
|
|
|
|
# Not the authorization type we were expecting.
|
|
|
|
$code = 400;
|
|
|
|
$msg = "Expected auth type $url_rec->{'auth_method'} but got "
|
|
|
|
. "$sent_method";
|
|
|
|
} elsif (($sent_method eq 'Digest'
|
|
|
|
&& &verify_auth_digest($authhdr, $url_rec, \$msg))
|
|
|
|
||
|
|
|
|
($sent_method eq 'Basic'
|
|
|
|
&& &verify_auth_basic($authhdr, $url_rec, \$msg))) {
|
|
|
|
# SUCCESSFUL AUTH: send expected message, headers, content.
|
|
|
|
($code, $msg) = @{$url_rec}{'code', 'msg'};
|
|
|
|
$headers = $url_rec->{headers};
|
|
|
|
$send_content = 1;
|
|
|
|
} else {
|
|
|
|
$code = 400;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($send_content, $code, $msg, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub verify_auth_digest {
|
|
|
|
return undef; # Not yet implemented.
|
|
|
|
}
|
|
|
|
|
|
|
|
sub verify_auth_basic {
|
|
|
|
require MIME::Base64;
|
|
|
|
my ($authhdr, $url_rec, $msgref) = @_;
|
|
|
|
my $expected = MIME::Base64::encode_base64($url_rec->{'user'} . ':'
|
|
|
|
. $url_rec->{'passwd'}, '');
|
|
|
|
my ($got) = $authhdr =~ /^Basic (.*)$/;
|
|
|
|
if ($got eq $expected) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
$$msgref = "Wanted ${expected} got ${got}";
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-02 09:27:23 -05:00
|
|
|
1;
|
|
|
|
|
|
|
|
# vim: et ts=4 sw=4
|
|
|
|
|