Mercurial > hg > nginx-tests
annotate h2.t @ 650:8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Tue, 18 Aug 2015 16:54:41 +0300 |
parents | 4e36550410b3 |
children | 9f66f0029dca |
rev | line source |
---|---|
646 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for HTTP/2 protocol [RFC7540]. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 use IO::Select; | |
16 use Socket qw/ CRLF /; | |
17 | |
18 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
19 | |
20 use lib 'lib'; | |
21 use Test::Nginx; | |
22 | |
23 ############################################################################### | |
24 | |
25 select STDERR; $| = 1; | |
26 select STDOUT; $| = 1; | |
27 | |
650
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
28 eval { require IO::Socket::SSL; }; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
29 plan(skip_all => 'IO::Socket::SSL not installed') if $@; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
30 eval { IO::Socket::SSL::SSL_VERIFY_NONE(); }; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
31 plan(skip_all => 'IO::Socket::SSL too old') if $@; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
32 |
646 | 33 my $t = Test::Nginx->new()->has(qw/http http_ssl http_v2 proxy cache/) |
650
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
34 ->has(qw/limit_conn rewrite realip shmem/) |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
35 ->has_daemon('openssl')->plan(142); |
646 | 36 |
37 $t->write_file_expand('nginx.conf', <<'EOF'); | |
38 | |
39 %%TEST_GLOBALS%% | |
40 | |
41 daemon off; | |
42 | |
43 events { | |
44 } | |
45 | |
46 http { | |
47 %%TEST_GLOBALS_HTTP%% | |
48 | |
49 proxy_cache_path %%TESTDIR%%/cache keys_zone=NAME:1m; | |
50 limit_conn_zone $binary_remote_addr zone=conn:1m; | |
51 output_buffers 2 16k; | |
52 | |
53 server { | |
54 listen 127.0.0.1:8080 http2; | |
55 listen 127.0.0.1:8081; | |
56 listen 127.0.0.1:8082 proxy_protocol http2; | |
57 listen 127.0.0.1:8084 http2 ssl; | |
58 server_name localhost; | |
59 | |
60 ssl_certificate_key localhost.key; | |
61 ssl_certificate localhost.crt; | |
62 | |
63 location / { | |
64 add_header X-Header X-Foo; | |
65 add_header X-Sent-Foo $http_x_foo; | |
66 add_header X-Referer $http_referer; | |
67 return 200 'body'; | |
68 } | |
69 location /t { | |
70 } | |
71 location /t3.html { | |
72 limit_conn conn 1; | |
73 } | |
74 location /gzip.html { | |
75 gzip on; | |
76 gzip_min_length 0; | |
77 alias %%TESTDIR%%/t2.html; | |
78 } | |
79 location /pp { | |
80 set_real_ip_from 127.0.0.1/32; | |
81 real_ip_header proxy_protocol; | |
82 alias %%TESTDIR%%/t2.html; | |
83 add_header X-PP $remote_addr; | |
84 } | |
650
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
85 location /h2 { |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
86 return 200 $http2; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
87 } |
646 | 88 location /redirect { |
89 error_page 405 /; | |
90 return 405; | |
91 } | |
92 location /return301 { | |
93 return 301; | |
94 } | |
95 location /return301_absolute { | |
96 return 301 text; | |
97 } | |
98 location /return301_relative { | |
99 return 301 /; | |
100 } | |
101 location /proxy/ { | |
102 add_header X-UC-a $upstream_cookie_a; | |
103 add_header X-UC-c $upstream_cookie_c; | |
104 proxy_pass http://127.0.0.1:8083/; | |
105 proxy_cache NAME; | |
106 proxy_cache_valid 1m; | |
107 } | |
108 location /proxy2/ { | |
109 add_header X-Body "$request_body"; | |
110 proxy_pass http://127.0.0.1:8081/; | |
111 proxy_cache NAME; | |
112 proxy_cache_valid 1m; | |
113 } | |
114 location /proxy_buffering_off { | |
115 proxy_pass http://127.0.0.1:8081/; | |
116 proxy_cache NAME; | |
117 proxy_cache_valid 1m; | |
118 proxy_buffering off; | |
119 } | |
120 location /set-cookie { | |
121 add_header Set-Cookie a=b; | |
122 add_header Set-Cookie c=d; | |
123 return 200; | |
124 } | |
125 location /cookie { | |
126 add_header X-Cookie $http_cookie; | |
127 add_header X-Cookie-a $cookie_a; | |
128 add_header X-Cookie-c $cookie_c; | |
129 return 200; | |
130 } | |
131 } | |
132 | |
133 server { | |
134 listen 127.0.0.1:8085 http2; | |
135 server_name localhost; | |
136 return 200 first; | |
137 } | |
138 | |
139 server { | |
140 listen 127.0.0.1:8085 http2; | |
141 server_name localhost2; | |
142 return 200 second; | |
143 } | |
144 } | |
145 | |
146 EOF | |
147 | |
148 $t->write_file('openssl.conf', <<EOF); | |
149 [ req ] | |
150 default_bits = 2048 | |
151 encrypt_key = no | |
152 distinguished_name = req_distinguished_name | |
153 [ req_distinguished_name ] | |
154 EOF | |
155 | |
156 my $d = $t->testdir(); | |
157 | |
158 foreach my $name ('localhost') { | |
159 system('openssl req -x509 -new ' | |
160 . "-config '$d/openssl.conf' -subj '/CN=$name/' " | |
161 . "-out '$d/$name.crt' -keyout '$d/$name.key' " | |
162 . ">>$d/openssl.out 2>&1") == 0 | |
163 or die "Can't create certificate for $name: $!\n"; | |
164 } | |
165 | |
166 $t->run_daemon(\&http_daemon); | |
167 $t->run()->waitforsocket('127.0.0.1:8083'); | |
168 | |
169 # file size is slightly beyond initial window size: 2**16 + 80 bytes | |
170 | |
171 $t->write_file('t1.html', | |
172 join('', map { sprintf "X%04dXXX", $_ } (1 .. 8202))); | |
173 $t->write_file('tbig.html', | |
174 join('', map { sprintf "X%04dXXX", $_ } (1 .. 8202))); | |
175 | |
176 $t->write_file('t2.html', 'SEE-THIS'); | |
177 $t->write_file('t3.html', 'SEE-THIS'); | |
178 | |
179 my %cframe = ( | |
180 0 => { name => 'DATA', value => \&data }, | |
181 1 => { name => 'HEADERS', value => \&headers }, | |
182 # 2 => { name => 'PRIORITY', value => \&priority }, | |
183 3 => { name => 'RST_STREAM', value => \&rst_stream }, | |
184 4 => { name => 'SETTINGS', value => \&settings }, | |
185 # 5 => { name => 'PUSH_PROIMSE', value => \&push_promise }, | |
186 6 => { name => 'PING', value => \&ping }, | |
187 7 => { name => 'GOAWAY', value => \&goaway }, | |
188 8 => { name => 'WINDOW_UPDATE', value => \&window_update }, | |
189 # 9 => { name => 'CONTINUATION', value => \&continuation }, | |
190 ); | |
191 | |
192 ############################################################################### | |
193 | |
194 # SETTINGS | |
195 | |
196 my $sess = new_session(); | |
197 my $frames = h2_read($sess, all => [ | |
198 { type => 'WINDOW_UPDATE' }, | |
199 { type => 'SETTINGS'} | |
200 ]); | |
201 | |
202 my ($frame) = grep { $_->{type} eq 'WINDOW_UPDATE' } @$frames; | |
203 ok($frame, 'WINDOW_UPDATE frame'); | |
204 is($frame->{flags}, 0, 'WINDOW_UPDATE zero flags'); | |
205 is($frame->{sid}, 0, 'WINDOW_UPDATE zero sid'); | |
206 is($frame->{length}, 4, 'WINDOW_UPDATE fixed length'); | |
207 | |
208 ($frame) = grep { $_->{type} eq 'SETTINGS' } @$frames; | |
209 ok($frame, 'SETTINGS frame'); | |
210 is($frame->{flags}, 0, 'SETTINGS flags'); | |
211 | |
212 h2_settings($sess, 1); | |
213 h2_settings($sess, 0); | |
214 | |
215 $frames = h2_read($sess, all => [{ type => 'SETTINGS' }]); | |
216 | |
217 ($frame) = grep { $_->{type} eq 'SETTINGS' } @$frames; | |
218 ok($frame, 'SETTINGS frame ack'); | |
219 is($frame->{flags}, 1, 'SETTINGS flags ack'); | |
220 | |
221 # PING | |
222 | |
223 h2_ping($sess, 'SEE-THIS'); | |
224 $frames = h2_read($sess, all => [{ type => 'PING' }]); | |
225 | |
226 ($frame) = grep { $_->{type} eq "PING" } @$frames; | |
227 ok($frame, 'PING frame'); | |
228 is($frame->{value}, 'SEE-THIS', 'PING payload'); | |
229 is($frame->{flags}, 1, 'PING flags ack'); | |
230 | |
231 # GET | |
232 | |
233 my $sid = new_stream($sess); | |
234 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
235 | |
236 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
237 ok($frame, 'HEADERS frame'); | |
238 is($frame->{sid}, $sid, 'HEADERS stream'); | |
239 is($frame->{headers}->{':status'}, 200, 'HEADERS status'); | |
240 is($frame->{headers}->{'x-header'}, 'X-Foo', 'HEADERS header'); | |
241 | |
242 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
243 ok($frame, 'DATA frame'); | |
244 is($frame->{length}, length 'body', 'DATA length'); | |
245 is($frame->{data}, 'body', 'DATA payload'); | |
246 | |
247 # GET in the new stream on same connection | |
248 | |
249 $sid = new_stream($sess); | |
250 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
251 | |
252 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
253 is($frame->{sid}, $sid, 'HEADERS stream 2'); | |
254 is($frame->{headers}->{':status'}, 200, 'HEADERS status 2'); | |
255 is($frame->{headers}->{'x-header'}, 'X-Foo', 'HEADERS header 2'); | |
256 | |
257 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
258 ok($frame, 'DATA frame 2'); | |
259 is($frame->{sid}, $sid, 'HEADERS stream 2'); | |
260 is($frame->{length}, length 'body', 'DATA length 2'); | |
261 is($frame->{data}, 'body', 'DATA payload 2'); | |
262 | |
263 # various HEADERS compression/encoding, see hpack() for mode details | |
264 | |
265 # 6.1. Indexed Header Field Representation | |
266 | |
267 $sid = new_stream($sess, { headers => [ | |
268 { name => ':method', value => 'GET', mode => 0 }, | |
269 { name => ':scheme', value => 'http', mode => 0 }, | |
270 { name => ':path', value => '/', mode => 0 }]}); | |
271 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
272 | |
273 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
274 is($frame->{headers}->{':status'}, 200, 'indexed header field'); | |
275 | |
276 # 6.2.1. Literal Header Field with Incremental Indexing | |
277 | |
278 $sid = new_stream($sess, { headers => [ | |
279 { name => ':method', value => 'GET', mode => 1, huff => 0 }, | |
280 { name => ':scheme', value => 'http', mode => 1, huff => 0 }, | |
281 { name => ':path', value => '/', mode => 1, huff => 0 }]}); | |
282 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
283 | |
284 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
285 is($frame->{headers}->{':status'}, 200, 'literal with indexing'); | |
286 | |
287 $sid = new_stream($sess, { headers => [ | |
288 { name => ':method', value => 'GET', mode => 1, huff => 1 }, | |
289 { name => ':scheme', value => 'http', mode => 1, huff => 1 }, | |
290 { name => ':path', value => '/', mode => 1, huff => 1 }]}); | |
291 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
292 | |
293 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
294 is($frame->{headers}->{':status'}, 200, 'literal with indexing - huffman'); | |
295 | |
296 # 6.2.1. Literal Header Field with Incremental Indexing -- New Name | |
297 | |
298 $sid = new_stream($sess, { headers => [ | |
299 { name => ':method', value => 'GET', mode => 2, huff => 0 }, | |
300 { name => ':scheme', value => 'http', mode => 2, huff => 0 }, | |
301 { name => ':path', value => '/', mode => 2, huff => 0 }]}); | |
302 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
303 | |
304 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
305 is($frame->{headers}->{':status'}, 200, 'literal with indexing - new'); | |
306 | |
307 $sid = new_stream($sess, { headers => [ | |
308 { name => ':method', value => 'GET', mode => 2, huff => 1 }, | |
309 { name => ':scheme', value => 'http', mode => 2, huff => 1 }, | |
310 { name => ':path', value => '/', mode => 2, huff => 1 }]}); | |
311 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
312 | |
313 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
314 is($frame->{headers}->{':status'}, 200, 'literal with indexing - new huffman'); | |
315 | |
316 # 6.2.2. Literal Header Field without Indexing | |
317 | |
318 $sid = new_stream($sess, { headers => [ | |
319 { name => ':method', value => 'GET', mode => 3, huff => 0 }, | |
320 { name => ':scheme', value => 'http', mode => 3, huff => 0 }, | |
321 { name => ':path', value => '/', mode => 3, huff => 0 }]}); | |
322 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
323 | |
324 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
325 is($frame->{headers}->{':status'}, 200, 'literal without indexing'); | |
326 | |
327 $sid = new_stream($sess, { headers => [ | |
328 { name => ':method', value => 'GET', mode => 3, huff => 1 }, | |
329 { name => ':scheme', value => 'http', mode => 3, huff => 1 }, | |
330 { name => ':path', value => '/', mode => 3, huff => 1 }]}); | |
331 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
332 | |
333 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
334 is($frame->{headers}->{':status'}, 200, 'literal without indexing - huffman'); | |
335 | |
336 # 6.2.2. Literal Header Field without Indexing -- New Name | |
337 | |
338 $sid = new_stream($sess, { headers => [ | |
339 { name => ':method', value => 'GET', mode => 4, huff => 0 }, | |
340 { name => ':scheme', value => 'http', mode => 4, huff => 0 }, | |
341 { name => ':path', value => '/', mode => 4, huff => 0 }]}); | |
342 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
343 | |
344 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
345 is($frame->{headers}->{':status'}, 200, 'literal without indexing - new'); | |
346 | |
347 $sid = new_stream($sess, { headers => [ | |
348 { name => ':method', value => 'GET', mode => 4, huff => 1 }, | |
349 { name => ':scheme', value => 'http', mode => 4, huff => 1 }, | |
350 { name => ':path', value => '/', mode => 4, huff => 1 }]}); | |
351 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
352 | |
353 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
354 is($frame->{headers}->{':status'}, 200, 'literal without indexing - new huffman'); | |
355 | |
356 # 6.2.3. Literal Header Field Never Indexed | |
357 | |
358 $sid = new_stream($sess, { headers => [ | |
359 { name => ':method', value => 'GET', mode => 5, huff => 0 }, | |
360 { name => ':scheme', value => 'http', mode => 5, huff => 0 }, | |
361 { name => ':path', value => '/', mode => 5, huff => 0 }]}); | |
362 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
363 | |
364 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
365 is($frame->{headers}->{':status'}, 200, 'literal never indexed'); | |
366 | |
367 $sid = new_stream($sess, { headers => [ | |
368 { name => ':method', value => 'GET', mode => 5, huff => 1 }, | |
369 { name => ':scheme', value => 'http', mode => 5, huff => 1 }, | |
370 { name => ':path', value => '/', mode => 5, huff => 1 }]}); | |
371 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
372 | |
373 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
374 is($frame->{headers}->{':status'}, 200, 'literal never indexed - huffman'); | |
375 | |
376 # 6.2.2. Literal Header Field Never Indexed -- New Name | |
377 | |
378 $sid = new_stream($sess, { headers => [ | |
379 { name => ':method', value => 'GET', mode => 6, huff => 0 }, | |
380 { name => ':scheme', value => 'http', mode => 6, huff => 0 }, | |
381 { name => ':path', value => '/', mode => 6, huff => 0 }]}); | |
382 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
383 | |
384 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
385 is($frame->{headers}->{':status'}, 200, 'literal never indexed - new'); | |
386 | |
387 $sid = new_stream($sess, { headers => [ | |
388 { name => ':method', value => 'GET', mode => 6, huff => 1 }, | |
389 { name => ':scheme', value => 'http', mode => 6, huff => 1 }, | |
390 { name => ':path', value => '/', mode => 6, huff => 1 }]}); | |
391 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
392 | |
393 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
394 is($frame->{headers}->{':status'}, 200, 'literal never indexed - new huffman'); | |
395 | |
396 # reuse literal with indexing | |
397 | |
398 $sess = new_session(); | |
399 $sid = new_stream($sess, { headers => [ | |
400 { name => ':method', value => 'GET', mode => 0 }, | |
401 { name => ':scheme', value => 'http', mode => 0 }, | |
402 { name => ':path', value => '/', mode => 0 }, | |
403 { name => 'referer', value => 'foo', mode => 1 }]}); | |
404 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
405 | |
406 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
407 is($frame->{headers}->{'x-referer'}, 'foo', 'value with indexing - new'); | |
408 | |
409 $sid = new_stream($sess, { headers => [ | |
410 { name => ':method', value => 'GET', mode => 0 }, | |
411 { name => ':scheme', value => 'http', mode => 0 }, | |
412 { name => ':path', value => '/', mode => 0 }, | |
413 { name => 'referer', value => 'foo', mode => 0 }]}); | |
414 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
415 | |
416 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
417 is($frame->{headers}->{'x-referer'}, 'foo', 'value with indexing - indexed'); | |
418 | |
419 $sid = new_stream($sess, { headers => [ | |
420 { name => ':method', value => 'GET', mode => 0 }, | |
421 { name => ':scheme', value => 'http', mode => 0 }, | |
422 { name => ':path', value => '/', mode => 0 }, | |
423 { name => 'x-foo', value => 'X-Bar', mode => 2 }]}); | |
424 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
425 | |
426 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
427 is($frame->{headers}->{'x-sent-foo'}, 'X-Bar', 'name with indexing - new'); | |
428 | |
429 # reuse literal with indexing - reused name | |
430 | |
431 $sid = new_stream($sess, { headers => [ | |
432 { name => ':method', value => 'GET', mode => 0 }, | |
433 { name => ':scheme', value => 'http', mode => 0 }, | |
434 { name => ':path', value => '/', mode => 0 }, | |
435 { name => 'x-foo', value => 'X-Bar', mode => 0 }]}); | |
436 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
437 | |
438 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
439 is($frame->{headers}->{'x-sent-foo'}, 'X-Bar', 'name with indexing - indexed'); | |
440 | |
441 # reuse literal with indexing - reused value with moved index | |
442 | |
443 $sid = new_stream($sess, { headers => [ | |
444 { name => ':method', value => 'GET', mode => 0 }, | |
445 { name => ':scheme', value => 'http', mode => 0 }, | |
446 { name => ':path', value => '/', mode => 0 }, | |
447 { name => 'referer', value => 'foo', mode => 0 }]}); | |
448 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
449 | |
450 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
451 is($frame->{headers}->{'x-referer'}, 'foo', 'moved index in dynamic table'); | |
452 | |
453 # 6.3. Dynamic Table Size Update | |
454 | |
455 # remove one of two indexed headers from the dynamic table | |
456 # 'x-foo' has index 0, and 'referer' has index 1 | |
457 | |
458 $sid = new_stream($sess, { table_size => 61, headers => [ | |
459 { name => ':method', value => 'GET', mode => 0 }, | |
460 { name => ':scheme', value => 'http', mode => 0 }, | |
461 { name => ':path', value => '/', mode => 0 }, | |
462 { name => 'x-foo', value => 'X-Bar', mode => 0 }]}); | |
463 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
464 | |
465 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
466 isnt($frame, undef, 'updated table size - remaining index'); | |
467 | |
468 $sid = new_stream($sess, { headers => [ | |
469 { name => ':method', value => 'GET', mode => 0 }, | |
470 { name => ':scheme', value => 'http', mode => 0 }, | |
471 { name => ':path', value => '/', mode => 0 }, | |
472 { name => 'referer', value => 'foo', mode => 0 }]}); | |
473 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
474 | |
475 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
476 is($frame, undef, 'updated table size - invalid index'); | |
477 | |
478 # 5.4.1. Connection Error Handling | |
479 # An endpoint that encounters a connection error SHOULD first send a | |
480 # GOAWAY frame <..> | |
481 | |
482 TODO: { | |
483 local $TODO = 'not yet'; | |
484 | |
485 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
486 isnt($frame, undef, 'updated table size - invalid index GOAWAY'); | |
487 | |
488 } | |
489 | |
490 # HEAD | |
491 | |
492 $sess = new_session(); | |
493 $sid = new_stream($sess, { method => 'HEAD' }); | |
494 $frames = h2_read($sess, all => [{ sid => $sid, fin => 0x4 }]); | |
495 | |
496 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
497 is($frame->{sid}, $sid, 'HEAD - HEADERS'); | |
498 is($frame->{headers}->{':status'}, 200, 'HEAD - HEADERS status'); | |
499 is($frame->{headers}->{'x-header'}, 'X-Foo', 'HEAD - HEADERS header'); | |
500 | |
501 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
502 is($frame, undef, 'HEAD - no body'); | |
503 | |
504 # GET with PROXY protocol | |
505 | |
506 my $proxy = 'PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678' . CRLF; | |
507 $sess = new_session(8082, proxy => $proxy); | |
508 $sid = new_stream($sess, { path => '/pp' }); | |
509 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
510 | |
511 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
512 ok($frame, 'PROXY HEADERS frame'); | |
513 is($frame->{headers}->{'x-pp'}, '192.0.2.1', 'PROXY remote addr'); | |
514 | |
515 # range filter | |
516 | |
517 $sess = new_session(); | |
518 $sid = new_stream($sess, { headers => [ | |
519 { name => ':method', value => 'GET', mode => 0 }, | |
520 { name => ':scheme', value => 'http', mode => 0 }, | |
521 { name => ':path', value => '/t1.html', mode => 1 }, | |
522 { name => 'range', value => 'bytes=10-19', mode => 1 }]}); | |
523 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
524 | |
525 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
526 is($frame->{headers}->{':status'}, 206, 'range - HEADERS status'); | |
527 | |
528 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
529 is($frame->{length}, 10, 'range - DATA length'); | |
530 is($frame->{data}, '002XXXX000', 'range - DATA payload'); | |
531 | |
650
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
532 # $http2 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
533 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
534 $sess = new_session(); |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
535 $sid = new_stream($sess, { path => '/h2' }); |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
536 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
537 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
538 ($frame) = grep { $_->{type} eq "DATA" } @$frames; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
539 is($frame->{data}, 'h2c', 'http variable - h2c'); |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
540 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
541 # SSL/TLS connection, NPN |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
542 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
543 SKIP: { |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
544 eval { IO::Socket::SSL->can_npn() or die; }; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
545 skip 'OpenSSL NPN support required', 1 if $@; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
546 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
547 $sess = new_session(8084, SSL => 1, npn => 'h2'); |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
548 $sid = new_stream($sess, { path => '/h2' }); |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
549 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
550 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
551 ($frame) = grep { $_->{type} eq "DATA" } @$frames; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
552 is($frame->{data}, 'h2', 'http variable - npn'); |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
553 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
554 } |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
555 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
556 # SSL/TLS connection, ALPN |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
557 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
558 SKIP: { |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
559 eval { IO::Socket::SSL->can_alpn() or die; }; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
560 skip 'OpenSSL ALPN support required', 1 if $@; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
561 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
562 $sess = new_session(8084, SSL => 1, alpn => 'h2'); |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
563 $sid = new_stream($sess, { path => '/h2' }); |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
564 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
565 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
566 ($frame) = grep { $_->{type} eq "DATA" } @$frames; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
567 is($frame->{data}, 'h2', 'http variable - alpn'); |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
568 |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
569 } |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
570 |
646 | 571 # CONTINUATION |
572 | |
650
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
573 $sess = new_session(); |
646 | 574 $sid = new_stream($sess, { continuation => 1, headers => [ |
575 { name => ':method', value => 'HEAD', mode => 1 }, | |
576 { name => ':scheme', value => 'http', mode => 0 }, | |
577 { name => ':path', value => '/', mode => 0 }]}); | |
578 h2_continue($sess, $sid, { continuation => 1, headers => [ | |
579 { name => 'x-foo', value => 'X-Bar', mode => 2 }]}); | |
580 h2_continue($sess, $sid, { headers => [ | |
581 { name => 'referer', value => 'foo', mode => 2 }]}); | |
582 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
583 | |
584 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
585 is($frame, undef, 'CONTINUATION - fragment 1'); | |
586 | |
587 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
588 is($frame->{headers}->{'x-sent-foo'}, 'X-Bar', 'CONTINUATION - fragment 2'); | |
589 is($frame->{headers}->{'x-referer'}, 'foo', 'CONTINUATION - fragment 3'); | |
590 | |
591 # frame padding | |
592 | |
593 $sess = new_session(); | |
594 $sid = new_stream($sess, { padding => 42, headers => [ | |
595 { name => ':method', value => 'GET', mode => 0 }, | |
596 { name => ':scheme', value => 'http', mode => 0 }, | |
597 { name => ':path', value => '/', mode => 0 }]}); | |
598 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
599 | |
600 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
601 is($frame->{headers}->{':status'}, 200, 'padding - HEADERS status'); | |
602 | |
603 $sid = new_stream($sess, { headers => [ | |
604 { name => ':method', value => 'GET', mode => 0 }, | |
605 { name => ':scheme', value => 'http', mode => 0 }, | |
606 { name => ':path', value => '/', mode => 0 }]}); | |
607 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
608 | |
609 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
610 is($frame->{headers}->{':status'}, 200, 'padding - next stream'); | |
611 | |
612 # request header field with multiple values | |
613 | |
614 # 8.1.2.5. Compressing the Cookie Header Field | |
615 # To allow for better compression efficiency, the Cookie header field | |
616 # MAY be split into separate header fields <..>. | |
617 | |
618 $sess = new_session(); | |
619 $sid = new_stream($sess, { headers => [ | |
620 { name => ':method', value => 'GET', mode => 0 }, | |
621 { name => ':scheme', value => 'http', mode => 0 }, | |
622 { name => ':path', value => '/cookie', mode => 2 }, | |
623 { name => 'cookie', value => 'a=b', mode => 2}, | |
624 { name => 'cookie', value => 'c=d', mode => 2}]}); | |
625 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
626 | |
627 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
628 is($frame->{headers}->{'x-cookie-a'}, 'b', | |
629 'multiple request header fields - cookie'); | |
630 is($frame->{headers}->{'x-cookie-c'}, 'd', | |
631 'multiple request header fields - cookie 2'); | |
632 is($frame->{headers}->{'x-cookie'}, 'a=b; c=d', | |
633 'multiple request header fields - semi-colon'); | |
634 | |
635 # request header field with multiple values to HTTP backend | |
636 | |
637 # 8.1.2.5. Compressing the Cookie Header Field | |
638 # these MUST be concatenated into a single octet string | |
639 # using the two-octet delimiter of 0x3B, 0x20 (the ASCII string "; ") | |
640 # before being passed into a non-HTTP/2 context, such as an HTTP/1.1 | |
641 # connection <..> | |
642 | |
643 $sess = new_session(); | |
644 $sid = new_stream($sess, { headers => [ | |
645 { name => ':method', value => 'GET', mode => 0 }, | |
646 { name => ':scheme', value => 'http', mode => 0 }, | |
647 { name => ':path', value => '/proxy/cookie', mode => 2 }, | |
648 { name => 'cookie', value => 'a=b', mode => 2 }, | |
649 { name => 'cookie', value => 'c=d', mode => 2 }]}); | |
650 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
651 | |
652 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
653 is($frame->{headers}->{'x-cookie'}, 'a=b; c=d', | |
654 'multiple request header fields proxied - semi-colon'); | |
655 | |
656 # response header field with multiple values | |
657 | |
658 $sess = new_session(); | |
659 $sid = new_stream($sess, { path => '/set-cookie' }); | |
660 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
661 | |
662 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
663 is($frame->{headers}->{'set-cookie'}[0], 'a=b', | |
664 'multiple response header fields - cookie'); | |
665 is($frame->{headers}->{'set-cookie'}[1], 'c=d', | |
666 'multiple response header fields - cookie 2'); | |
667 | |
668 # response header field with multiple values from HTTP backend | |
669 | |
670 $sess = new_session(); | |
671 $sid = new_stream($sess, { path => '/proxy/set-cookie' }); | |
672 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
673 | |
674 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
675 is($frame->{headers}->{'set-cookie'}[0], 'a=b', | |
676 'multiple response header proxied - cookie'); | |
677 is($frame->{headers}->{'set-cookie'}[1], 'c=d', | |
678 'multiple response header proxied - cookie 2'); | |
679 is($frame->{headers}->{'x-uc-a'}, 'b', | |
680 'multiple response header proxied - upstream cookie'); | |
681 is($frame->{headers}->{'x-uc-c'}, 'd', | |
682 'multiple response header proxied - upstream cookie 2'); | |
683 | |
684 # internal redirect | |
685 | |
686 $sess = new_session(); | |
687 $sid = new_stream($sess, { path => '/redirect' }); | |
688 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
689 | |
690 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
691 is($frame->{headers}->{':status'}, 405, 'redirect - HEADERS'); | |
692 | |
693 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
694 ok($frame, 'redirect - DATA'); | |
695 is($frame->{data}, 'body', 'redirect - DATA payload'); | |
696 | |
697 # return 301 with absolute URI | |
698 | |
699 $sess = new_session(); | |
700 $sid = new_stream($sess, { path => '/return301_absolute' }); | |
701 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
702 | |
703 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
704 is($frame->{headers}->{':status'}, 301, 'return 301 absolute - status'); | |
705 is($frame->{headers}->{'location'}, 'text', 'return 301 absolute - location'); | |
706 | |
707 # return 301 with relative URI | |
708 | |
709 $sess = new_session(); | |
710 $sid = new_stream($sess, { path => '/return301_relative' }); | |
711 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
712 | |
713 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
714 is($frame->{headers}->{':status'}, 301, 'return 301 relative - status'); | |
715 is($frame->{headers}->{'location'}, 'http://127.0.0.1:8080/', | |
716 'return 301 relative - location'); | |
717 | |
718 # return 301 with relative URI and ':authority' request header field | |
719 | |
720 $sess = new_session(); | |
721 $sid = new_stream($sess, { headers => [ | |
722 { name => ':method', value => 'GET', mode => 0 }, | |
723 { name => ':scheme', value => 'http', mode => 0 }, | |
724 { name => ':path', value => '/return301_relative', mode => 2 }, | |
725 { name => ':authority', value => 'localhost', mode => 2 }]}); | |
726 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
727 | |
728 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
729 is($frame->{headers}->{':status'}, 301, | |
730 'return 301 relative - authority - status'); | |
731 is($frame->{headers}->{'location'}, 'http://localhost:8080/', | |
732 'return 301 relative - authority - location'); | |
733 | |
734 # return 301 with relative URI and 'host' request header field | |
735 | |
736 $sess = new_session(); | |
737 $sid = new_stream($sess, { headers => [ | |
738 { name => ':method', value => 'GET', mode => 0 }, | |
739 { name => ':scheme', value => 'http', mode => 0 }, | |
740 { name => ':path', value => '/return301_relative', mode => 2 }, | |
741 { name => 'host', value => 'localhost', mode => 2 }]}); | |
742 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
743 | |
744 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
745 is($frame->{headers}->{':status'}, 301, | |
746 'return 301 relative - host - status'); | |
747 is($frame->{headers}->{'location'}, 'http://localhost:8080/', | |
748 'return 301 relative - host - location'); | |
749 | |
750 # virtual host | |
751 | |
752 $sess = new_session(8085); | |
753 $sid = new_stream($sess, { headers => [ | |
754 { name => ':method', value => 'GET', mode => 0 }, | |
755 { name => ':scheme', value => 'http', mode => 0 }, | |
756 { name => ':path', value => '/', mode => 0 }, | |
757 { name => 'host', value => 'localhost', mode => 2 }]}); | |
758 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
759 | |
760 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
761 is($frame->{headers}->{':status'}, 200, | |
762 'virtual host - host - status'); | |
763 | |
764 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
765 is($frame->{data}, 'first', 'virtual host - host - DATA'); | |
766 | |
767 $sid = new_stream($sess, { headers => [ | |
768 { name => ':method', value => 'GET', mode => 0 }, | |
769 { name => ':scheme', value => 'http', mode => 0 }, | |
770 { name => ':path', value => '/', mode => 0 }, | |
771 { name => ':authority', value => 'localhost', mode => 2 }]}); | |
772 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
773 | |
774 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
775 is($frame->{headers}->{':status'}, 200, | |
776 'virtual host - authority - status'); | |
777 | |
778 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
779 is($frame->{data}, 'first', 'virtual host - authority - DATA'); | |
780 | |
781 # virtual host - second | |
782 | |
783 $sid = new_stream($sess, { headers => [ | |
784 { name => ':method', value => 'GET', mode => 0 }, | |
785 { name => ':scheme', value => 'http', mode => 0 }, | |
786 { name => ':path', value => '/', mode => 0 }, | |
787 { name => 'host', value => 'localhost2', mode => 2 }]}); | |
788 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
789 | |
790 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
791 is($frame->{headers}->{':status'}, 200, | |
792 'virtual host 2 - host - status'); | |
793 | |
794 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
795 is($frame->{data}, 'second', 'virtual host 2 - host - DATA'); | |
796 | |
797 $sid = new_stream($sess, { headers => [ | |
798 { name => ':method', value => 'GET', mode => 0 }, | |
799 { name => ':scheme', value => 'http', mode => 0 }, | |
800 { name => ':path', value => '/', mode => 0 }, | |
801 { name => ':authority', value => 'localhost2', mode => 2 }]}); | |
802 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
803 | |
804 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
805 is($frame->{headers}->{':status'}, 200, | |
806 'virtual host 2 - authority - status'); | |
807 | |
808 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
809 is($frame->{data}, 'second', 'virtual host 2 - authority - DATA'); | |
810 | |
811 # gzip tests for internal nginx version | |
812 | |
813 $sess = new_session(); | |
814 $sid = new_stream($sess, { headers => [ | |
815 { name => ':method', value => 'GET', mode => 0 }, | |
816 { name => ':scheme', value => 'http', mode => 0 }, | |
817 { name => ':path', value => '/gzip.html' }, | |
818 { name => 'accept-encoding', value => 'gzip' }]}); | |
819 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
820 | |
821 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
822 is($frame->{headers}->{'content-encoding'}, 'gzip', 'gzip - encoding'); | |
823 | |
824 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
647
4e36550410b3
Tests: h2.t fixes for older perl versions, and gzip test.
Sergey Kandaurov <pluknet@nginx.com>
parents:
646
diff
changeset
|
825 gunzip_like($frame->{data}, qr/^SEE-THIS\Z/, 'gzip - DATA'); |
646 | 826 |
827 # simple proxy cache test | |
828 | |
829 $sess = new_session(); | |
830 $sid = new_stream($sess, { path => '/proxy2/t2.html?2' }); | |
831 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
832 | |
833 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
834 is($frame->{headers}->{':status'}, '200', 'proxy cache'); | |
835 | |
836 my $etag = $frame->{headers}->{'etag'}; | |
837 | |
838 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
839 is($frame->{length}, length 'SEE-THIS', 'proxy cache - DATA'); | |
840 is($frame->{data}, 'SEE-THIS', 'proxy cache - DATA payload'); | |
841 | |
842 $sid = new_stream($sess, { headers => [ | |
843 { name => ':method', value => 'GET', mode => 0 }, | |
844 { name => ':scheme', value => 'http', mode => 0 }, | |
845 { name => ':path', value => '/proxy2/t2.html?2' }, | |
846 { name => 'if-none-match', value => $etag }]}); | |
847 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
848 | |
849 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
850 is($frame->{headers}->{':status'}, 304, 'proxy cache conditional'); | |
851 | |
852 # HEADERS could be received with fin, followed by DATA | |
853 | |
854 $sess = new_session(); | |
855 $sid = new_stream($sess, { path => '/proxy2/t2.html', method => 'HEAD' }); | |
856 | |
857 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
858 push @$frames, $_ for @{h2_read($sess, all => [{ sid => $sid }])}; | |
859 ok(!grep ({ $_->{type} eq "DATA" } @$frames), 'proxy cache HEAD - no body'); | |
860 | |
861 # HEAD on empty cache with proxy_buffering off | |
862 | |
863 $sess = new_session(); | |
864 $sid = new_stream($sess, | |
865 { path => '/proxy_buffering_off/t2.html?1', method => 'HEAD' }); | |
866 | |
867 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
868 push @$frames, $_ for @{h2_read($sess, all => [{ sid => $sid }])}; | |
869 ok(!grep ({ $_->{type} eq "DATA" } @$frames), | |
870 'proxy cache HEAD buffering off - no body'); | |
871 | |
872 # request body (uses proxied response) | |
873 | |
874 $sess = new_session(); | |
875 $sid = new_stream($sess, { path => '/proxy2/t2.html', body => 'TEST' }); | |
876 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
877 | |
878 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
879 is($frame->{headers}->{'x-body'}, 'TEST', 'request body'); | |
880 | |
881 # request body with padding (uses proxied response) | |
882 | |
883 $sess = new_session(); | |
884 $sid = new_stream($sess, | |
885 { path => '/proxy2/t2.html', body => 'TEST', body_padding => 42 }); | |
886 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
887 | |
888 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
889 is($frame->{headers}->{'x-body'}, 'TEST', 'request body with padding'); | |
890 | |
891 $sid = new_stream($sess); | |
892 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
893 | |
894 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
895 is($frame->{headers}->{':status'}, '200', 'request body with padding - next'); | |
896 | |
897 # initial window size, client side | |
898 | |
899 # 6.9.2. Initial Flow-Control Window Size | |
900 # When an HTTP/2 connection is first established, new streams are | |
901 # created with an initial flow-control window size of 65,535 octets. | |
902 # The connection flow-control window is also 65,535 octets. | |
903 | |
904 $sess = new_session(); | |
905 $sid = new_stream($sess, { path => '/t1.html' }); | |
906 $frames = h2_read($sess, all => [{ sid => $sid, length => 2**16 - 1 }]); | |
907 | |
908 # with the default http2_chunk_size, data is divided into 8 data frames | |
909 | |
910 my @data = grep { $_->{type} eq "DATA" } @$frames; | |
911 my $lengths = join ' ', map { $_->{length} } @data; | |
912 is($lengths, '8192 8192 8192 8192 8192 8192 8192 8191', | |
913 'iws - stream blocked on initial window size'); | |
914 | |
915 h2_ping($sess, 'SEE-THIS'); | |
916 $frames = h2_read($sess, all => [{ type => 'PING' }]); | |
917 | |
918 ($frame) = grep { $_->{type} eq "PING" && $_->{flags} & 0x1 } @$frames; | |
919 ok($frame, 'iws - PING not blocked'); | |
920 | |
921 h2_window($sess, 2**16, $sid); | |
922 $frames = h2_read($sess); | |
923 is(@$frames, 0, 'iws - updated stream window'); | |
924 | |
925 h2_window($sess, 2**16); | |
926 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
927 | |
928 # with the default output_buffers, the remain data would be split | |
929 # on buffers boundary into separate data frames | |
930 | |
931 @data = grep { $_->{type} eq "DATA" } @$frames; | |
932 my $sum = eval join '+', map { $_->{length} } @data; | |
933 is($sum, 81, 'iws - updated connection window'); | |
934 | |
935 # SETTINGS (initial window size, client side) | |
936 | |
937 # 6.9.2. Initial Flow-Control Window Size | |
938 # Both endpoints can adjust the initial window size for new streams by | |
939 # including a value for SETTINGS_INITIAL_WINDOW_SIZE in the SETTINGS | |
940 # frame that forms part of the connection preface. The connection | |
941 # flow-control window can only be changed using WINDOW_UPDATE frames. | |
942 | |
943 $sess = new_session(); | |
944 h2_settings($sess, 0, 0x4 => 2**17); | |
945 h2_window($sess, 2**17); | |
946 | |
947 $sid = new_stream($sess, { path => '/t1.html' }); | |
948 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
949 | |
950 @data = grep { $_->{type} eq "DATA" } @$frames; | |
951 $sum = eval join '+', map { $_->{length} } @data; | |
952 is($sum, 2**16 + 80, 'iws - increased'); | |
953 | |
954 # probe for negative available space in a flow control window | |
955 | |
956 # 6.9.2. Initial Flow-Control Window Size | |
957 # A change to SETTINGS_INITIAL_WINDOW_SIZE can cause the available | |
958 # space in a flow-control window to become negative. A sender MUST | |
959 # track the negative flow-control window and MUST NOT send new flow- | |
960 # controlled frames until it receives WINDOW_UPDATE frames that cause | |
961 # the flow-control window to become positive. | |
962 | |
963 $sess = new_session(); | |
964 $sid = new_stream($sess, { path => '/t1.html' }); | |
965 h2_read($sess, all => [{ sid => $sid, length => 2**16 - 1 }]); | |
966 | |
967 h2_window($sess, 1); | |
968 h2_settings($sess, 0, 0x4 => 42); | |
969 h2_window($sess, 1024, $sid); | |
970 | |
971 $frames = h2_read($sess, all => [{ type => 'SETTINGS' }]); | |
972 | |
973 ($frame) = grep { $_->{type} eq 'SETTINGS' } @$frames; | |
974 ok($frame, 'negative window - SETTINGS frame ack'); | |
975 is($frame->{flags}, 1, 'negative window - SETTINGS flags ack'); | |
976 | |
977 ($frame) = grep { $_->{type} ne 'SETTINGS' } @$frames; | |
978 is($frame, undef, 'negative window - no data'); | |
979 | |
980 # predefined window size, minus new iws settings, minus window update | |
981 | |
982 h2_window($sess, 2**16 - 1 - 42 - 1024, $sid); | |
983 | |
984 $frames = h2_read($sess); | |
985 is(@$frames, 0, 'zero window - no data'); | |
986 | |
987 h2_window($sess, 1, $sid); | |
988 | |
989 $frames = h2_read($sess, all => [{ sid => $sid, length => 1 }]); | |
990 is(@$frames, 1, 'positive window'); | |
991 is(@$frames[0]->{type}, 'DATA', 'positive window - data'); | |
992 is(@$frames[0]->{length}, 1, 'positive window - data length'); | |
993 | |
994 # stream multiplexing + WINDOW_UPDATE | |
995 | |
996 $sess = new_session(); | |
997 $sid = new_stream($sess, { path => '/t1.html' }); | |
998 $frames = h2_read($sess, all => [{ sid => $sid, length => 2**16 - 1 }]); | |
999 | |
1000 @data = grep { $_->{type} eq "DATA" } @$frames; | |
1001 $sum = eval join '+', map { $_->{length} } @data; | |
1002 is($sum, 2**16 - 1, 'multiple - stream1 data'); | |
1003 | |
1004 my $sid2 = new_stream($sess, { path => '/t1.html' }); | |
1005 $frames = h2_read($sess, all => [{ sid => $sid2, fin => 0x4 }]); | |
1006 | |
1007 @data = grep { $_->{type} eq "DATA" } @$frames; | |
1008 is(@data, 0, 'multiple - stream2 no data'); | |
1009 | |
1010 h2_window($sess, 2**17, $sid); | |
1011 h2_window($sess, 2**17, $sid2); | |
1012 h2_window($sess, 2**17); | |
1013 | |
1014 $frames = h2_read($sess, all => [ | |
1015 { sid => $sid, fin => 1 }, | |
1016 { sid => $sid2, fin => 1 } | |
1017 ]); | |
1018 | |
1019 @data = grep { $_->{type} eq "DATA" && $_->{sid} == $sid } @$frames; | |
1020 $sum = eval join '+', map { $_->{length} } @data; | |
1021 is($sum, 81, 'multiple - stream1 remain data'); | |
1022 | |
1023 @data = grep { $_->{type} eq "DATA" && $_->{sid} == $sid2 } @$frames; | |
1024 $sum = eval join '+', map { $_->{length} } @data; | |
1025 is($sum, 2**16 + 80, 'multiple - stream2 full data'); | |
1026 | |
1027 # stream muliplexing + PRIORITY frames | |
1028 | |
1029 $sess = new_session(); | |
1030 $sid = new_stream($sess, { path => '/t1.html' }); | |
1031 h2_read($sess, all => [{ sid => $sid, length => 2**16 - 1 }]); | |
1032 | |
1033 $sid2 = new_stream($sess, { path => '/t2.html' }); | |
1034 h2_read($sess, all => [{ sid => $sid2, fin => 0x4 }]); | |
1035 | |
1036 h2_priority($sess, 0, $sid); | |
1037 h2_priority($sess, 255, $sid2); | |
1038 | |
1039 h2_window($sess, 2**17, $sid); | |
1040 h2_window($sess, 2**17, $sid2); | |
1041 h2_window($sess, 2**17); | |
1042 | |
1043 $frames = h2_read($sess, all => [ | |
1044 { sid => $sid, fin => 1 }, | |
1045 { sid => $sid2, fin => 1 } | |
1046 ]); | |
1047 | |
1048 @data = grep { $_->{type} eq "DATA" } @$frames; | |
1049 is(join(' ', map { $_->{sid} } @data), "$sid2 $sid", 'weight - PRIORITY 1'); | |
1050 | |
1051 # and vice versa | |
1052 | |
1053 $sess = new_session(); | |
1054 $sid = new_stream($sess, { path => '/t1.html' }); | |
1055 h2_read($sess, all => [{ sid => $sid, length => 2**16 - 1 }]); | |
1056 | |
1057 $sid2 = new_stream($sess, { path => '/t2.html' }); | |
1058 h2_read($sess, all => [{ sid => $sid2, fin => 0x4 }]); | |
1059 | |
1060 h2_priority($sess, 255, $sid); | |
1061 h2_priority($sess, 0, $sid2); | |
1062 | |
1063 h2_window($sess, 2**17, $sid); | |
1064 h2_window($sess, 2**17, $sid2); | |
1065 h2_window($sess, 2**17); | |
1066 | |
1067 $frames = h2_read($sess, all => [ | |
1068 { sid => $sid, fin => 1 }, | |
1069 { sid => $sid2, fin => 1 } | |
1070 ]); | |
1071 | |
1072 @data = grep { $_->{type} eq "DATA" } @$frames; | |
1073 is(join(' ', map { $_->{sid} } @data), "$sid $sid2", 'weight - PRIORITY 2'); | |
1074 | |
1075 # stream muliplexing + HEADERS PRIORITY flag | |
1076 | |
1077 $sess = new_session(); | |
1078 $sid = new_stream($sess, { path => '/t1.html', prio => 0 }); | |
1079 h2_read($sess, all => [{ sid => $sid, length => 2**16 - 1 }]); | |
1080 | |
1081 $sid2 = new_stream($sess, { path => '/t2.html', prio => 255 }); | |
1082 h2_read($sess, all => [{ sid => $sid2, fin => 0x4 }]); | |
1083 | |
1084 h2_window($sess, 2**17, $sid); | |
1085 h2_window($sess, 2**17, $sid2); | |
1086 h2_window($sess, 2**17); | |
1087 | |
1088 $frames = h2_read($sess, all => [ | |
1089 { sid => $sid, fin => 1 }, | |
1090 { sid => $sid2, fin => 1 } | |
1091 ]); | |
1092 | |
1093 @data = grep { $_->{type} eq "DATA" } @$frames; | |
1094 my $sids = join ' ', map { $_->{sid} } @data; | |
1095 is($sids, "$sid2 $sid", 'weight - HEADERS PRIORITY 1'); | |
1096 | |
1097 # and vice versa | |
1098 | |
1099 $sess = new_session(); | |
1100 $sid = new_stream($sess, { path => '/t1.html', prio => 255 }); | |
1101 h2_read($sess, all => [{ sid => $sid, length => 2**16 - 1 }]); | |
1102 | |
1103 $sid2 = new_stream($sess, { path => '/t2.html', prio => 0 }); | |
1104 h2_read($sess, all => [{ sid => $sid2, fin => 0x4 }]); | |
1105 | |
1106 h2_window($sess, 2**17, $sid); | |
1107 h2_window($sess, 2**17, $sid2); | |
1108 h2_window($sess, 2**17); | |
1109 | |
1110 $frames = h2_read($sess, all => [ | |
1111 { sid => $sid, fin => 1 }, | |
1112 { sid => $sid2, fin => 1 } | |
1113 ]); | |
1114 | |
1115 @data = grep { $_->{type} eq "DATA" } @$frames; | |
1116 $sids = join ' ', map { $_->{sid} } @data; | |
1117 is($sids, "$sid $sid2", 'weight - HEADERS PRIORITY 2'); | |
1118 | |
1119 # 5.3.1. Stream Dependencies | |
1120 | |
1121 # PRIORITY frame | |
1122 | |
1123 $sess = new_session(); | |
1124 | |
1125 h2_priority($sess, 16, 3, 0); | |
1126 h2_priority($sess, 16, 1, 3); | |
1127 | |
1128 $sid = new_stream($sess, { path => '/t1.html' }); | |
1129 h2_read($sess, all => [{ sid => $sid, length => 2**16 - 1 }]); | |
1130 | |
1131 $sid2 = new_stream($sess, { path => '/t2.html' }); | |
1132 h2_read($sess, all => [{ sid => $sid2, fin => 0x4 }]); | |
1133 | |
1134 h2_window($sess, 2**17, $sid); | |
1135 h2_window($sess, 2**17, $sid2); | |
1136 h2_window($sess, 2**17); | |
1137 | |
1138 $frames = h2_read($sess, all => [ | |
1139 { sid => $sid, fin => 1 }, | |
1140 { sid => $sid2, fin => 1 }, | |
1141 ]); | |
1142 | |
1143 @data = grep { $_->{type} eq "DATA" } @$frames; | |
1144 $sids = join ' ', map { $_->{sid} } @data; | |
1145 is($sids, "$sid2 $sid", 'dependency - PRIORITY 1'); | |
1146 | |
1147 # and vice versa | |
1148 | |
1149 $sess = new_session(); | |
1150 | |
1151 h2_priority($sess, 16, 1, 0); | |
1152 h2_priority($sess, 16, 3, 1); | |
1153 | |
1154 $sid = new_stream($sess, { path => '/t1.html' }); | |
1155 h2_read($sess, all => [{ sid => $sid, length => 2**16 - 1 }]); | |
1156 | |
1157 $sid2 = new_stream($sess, { path => '/t2.html' }); | |
1158 h2_read($sess, all => [{ sid => $sid2, fin => 0x4 }]); | |
1159 | |
1160 h2_window($sess, 2**17, $sid); | |
1161 h2_window($sess, 2**17, $sid2); | |
1162 h2_window($sess, 2**17); | |
1163 | |
1164 $frames = h2_read($sess, all => [ | |
1165 { sid => $sid, fin => 1 }, | |
1166 { sid => $sid2, fin => 1 }, | |
1167 ]); | |
1168 | |
1169 @data = grep { $_->{type} eq "DATA" } @$frames; | |
1170 $sids = join ' ', map { $_->{sid} } @data; | |
1171 is($sids, "$sid $sid2", 'dependency - PRIORITY 2'); | |
1172 | |
1173 # HEADERS PRIORITY flag, reprioritize prior PRIORITY frame records | |
1174 | |
1175 $sess = new_session(); | |
1176 | |
1177 h2_priority($sess, 16, 1, 0); | |
1178 h2_priority($sess, 16, 3, 0); | |
1179 | |
1180 $sid = new_stream($sess, { path => '/t1.html', dep => 3 }); | |
1181 h2_read($sess, all => [{ sid => $sid, length => 2**16 - 1 }]); | |
1182 | |
1183 $sid2 = new_stream($sess, { path => '/t2.html' }); | |
1184 h2_read($sess, all => [{ sid => $sid2, fin => 0x4 }]); | |
1185 | |
1186 h2_window($sess, 2**17, $sid); | |
1187 h2_window($sess, 2**17, $sid2); | |
1188 h2_window($sess, 2**17); | |
1189 | |
1190 $frames = h2_read($sess, all => [ | |
1191 { sid => $sid, fin => 1 }, | |
1192 { sid => $sid2, fin => 1 }, | |
1193 ]); | |
1194 | |
1195 @data = grep { $_->{type} eq "DATA" } @$frames; | |
1196 $sids = join ' ', map { $_->{sid} } @data; | |
1197 is($sids, "$sid2 $sid", 'dependency - HEADERS PRIORITY 1'); | |
1198 | |
1199 # and vice versa | |
1200 | |
1201 $sess = new_session(); | |
1202 | |
1203 h2_priority($sess, 16, 1, 0); | |
1204 h2_priority($sess, 16, 3, 0); | |
1205 | |
1206 $sid = new_stream($sess, { path => '/t1.html' }); | |
1207 h2_read($sess, all => [{ sid => $sid, length => 2**16 - 1 }]); | |
1208 | |
1209 $sid2 = new_stream($sess, { path => '/t2.html', dep => 1 }); | |
1210 h2_read($sess, all => [{ sid => $sid2, fin => 0x4 }]); | |
1211 | |
1212 h2_window($sess, 2**17, $sid); | |
1213 h2_window($sess, 2**17, $sid2); | |
1214 h2_window($sess, 2**17); | |
1215 | |
1216 $frames = h2_read($sess, all => [ | |
1217 { sid => $sid, fin => 1 }, | |
1218 { sid => $sid2, fin => 1 }, | |
1219 ]); | |
1220 | |
1221 @data = grep { $_->{type} eq "DATA" } @$frames; | |
1222 $sids = join ' ', map { $_->{sid} } @data; | |
1223 is($sids, "$sid $sid2", 'dependency - HEADERS PRIORITY 2'); | |
1224 | |
1225 # limit_conn | |
1226 | |
1227 $sess = new_session(); | |
1228 h2_settings($sess, 0, 0x4 => 1); | |
1229 | |
1230 $sid = new_stream($sess, { path => '/t3.html' }); | |
1231 $frames = h2_read($sess, all => [{ sid => $sid, length => 1 }]); | |
1232 | |
1233 ($frame) = grep { $_->{type} eq "HEADERS" && $_->{sid} == $sid } @$frames; | |
1234 is($frame->{headers}->{':status'}, 200, 'limit_conn first stream'); | |
1235 | |
1236 $sid2 = new_stream($sess, { path => '/t3.html' }); | |
1237 $frames = h2_read($sess, all => [{ sid => $sid2, fin => 0 }]); | |
1238 | |
1239 ($frame) = grep { $_->{type} eq "HEADERS" && $_->{sid} == $sid2 } @$frames; | |
1240 is($frame->{headers}->{':status'}, 503, 'limit_conn rejected'); | |
1241 | |
1242 h2_settings($sess, 0, 0x4 => 2**16); | |
1243 | |
1244 h2_read($sess, all => [ | |
1245 { sid => $sid, fin => 1 }, | |
1246 { sid => $sid2, fin => 1 } | |
1247 ]); | |
1248 | |
1249 # limit_conn + client's RST_STREAM | |
1250 | |
1251 $sess = new_session(); | |
1252 h2_settings($sess, 0, 0x4 => 1); | |
1253 | |
1254 $sid = new_stream($sess, { path => '/t3.html' }); | |
1255 $frames = h2_read($sess, all => [{ sid => $sid, length => 1 }]); | |
1256 h2_rst($sess, $sid, 5); | |
1257 | |
1258 ($frame) = grep { $_->{type} eq "HEADERS" && $_->{sid} == $sid } @$frames; | |
1259 is($frame->{headers}->{':status'}, 200, 'RST_STREAM 1'); | |
1260 | |
1261 $sid2 = new_stream($sess, { path => '/t3.html' }); | |
1262 $frames = h2_read($sess, all => [{ sid => $sid2, fin => 0 }]); | |
1263 | |
1264 TODO: { | |
1265 local $TODO = 'not yet'; | |
1266 | |
1267 ($frame) = grep { $_->{type} eq "HEADERS" && $_->{sid} == $sid2 } @$frames; | |
1268 is($frame->{headers}->{':status'}, 200, 'RST_STREAM 2'); | |
1269 | |
1270 } | |
1271 | |
1272 | |
1273 # some invalid cases below | |
1274 | |
1275 # GOAWAY on SYN_STREAM with even StreamID | |
1276 | |
1277 TODO: { | |
1278 local $TODO = 'not yet'; | |
1279 | |
1280 $sess = new_session(); | |
1281 new_stream($sess, { path => '/' }, 2); | |
1282 $frames = h2_read($sess, all => [{ type => 'GOAWAY' }]); | |
1283 | |
1284 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
1285 ok($frame, 'even stream - GOAWAY frame'); | |
1286 is($frame->{code}, 1, 'even stream - error code'); | |
1287 is($frame->{sid}, 0, 'even stream - last used stream'); | |
1288 | |
1289 } | |
1290 | |
1291 # GOAWAY on SYN_STREAM with backward StreamID | |
1292 | |
1293 TODO: { | |
1294 local $TODO = 'not yet'; | |
1295 | |
1296 $sess = new_session(); | |
1297 $sid = new_stream($sess, { path => '/' }, 3); | |
1298 h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
1299 | |
1300 $sid2 = new_stream($sess, { path => '/' }, 1); | |
1301 $frames = h2_read($sess, all => [{ type => 'GOAWAY' }]); | |
1302 | |
1303 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
1304 ok($frame, 'backward stream - GOAWAY frame'); | |
1305 is($frame->{code}, 1, 'backward stream - error code'); | |
1306 is($frame->{sid}, $sid, 'backward stream - last used stream'); | |
1307 | |
1308 } | |
1309 | |
1310 # RST_STREAM on the second SYN_STREAM with same StreamID | |
1311 | |
1312 TODO: { | |
1313 local $TODO = 'not yet'; | |
1314 | |
1315 $sess = new_session(); | |
1316 $sid = new_stream($sess, { path => '/' }); | |
1317 h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
1318 | |
1319 $sid2 = new_stream($sess, { path => '/' }, $sid); | |
1320 $frames = h2_read($sess, all => [{ type => 'RST_STREAM' }]); | |
1321 | |
1322 ($frame) = grep { $_->{type} eq "RST_STREAM" } @$frames; | |
1323 ok($frame, 'dup stream - RST_STREAM frame'); | |
1324 is($frame->{code}, 1, 'dup stream - error code'); | |
1325 is($frame->{sid}, $sid, 'dup stream - stream'); | |
1326 | |
1327 } | |
1328 | |
1329 # missing mandatory request header ':scheme' | |
1330 | |
1331 TODO: { | |
1332 local $TODO = 'not yet'; | |
1333 | |
1334 $sess = new_session(); | |
1335 $sid = new_stream($sess, { headers => [ | |
1336 { name => ':method', value => 'GET', mode => 0 }, | |
1337 { name => ':path', value => '/', mode => 0 }]}); | |
1338 $frames = h2_read($sess, all => [{ sid => $sid, fin => 1 }]); | |
1339 | |
1340 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
1341 is($frame->{headers}->{':status'}, 400, 'incomplete headers'); | |
1342 | |
1343 } | |
1344 | |
1345 # GOAWAY - force closing a connection by server | |
1346 | |
1347 $sid = new_stream($sess, { path => 't1.html' }); | |
1348 h2_read($sess, all => [{ sid => $sid, length => 2**16 - 1 }]); | |
1349 | |
1350 $t->stop(); | |
1351 | |
1352 TODO: { | |
1353 local $TODO = 'not yet'; | |
1354 | |
1355 $frames = h2_read($sess, all => [{ type => 'GOAWAY' }]); | |
1356 | |
1357 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
1358 ok($frame, 'GOAWAY on connection close'); | |
1359 | |
1360 } | |
1361 | |
1362 ############################################################################### | |
1363 | |
1364 sub h2_ping { | |
1365 my ($sess, $payload) = @_; | |
1366 | |
1367 raw_write($sess->{socket}, pack("x2C2x5a8", 8, 0x6, $payload)); | |
1368 } | |
1369 | |
1370 sub h2_rst { | |
1371 my ($sess, $stream, $error) = @_; | |
1372 | |
1373 raw_write($sess->{socket}, pack("x2C2xNN", 4, 0x3, $stream, $error)); | |
1374 } | |
1375 | |
1376 sub h2_priority { | |
1377 my ($sess, $w, $stream, $dep) = @_; | |
1378 | |
1379 $stream = 0 unless defined $stream; | |
1380 $dep = 0 unless defined $dep; | |
1381 raw_write($sess->{socket}, pack("x2C2xNNC", 5, 0x2, $stream, $dep, $w)); | |
1382 } | |
1383 | |
1384 sub h2_window { | |
1385 my ($sess, $win, $stream) = @_; | |
1386 | |
1387 $stream = 0 unless defined $stream; | |
1388 raw_write($sess->{socket}, pack("x2C2xNN", 4, 0x8, $stream, $win)); | |
1389 } | |
1390 | |
1391 sub h2_settings { | |
1392 my ($sess, $ack, %extra) = @_; | |
1393 | |
1394 my $len = 6 * keys %extra; | |
1395 my $buf = pack_length($len) . pack "CCx4", 0x4, $ack ? 0x1 : 0x0; | |
1396 $buf .= join '', map { pack "nN", $_, $extra{$_} } keys %extra; | |
1397 raw_write($sess->{socket}, $buf); | |
1398 } | |
1399 | |
1400 sub h2_continue { | |
1401 my ($ctx, $stream, $uri) = @_; | |
1402 | |
1403 $uri->{h2_continue} = 1; | |
1404 return new_stream($ctx, $uri, $stream); | |
1405 } | |
1406 | |
1407 sub new_stream { | |
1408 my ($ctx, $uri, $stream) = @_; | |
1409 my ($input, $buf); | |
1410 my ($d, $status); | |
1411 | |
1412 my $host = $uri->{host} || '127.0.0.1:8080'; | |
1413 my $method = $uri->{method} || 'GET'; | |
1414 my $scheme = $uri->{scheme} || 'http'; | |
1415 my $path = $uri->{path} || '/'; | |
1416 my $headers = $uri->{headers}; | |
1417 my $body = $uri->{body}; | |
1418 my $prio = $uri->{prio}; | |
1419 my $dep = $uri->{dep}; | |
1420 | |
1421 my $pad = defined $uri->{padding} ? $uri->{padding} : 0; | |
1422 my $padlen = defined $uri->{padding} ? 1 : 0; | |
1423 my $bpad = defined $uri->{body_padding} ? $uri->{body_padding} : 0; | |
1424 my $bpadlen = defined $uri->{body_padding} ? 1 : 0; | |
1425 | |
1426 my $type = defined $uri->{h2_continue} ? 0x9 : 0x1; | |
1427 my $flags = defined $uri->{continuation} ? 0x0 : 0x4; | |
1428 $flags |= 0x1 unless defined $body; | |
1429 $flags |= 0x8 if $padlen; | |
1430 $flags |= 0x20 if defined $dep || defined $prio; | |
1431 | |
1432 if ($stream) { | |
1433 $ctx->{last_stream} = $stream; | |
1434 } else { | |
1435 $ctx->{last_stream} += 2; | |
1436 } | |
1437 | |
1438 $buf = pack("xxx"); # Length stub | |
1439 $buf .= pack("CC", $type, $flags); # END_HEADERS | |
1440 $buf .= pack("N", $ctx->{last_stream}); # Stream-ID | |
1441 | |
1442 $dep = 0 if defined $prio and not defined $dep; | |
1443 $prio = 16 if defined $dep and not defined $prio; | |
1444 | |
1445 unless ($headers) { | |
1446 $input = hpack($ctx, ":method", $method); | |
1447 $input .= hpack($ctx, ":scheme", $scheme); | |
1448 $input .= hpack($ctx, ":path", $path); | |
1449 $input .= hpack($ctx, ":authority", $host); | |
1450 $input .= hpack($ctx, "content-length", length($body)) if $body; | |
1451 | |
1452 } else { | |
1453 $input = join '', map { | |
1454 hpack($ctx, $_->{name}, $_->{value}, | |
1455 mode => $_->{mode}, huff => $_->{huff}) | |
1456 } @$headers if $headers; | |
1457 } | |
1458 | |
1459 # 5.1. Integer Representation | |
1460 | |
1461 sub intpack { | |
1462 my $d = shift; | |
1463 return pack('B8', '001' . sprintf("%5b", $d)) if $d < 31; | |
1464 | |
1465 my $o = '00111111'; | |
1466 $d -= 31; | |
1467 while ($d >= 128) { | |
1468 $o .= sprintf("%8b", $d % 128 + 128); | |
1469 $d /= 128; | |
1470 } | |
1471 $o .= sprintf("%08b", $d); | |
1472 return pack('B*', $o); | |
1473 } | |
1474 | |
1475 $input = intpack($uri->{table_size}) . $input | |
1476 if defined $uri->{table_size}; | |
1477 | |
1478 # set length, attach headers, padding, priority | |
1479 | |
1480 my $hlen = length($input) + $pad + $padlen; | |
1481 $hlen += 5 if $flags & 0x20; | |
1482 $buf |= pack_length($hlen); | |
1483 | |
1484 $buf .= pack 'C', $pad if $padlen; # Pad Length? | |
1485 $buf .= pack 'NC', $dep, $prio if $flags & 0x20; | |
1486 $buf .= $input; | |
1487 $buf .= (pack 'C', 0) x $pad if $padlen; # Padding | |
1488 | |
1489 if (defined $body) { | |
1490 $buf .= pack_length(length($body) + $bpad + $bpadlen); | |
1491 my $flags = $bpadlen ? 0x8 : 0x0; | |
1492 $buf .= pack 'CC', 0x0, 0x1 | $flags; # DATA, END_STREAM | |
1493 $buf .= pack 'N', $ctx->{last_stream}; | |
1494 $buf .= pack 'C', $bpad if $bpadlen; # DATA Pad Length? | |
1495 $buf .= $body; | |
1496 $buf .= (pack 'C', 0) x $bpad if $bpadlen; # DATA Padding | |
1497 } | |
1498 | |
1499 raw_write($ctx->{socket}, $buf); | |
1500 return $ctx->{last_stream}; | |
1501 } | |
1502 | |
1503 sub h2_read { | |
1504 my ($sess, %extra) = @_; | |
1505 my (@got); | |
1506 my $s = $sess->{socket}; | |
1507 my $buf = ''; | |
1508 | |
1509 while (1) { | |
1510 $buf = raw_read($s, $buf, 9); | |
1511 last unless length $buf; | |
1512 | |
1513 my $length = unpack_length($buf); | |
1514 my $type = unpack('x3C', $buf); | |
1515 my $flags = unpack('x4C', $buf); | |
1516 | |
1517 $buf = raw_read($s, $buf, $length + 9); | |
1518 last unless length $buf; | |
1519 | |
1520 my $frame = $cframe{$type}{value}($sess, $buf, $length); | |
1521 $frame->{length} = $length; | |
1522 $frame->{type} = $cframe{$type}{name}; | |
1523 $frame->{flags} = $flags; | |
1524 push @got, $frame; | |
1525 | |
1526 $buf = substr($buf, $length + 9); | |
1527 | |
1528 last unless test_fin($got[-1], $extra{all}); | |
1529 }; | |
1530 return \@got; | |
1531 } | |
1532 | |
1533 sub test_fin { | |
1534 my ($frame, $all) = @_; | |
1535 my @test = @{$all}; | |
1536 | |
1537 # wait for the specified DATA length | |
1538 | |
1539 for (@test) { | |
1540 if ($_->{length} && $frame->{type} eq 'DATA') { | |
1541 # check also for StreamID if needed | |
1542 | |
1543 if (!$_->{sid} || $_->{sid} == $frame->{sid}) { | |
1544 $_->{length} -= $frame->{length}; | |
1545 } | |
1546 } | |
1547 } | |
1548 @test = grep { !(defined $_->{length} && $_->{length} == 0) } @test; | |
1549 | |
1550 # wait for the fin flag | |
1551 | |
1552 @test = grep { !(defined $_->{fin} | |
1553 && $_->{sid} == $frame->{sid} && $_->{fin} & $frame->{flags}) | |
1554 } @test if defined $frame->{flags}; | |
1555 | |
1556 # wait for the specified frame | |
1557 | |
1558 @test = grep { !($_->{type} && $_->{type} eq $frame->{type}) } @test; | |
1559 | |
1560 @{$all} = @test; | |
1561 } | |
1562 | |
1563 sub headers { | |
1564 my ($ctx, $buf, $len) = @_; | |
1565 my %payload; | |
1566 my $skip = 9; | |
1567 | |
1568 my $stream = unpack "x5 B32", $buf; | |
1569 substr($stream, 0, 1) = 0; | |
1570 $stream = unpack("N", pack("B32", $stream)); | |
1571 $payload{sid} = $stream; | |
1572 | |
1573 my $headers = substr($buf, $skip); | |
1574 $payload{headers} = hunpack($ctx, $headers, $len); | |
1575 | |
1576 | |
1577 return \%payload; | |
1578 } | |
1579 | |
1580 sub data { | |
1581 my ($ctx, $buf, $len) = @_; | |
1582 my %frame; | |
1583 | |
1584 my $stream = unpack "x5 B32", $buf; | |
1585 substr($stream, 0, 1) = 0; | |
1586 $stream = unpack("N", pack("B32", $stream)); | |
1587 $frame{sid} = $stream; | |
1588 | |
1589 $frame{data} = substr($buf, 9, $len); | |
1590 return \%frame; | |
1591 } | |
1592 | |
1593 sub settings { | |
1594 my ($ctx, $buf, $len) = @_; | |
1595 my %payload; | |
1596 my $skip = 9; | |
1597 | |
1598 my $stream = unpack "x5 B32", $buf; | |
1599 substr($stream, 0, 1) = 0; | |
1600 $stream = unpack("N", pack("B32", $stream)); | |
1601 $payload{sid} = $stream; | |
1602 | |
1603 fail("stream identifier field is anything other than 0x0") | |
1604 if $stream; | |
1605 | |
1606 for (1 .. $len / 6) { | |
1607 my $id = hex unpack "\@$skip n", $buf; $skip += 2; | |
1608 $payload{$id} = unpack "\@$skip N", $buf; $skip += 4; | |
1609 } | |
1610 return \%payload; | |
1611 } | |
1612 | |
1613 sub ping { | |
1614 my ($ctx, $buf, $len) = @_; | |
1615 my %payload; | |
1616 my $skip = 9; | |
1617 | |
1618 $payload{value} = unpack "\@$skip a8", $buf; | |
1619 return \%payload; | |
1620 } | |
1621 | |
1622 sub rst_stream { | |
1623 my ($ctx, $buf, $len) = @_; | |
1624 my %payload; | |
1625 my $skip = 9; | |
1626 | |
1627 my $stream = unpack "x5 B32", $buf; | |
1628 substr($stream, 0, 1) = 0; | |
1629 $stream = unpack("N", pack("B32", $stream)); | |
1630 $payload{sid} = $stream; | |
1631 | |
1632 $payload{code} = unpack "\@$skip N", $buf; | |
1633 return \%payload; | |
1634 } | |
1635 | |
1636 sub goaway { | |
1637 my ($ctx, $buf, $len) = @_; | |
1638 my %payload; | |
1639 my $skip = 9; | |
1640 | |
1641 my $stream = unpack "x5 B32", $buf; | |
1642 substr($stream, 0, 1) = 0; | |
1643 $stream = unpack("N", pack("B32", $stream)); | |
1644 $payload{sid} = $stream; | |
1645 | |
1646 fail("stream identifier field is anything other than 0x0") | |
1647 if $stream; | |
1648 | |
1649 $stream = unpack "\@$skip B32", $buf; | |
1650 substr($stream, 0, 1) = 0; | |
1651 $stream = unpack("N", pack("B32", $stream)); | |
1652 $payload{last_sid} = $stream; | |
1653 $skip += 4; | |
1654 | |
1655 $len = $len - $skip; | |
1656 $payload{debug} = unpack "\@$skip H$len", $buf; | |
1657 return \%payload; | |
1658 } | |
1659 | |
1660 sub window_update { | |
1661 my ($ctx, $buf, $len) = @_; | |
1662 my %payload; | |
1663 my $skip = 5; | |
1664 | |
1665 my $stream = unpack "\@$skip B32", $buf; $skip += 4; | |
1666 substr($stream, 0, 1) = 0; | |
1667 $stream = unpack("N", pack("B32", $stream)); | |
1668 $payload{sid} = $stream; | |
1669 | |
1670 my $value = unpack "\@$skip B32", $buf; | |
1671 substr($value, 0, 1) = 0; | |
1672 $payload{wdelta} = unpack("N", pack("B32", $value)); | |
1673 return \%payload; | |
1674 } | |
1675 | |
1676 sub pack_length { | |
1677 pack 'c3', unpack 'xc3', pack 'N', $_[0]; | |
1678 } | |
1679 | |
1680 sub unpack_length { | |
1681 unpack 'N', pack 'xc3', unpack 'c3', $_[0]; | |
1682 } | |
1683 | |
1684 sub raw_read { | |
1685 my ($s, $buf, $len) = @_; | |
1686 my $got = ''; | |
1687 | |
1688 while (length($buf) < $len && IO::Select->new($s)->can_read(1)) { | |
650
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
1689 $s->sysread($got, 16384) or last; |
646 | 1690 log_in($got); |
1691 $buf .= $got; | |
1692 } | |
1693 return $buf; | |
1694 } | |
1695 | |
1696 sub raw_write { | |
1697 my ($s, $message) = @_; | |
1698 | |
1699 local $SIG{PIPE} = 'IGNORE'; | |
1700 | |
1701 while (IO::Select->new($s)->can_write(0.4)) { | |
1702 log_out($message); | |
1703 my $n = $s->syswrite($message); | |
1704 last unless $n; | |
1705 $message = substr($message, $n); | |
1706 last unless length $message; | |
1707 } | |
1708 } | |
1709 | |
1710 sub new_session { | |
1711 my ($port, %extra) = @_; | |
1712 my ($s); | |
1713 | |
650
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
1714 $s = new_socket($port, %extra); |
646 | 1715 |
1716 if ($extra{proxy}) { | |
1717 raw_write($s, $extra{proxy}); | |
1718 } | |
1719 | |
1720 # preface | |
1721 | |
1722 raw_write($s, 'PRI * HTTP/2.0' . CRLF . CRLF . 'SM' . CRLF . CRLF); | |
1723 | |
1724 return { socket => $s, last_stream => -1, | |
1725 dynamic_encode => [ static_table() ], | |
1726 dynamic_decode => [ static_table() ], | |
1727 static_table_size => scalar @{[static_table()]} }; | |
1728 } | |
1729 | |
1730 sub new_socket { | |
650
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
1731 my ($port, %extra) = @_; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
1732 my $npn = $extra{'npn'}; |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
1733 my $alpn = $extra{'alpn'}; |
646 | 1734 my $s; |
1735 | |
1736 $port = 8080 unless defined $port; | |
1737 | |
1738 eval { | |
1739 local $SIG{ALRM} = sub { die "timeout\n" }; | |
1740 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
1741 alarm(2); | |
1742 $s = IO::Socket::INET->new( | |
1743 Proto => 'tcp', | |
1744 PeerAddr => "127.0.0.1:$port", | |
1745 ); | |
650
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
1746 IO::Socket::SSL->start_SSL($s, |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
1747 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(), |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
1748 SSL_npn_protocols => $npn ? [ $npn ] : undef, |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
1749 SSL_alpn_protocols => $alpn ? [ $alpn ] : undef, |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
1750 SSL_error_trap => sub { die $_[1] } |
8a2cf558b8ae
Tests: HTTP/2 tests for $http2 variable.
Sergey Kandaurov <pluknet@nginx.com>
parents:
647
diff
changeset
|
1751 ) if $extra{'SSL'}; |
646 | 1752 alarm(0); |
1753 }; | |
1754 alarm(0); | |
1755 | |
1756 if ($@) { | |
1757 log_in("died: $@"); | |
1758 return undef; | |
1759 } | |
1760 | |
1761 return $s; | |
1762 } | |
1763 | |
1764 sub static_table { | |
1765 [ '', '' ], # unused | |
1766 [ ':authority', '' ], | |
1767 [ ':method', 'GET' ], | |
1768 [ ':method', 'POST' ], | |
1769 [ ':path', '/' ], | |
1770 [ ':path', '/index.html' ], | |
1771 [ ':scheme', 'http' ], | |
1772 [ ':scheme', 'https' ], | |
1773 [ ':status', '200' ], | |
1774 [ ':status', '204' ], | |
1775 [ ':status', '206' ], | |
1776 [ ':status', '304' ], | |
1777 [ ':status', '400' ], | |
1778 [ ':status', '404' ], | |
1779 [ ':status', '500' ], | |
1780 [ 'accept-charset', '' ], | |
1781 [ 'accept-encoding', 'gzip, deflate' ], | |
1782 [ 'accept-language', '' ], | |
1783 [ 'accept-ranges', '' ], | |
1784 [ 'accept', '' ], | |
1785 [ 'access-control-allow-origin', | |
1786 '' ], | |
1787 [ 'age', '' ], | |
1788 [ 'allow', '' ], | |
1789 [ 'authorization', '' ], | |
1790 [ 'cache-control', '' ], | |
1791 [ 'content-disposition', | |
1792 '' ], | |
1793 [ 'content-encoding', '' ], | |
1794 [ 'content-language', '' ], | |
1795 [ 'content-length', '' ], | |
1796 [ 'content-location', '' ], | |
1797 [ 'content-range', '' ], | |
1798 [ 'content-type', '' ], | |
1799 [ 'cookie', '' ], | |
1800 [ 'date', '' ], | |
1801 [ 'etag', '' ], | |
1802 [ 'expect', '' ], | |
1803 [ 'expires', '' ], | |
1804 [ 'from', '' ], | |
1805 [ 'host', '' ], | |
1806 [ 'if-match', '' ], | |
1807 [ 'if-modified-since', '' ], | |
1808 [ 'if-none-match', '' ], | |
1809 [ 'if-range', '' ], | |
1810 [ 'if-unmodified-since', | |
1811 '' ], | |
1812 [ 'last-modified', '' ], | |
1813 [ 'link', '' ], | |
1814 [ 'location', '' ], | |
1815 [ 'max-forwards', '' ], | |
1816 [ 'proxy-authenticate', '' ], | |
1817 [ 'proxy-authorization', | |
1818 '' ], | |
1819 [ 'range', '' ], | |
1820 [ 'referer', '' ], | |
1821 [ 'refresh', '' ], | |
1822 [ 'retry-after', '' ], | |
1823 [ 'server', '' ], | |
1824 [ 'set-cookie', '' ], | |
1825 [ 'strict-transport-security', | |
1826 '' ], | |
1827 [ 'transfer-encoding', '' ], | |
1828 [ 'user-agent', '' ], | |
1829 [ 'vary', '' ], | |
1830 [ 'via', '' ], | |
1831 [ 'www-authenticate', '' ], | |
1832 } | |
1833 | |
1834 sub hpack { | |
1835 my ($ctx, $name, $value, %extra) = @_; | |
1836 my @table = @{$ctx->{dynamic_encode}}; | |
1837 my $mode = defined $extra{mode} ? $extra{mode} : 1; | |
1838 my $huff = $extra{huff}; | |
1839 | |
1840 my ($index, $buf) = 0; | |
1841 | |
1842 # 6.1. Indexed Header Field Representation | |
1843 | |
1844 if ($mode == 0) { | |
1845 ++$index until $index > $#table | |
1846 or $table[$index][0] eq $name | |
1847 and $table[$index][1] eq $value; | |
1848 $buf = pack('B*', '1' . sprintf("%7b", $index)); | |
1849 } | |
1850 | |
1851 # 6.2.1. Literal Header Field with Incremental Indexing | |
1852 | |
1853 if ($mode == 1) { | |
1854 splice @{$ctx->{dynamic_encode}}, $ctx->{static_table_size}, 0, | |
1855 [ $name, $value ]; | |
1856 | |
1857 ++$index until $index > $#table or $table[$index][0] eq $name; | |
1858 my $value = $huff ? huff($value) : $value; | |
1859 | |
1860 $buf = pack('B*', '01' . sprintf("%6b", $index) | |
1861 . ($huff ? '1' : '0') . sprintf("%7b", length($value))); | |
1862 $buf .= $value; | |
1863 } | |
1864 | |
1865 # 6.2.1. Literal Header Field with Incremental Indexing -- New Name | |
1866 | |
1867 if ($mode == 2) { | |
1868 splice @{$ctx->{dynamic_encode}}, $ctx->{static_table_size}, 0, | |
1869 [ $name, $value ]; | |
1870 | |
1871 my $name = $huff ? huff($name) : $name; | |
1872 my $value = $huff ? huff($value) : $value; | |
1873 my $hbit = ($huff ? '1' : '0'); | |
1874 | |
1875 $buf = pack('B*', '01000000'); | |
1876 $buf .= pack('B*', $hbit . sprintf("%7b", length($name))); | |
1877 $buf .= $name; | |
1878 $buf .= pack('B*', $hbit . sprintf("%7b", length($value))); | |
1879 $buf .= $value; | |
1880 } | |
1881 | |
1882 # 6.2.2. Literal Header Field without Indexing | |
1883 | |
1884 if ($mode == 3) { | |
1885 ++$index until $index > $#table or $table[$index][0] eq $name; | |
1886 my $value = $huff ? huff($value) : $value; | |
1887 | |
1888 $buf = pack('B*', '0000' . sprintf("%4b", $index) | |
1889 . ($huff ? '1' : '0') . sprintf("%7b", length($value))); | |
1890 $buf .= $value; | |
1891 } | |
1892 | |
1893 # 6.2.2. Literal Header Field without Indexing -- New Name | |
1894 | |
1895 if ($mode == 4) { | |
1896 my $name = $huff ? huff($name) : $name; | |
1897 my $value = $huff ? huff($value) : $value; | |
1898 my $hbit = ($huff ? '1' : '0'); | |
1899 | |
1900 $buf = pack('B*', '00000000'); | |
1901 $buf .= pack('B*', $hbit . sprintf("%7b", length($name))); | |
1902 $buf .= $name; | |
1903 $buf .= pack('B*', $hbit . sprintf("%7b", length($value))); | |
1904 $buf .= $value; | |
1905 } | |
1906 | |
1907 # 6.2.3. Literal Header Field Never Indexed | |
1908 | |
1909 if ($mode == 5) { | |
1910 ++$index until $index > $#table or $table[$index][0] eq $name; | |
1911 my $value = $huff ? huff($value) : $value; | |
1912 | |
1913 $buf = pack('B*', '0001' . sprintf("%4b", $index) | |
1914 . ($huff ? '1' : '0') . sprintf("%7b", length($value))); | |
1915 $buf .= $value; | |
1916 } | |
1917 | |
1918 # 6.2.3. Literal Header Field Never Indexed -- New Name | |
1919 | |
1920 if ($mode == 6) { | |
1921 my $name = $huff ? huff($name) : $name; | |
1922 my $value = $huff ? huff($value) : $value; | |
1923 my $hbit = ($huff ? '1' : '0'); | |
1924 | |
1925 $buf = pack('B*', '00010000'); | |
1926 $buf .= pack('B*', $hbit . sprintf("%7b", length($name))); | |
1927 $buf .= $name; | |
1928 $buf .= pack('B*', $hbit . sprintf("%7b", length($value))); | |
1929 $buf .= $value; | |
1930 } | |
1931 | |
1932 return $buf; | |
1933 } | |
1934 | |
1935 sub hunpack { | |
1936 my ($ctx, $data, $length) = @_; | |
1937 my @table = @{$ctx->{dynamic_decode}}; | |
1938 my %headers; | |
1939 my $skip = 0; | |
1940 my ($name, $value); | |
1941 | |
1942 sub index { | |
1943 my ($b, $i) = @_; | |
1944 unpack("C", pack("B8", '0' x $i . substr($b, $i, 8 - $i))); | |
1945 } | |
1946 | |
1947 sub field { | |
1948 my ($b, $s) = @_; | |
1949 my $len = unpack("\@$s B8", $b); | |
1950 my $huff = substr($len, 0, 1) ? 1 : 0; | |
1951 $len = unpack("C", pack("B8", '0' . substr($len, 1, 8))); | |
1952 $s++; | |
1953 | |
1954 my $field = substr($b, $s, $len); | |
1955 $field = $huff ? dehuff($field) : $field; | |
1956 $s += $len; | |
1957 return ($field, $s); | |
1958 } | |
1959 | |
1960 sub add { | |
1961 my ($h, $n, $v) = @_; | |
1962 return $h->{$n} = $v unless exists $h->{$n}; | |
1963 $h->{$n} = [ $h->{$n} ]; | |
647
4e36550410b3
Tests: h2.t fixes for older perl versions, and gzip test.
Sergey Kandaurov <pluknet@nginx.com>
parents:
646
diff
changeset
|
1964 push @{$h->{$n}}, $v; |
646 | 1965 } |
1966 | |
1967 while ($skip < $length) { | |
1968 my $ib = unpack("\@$skip B8", $data); | |
1969 | |
1970 if (substr($ib, 0, 1) eq '1') { | |
1971 my $index = &index($ib, 1); | |
1972 add(\%headers, $table[$index][0], $table[$index][1]); | |
1973 $skip += 1; | |
1974 next; | |
1975 } | |
1976 | |
1977 if (substr($ib, 0, 2) eq '01') { | |
1978 $name = $table[&index($ib, 2)][0]; | |
1979 $skip++; | |
1980 | |
1981 ($name, $skip) = field($data, $skip) unless $name; | |
1982 ($value, $skip) = field($data, $skip); | |
1983 | |
1984 splice @{$ctx->{dynamic_decode}}, | |
1985 $ctx->{static_table_size}, 0, [ $name, $value ]; | |
1986 add(\%headers, $name, $value); | |
1987 next; | |
1988 } | |
1989 | |
1990 if (substr($ib, 0, 4) eq '0000') { | |
1991 $name = $table[&index($ib, 4)][0]; | |
1992 $skip++; | |
1993 | |
1994 ($name, $skip) = field($data, $skip) unless $name; | |
1995 ($value, $skip) = field($data, $skip); | |
1996 | |
1997 add(\%headers, $name, $value); | |
1998 next; | |
1999 } | |
2000 } | |
2001 | |
2002 return \%headers; | |
2003 } | |
2004 | |
2005 sub huff_code { scalar { | |
2006 pack('C', 0) => '1111111111000', | |
2007 pack('C', 1) => '11111111111111111011000', | |
2008 pack('C', 2) => '1111111111111111111111100010', | |
2009 pack('C', 3) => '1111111111111111111111100011', | |
2010 pack('C', 4) => '1111111111111111111111100100', | |
2011 pack('C', 5) => '1111111111111111111111100101', | |
2012 pack('C', 6) => '1111111111111111111111100110', | |
2013 pack('C', 7) => '1111111111111111111111100111', | |
2014 pack('C', 8) => '1111111111111111111111101000', | |
2015 pack('C', 9) => '111111111111111111101010', | |
2016 pack('C', 10) => '111111111111111111111111111100', | |
2017 pack('C', 11) => '1111111111111111111111101001', | |
2018 pack('C', 12) => '1111111111111111111111101010', | |
2019 pack('C', 13) => '111111111111111111111111111101', | |
2020 pack('C', 14) => '1111111111111111111111101011', | |
2021 pack('C', 15) => '1111111111111111111111101100', | |
2022 pack('C', 16) => '1111111111111111111111101101', | |
2023 pack('C', 17) => '1111111111111111111111101110', | |
2024 pack('C', 18) => '1111111111111111111111101111', | |
2025 pack('C', 19) => '1111111111111111111111110000', | |
2026 pack('C', 20) => '1111111111111111111111110001', | |
2027 pack('C', 21) => '1111111111111111111111110010', | |
2028 pack('C', 22) => '111111111111111111111111111110', | |
2029 pack('C', 23) => '1111111111111111111111110011', | |
2030 pack('C', 24) => '1111111111111111111111110100', | |
2031 pack('C', 25) => '1111111111111111111111110101', | |
2032 pack('C', 26) => '1111111111111111111111110110', | |
2033 pack('C', 27) => '1111111111111111111111110111', | |
2034 pack('C', 28) => '1111111111111111111111111000', | |
2035 pack('C', 29) => '1111111111111111111111111001', | |
2036 pack('C', 30) => '1111111111111111111111111010', | |
2037 pack('C', 31) => '1111111111111111111111111011', | |
2038 pack('C', 32) => '010100', | |
2039 pack('C', 33) => '1111111000', | |
2040 pack('C', 34) => '1111111001', | |
2041 pack('C', 35) => '111111111010', | |
2042 pack('C', 36) => '1111111111001', | |
2043 pack('C', 37) => '010101', | |
2044 pack('C', 38) => '11111000', | |
2045 pack('C', 39) => '11111111010', | |
2046 pack('C', 40) => '1111111010', | |
2047 pack('C', 41) => '1111111011', | |
2048 pack('C', 42) => '11111001', | |
2049 pack('C', 43) => '11111111011', | |
2050 pack('C', 44) => '11111010', | |
2051 pack('C', 45) => '010110', | |
2052 pack('C', 46) => '010111', | |
2053 pack('C', 47) => '011000', | |
2054 pack('C', 48) => '00000', | |
2055 pack('C', 49) => '00001', | |
2056 pack('C', 50) => '00010', | |
2057 pack('C', 51) => '011001', | |
2058 pack('C', 52) => '011010', | |
2059 pack('C', 53) => '011011', | |
2060 pack('C', 54) => '011100', | |
2061 pack('C', 55) => '011101', | |
2062 pack('C', 56) => '011110', | |
2063 pack('C', 57) => '011111', | |
2064 pack('C', 58) => '1011100', | |
2065 pack('C', 59) => '11111011', | |
2066 pack('C', 60) => '111111111111100', | |
2067 pack('C', 61) => '100000', | |
2068 pack('C', 62) => '111111111011', | |
2069 pack('C', 63) => '1111111100', | |
2070 pack('C', 64) => '1111111111010', | |
2071 pack('C', 65) => '100001', | |
2072 pack('C', 66) => '1011101', | |
2073 pack('C', 67) => '1011110', | |
2074 pack('C', 68) => '1011111', | |
2075 pack('C', 69) => '1100000', | |
2076 pack('C', 70) => '1100001', | |
2077 pack('C', 71) => '1100010', | |
2078 pack('C', 72) => '1100011', | |
2079 pack('C', 73) => '1100100', | |
2080 pack('C', 74) => '1100101', | |
2081 pack('C', 75) => '1100110', | |
2082 pack('C', 76) => '1100111', | |
2083 pack('C', 77) => '1101000', | |
2084 pack('C', 78) => '1101001', | |
2085 pack('C', 79) => '1101010', | |
2086 pack('C', 80) => '1101011', | |
2087 pack('C', 81) => '1101100', | |
2088 pack('C', 82) => '1101101', | |
2089 pack('C', 83) => '1101110', | |
2090 pack('C', 84) => '1101111', | |
2091 pack('C', 85) => '1110000', | |
2092 pack('C', 86) => '1110001', | |
2093 pack('C', 87) => '1110010', | |
2094 pack('C', 88) => '11111100', | |
2095 pack('C', 89) => '1110011', | |
2096 pack('C', 90) => '11111101', | |
2097 pack('C', 91) => '1111111111011', | |
2098 pack('C', 92) => '1111111111111110000', | |
2099 pack('C', 93) => '1111111111100', | |
2100 pack('C', 94) => '11111111111100', | |
2101 pack('C', 95) => '100010', | |
2102 pack('C', 96) => '111111111111101', | |
2103 pack('C', 97) => '00011', | |
2104 pack('C', 98) => '100011', | |
2105 pack('C', 99) => '00100', | |
2106 pack('C', 100) => '100100', | |
2107 pack('C', 101) => '00101', | |
2108 pack('C', 102) => '100101', | |
2109 pack('C', 103) => '100110', | |
2110 pack('C', 104) => '100111', | |
2111 pack('C', 105) => '00110', | |
2112 pack('C', 106) => '1110100', | |
2113 pack('C', 107) => '1110101', | |
2114 pack('C', 108) => '101000', | |
2115 pack('C', 109) => '101001', | |
2116 pack('C', 110) => '101010', | |
2117 pack('C', 111) => '00111', | |
2118 pack('C', 112) => '101011', | |
2119 pack('C', 113) => '1110110', | |
2120 pack('C', 114) => '101100', | |
2121 pack('C', 115) => '01000', | |
2122 pack('C', 116) => '01001', | |
2123 pack('C', 117) => '101101', | |
2124 pack('C', 118) => '1110111', | |
2125 pack('C', 119) => '1111000', | |
2126 pack('C', 120) => '1111001', | |
2127 pack('C', 121) => '1111010', | |
2128 pack('C', 122) => '1111011', | |
2129 pack('C', 123) => '111111111111110', | |
2130 pack('C', 124) => '11111111100', | |
2131 pack('C', 125) => '11111111111101', | |
2132 pack('C', 126) => '1111111111101', | |
2133 pack('C', 127) => '1111111111111111111111111100', | |
2134 pack('C', 128) => '11111111111111100110', | |
2135 pack('C', 129) => '1111111111111111010010', | |
2136 pack('C', 130) => '11111111111111100111', | |
2137 pack('C', 131) => '11111111111111101000', | |
2138 pack('C', 132) => '1111111111111111010011', | |
2139 pack('C', 133) => '1111111111111111010100', | |
2140 pack('C', 134) => '1111111111111111010101', | |
2141 pack('C', 135) => '11111111111111111011001', | |
2142 pack('C', 136) => '1111111111111111010110', | |
2143 pack('C', 137) => '11111111111111111011010', | |
2144 pack('C', 138) => '11111111111111111011011', | |
2145 pack('C', 139) => '11111111111111111011100', | |
2146 pack('C', 140) => '11111111111111111011101', | |
2147 pack('C', 141) => '11111111111111111011110', | |
2148 pack('C', 142) => '111111111111111111101011', | |
2149 pack('C', 143) => '11111111111111111011111', | |
2150 pack('C', 144) => '111111111111111111101100', | |
2151 pack('C', 145) => '111111111111111111101101', | |
2152 pack('C', 146) => '1111111111111111010111', | |
2153 pack('C', 147) => '11111111111111111100000', | |
2154 pack('C', 148) => '111111111111111111101110', | |
2155 pack('C', 149) => '11111111111111111100001', | |
2156 pack('C', 150) => '11111111111111111100010', | |
2157 pack('C', 151) => '11111111111111111100011', | |
2158 pack('C', 152) => '11111111111111111100100', | |
2159 pack('C', 153) => '111111111111111011100', | |
2160 pack('C', 154) => '1111111111111111011000', | |
2161 pack('C', 155) => '11111111111111111100101', | |
2162 pack('C', 156) => '1111111111111111011001', | |
2163 pack('C', 157) => '11111111111111111100110', | |
2164 pack('C', 158) => '11111111111111111100111', | |
2165 pack('C', 159) => '111111111111111111101111', | |
2166 pack('C', 160) => '1111111111111111011010', | |
2167 pack('C', 161) => '111111111111111011101', | |
2168 pack('C', 162) => '11111111111111101001', | |
2169 pack('C', 163) => '1111111111111111011011', | |
2170 pack('C', 164) => '1111111111111111011100', | |
2171 pack('C', 165) => '11111111111111111101000', | |
2172 pack('C', 166) => '11111111111111111101001', | |
2173 pack('C', 167) => '111111111111111011110', | |
2174 pack('C', 168) => '11111111111111111101010', | |
2175 pack('C', 169) => '1111111111111111011101', | |
2176 pack('C', 170) => '1111111111111111011110', | |
2177 pack('C', 171) => '111111111111111111110000', | |
2178 pack('C', 172) => '111111111111111011111', | |
2179 pack('C', 173) => '1111111111111111011111', | |
2180 pack('C', 174) => '11111111111111111101011', | |
2181 pack('C', 175) => '11111111111111111101100', | |
2182 pack('C', 176) => '111111111111111100000', | |
2183 pack('C', 177) => '111111111111111100001', | |
2184 pack('C', 178) => '1111111111111111100000', | |
2185 pack('C', 179) => '111111111111111100010', | |
2186 pack('C', 180) => '11111111111111111101101', | |
2187 pack('C', 181) => '1111111111111111100001', | |
2188 pack('C', 182) => '11111111111111111101110', | |
2189 pack('C', 183) => '11111111111111111101111', | |
2190 pack('C', 184) => '11111111111111101010', | |
2191 pack('C', 185) => '1111111111111111100010', | |
2192 pack('C', 186) => '1111111111111111100011', | |
2193 pack('C', 187) => '1111111111111111100100', | |
2194 pack('C', 188) => '11111111111111111110000', | |
2195 pack('C', 189) => '1111111111111111100101', | |
2196 pack('C', 190) => '1111111111111111100110', | |
2197 pack('C', 191) => '11111111111111111110001', | |
2198 pack('C', 192) => '11111111111111111111100000', | |
2199 pack('C', 193) => '11111111111111111111100001', | |
2200 pack('C', 194) => '11111111111111101011', | |
2201 pack('C', 195) => '1111111111111110001', | |
2202 pack('C', 196) => '1111111111111111100111', | |
2203 pack('C', 197) => '11111111111111111110010', | |
2204 pack('C', 198) => '1111111111111111101000', | |
2205 pack('C', 199) => '1111111111111111111101100', | |
2206 pack('C', 200) => '11111111111111111111100010', | |
2207 pack('C', 201) => '11111111111111111111100011', | |
2208 pack('C', 202) => '11111111111111111111100100', | |
2209 pack('C', 203) => '111111111111111111111011110', | |
2210 pack('C', 204) => '111111111111111111111011111', | |
2211 pack('C', 205) => '11111111111111111111100101', | |
2212 pack('C', 206) => '111111111111111111110001', | |
2213 pack('C', 207) => '1111111111111111111101101', | |
2214 pack('C', 208) => '1111111111111110010', | |
2215 pack('C', 209) => '111111111111111100011', | |
2216 pack('C', 210) => '11111111111111111111100110', | |
2217 pack('C', 211) => '111111111111111111111100000', | |
2218 pack('C', 212) => '111111111111111111111100001', | |
2219 pack('C', 213) => '11111111111111111111100111', | |
2220 pack('C', 214) => '111111111111111111111100010', | |
2221 pack('C', 215) => '111111111111111111110010', | |
2222 pack('C', 216) => '111111111111111100100', | |
2223 pack('C', 217) => '111111111111111100101', | |
2224 pack('C', 218) => '11111111111111111111101000', | |
2225 pack('C', 219) => '11111111111111111111101001', | |
2226 pack('C', 220) => '1111111111111111111111111101', | |
2227 pack('C', 221) => '111111111111111111111100011', | |
2228 pack('C', 222) => '111111111111111111111100100', | |
2229 pack('C', 223) => '111111111111111111111100101', | |
2230 pack('C', 224) => '11111111111111101100', | |
2231 pack('C', 225) => '111111111111111111110011', | |
2232 pack('C', 226) => '11111111111111101101', | |
2233 pack('C', 227) => '111111111111111100110', | |
2234 pack('C', 228) => '1111111111111111101001', | |
2235 pack('C', 229) => '111111111111111100111', | |
2236 pack('C', 230) => '111111111111111101000', | |
2237 pack('C', 231) => '11111111111111111110011', | |
2238 pack('C', 232) => '1111111111111111101010', | |
2239 pack('C', 233) => '1111111111111111101011', | |
2240 pack('C', 234) => '1111111111111111111101110', | |
2241 pack('C', 235) => '1111111111111111111101111', | |
2242 pack('C', 236) => '111111111111111111110100', | |
2243 pack('C', 237) => '111111111111111111110101', | |
2244 pack('C', 238) => '11111111111111111111101010', | |
2245 pack('C', 239) => '11111111111111111110100', | |
2246 pack('C', 240) => '11111111111111111111101011', | |
2247 pack('C', 241) => '111111111111111111111100110', | |
2248 pack('C', 242) => '11111111111111111111101100', | |
2249 pack('C', 243) => '11111111111111111111101101', | |
2250 pack('C', 244) => '111111111111111111111100111', | |
2251 pack('C', 245) => '111111111111111111111101000', | |
2252 pack('C', 246) => '111111111111111111111101001', | |
2253 pack('C', 247) => '111111111111111111111101010', | |
2254 pack('C', 248) => '111111111111111111111101011', | |
2255 pack('C', 249) => '1111111111111111111111111110', | |
2256 pack('C', 250) => '111111111111111111111101100', | |
2257 pack('C', 251) => '111111111111111111111101101', | |
2258 pack('C', 252) => '111111111111111111111101110', | |
2259 pack('C', 253) => '111111111111111111111101111', | |
2260 pack('C', 254) => '111111111111111111111110000', | |
2261 pack('C', 255) => '11111111111111111111101110', | |
2262 '_eos' => '111111111111111111111111111111', | |
2263 }}; | |
2264 | |
2265 sub huff { | |
2266 my ($string) = @_; | |
2267 my $code = &huff_code; | |
2268 | |
2269 my $ret = join '', map { $code->{$_} } (split //, $string); | |
2270 my $len = length($ret) + (8 - length($ret) % 8); | |
2271 $ret .= $code->{_eos}; | |
2272 | |
2273 return pack("B$len", $ret); | |
2274 } | |
2275 | |
2276 sub dehuff { | |
2277 my ($string) = @_; | |
2278 my $code = &huff_code; | |
2279 my %decode = reverse %$code; | |
2280 | |
2281 my $ret = ''; my $c = ''; | |
2282 for (split //, unpack('B*', $string)) { | |
2283 $c .= $_; | |
2284 next unless exists $decode{$c}; | |
2285 last if $decode{$c} eq '_eos'; | |
2286 | |
2287 $ret .= $decode{$c}; | |
2288 $c = ''; | |
2289 } | |
2290 | |
2291 return $ret; | |
2292 } | |
2293 | |
2294 ############################################################################### | |
2295 | |
647
4e36550410b3
Tests: h2.t fixes for older perl versions, and gzip test.
Sergey Kandaurov <pluknet@nginx.com>
parents:
646
diff
changeset
|
2296 sub gunzip_like { |
4e36550410b3
Tests: h2.t fixes for older perl versions, and gzip test.
Sergey Kandaurov <pluknet@nginx.com>
parents:
646
diff
changeset
|
2297 my ($in, $re, $name) = @_; |
646 | 2298 |
2299 SKIP: { | |
2300 eval { require IO::Uncompress::Gunzip; }; | |
2301 Test::More::skip( | |
2302 "IO::Uncompress::Gunzip not installed", 1) if $@; | |
2303 | |
2304 my $out; | |
2305 | |
2306 IO::Uncompress::Gunzip::gunzip(\$in => \$out); | |
2307 | |
647
4e36550410b3
Tests: h2.t fixes for older perl versions, and gzip test.
Sergey Kandaurov <pluknet@nginx.com>
parents:
646
diff
changeset
|
2308 like($out, $re, $name); |
646 | 2309 } |
2310 } | |
2311 | |
2312 ############################################################################### | |
2313 | |
2314 # for tests with multiple header fields | |
2315 | |
2316 sub http_daemon { | |
2317 my $server = IO::Socket::INET->new( | |
2318 Proto => 'tcp', | |
2319 LocalHost => '127.0.0.1', | |
2320 LocalPort => 8083, | |
2321 Listen => 5, | |
2322 Reuse => 1 | |
2323 ) | |
2324 or die "Can't create listening socket: $!\n"; | |
2325 | |
2326 local $SIG{PIPE} = 'IGNORE'; | |
2327 | |
2328 while (my $client = $server->accept()) { | |
2329 $client->autoflush(1); | |
2330 | |
2331 my $headers = ''; | |
2332 my $uri = ''; | |
2333 my $cookie = ''; | |
2334 | |
2335 while (<$client>) { | |
2336 $headers .= $_; | |
2337 last if (/^\x0d?\x0a?$/); | |
2338 } | |
2339 | |
2340 next if $headers eq ''; | |
2341 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; | |
2342 $cookie = $1 if $headers =~ /Cookie: (.+)/i; | |
2343 | |
2344 if ($uri eq '/cookie') { | |
2345 | |
2346 print $client <<EOF; | |
2347 HTTP/1.1 200 OK | |
2348 Connection: close | |
2349 X-Cookie: $cookie | |
2350 | |
2351 EOF | |
2352 | |
2353 } elsif ($uri eq '/set-cookie') { | |
2354 | |
2355 print $client <<EOF; | |
2356 HTTP/1.1 200 OK | |
2357 Connection: close | |
2358 Set-Cookie: a=b | |
2359 Set-Cookie: c=d | |
2360 | |
2361 EOF | |
2362 | |
2363 } | |
2364 | |
2365 } continue { | |
2366 close $client; | |
2367 } | |
2368 } | |
2369 | |
2370 ############################################################################### |