]> sjero.net Git - wget/blobdiff - tests/HTTPServer.pm
Allow proxied-https-auth test to function when building outside of source dir.
[wget] / tests / HTTPServer.pm
index a307b737310cb644abb489d1132d06211d02e38f..e3c38e6f5f02c31a580813591b6e21a2726a6dc2 100644 (file)
@@ -1,8 +1,7 @@
-#!/usr/bin/perl -w
-
 package HTTPServer;
 
 use strict;
+use warnings;
 
 use HTTP::Daemon;
 use HTTP::Status;
@@ -23,7 +22,7 @@ sub run {
         if (!$initialized) {
             $synch_callback->();
             $initialized = 1;
-        }        
+        }
         my $con = $self->accept();
         print STDERR "Accepted a new connection\n" if $log;
         while (my $req = $con->get_request) {
@@ -45,14 +44,14 @@ sub run {
             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");
-                
+
                 my $url_rec = $urls->{$url_path};
                 $self->send_response($req, $url_rec, $con);
             } else {
                 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;
@@ -92,13 +91,13 @@ sub send_response {
             print $con $content;
             next;
         }
-        if ($req->header("Range")) {
+        if ($req->header("Range") && !$url_rec->{'force_code'}) {
             $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);
-            if ($len) {
+            if ($len > 0) {
                 $resp->header("Accept-Ranges" => "bytes");
                 $resp->header("Content-Length" => $len);
                 $resp->header("Content-Range"
@@ -120,6 +119,7 @@ sub send_response {
             next;
         }
         # fill in content
+        $content = $self->_substitute_port($content);
         $resp->content($content);
         print STDERR "HTTP::Response with content: \n", $resp->as_string if $log;
     }
@@ -143,8 +143,7 @@ sub handle_auth {
     my $authhdr = $req->header('Authorization');
 
     # Have we sent the challenge yet?
-    unless (defined $url_rec->{auth_challenged}
-        && $url_rec->{auth_challenged}) {
+    unless ($url_rec->{auth_challenged} || $url_rec->{auth_no_challenge}) {
         # Since we haven't challenged yet, we'd better not
         # have received authentication (for our testing purposes).
         if ($authhdr) {
@@ -165,6 +164,9 @@ sub handle_auth {
         # failed it.
         $code = 400;
         $msg  = "You didn't send auth after I sent challenge";
+        if ($url_rec->{auth_no_challenge}) {
+            $msg = "--auth-no-challenge but no auth sent."
+        }
     } else {
         my ($sent_method) = ($authhdr =~ /^(\S+)/g);
         unless ($sent_method eq $url_rec->{'auth_method'}) {
@@ -207,6 +209,13 @@ sub verify_auth_basic {
     }
 }
 
+sub _substitute_port {
+    my $self = shift;
+    my $ret = shift;
+    $ret =~ s/{{port}}/$self->sockport/eg;
+    return $ret;
+}
+
 1;
 
 # vim: et ts=4 sw=4