]> sjero.net Git - wget/blobdiff - tests/HTTPServer.pm
[svn] Minor fixes to test suite.
[wget] / tests / HTTPServer.pm
index 6747359ec648cde68f435f1946194a4bb0b434af..d29ea307f5dcbc80cbdb1389be5cb6811d8efa16 100755 (executable)
@@ -16,15 +16,25 @@ my $CRLF = "\015\012"; # "\r\n" is not portable
 my $log = undef;
 
 sub run {
-    my ($self, $urls) = @_;
+    my ($self, $urls, $synch_callback) = @_;
+    my $initialized = 0;
+
+    while (1) {
+        if (!$initialized) {
+            $synch_callback->();
+            $initialized = 1;
+        }        
                                 
-    while (my $con = $self->accept) {
+        my $con = $self->accept();
         print STDERR "Accepted a new connection\n" if $log;
         while (my $req = $con->get_request) {
             my $url_path = $req->url->path;
-            if ($url_path =~ m{/$}) {
+            if ($url_path =~ m{/$}) { # append 'index.html'
                 $url_path .= 'index.html';
             }
+            #if ($url_path =~ m{^/}) { # remove trailing '/'
+            #    $url_path = substr ($url_path, 1);
+            #}
             if ($log) {
                 print STDERR "Method: ", $req->method, "\n";
                 print STDERR "Path: ", $url_path, "\n";
@@ -58,7 +68,24 @@ sub run {
                             $con->send_basic_header($tmp->{code}, $resp->message, $resp->protocol);
                             print $con $resp->headers_as_string($CRLF);
                             print $con $CRLF;
-                            print $con $tmp->{content};                                
+                            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
@@ -77,7 +104,6 @@ sub run {
         }
         print STDERR "Closing connection\n" if $log;
         $con->close;
-        undef($con);
     }
 }