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