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