|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Sergey Kandaurov |
| 4 | +# (C) Nginx, Inc. |
| 5 | + |
| 6 | +# Tests for the proxy_pass directive with HTTP/2 and variables. |
| 7 | + |
| 8 | +############################################################################### |
| 9 | + |
| 10 | +use warnings; |
| 11 | +use strict; |
| 12 | + |
| 13 | +use Test::More; |
| 14 | + |
| 15 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 16 | + |
| 17 | +use lib 'lib'; |
| 18 | +use Test::Nginx; |
| 19 | + |
| 20 | +############################################################################### |
| 21 | + |
| 22 | +select STDERR; $| = 1; |
| 23 | +select STDOUT; $| = 1; |
| 24 | + |
| 25 | +my $t = Test::Nginx->new()->has(qw/http http_ssl http_v2 proxy rewrite/) |
| 26 | + ->has_daemon('openssl')->write_file_expand('nginx.conf', <<'EOF'); |
| 27 | +
|
| 28 | +%%TEST_GLOBALS%% |
| 29 | +
|
| 30 | +daemon off; |
| 31 | +
|
| 32 | +events { |
| 33 | +} |
| 34 | +
|
| 35 | +http { |
| 36 | + %%TEST_GLOBALS_HTTP%% |
| 37 | +
|
| 38 | + upstream u { |
| 39 | + server 127.0.0.1:8081; |
| 40 | + } |
| 41 | +
|
| 42 | + resolver 127.0.0.1:%%PORT_8982_UDP%%; |
| 43 | +
|
| 44 | + server { |
| 45 | + listen 127.0.0.1:8080; |
| 46 | + server_name localhost; |
| 47 | +
|
| 48 | + location / { |
| 49 | + proxy_pass http://$host:%%PORT_8081%%; |
| 50 | + proxy_http_version 2.0; |
| 51 | + } |
| 52 | +
|
| 53 | + location /http { |
| 54 | + proxy_pass http://$host:%%PORT_8081%%; |
| 55 | + proxy_http_version 2.0; |
| 56 | + } |
| 57 | +
|
| 58 | + location /https { |
| 59 | + proxy_pass https://$host:%%PORT_8082%%; |
| 60 | + proxy_http_version 2.0; |
| 61 | + } |
| 62 | +
|
| 63 | + location /arg { |
| 64 | + proxy_pass $arg_b; |
| 65 | + proxy_http_version 2.0; |
| 66 | + } |
| 67 | +
|
| 68 | + location /stub { |
| 69 | + proxy_pass http://127.0.0.1:%%PORT_8081%%/backend; |
| 70 | + proxy_http_version 2.0; |
| 71 | + } |
| 72 | +
|
| 73 | + location /upath { |
| 74 | + proxy_pass http://u/backend; |
| 75 | + proxy_http_version 2.0; |
| 76 | + } |
| 77 | + } |
| 78 | +
|
| 79 | + server { |
| 80 | + listen 127.0.0.1:8081 http2; |
| 81 | + listen 127.0.0.1:8082 http2 ssl; |
| 82 | + server_name localhost; |
| 83 | +
|
| 84 | + ssl_certificate_key localhost.key; |
| 85 | + ssl_certificate localhost.crt; |
| 86 | +
|
| 87 | + location / { |
| 88 | + return 200 $http_host; |
| 89 | + } |
| 90 | +
|
| 91 | + location /backend { |
| 92 | + return 200 "SEE-THIS"; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | +
|
| 97 | +EOF |
| 98 | + |
| 99 | +$t->write_file('openssl.conf', <<EOF); |
| 100 | +[ req ] |
| 101 | +default_bits = 2048 |
| 102 | +encrypt_key = no |
| 103 | +distinguished_name = req_distinguished_name |
| 104 | +[ req_distinguished_name ] |
| 105 | +EOF |
| 106 | + |
| 107 | +my $d = $t->testdir(); |
| 108 | + |
| 109 | +foreach my $name ('localhost') { |
| 110 | + system('openssl req -x509 -new ' |
| 111 | + . "-config $d/openssl.conf -subj /CN=$name/ " |
| 112 | + . "-out $d/$name.crt -keyout $d/$name.key " |
| 113 | + . ">>$d/openssl.out 2>&1") == 0 |
| 114 | + or die "Can't create certificate for $name: $!\n"; |
| 115 | +} |
| 116 | + |
| 117 | +$t->run_daemon(\&dns_daemon, port(8982), $t); |
| 118 | + |
| 119 | +# suppress deprecation warning |
| 120 | + |
| 121 | +open OLDERR, ">&", \*STDERR; close STDERR; |
| 122 | +$t->run()->plan(7); |
| 123 | +open STDERR, ">&", \*OLDERR; |
| 124 | + |
| 125 | +$t->waitforfile($t->testdir . '/' . port(8982)); |
| 126 | + |
| 127 | +############################################################################### |
| 128 | + |
| 129 | +like(http_get('/basic'), qr/200 OK/, 'no scheme'); |
| 130 | +like(http_get('/http'), qr/200 OK/, 'http scheme'); |
| 131 | + |
| 132 | +SKIP: { |
| 133 | +skip 'OpenSSL too old', 1 |
| 134 | + if $t->has_module('OpenSSL') |
| 135 | + and not $t->has_feature('openssl:1.0.2'); |
| 136 | + |
| 137 | +like(http_get('/https'), qr/200 OK/, 'https scheme'); |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +like(http_get('/arg?b=http://127.0.0.1:' . port(8081)), qr/200 OK/, 'addrs'); |
| 142 | +like(http_get('/arg?b=http://u'), qr/200 OK/, 'no_port'); |
| 143 | + |
| 144 | +like(http_get('/stub'), qr/SEE-THIS/, 'proxy_pass with uri'); |
| 145 | +like(http_get('/upath'), qr/SEE-THIS/, 'proxy_pass with uri to upstream'); |
| 146 | + |
| 147 | +############################################################################### |
| 148 | + |
| 149 | +sub reply_handler { |
| 150 | + my ($recv_data) = @_; |
| 151 | + |
| 152 | + my (@name, @rdata); |
| 153 | + |
| 154 | + use constant NOERROR => 0; |
| 155 | + use constant A => 1; |
| 156 | + use constant IN => 1; |
| 157 | + |
| 158 | + # default values |
| 159 | + |
| 160 | + my ($hdr, $rcode, $ttl) = (0x8180, NOERROR, 3600); |
| 161 | + |
| 162 | + # decode name |
| 163 | + |
| 164 | + my ($len, $offset) = (undef, 12); |
| 165 | + while (1) { |
| 166 | + $len = unpack("\@$offset C", $recv_data); |
| 167 | + last if $len == 0; |
| 168 | + $offset++; |
| 169 | + push @name, unpack("\@$offset A$len", $recv_data); |
| 170 | + $offset += $len; |
| 171 | + } |
| 172 | + |
| 173 | + $offset -= 1; |
| 174 | + my ($id, $type, $class) = unpack("n x$offset n2", $recv_data); |
| 175 | + |
| 176 | + my $name = join('.', @name); |
| 177 | + if ($name eq 'localhost' && $type == A) { |
| 178 | + push @rdata, rd_addr($ttl, '127.0.0.1'); |
| 179 | + } |
| 180 | + |
| 181 | + $len = @name; |
| 182 | + pack("n6 (C/a*)$len x n2", $id, $hdr | $rcode, 1, scalar @rdata, |
| 183 | + 0, 0, @name, $type, $class) . join('', @rdata); |
| 184 | +} |
| 185 | + |
| 186 | +sub rd_addr { |
| 187 | + my ($ttl, $addr) = @_; |
| 188 | + |
| 189 | + my $code = 'split(/\./, $addr)'; |
| 190 | + |
| 191 | + return pack 'n3N', 0xc00c, A, IN, $ttl if $addr eq ''; |
| 192 | + |
| 193 | + pack 'n3N nC4', 0xc00c, A, IN, $ttl, eval "scalar $code", eval($code); |
| 194 | +} |
| 195 | + |
| 196 | +sub dns_daemon { |
| 197 | + my ($port, $t) = @_; |
| 198 | + |
| 199 | + my ($data, $recv_data); |
| 200 | + my $socket = IO::Socket::INET->new( |
| 201 | + LocalAddr => '127.0.0.1', |
| 202 | + LocalPort => $port, |
| 203 | + Proto => 'udp', |
| 204 | + ) |
| 205 | + or die "Can't create listening socket: $!\n"; |
| 206 | + |
| 207 | + # signal we are ready |
| 208 | + |
| 209 | + open my $fh, '>', $t->testdir() . '/' . $port; |
| 210 | + close $fh; |
| 211 | + |
| 212 | + while (1) { |
| 213 | + $socket->recv($recv_data, 65536); |
| 214 | + $data = reply_handler($recv_data); |
| 215 | + $socket->send($data); |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +############################################################################### |
0 commit comments