+ $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 ($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) {
+ $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";
+ 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'}) {
+ # 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;
+ }
+}
+
+sub verify_request_headers {
+ my ($self, $req, $url_rec) = @_;
+
+ return 1 unless exists $url_rec->{'request_headers'};
+ for my $hdrname (keys %{$url_rec->{'request_headers'}}) {
+ my $rhdr = $req->header ($hdrname);
+ my $ehdr = $url_rec->{'request_headers'}{$hdrname};
+ unless (defined $rhdr && $rhdr =~ $ehdr) {
+ print STDERR "\n*** Mismatch on $hdrname: $rhdr =~ $ehdr\n";
+ return undef;
+ }
+ }
+
+ return 1;
+}
+
+sub _substitute_port {
+ my $self = shift;
+ my $ret = shift;
+ $ret =~ s/{{port}}/$self->sockport/eg;
+ return $ret;