]> sjero.net Git - wget/blobdiff - tests/HTTPServer.pm
[svn] Added support for Range header.
[wget] / tests / HTTPServer.pm
index c7de835fd22e7ba972427b993427141b0ac0de3a..88224f4f9be1c5b456ba7f2d6478462dc146b3a3 100755 (executable)
 #!/usr/bin/perl -w
 
+package HTTPServer;
+
+use strict;
+
 use HTTP::Daemon;
 use HTTP::Status;
 use HTTP::Headers;
 use HTTP::Response;
 
-use strict;
+our @ISA=qw(HTTP::Daemon);
+my $VERSION = 0.01;
 
-package HTTPServer;
+my $CRLF = "\015\012"; # "\r\n" is not portable
+my $log = undef;
+
+sub run {
+    my ($self, $urls, $synch_callback) = @_;
+    my $initialized = 0;
 
-sub run_daemon {
-    my %urls = @_;
-    my $server = HTTP::Daemon->new (LocalAddr => 'localhost', 
-                                    LocalPort => '8080',
-                                    ReuseAddr => 1) or die "Cannot create server!!!";
+    while (1) {
+        if (!$initialized) {
+            $synch_callback->();
+            $initialized = 1;
+        }        
                                 
-    while (my $con = $server->accept) {
+        my $con = $self->accept();
+        print STDERR "Accepted a new connection\n" if $log;
         while (my $req = $con->get_request) {
-            # print STDERR "method: ", $req->method, "\n";
-            if ($req->method eq "GET" and $urls{$req->url->path}) {
-                # print STDERR "requested URL: ", $req->url->path, "\n";
+            my $url_path = $req->url->path;
+            if ($url_path =~ m{/$}) {
+                $url_path .= 'index.html';
+            }
+            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;
+                next unless ($req->method eq "HEAD" || $req->method eq "GET");
                 
                 # create response
-                my $tmp = $urls{$req->url->path};
-                my $resp = HTTP::Response->new ($tmp->{'code'},
-                                                $tmp->{'msg'});
-                # print STDERR "HTTP::Response: \n", $resp->as_string;
+                my $tmp = $urls->{$url_path};
+                my $resp = HTTP::Response->new ($tmp->{code}, $tmp->{msg});
+                print STDERR "HTTP::Response: \n", $resp->as_string if $log;
                 
-                # 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;
-
-                # fill in content
-                $resp->content($tmp->{content});
-                # print STDERR "HTTP::Response with content: \n", $resp->as_string;
+                #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;
+                            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);
+                            next;
+                        }
+                        # fill in content
+                        $resp->content($tmp->{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;
+                print STDERR "HTTP::Response sent: \n", $resp->as_string if $log;
             } else {
-                print STDERR "requested wrong URL: ", $req->url->path, "\n";
+                print STDERR "Requested wrong URL: ", $url_path, "\n" if $log;
                 $con->send_error($HTTP::Status::RC_FORBIDDEN);
-            }
+                last;
+            }            
         }
+        print STDERR "Closing connection\n" if $log;
         $con->close;
-        undef($con);
-    }
-}
-
-sub run {
-    my $pid = fork();
-
-    if($pid == 0) {
-        run_daemon(@_);
     }
-
-    return $pid;
 }
 
 1;