Mercurial > hg > nginx-tests
annotate spdy.t @ 376:ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Mon, 24 Feb 2014 12:52:35 +0400 |
parents | ac804fd1eb56 |
children | ba95a443ff1f |
rev | line source |
---|---|
374 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for SPDY protocol version 3.1. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 use IO::Select; | |
16 | |
17 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
18 | |
19 use lib 'lib'; | |
20 use Test::Nginx; | |
21 | |
22 ############################################################################### | |
23 | |
24 select STDERR; $| = 1; | |
25 select STDOUT; $| = 1; | |
26 | |
27 eval { | |
28 require Compress::Raw::Zlib; | |
29 Compress::Raw::Zlib->Z_OK; | |
30 Compress::Raw::Zlib->Z_SYNC_FLUSH; | |
31 Compress::Raw::Zlib->Z_NO_COMPRESSION; | |
32 Compress::Raw::Zlib->WANT_GZIP_OR_ZLIB; | |
33 }; | |
34 plan(skip_all => 'Compress::Raw::Zlib not installed') if $@; | |
35 plan(skip_all => 'win32') if $^O eq 'MSWin32'; | |
36 | |
37 my $t = Test::Nginx->new()->has(qw/http proxy cache limit_conn rewrite spdy/); | |
38 | |
39 plan(skip_all => 'no SPDY/3.1') unless $t->has_version('1.5.10'); | |
40 | |
41 $t->plan(72)->write_file_expand('nginx.conf', <<'EOF'); | |
42 | |
43 %%TEST_GLOBALS%% | |
44 | |
45 daemon off; | |
46 | |
47 events { | |
48 } | |
49 | |
50 http { | |
51 %%TEST_GLOBALS_HTTP%% | |
52 | |
53 proxy_cache_path %%TESTDIR%%/cache keys_zone=NAME:10m; | |
54 limit_conn_zone $binary_remote_addr zone=conn:1m; | |
55 | |
56 server { | |
57 listen 127.0.0.1:8080 spdy; | |
58 listen 127.0.0.1:8081; | |
59 server_name localhost; | |
60 | |
61 location /s { | |
62 add_header X-Header X-Foo; | |
63 return 200 'body'; | |
64 } | |
65 location /spdy { | |
66 return 200 $spdy; | |
67 } | |
68 location /prio { | |
69 return 200 $spdy_request_priority; | |
70 } | |
71 location /chunk_size { | |
72 spdy_chunk_size 1; | |
73 return 200 'body'; | |
74 } | |
75 location /redirect { | |
76 error_page 405 /s; | |
77 return 405; | |
78 } | |
79 location /proxy { | |
80 add_header X-Body "$request_body"; | |
81 proxy_pass http://127.0.0.1:8081/; | |
82 proxy_cache NAME; | |
83 proxy_cache_valid 1m; | |
84 } | |
85 location /t3.html { | |
86 limit_conn conn 1; | |
87 } | |
88 } | |
89 } | |
90 | |
91 EOF | |
92 | |
93 $t->run(); | |
94 | |
95 # file size is slightly beyond initial window size: 2**16 + 80 bytes | |
96 | |
97 $t->write_file('t1.html', | |
98 join('', map { sprintf "X%04dXXX", $_ } (1 .. 8202))); | |
99 | |
100 $t->write_file('t2.html', 'SEE-THIS'); | |
101 $t->write_file('t3.html', 'SEE-THIS'); | |
102 | |
103 my %cframe = ( | |
104 2 => \&syn_reply, | |
105 3 => \&rst_stream, | |
106 4 => \&settings, | |
107 6 => \&ping, | |
108 7 => \&goaway, | |
109 9 => \&window_update | |
110 ); | |
111 | |
112 ############################################################################### | |
113 | |
114 # PING | |
115 | |
116 my $sess = new_session(); | |
117 spdy_ping($sess, 0x12345678); | |
118 my $frames = spdy_read($sess); | |
119 | |
120 my ($frame) = grep { $_->{type} eq "PING" } @$frames; | |
121 ok($frame, 'PING frame'); | |
122 is($frame->{value}, 0x12345678, 'PING payload'); | |
123 | |
124 # GET | |
125 | |
126 $sess = new_session(); | |
127 my $sid1 = spdy_stream($sess, { path => '/s' }); | |
128 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
129 | |
130 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
131 ok($frame, 'SYN_REPLAY frame'); | |
132 is($frame->{sid}, $sid1, 'SYN_REPLAY stream'); | |
133 is($frame->{headers}->{':status'}, 200, 'SYN_REPLAY status'); | |
134 is($frame->{headers}->{'x-header'}, 'X-Foo', 'SYN_REPLAY header'); | |
135 | |
136 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
137 ok($frame, 'DATA frame'); | |
138 is($frame->{length}, length 'body', 'DATA length'); | |
139 is($frame->{data}, 'body', 'DATA payload'); | |
140 | |
141 # GET in new SPDY stream in same session | |
142 | |
143 my $sid2 = spdy_stream($sess, { path => '/s' }); | |
144 $frames = spdy_read($sess, all => [{ sid => $sid2, fin => 1 }]); | |
145 | |
146 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
147 is($frame->{sid}, $sid2, 'SYN_REPLAY stream 2'); | |
148 is($frame->{headers}->{':status'}, 200, 'SYN_REPLAY status 2'); | |
149 is($frame->{headers}->{'x-header'}, 'X-Foo', 'SYN_REPLAY header 2'); | |
150 | |
151 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
152 ok($frame, 'DATA frame 2'); | |
153 is($frame->{sid}, $sid2, 'SYN_REPLAY stream 2'); | |
154 is($frame->{length}, length 'body', 'DATA length 2'); | |
155 is($frame->{data}, 'body', 'DATA payload 2'); | |
156 | |
157 # HEAD | |
158 | |
159 $sess = new_session(); | |
160 $sid1 = spdy_stream($sess, { path => '/s', method => 'HEAD' }); | |
161 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
162 | |
163 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
164 is($frame->{sid}, $sid1, 'SYN_REPLAY stream HEAD'); | |
165 is($frame->{headers}->{':status'}, 200, 'SYN_REPLAY status HEAD'); | |
166 is($frame->{headers}->{'x-header'}, 'X-Foo', 'SYN_REPLAY header HEAD'); | |
167 | |
168 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
169 is($frame, undef, 'HEAD no body'); | |
170 | |
171 # request header | |
172 | |
173 $sess = new_session(); | |
174 $sid1 = spdy_stream($sess, { path => '/t1.html', | |
175 headers => { "range" => "bytes=10-19" } | |
176 }); | |
177 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
178 | |
179 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
180 is($frame->{headers}->{':status'}, 206, 'SYN_REPLAY status range'); | |
181 | |
182 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
183 is($frame->{length}, 10, 'DATA length range'); | |
184 is($frame->{data}, '002XXXX000', 'DATA payload range'); | |
185 | |
186 # $spdy | |
187 | |
188 $sess = new_session(); | |
189 $sid1 = spdy_stream($sess, { path => '/spdy' }); | |
190 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
191 | |
192 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
193 is($frame->{data}, '3.1', 'spdy variable'); | |
194 | |
195 # spdy_chunk_size=1 | |
196 | |
197 $sess = new_session(); | |
198 $sid1 = spdy_stream($sess, { path => '/chunk_size' }); | |
199 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
200 | |
201 my @data = grep { $_->{type} eq "DATA" } @$frames; | |
202 is(@data, 4, 'chunk_size body chunks'); | |
203 is($data[0]->{data}, 'b', 'chunk_size body 1'); | |
204 is($data[1]->{data}, 'o', 'chunk_size body 2'); | |
205 is($data[2]->{data}, 'd', 'chunk_size body 3'); | |
206 is($data[3]->{data}, 'y', 'chunk_size body 4'); | |
207 | |
208 # redirect | |
209 | |
210 $sess = new_session(); | |
211 $sid1 = spdy_stream($sess, { path => '/redirect' }); | |
212 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
213 | |
214 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
215 is($frame->{headers}->{':status'}, 405, 'SYN_REPLAY status with redirect'); | |
216 | |
217 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
218 ok($frame, 'DATA frame with redirect'); | |
219 is($frame->{data}, 'body', 'DATA payload with redirect'); | |
220 | |
221 # ensure that HEAD-like requests, i.e., without response body, do not lead to | |
222 # client connection close due to cache filling up with upstream response body | |
223 | |
224 TODO: { | |
225 local $TODO = 'premature client connection close'; | |
226 | |
227 $sess = new_session(); | |
228 $sid1 = spdy_stream($sess, { path => '/proxy/t2.html', method => 'HEAD' }); | |
229 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
230 | |
231 $sid2 = spdy_stream($sess, { path => '/' }); | |
232 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
233 ok(grep ({ $_->{type} eq "SYN_REPLY" } @$frames), 'proxy cache headers only'); | |
234 | |
235 } | |
236 | |
237 # simple proxy cache test | |
238 | |
239 $sess = new_session(); | |
240 $sid1 = spdy_stream($sess, { path => '/proxy/t2.html' }); | |
241 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
242 | |
243 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
244 is($frame->{headers}->{':status'}, '200 OK', 'proxy cache unconditional'); | |
245 | |
376
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
246 my $etag = $frame->{headers}->{'etag'}; |
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
247 |
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
248 SKIP: { |
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
249 skip 'no etag', 1 unless defined $etag; |
374 | 250 |
251 $sid2 = spdy_stream($sess, { path => '/proxy/t2.html', | |
376
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
252 headers => { "if-none-match" => $etag } |
374 | 253 }); |
254 $frames = spdy_read($sess, all => [{ sid => $sid2, fin => 1 }]); | |
255 | |
256 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
257 is($frame->{headers}->{':status'}, 304, 'proxy cache conditional'); | |
258 | |
376
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
259 } |
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
260 |
374 | 261 # request body (uses proxied response) |
262 | |
263 $sess = new_session(); | |
264 $sid1 = spdy_stream($sess, { path => '/proxy/t2.html', body => 'TEST' }); | |
265 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
266 | |
267 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
268 is($frame->{headers}->{'x-body'}, 'TEST', 'request body'); | |
269 | |
270 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
271 is($frame->{length}, length 'SEE-THIS', 'proxied response length'); | |
272 is($frame->{data}, 'SEE-THIS', 'proxied response'); | |
273 | |
274 # WINDOW_UPDATE (client side) | |
275 | |
276 $sess = new_session(); | |
277 $sid1 = spdy_stream($sess, { path => '/t1.html' }); | |
278 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 0 }]); | |
279 | |
280 @data = grep { $_->{type} eq "DATA" } @$frames; | |
281 my $sum = eval join '+', map { $_->{length} } @data; | |
282 is($sum, 2**16, 'iws - stream blocked on initial window size'); | |
283 | |
284 spdy_ping($sess, 0xf00ff00f); | |
285 $frames = spdy_read($sess); | |
286 | |
287 ($frame) = grep { $_->{type} eq "PING" } @$frames; | |
288 ok($frame, 'iws - PING not blocked'); | |
289 | |
290 spdy_window($sess, 2**16, $sid1); | |
291 $frames = spdy_read($sess); | |
292 is(@$frames, 0, 'iws - updated stream window'); | |
293 | |
294 spdy_window($sess, 2**16); | |
295 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
296 | |
297 @data = grep { $_->{type} eq "DATA" } @$frames; | |
298 $sum = eval join '+', map { $_->{length} } @data; | |
299 is($sum, 80, 'iws - updated connection window'); | |
300 | |
301 # SETTINGS (initial window size, client side) | |
302 | |
303 $sess = new_session(); | |
304 spdy_settings($sess, 7 => 2**17); | |
305 spdy_window($sess, 2**17); | |
306 | |
307 $sid1 = spdy_stream($sess, { path => '/t1.html' }); | |
308 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
309 | |
310 @data = grep { $_->{type} eq "DATA" } @$frames; | |
311 $sum = eval join '+', map { $_->{length} } @data; | |
312 is($sum, 2**16 + 80, 'increased initial window size'); | |
313 | |
314 # probe for negative available space in a flow control window | |
315 | |
316 $sess = new_session(); | |
317 $sid1 = spdy_stream($sess, { path => '/t1.html' }); | |
318 spdy_read($sess, all => [{ sid => $sid1, fin => 0 }]); | |
319 | |
320 spdy_window($sess, 1); | |
321 spdy_settings($sess, 7 => 42); | |
322 spdy_window($sess, 1024, $sid1); | |
323 | |
324 $frames = spdy_read($sess); | |
325 is(@$frames, 0, 'negative window - no data'); | |
326 | |
327 spdy_window($sess, 2**16 - 42 - 1024, $sid1); | |
328 $frames = spdy_read($sess); | |
329 is(@$frames, 0, 'zero window - no data'); | |
330 | |
331 spdy_window($sess, 1, $sid1); | |
332 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 0 }]); | |
333 is(@$frames, 1, 'positive window - data'); | |
334 is(@$frames[0]->{length}, 1, 'positive window - data length'); | |
335 | |
336 # stream multiplexing | |
337 | |
338 $sess = new_session(); | |
339 $sid1 = spdy_stream($sess, { path => '/t1.html' }); | |
340 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 0 }]); | |
341 | |
342 @data = grep { $_->{type} eq "DATA" } @$frames; | |
343 $sum = eval join '+', map { $_->{length} } @data; | |
344 is($sum, 2**16, 'multiple - stream1 data'); | |
345 | |
346 $sid2 = spdy_stream($sess, { path => '/t1.html' }); | |
347 $frames = spdy_read($sess, all => [{ sid => $sid2, fin => 0 }]); | |
348 | |
349 @data = grep { $_->{type} eq "DATA" } @$frames; | |
350 is(@data, 0, 'multiple - stream2 no data'); | |
351 | |
352 spdy_window($sess, 2**17, $sid1); | |
353 spdy_window($sess, 2**17, $sid2); | |
354 spdy_window($sess, 2**17); | |
355 | |
356 $frames = spdy_read($sess, all => [ | |
357 { sid => $sid1, fin => 1 }, | |
358 { sid => $sid2, fin => 1 } | |
359 ]); | |
360 | |
361 @data = grep { $_->{type} eq "DATA" && $_->{sid} == $sid1 } @$frames; | |
362 $sum = eval join '+', map { $_->{length} } @data; | |
363 is($sum, 80, 'multiple - stream1 remain data'); | |
364 | |
365 @data = grep { $_->{type} eq "DATA" && $_->{sid} == $sid2 } @$frames; | |
366 $sum = eval join '+', map { $_->{length} } @data; | |
367 is($sum, 2**16 + 80, 'multiple - stream2 full data'); | |
368 | |
369 # request priority parsing in $spdy_request_priority | |
370 | |
371 $sess = new_session(); | |
372 $sid1 = spdy_stream($sess, { path => '/prio', prio => 0 }); | |
373 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
374 | |
375 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
376 is($frame->{data}, 0, 'priority 0'); | |
377 | |
378 $sid1 = spdy_stream($sess, { path => '/prio', prio => 1 }); | |
379 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
380 | |
381 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
382 is($frame->{data}, 1, 'priority 1'); | |
383 | |
384 $sid1 = spdy_stream($sess, { path => '/prio', prio => 7 }); | |
385 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
386 | |
387 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
388 is($frame->{data}, 7, 'priority 7'); | |
389 | |
390 # stream muliplexing + priority | |
391 | |
392 TODO: { | |
393 local $TODO = 'reversed priority' unless $t->has_version('1.5.11'); | |
394 | |
395 $sess = new_session(); | |
396 $sid1 = spdy_stream($sess, { path => '/t1.html', prio => 7 }); | |
397 $sid2 = spdy_stream($sess, { path => '/t2.html', prio => 0 }); | |
398 spdy_read($sess); | |
399 | |
400 spdy_window($sess, 2**17, $sid1); | |
401 spdy_window($sess, 2**17, $sid2); | |
402 spdy_window($sess, 2**17); | |
403 | |
404 $frames = spdy_read($sess, all => [ | |
405 { sid => $sid1, fin => 1 }, | |
406 { sid => $sid2, fin => 1 } | |
407 ]); | |
408 | |
409 @data = grep { $_->{type} eq "DATA" } @$frames; | |
410 is(join (' ', map { $_->{sid} } @data), "$sid2 $sid1", 'multiple priority 1'); | |
411 | |
412 # and vice versa | |
413 | |
414 $sess = new_session(); | |
415 $sid1 = spdy_stream($sess, { path => '/t1.html', prio => 0 }); | |
416 $sid2 = spdy_stream($sess, { path => '/t2.html', prio => 7 }); | |
417 spdy_read($sess); | |
418 | |
419 spdy_window($sess, 2**17, $sid1); | |
420 spdy_window($sess, 2**17, $sid2); | |
421 spdy_window($sess, 2**17); | |
422 | |
423 $frames = spdy_read($sess, all => [ | |
424 { sid => $sid1, fin => 1 }, | |
425 { sid => $sid2, fin => 1 } | |
426 ]); | |
427 | |
428 @data = grep { $_->{type} eq "DATA" } @$frames; | |
429 is(join (' ', map { $_->{sid} } @data), "$sid1 $sid2", 'multiple priority 2'); | |
430 | |
431 } | |
432 | |
433 # limit_conn | |
434 | |
435 $sess = new_session(); | |
436 spdy_settings($sess, 7 => 1); | |
437 $sid1 = spdy_stream($sess, { path => '/t3.html' }); | |
438 $sid2 = spdy_stream($sess, { path => '/t3.html' }); | |
439 $frames = spdy_read($sess, all => [ | |
440 { sid => $sid1, fin => 0 }, | |
441 { sid => $sid2, fin => 0 } | |
442 ]); | |
443 | |
444 ($frame) = grep { $_->{type} eq "SYN_REPLY" && $_->{sid} == $sid1 } @$frames; | |
445 is($frame->{headers}->{':status'}, 200, 'conn_limit 1'); | |
446 | |
447 ($frame) = grep { $_->{type} eq "SYN_REPLY" && $_->{sid} == $sid2 } @$frames; | |
448 is($frame->{headers}->{':status'}, 503, 'conn_limit 2'); | |
449 | |
450 # limit_conn + client's RST_STREAM | |
451 | |
452 $sess = new_session(); | |
453 spdy_settings($sess, 7 => 1); | |
454 $sid1 = spdy_stream($sess, { path => '/t3.html' }); | |
455 spdy_rst($sess, $sid1, 5); | |
456 $sid2 = spdy_stream($sess, { path => '/t3.html' }); | |
457 $frames = spdy_read($sess, all => [ | |
458 { sid => $sid1, fin => 0 }, | |
459 { sid => $sid2, fin => 0 } | |
460 ]); | |
461 | |
462 ($frame) = grep { $_->{type} eq "SYN_REPLY" && $_->{sid} == $sid1 } @$frames; | |
463 is($frame->{headers}->{':status'}, 200, 'RST_STREAM 1'); | |
464 | |
465 ($frame) = grep { $_->{type} eq "SYN_REPLY" && $_->{sid} == $sid2 } @$frames; | |
466 is($frame->{headers}->{':status'}, 200, 'RST_STREAM 2'); | |
467 | |
468 # GOAWAY on SYN_STREAM with even StreamID | |
469 | |
470 TODO: { | |
471 local $TODO = 'not yet'; | |
472 | |
473 $sess = new_session(); | |
474 spdy_stream($sess, { path => '/s' }, 2); | |
475 $frames = spdy_read($sess); | |
476 | |
477 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
478 ok($frame, 'even stream - GOAWAY frame'); | |
479 is($frame->{code}, 1, 'even stream - error code'); | |
480 is($frame->{sid}, 0, 'even stream - last used stream'); | |
481 | |
482 } | |
483 | |
484 # GOAWAY on SYN_STREAM with backward StreamID | |
485 | |
486 TODO: { | |
487 local $TODO = 'not yet'; | |
488 | |
489 $sess = new_session(); | |
490 $sid1 = spdy_stream($sess, { path => '/s' }, 3); | |
491 spdy_read($sess); | |
492 | |
493 $sid2 = spdy_stream($sess, { path => '/s' }, 1); | |
494 $frames = spdy_read($sess); | |
495 | |
496 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
497 ok($frame, 'backward stream - GOAWAY frame'); | |
498 is($frame->{code}, 1, 'backward stream - error code'); | |
499 is($frame->{sid}, $sid1, 'backward stream - last used stream'); | |
500 | |
501 } | |
502 | |
503 # RST_STREAM on the second SYN_STREAM with same StreamID | |
504 | |
505 TODO: { | |
506 local $TODO = 'not yet'; | |
507 | |
508 $sess = new_session(); | |
509 $sid1 = spdy_stream($sess, { path => '/s' }, 3); | |
510 spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
511 $sid2 = spdy_stream($sess, { path => '/s' }, 3); | |
512 $frames = spdy_read($sess); | |
513 | |
514 ($frame) = grep { $_->{type} eq "RST_STREAM" } @$frames; | |
515 ok($frame, 'dup stream - RST_STREAM frame'); | |
516 is($frame->{code}, 1, 'dup stream - error code'); | |
517 is($frame->{sid}, $sid1, 'dup stream - stream'); | |
518 | |
519 } | |
520 | |
521 # awkward protocol version | |
522 | |
523 TODO: { | |
524 local $TODO = 'not yet' unless $t->has_version('1.5.11'); | |
525 | |
526 $sess = new_session(); | |
527 $sid1 = spdy_stream($sess, { path => '/s', version => 'HTTP/1.10' }); | |
528 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
529 | |
530 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
531 is($frame->{headers}->{':status'}, 200, 'awkward version'); | |
532 | |
533 } | |
534 | |
535 # missing mandatory request header | |
536 | |
537 $sess = new_session(); | |
538 $sid1 = spdy_stream($sess, { path => '/s', version => '' }); | |
539 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
540 | |
541 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
542 is($frame->{headers}->{':status'}, 400, 'incomplete headers'); | |
543 | |
544 # GOAWAY before closing a connection by server | |
545 | |
546 $t->stop(); | |
547 | |
548 TODO: { | |
549 local $TODO = 'not yet'; | |
550 | |
551 $frames = spdy_read($sess); | |
552 | |
553 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
554 ok($frame, 'GOAWAY on connection close'); | |
555 | |
556 } | |
557 | |
558 ############################################################################### | |
559 | |
560 sub spdy_ping { | |
561 my ($sess, $payload) = @_; | |
562 | |
563 raw_write($sess->{socket}, pack("N3", 0x80030006, 0x4, $payload)); | |
564 } | |
565 | |
566 sub spdy_rst { | |
567 my ($sess, $sid, $error) = @_; | |
568 | |
569 raw_write($sess->{socket}, pack("N4", 0x80030003, 0x8, $sid, $error)); | |
570 } | |
571 | |
572 sub spdy_window { | |
573 my ($sess, $win, $stream) = @_; | |
574 | |
575 $stream = 0 unless defined $stream; | |
576 raw_write($sess->{socket}, pack("N4", 0x80030009, 8, $stream, $win)); | |
577 } | |
578 | |
579 sub spdy_settings { | |
580 my ($sess, %extra) = @_; | |
581 | |
582 my $cnt = keys %extra; | |
583 my $len = 4 + 8 * $cnt; | |
584 | |
585 my $buf = pack "N3", 0x80030004, $len, $cnt; | |
586 $buf .= join '', map { pack "N2", $_, $extra{$_} } keys %extra; | |
587 raw_write($sess->{socket}, $buf); | |
588 } | |
589 | |
590 sub spdy_read { | |
591 my ($sess, %extra) = @_; | |
592 my ($skip, $length, @got); | |
593 | |
594 again: | |
595 my $buf = raw_read($sess->{socket}) or return undef; | |
596 | |
597 for ($skip = 0; $skip < length $buf; $skip += $length + 8) { | |
598 my $type = unpack("\@$skip B", $buf); | |
599 $length = hex unpack("\@$skip x5 H6", $buf); | |
600 if ($type == 0) { | |
601 push @got, dframe($skip, $buf); | |
602 test_fin($got[-1], $extra{all}); | |
603 next; | |
604 } | |
605 | |
606 my $ctype = unpack("\@$skip x2 n", $buf); | |
607 push @got, $cframe{$ctype}($sess, $skip, $buf); | |
608 test_fin($got[-1], $extra{all}); | |
609 } | |
610 goto again if %extra && @{$extra{all}}; | |
611 return \@got; | |
612 } | |
613 | |
614 sub test_fin { | |
615 my ($frame, $all) = @_; | |
616 | |
617 @{$all} = grep { | |
618 !($_->{sid} == $frame->{sid} && $_->{fin} == $frame->{fin}) | |
619 } @{$all} if defined $frame->{fin}; | |
620 } | |
621 | |
622 sub dframe { | |
623 my ($skip, $buf) = @_; | |
624 my %frame; | |
625 | |
626 my $stream = unpack "\@$skip B32", $buf; $skip += 4; | |
627 substr($stream, 0, 1) = 0; | |
628 $stream = unpack("N", pack("B32", $stream)); | |
629 $frame{sid} = $stream; | |
630 | |
631 my $flags = unpack "\@$skip B8", $buf; $skip += 1; | |
632 $frame{fin} = substr($flags, 7, 1); | |
633 | |
634 my $length = hex (unpack "\@$skip H6", $buf); $skip += 3; | |
635 $frame{length} = $length; | |
636 | |
637 $frame{data} = substr($buf, $skip, $length); | |
638 $frame{type} = "DATA"; | |
639 return \%frame; | |
640 } | |
641 | |
642 sub spdy_stream { | |
643 my ($ctx, $uri, $stream) = @_; | |
644 my ($input, $output, $buf); | |
645 my ($d, $status); | |
646 | |
647 my $host = $uri->{host} || '127.0.0.1:8080'; | |
648 my $method = $uri->{method} || 'GET'; | |
649 my $headers = $uri->{headers} || {}; | |
650 my $body = $uri->{body}; | |
651 my $prio = defined $uri->{prio} ? $uri->{prio} : 4; | |
652 my $version = defined $uri->{version} ? $uri->{version} : "HTTP/1.1"; | |
653 | |
654 if ($stream) { | |
655 $ctx->{last_stream} = $stream; | |
656 } else { | |
657 $ctx->{last_stream} += 2; | |
658 } | |
659 | |
660 $buf = pack("NC", 0x80030001, not $body); | |
661 $buf .= pack("xxx"); # Length stub | |
662 $buf .= pack("N", $ctx->{last_stream}); # Stream-ID | |
663 $buf .= pack("N", 0); # Assoc. Stream-ID | |
664 $buf .= pack("n", $prio << 13); | |
665 | |
666 my $ent = 4 + keys %{$headers}; | |
667 $ent++ if $body; | |
668 $ent++ if $version; | |
669 | |
670 $input = pack("N", $ent); | |
671 $input .= hpack(":host", $host); | |
672 $input .= hpack(":method", $method); | |
673 $input .= hpack(":path", $uri->{path}); | |
674 $input .= hpack(":scheme", "http"); | |
675 if ($version) { | |
676 $input .= hpack(":version", $version); | |
677 } | |
678 if ($body) { | |
679 $input .= hpack("content-length", length $body); | |
680 } | |
681 $input .= join '', map { hpack($_, $headers->{$_}) } keys %{$headers}; | |
682 | |
683 $d = $ctx->{zlib}->{d}; | |
684 $status = $d->deflate($input => \my $start); | |
685 $status == Compress::Raw::Zlib->Z_OK or fail "deflate failed"; | |
686 $status = $d->flush(\my $tail => Compress::Raw::Zlib->Z_SYNC_FLUSH); | |
687 $status == Compress::Raw::Zlib->Z_OK or fail "flush failed"; | |
688 $output = $start . $tail; | |
689 | |
690 my $len = ''; | |
691 vec($len, 7, 8) = (length $output) + 10; | |
692 $buf |= $len; | |
693 $buf .= $output; | |
694 | |
695 if (defined $body) { | |
696 $buf .= pack "NCxn", $ctx->{last_stream}, 0x01, length $body; | |
697 $buf .= $body; | |
698 } | |
699 | |
700 raw_write($ctx->{socket}, $buf); | |
701 return $ctx->{last_stream}; | |
702 } | |
703 | |
704 sub syn_reply { | |
705 my ($ctx, $skip, $buf) = @_; | |
706 my ($i, $status); | |
707 my %payload; | |
708 | |
709 $skip += 4; | |
710 my $flags = unpack "\@$skip B8", $buf; $skip += 1; | |
711 $payload{fin} = substr($flags, 7, 1); | |
712 | |
713 my $length = hex unpack "\@$skip H6", $buf; $skip += 3; | |
714 $payload{length} = $length; | |
715 $payload{type} = 'SYN_REPLY'; | |
716 | |
717 my $stream = unpack "\@$skip B32", $buf; $skip += 4; | |
718 substr($stream, 0, 1) = 0; | |
719 $stream = unpack("N", pack("B32", $stream)); | |
720 $payload{sid} = $stream; | |
721 | |
722 my $input = substr($buf, $skip, $length - 4); | |
723 $i = $ctx->{zlib}->{i}; | |
724 | |
725 $status = $i->inflate($input => \my $out); | |
726 fail "Failed: $status" unless $status == Compress::Raw::Zlib->Z_OK; | |
727 $payload{headers} = hunpack($out); | |
728 return \%payload; | |
729 } | |
730 | |
731 sub rst_stream { | |
732 my ($ctx, $skip, $buf) = @_; | |
733 my %payload; | |
734 | |
735 $skip += 5; | |
736 $payload{length} = hex(unpack "\@$skip H6", $buf); $skip += 3; | |
737 $payload{type} = 'RST_STREAM'; | |
738 $payload{sid} = unpack "\@$skip N", $buf; $skip += 4; | |
739 $payload{code} = unpack "\@$skip N", $buf; | |
740 return \%payload; | |
741 } | |
742 | |
743 sub settings { | |
744 my ($ctx, $skip, $buf) = @_; | |
745 my %payload; | |
746 | |
747 $skip += 4; | |
748 $payload{flags} = unpack "\@$skip H", $buf; $skip += 1; | |
749 $payload{length} = hex(unpack "\@$skip H6", $buf); $skip += 3; | |
750 $payload{type} = 'SETTINGS'; | |
751 | |
752 my $nent = unpack "\@$skip N", $buf; $skip += 4; | |
753 for (1 .. $nent) { | |
754 my $flags = hex unpack "\@$skip H2", $buf; $skip += 1; | |
755 my $id = hex unpack "\@$skip H6", $buf; $skip += 3; | |
756 $payload{$id}{flags} = $flags; | |
757 $payload{$id}{value} = unpack "\@$skip N", $buf; $skip += 4; | |
758 } | |
759 return \%payload; | |
760 } | |
761 | |
762 sub ping { | |
763 my ($ctx, $skip, $buf) = @_; | |
764 my %payload; | |
765 | |
766 $skip += 5; | |
767 $payload{length} = hex(unpack "\@$skip H6", $buf); $skip += 3; | |
768 $payload{type} = 'PING'; | |
769 $payload{value} = unpack "\@$skip N", $buf; | |
770 return \%payload; | |
771 } | |
772 | |
773 sub goaway { | |
774 my ($ctx, $skip, $buf) = @_; | |
775 my %payload; | |
776 | |
777 $skip += 5; | |
778 $payload{length} = hex unpack "\@$skip H6", $buf; $skip += 3; | |
779 $payload{type} = 'GOAWAY'; | |
780 $payload{sid} = unpack "\@$skip N", $buf; $skip += 4; | |
781 $payload{code} = unpack "\@$skip N", $buf; | |
782 return \%payload; | |
783 } | |
784 | |
785 sub window_update { | |
786 my ($ctx, $skip, $buf) = @_; | |
787 my %payload; | |
788 | |
789 $skip += 5; | |
790 | |
791 $payload{length} = hex(unpack "\@$skip H6", $buf); $skip += 3; | |
792 $payload{type} = 'WINDOW_UPDATE'; | |
793 | |
794 my $stream = unpack "\@$skip B32", $buf; $skip += 4; | |
795 substr($stream, 0, 1) = 0; | |
796 $stream = unpack("N", pack("B32", $stream)); | |
797 $payload{sid} = $stream; | |
798 | |
799 my $value = unpack "\@$skip B32", $buf; | |
800 substr($value, 0, 1) = 0; | |
801 $payload{wdelta} = unpack("N", pack("B32", $value)); | |
802 return \%payload; | |
803 } | |
804 | |
805 sub hpack { | |
806 my ($name, $value) = @_; | |
807 | |
808 pack("N", length($name)) . $name . pack("N", length($value)) . $value; | |
809 } | |
810 | |
811 sub hunpack { | |
812 my ($data) = @_; | |
813 my %headers; | |
814 my $skip = 0; | |
815 | |
816 my $nent = unpack "\@$skip N", $data; $skip += 4; | |
817 for (1 .. $nent) { | |
818 my $len = unpack("\@$skip N", $data); $skip += 4; | |
819 my $name = unpack("\@$skip A$len", $data); $skip += $len; | |
820 | |
821 $len = unpack("\@$skip N", $data); $skip += 4; | |
822 my $value = unpack("\@$skip A$len", $data); $skip += $len; | |
823 | |
824 $headers{$name} = $value; | |
825 } | |
826 return \%headers; | |
827 } | |
828 | |
829 sub raw_read { | |
830 my ($s) = @_; | |
831 my ($got, $buf); | |
832 | |
833 $s->blocking(0); | |
834 while (IO::Select->new($s)->can_read(0.4)) { | |
835 my $n = $s->sysread($buf, 1024); | |
836 last unless $n; | |
837 $got .= $buf; | |
838 }; | |
839 log_in($got); | |
840 return $got; | |
841 } | |
842 | |
843 sub raw_write { | |
844 my ($s, $message) = @_; | |
845 | |
846 local $SIG{PIPE} = 'IGNORE'; | |
847 | |
848 $s->blocking(0); | |
849 while (IO::Select->new($s)->can_write(0.4)) { | |
850 log_out($message); | |
851 my $n = $s->syswrite($message); | |
852 last unless $n; | |
853 $message = substr($message, $n); | |
854 last unless length $message; | |
855 } | |
856 } | |
857 | |
858 sub new_session { | |
859 my ($d, $i, $status); | |
860 | |
861 ($d, $status) = Compress::Raw::Zlib::Deflate->new( | |
862 -WindowBits => 12, | |
863 -Dictionary => dictionary(), | |
864 -Level => Compress::Raw::Zlib->Z_NO_COMPRESSION | |
865 ); | |
866 fail "Zlib failure: $status" unless $d; | |
867 | |
868 ($i, $status) = Compress::Raw::Zlib::Inflate->new( | |
869 -WindowBits => Compress::Raw::Zlib->WANT_GZIP_OR_ZLIB, | |
870 -Dictionary => dictionary() | |
871 ); | |
872 fail "Zlib failure: $status" unless $i; | |
873 | |
874 return { zlib => { i => $i, d => $d }, | |
875 socket => new_socket(), last_stream => -1 }; | |
876 } | |
877 | |
878 sub new_socket { | |
879 my $s; | |
880 | |
881 eval { | |
882 local $SIG{ALRM} = sub { die "timeout\n" }; | |
883 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
884 alarm(2); | |
885 $s = IO::Socket::INET->new( | |
886 Proto => 'tcp', | |
887 PeerAddr => '127.0.0.1:8080', | |
888 ); | |
889 alarm(0); | |
890 }; | |
891 alarm(0); | |
892 | |
893 if ($@) { | |
894 log_in("died: $@"); | |
895 return undef; | |
896 } | |
897 | |
898 return $s; | |
899 } | |
900 | |
901 sub dictionary { | |
902 join('', (map pack('N/a*', $_), qw( | |
903 options | |
904 head | |
905 post | |
906 put | |
907 delete | |
908 trace | |
909 accept | |
910 accept-charset | |
911 accept-encoding | |
912 accept-language | |
913 accept-ranges | |
914 age | |
915 allow | |
916 authorization | |
917 cache-control | |
918 connection | |
919 content-base | |
920 content-encoding | |
921 content-language | |
922 content-length | |
923 content-location | |
924 content-md5 | |
925 content-range | |
926 content-type | |
927 date | |
928 etag | |
929 expect | |
930 expires | |
931 from | |
932 host | |
933 if-match | |
934 if-modified-since | |
935 if-none-match | |
936 if-range | |
937 if-unmodified-since | |
938 last-modified | |
939 location | |
940 max-forwards | |
941 pragma | |
942 proxy-authenticate | |
943 proxy-authorization | |
944 range | |
945 referer | |
946 retry-after | |
947 server | |
948 te | |
949 trailer | |
950 transfer-encoding | |
951 upgrade | |
952 user-agent | |
953 vary | |
954 via | |
955 warning | |
956 www-authenticate | |
957 method | |
958 get | |
959 status), "200 OK", | |
960 qw(version HTTP/1.1 url public set-cookie keep-alive origin)), | |
961 "100101201202205206300302303304305306307402405406407408409410", | |
962 "411412413414415416417502504505", | |
963 "203 Non-Authoritative Information", | |
964 "204 No Content", | |
965 "301 Moved Permanently", | |
966 "400 Bad Request", | |
967 "401 Unauthorized", | |
968 "403 Forbidden", | |
969 "404 Not Found", | |
970 "500 Internal Server Error", | |
971 "501 Not Implemented", | |
972 "503 Service Unavailable", | |
973 "Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec", | |
974 " 00:00:00", | |
975 " Mon, Tue, Wed, Thu, Fri, Sat, Sun, GMT", | |
976 "chunked,text/html,image/png,image/jpg,image/gif,", | |
977 "application/xml,application/xhtml+xml,text/plain,", | |
978 "text/javascript,public", "privatemax-age=gzip,deflate,", | |
979 "sdchcharset=utf-8charset=iso-8859-1,utf-,*,enq=0." | |
980 ); | |
981 } | |
982 | |
983 ############################################################################### |