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