183
|
1 #!/usr/bin/perl
|
|
2
|
|
3 # (C) Maxim Dounin
|
|
4 # (C) Valentin Bartenev
|
|
5
|
|
6 # Tests for host parsing in requests.
|
|
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 rewrite/)->plan(35);
|
|
26
|
|
27 $t->write_file_expand('nginx.conf', <<'EOF');
|
|
28
|
|
29 %%TEST_GLOBALS%%
|
|
30
|
|
31 daemon off;
|
|
32
|
|
33 events {
|
|
34 }
|
|
35
|
|
36 http {
|
|
37 %%TEST_GLOBALS_HTTP%%
|
|
38
|
|
39 server {
|
|
40 listen 127.0.0.1:8080;
|
|
41 server_name localhost;
|
|
42
|
|
43 location / {
|
|
44 return 200 $host;
|
|
45 }
|
|
46 }
|
|
47 }
|
|
48
|
|
49 EOF
|
|
50
|
|
51 $t->run();
|
|
52
|
|
53 ###############################################################################
|
|
54
|
|
55
|
|
56 is(http_host_header('www.abcd-ef.g02.xyz'), 'www.abcd-ef.g02.xyz',
|
|
57 'domain w/o port (host header)');
|
|
58 is(http_host_header('abcd-ef.g02.xyz:8080'), 'abcd-ef.g02.xyz',
|
|
59 'domain w/port (host header)');
|
|
60
|
|
61 is(http_absolute_path('abcd-ef.g02.xyz'), 'abcd-ef.g02.xyz',
|
|
62 'domain w/o port (absolute request)');
|
|
63 is(http_absolute_path('www.abcd-ef.g02.xyz:10'), 'www.abcd-ef.g02.xyz',
|
|
64 'domain w/port (absolute request)');
|
|
65
|
|
66
|
|
67 is(http_host_header('www.abcd-ef.g02.xyz.'), 'www.abcd-ef.g02.xyz',
|
|
68 'domain w/ ending dot w/o port (host header)');
|
|
69
|
|
70 TODO:{
|
|
71 local $TODO = 'fix this';
|
|
72
|
|
73 is(http_host_header('abcd-ef.g02.xyz.:88'), 'abcd-ef.g02.xyz',
|
|
74 'domain w/ ending dot w/port (host header)');
|
|
75
|
|
76 }
|
|
77
|
|
78 is(http_absolute_path('www.abcd-ef.g02.xyz.'), 'www.abcd-ef.g02.xyz',
|
|
79 'domain w/ ending dot w/o port (absolute request)');
|
|
80 is(http_absolute_path('abcd-ef.g02.xyz.:2'), 'abcd-ef.g02.xyz',
|
|
81 'domain w/ ending dot w/port (absolute request)');
|
|
82
|
|
83
|
|
84 is(http_absolute_path('AbC-d93.0.34ZhGt-s.nk.Ru'), 'abc-d93.0.34zhgt-s.nk.ru',
|
|
85 'mixed case domain w/o port (absolute request)');
|
|
86 is(http_host_header('AbC-d93.0.34ZhGt-s.nk.Ru:88'), 'abc-d93.0.34zhgt-s.nk.ru',
|
|
87 'mixed case domain w/port (host header)');
|
|
88
|
|
89
|
|
90 is(http_host_header('123.40.56.78'), '123.40.56.78',
|
|
91 'ipv4 w/o port (host header)');
|
|
92 is(http_host_header('123.49.0.78:987'), '123.49.0.78',
|
|
93 'ipv4 w/port (host header)');
|
|
94
|
|
95 is(http_absolute_path('123.49.0.78'), '123.49.0.78',
|
|
96 'ipv4 w/o port (absolute request)');
|
|
97 is(http_absolute_path('123.40.56.78:123'), '123.40.56.78',
|
|
98 'ipv4 w/port (absolute request)');
|
|
99
|
|
100 TODO: {
|
|
101 local $TODO = 'ipv6 literals';
|
|
102
|
|
103 is(http_host_header('[abcd::ef98:0:7654:321]'), '[abcd::ef98:0:7654:321]',
|
|
104 'ipv6 literal w/o port (host header)');
|
|
105
|
|
106 }
|
|
107
|
|
108 is(http_host_header('[abcd::ef98:0:7654:321]:80'), '[abcd::ef98:0:7654:321]',
|
|
109 'ipv6 literal w/port (host header)');
|
|
110
|
|
111 TODO: {
|
|
112 local $TODO = 'ipv6 literals';
|
|
113
|
|
114 is(http_absolute_path('[abcd::ef98:0:7654:321]'), '[abcd::ef98:0:7654:321]',
|
|
115 'ipv6 literal w/o port (absolute request)');
|
|
116 is(http_absolute_path('[abcd::ef98:0:7654:321]:5'), '[abcd::ef98:0:7654:321]',
|
|
117 'ipv6 literal w/port (absolute request)');
|
|
118
|
|
119 is(http_host_header('[::ffff:12.30.67.89]'), '[::ffff:12.30.67.89]',
|
|
120 'ipv4-mapped ipv6 w/o port (host header)');
|
|
121 }
|
|
122
|
|
123 is(http_host_header('[::123.45.67.89]:4321'), '[::123.45.67.89]',
|
|
124 'ipv4-mapped ipv6 w/port (host header)');
|
|
125
|
|
126 TODO: {
|
|
127 local $TODO = 'ipv6 literals';
|
|
128
|
|
129 is(http_absolute_path('[::123.45.67.89]'), '[::123.45.67.89]',
|
|
130 'ipv4-mapped ipv6 w/o port (absolute request)');
|
|
131 is(http_absolute_path('[::ffff:12.30.67.89]:4321'), '[::ffff:12.30.67.89]',
|
|
132 'ipv4-mapped ipv6 w/port (absolute request)');
|
|
133
|
|
134 }
|
|
135
|
|
136 like(http_host_header('example.com/\:552', 1), qr/400/,
|
|
137 'domain w/ path separators (host header)');
|
|
138 like(http_absolute_path('\e/xample.com', 1), qr/400/,
|
|
139 'domain w/ path separators (absolute request)');
|
|
140
|
|
141 like(http_host_header('..examp-LE.com', 1), qr/400/,
|
|
142 'domain w/ double dot (host header)');
|
|
143 like(http_absolute_path('com.exa-m.45..:', 1), qr/400/,
|
|
144 'domain w/ double dot (absolute request)');
|
|
145
|
|
146
|
|
147 like(http_host_header('[abcd::e\f98:0/:7654:321]', 1), qr/400/,
|
|
148 'ipv6 literal w/ path separators (host header)');
|
|
149 like(http_absolute_path('[abcd\::ef98:0:7654:321/]:12', 1), qr/400/,
|
|
150 'ipv6 literal w/ path separators (absolute request)');
|
|
151
|
|
152 like(http_host_header('[abcd::ef98:0:7654:321]..:98', 1), qr/400/,
|
|
153 'ipv6 literal w/ double dot (host header)');
|
|
154 like(http_absolute_path('[ab..cd::ef98:0:7654:321]', 1), qr/400/,
|
|
155 'ipv6 literal w/ double dot (absolute request)');
|
|
156
|
|
157
|
|
158 like(http_host_header('[abcd::ef98:0:7654:321]..:98', 1), qr/400/,
|
|
159 'ipv6 literal w/ double dot (host header)');
|
|
160 like(http_absolute_path('[ab..cd::ef98:0:7654:321]', 1), qr/400/,
|
|
161 'ipv6 literal w/ double dot (absolute request)');
|
|
162
|
|
163
|
|
164 # As per RFC 3986,
|
|
165 # http://tools.ietf.org/html/rfc3986#section-3.2.2
|
|
166 #
|
|
167 # IP-literal = "[" ( IPv6address / IPvFuture ) "]"
|
|
168 #
|
|
169 # IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
|
|
170 #
|
|
171 # sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
|
|
172 # / "*" / "+" / "," / ";" / "="
|
|
173 #
|
|
174 # unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
|
175 #
|
|
176
|
|
177 TODO: {
|
|
178 local $TODO = 'IPvFuture';
|
|
179
|
|
180 is(http_host_header(
|
|
181 '[v0123456789aBcDeF.!$&\'()*+,;=-._~AbCdEfGhIjKlMnOpQrStUvWxYz'
|
|
182 . '0123456789:]'),
|
|
183 '[v0123456789abcdef.!$&\'()*+,;=-._~abcdefghijklmnopqrstuvwxyz'
|
|
184 . '0123456789:]',
|
|
185 'IPvFuture all symbols (host header)');
|
|
186
|
|
187 is(http_absolute_path(
|
|
188 '[v0123456789aBcDeF.!$&\'()*+,;=-._~AbCdEfGhIjKlMnOpQrStUvWxYz'
|
|
189 . '0123456789:]'),
|
|
190 '[v0123456789abcdef.!$&\'()*+,;=-._~abcdefghijklmnopqrstuvwxyz'
|
|
191 . '0123456789:]',
|
|
192 'IPvFuture all symbols (absolute request)');
|
|
193
|
|
194 }
|
|
195
|
|
196 TODO: {
|
|
197 local $TODO = 'or not TODO';
|
|
198
|
|
199 is(http_host_header('123.40.56.78:9000:80'), '123.40.56.78',
|
|
200 'double port hack');
|
|
201
|
|
202 }
|
|
203
|
|
204 ###############################################################################
|
|
205
|
|
206 sub http_host_header {
|
|
207 my ($host, $all) = @_;
|
|
208 my ($r) = http(<<EOF);
|
|
209 GET / HTTP/1.0
|
|
210 Host: $host
|
|
211
|
|
212 EOF
|
|
213 return ($all ? $r : Test::Nginx::http_content($r));
|
|
214 }
|
|
215
|
|
216 sub http_absolute_path {
|
|
217 my ($host, $all) = @_;
|
|
218 my ($r) = http(<<EOF);
|
|
219 GET http://$host/ HTTP/1.0
|
|
220 Host: localhost
|
|
221
|
|
222 EOF
|
|
223 return ($all ? $r : Test::Nginx::http_content($r));
|
|
224 }
|