]> sjero.net Git - wget/blob - tests/Test-proxied-https-auth.px
Make Test-proxied-https-auth.px pass, and ensure it returns the proper exit status...
[wget] / tests / Test-proxied-https-auth.px
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use WgetTest;  # For $WGETPATH.
6 use HTTP::Daemon;
7 use HTTP::Request;
8 use IO::Socket::SSL;
9
10 my $SOCKET = HTTP::Daemon->new (LocalAddr => 'localhost',
11     ReuseAddr => 1) or die "Cannot create server!!!";
12
13 sub get_request {
14     my $conn = shift;
15     my $content = '';
16     my $line;
17
18     while (defined ($line = <$conn>)) {
19         $content .= $line;
20         last if $line eq "\r\n";
21     }
22
23     my $rqst = HTTP::Request->parse($content)
24         or die "Couldn't parse request:\n$content\n";
25
26     return $rqst;
27 }
28
29 sub do_server {
30     my $alrm = alarm 10;
31
32     my $s = $SOCKET;
33     my $conn;
34     my $rqst;
35     my $rspn;
36     for my $expect_inner_auth (0, 1) {
37         $conn = $s->accept;
38         $rqst = $conn->get_request;
39
40         # TODO: expect no auth the first time, request it, expect it the second
41         #   time.
42
43         die "Method not CONNECT\n" if ($rqst->method ne 'CONNECT');
44         $rspn = HTTP::Response->new(200, 'OK');
45         $conn->send_response($rspn);
46
47         $conn = IO::Socket::SSL->new_from_fd($conn->fileno, SSL_server => 1,
48             SSL_passwd_cb => sub { return "Hello"; })
49             or die "Couldn't initiate SSL";
50
51         $rqst = &get_request($conn)
52             or die "Didn't get proxied request\n";
53
54         unless ($expect_inner_auth) {
55             die "Early proxied auth\n" if $rqst->header('Authorization');
56
57             # TODO: handle non-persistent connection here.
58             $rspn = HTTP::Response->new(401, 'Unauthorized', [
59                 'WWW-Authenticate' => 'Basic realm="gondor"',
60                 Connection => 'close'
61                 ]);
62             $rspn->protocol('HTTP/1.0');
63             print $rspn->as_string;
64             print $conn $rspn->as_string;
65         } else {
66             die "No proxied auth\n" unless $rqst->header('Authorization');
67
68             $rspn = HTTP::Response->new(200, 'OK', [
69                 'Content-Type' => 'text/plain',
70                 'Connection' => 'close',
71                 ], "foobarbaz\n");
72             $rspn->protocol('HTTP/1.0');
73             print "=====\n";
74             print $rspn->as_string;
75             print "\n=====\n";
76             print $conn $rspn->as_string;
77         }
78         $conn->close;
79     }
80     undef $conn;
81     undef $s;
82     alarm $alrm;
83 }
84
85 sub fork_server {
86     my $pid = fork;
87     die "Couldn't fork" if ($pid < 0);
88     return $pid if $pid;
89
90     &do_server;
91     exit;
92 }
93
94 system ('rm -f needs-auth.txt');
95 my $pid = &fork_server;
96
97 sleep 1;
98 my $cmdline = $WgetTest::WGETPATH . " --user=fiddle-dee-dee"
99     . " --password=Dodgson -e https_proxy=localhost:{{port}}"
100     . " --no-check-certificate"
101     . " https://no.such.domain/needs-auth.txt";
102 $cmdline =~ s/{{port}}/$SOCKET->sockport()/e;
103
104 my $code = system($cmdline);
105 system ('rm -f needs-auth.txt');
106
107 warn "Got code: $code\n" if $code;
108 kill ('TERM', $pid);
109 exit ($code >> 8);