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