Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # (C) Sergey Kandaurov |
| 4 | # (C) Nginx, Inc. |
| 5 | |
| 6 | # Tests for HTTP/2 protocol with unbuffered request body. |
| 7 | |
| 8 | ############################################################################### |
| 9 | |
| 10 | use warnings; |
| 11 | use strict; |
| 12 | |
| 13 | use Test::More; |
| 14 | |
| 15 | use Socket qw/ CRLF /; |
| 16 | |
| 17 | BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 18 | |
| 19 | use lib 'lib'; |
| 20 | use Test::Nginx; |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 21 | use Test::Nginx::HTTP2; |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 22 | |
| 23 | ############################################################################### |
| 24 | |
| 25 | select STDERR; $| = 1; |
| 26 | select STDOUT; $| = 1; |
| 27 | |
| 28 | my $t = Test::Nginx->new()->has(qw/http http_v2 proxy/); |
| 29 | |
| 30 | $t->write_file_expand('nginx.conf', <<'EOF'); |
| 31 | |
| 32 | %%TEST_GLOBALS%% |
| 33 | |
| 34 | daemon off; |
| 35 | |
| 36 | events { |
| 37 | } |
| 38 | |
| 39 | http { |
| 40 | %%TEST_GLOBALS_HTTP%% |
| 41 | |
| 42 | server { |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 43 | listen 127.0.0.1:8080 http2; |
Sergey Kandaurov | ece1523 | 2016-07-06 15:57:26 +0300 | [diff] [blame] | 44 | listen 127.0.0.1:8082; |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 45 | server_name localhost; |
| 46 | |
| 47 | location / { |
| 48 | proxy_request_buffering off; |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 49 | proxy_pass http://127.0.0.1:8081/; |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 50 | client_body_buffer_size 1k; |
| 51 | } |
| 52 | location /chunked { |
| 53 | proxy_request_buffering off; |
| 54 | proxy_http_version 1.1; |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 55 | proxy_pass http://127.0.0.1:8081/; |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 56 | client_body_buffer_size 1k; |
| 57 | } |
Sergey Kandaurov | ece1523 | 2016-07-06 15:57:26 +0300 | [diff] [blame] | 58 | location /abort { |
| 59 | proxy_request_buffering off; |
| 60 | proxy_http_version 1.1; |
| 61 | proxy_pass http://127.0.0.1:8082/; |
| 62 | } |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | EOF |
| 67 | |
| 68 | $t->run(); |
Sergey Kandaurov | ece1523 | 2016-07-06 15:57:26 +0300 | [diff] [blame] | 69 | $t->plan(49); |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 70 | |
| 71 | ############################################################################### |
| 72 | |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 73 | # unbuffered request body |
| 74 | |
Sergey Kandaurov | 9473f14 | 2017-11-24 19:58:40 +0300 | [diff] [blame] | 75 | my $f = get_body('/', 'content-length' => 10); |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 76 | ok($f->{headers}, 'request'); |
| 77 | is($f->{upload}('01234', body_more => 1), '01234', 'part'); |
| 78 | is($f->{upload}('56789'), '56789', 'part 2'); |
| 79 | is($f->{http_end}(), 200, 'response'); |
| 80 | |
| 81 | $f = get_body('/', 'content-length' => 10); |
| 82 | ok($f->{headers}, 'much'); |
| 83 | is($f->{upload}('0123456789', body_more => 1), '0123456789', 'much - part'); |
| 84 | is($f->{upload}('many'), '', 'much - part 2'); |
| 85 | is($f->{http_end}(), 400, 'much - response'); |
| 86 | |
| 87 | $f = get_body('/', 'content-length' => 10); |
| 88 | ok($f->{headers}, 'less'); |
| 89 | is($f->{upload}('0123', body_more => 1), '0123', 'less - part'); |
| 90 | is($f->{upload}('56789'), '', 'less - part 2'); |
| 91 | is($f->{http_end}(), 400, 'less - response'); |
| 92 | |
| 93 | $f = get_body('/', 'content-length' => 18); |
| 94 | ok($f->{headers}, 'many'); |
| 95 | is($f->{upload}('01234many', body_split => [ 5 ], body_more => 1), |
| 96 | '01234many', 'many - part'); |
| 97 | is($f->{upload}('56789many', body_split => [ 5 ]), |
| 98 | '56789many', 'many - part 2'); |
| 99 | is($f->{http_end}(), 200, 'many - response'); |
| 100 | |
| 101 | $f = get_body('/', 'content-length' => 0); |
| 102 | ok($f->{headers}, 'empty'); |
Sergey Kandaurov | 42e2414 | 2016-06-15 13:27:56 +0300 | [diff] [blame] | 103 | is($f->{upload}('', body_more => 1, wait => 0.2), '', 'empty - part'); |
| 104 | is($f->{upload}('', wait => 0.2), '', 'empty - part 2'); |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 105 | is($f->{http_end}(), 200, 'empty - response'); |
| 106 | |
| 107 | $f = get_body('/', 'content-length' => 1536); |
| 108 | ok($f->{headers}, 'buffer'); |
| 109 | is($f->{upload}('0123' x 128, body_more => 1), '0123' x 128, |
| 110 | 'buffer - below'); |
| 111 | is($f->{upload}('4567' x 128, body_more => 1), '4567' x 128, |
| 112 | 'buffer - equal'); |
| 113 | is($f->{upload}('89AB' x 128), '89AB' x 128, 'buffer - above'); |
| 114 | is($f->{http_end}(), 200, 'buffer - response'); |
| 115 | |
| 116 | $f = get_body('/', 'content-length' => 10); |
| 117 | ok($f->{headers}, 'split'); |
| 118 | is($f->{upload}('0123456789', split => [ 14 ]), '0123456789', 'split'); |
| 119 | is($f->{http_end}(), 200, 'split - response'); |
| 120 | |
| 121 | # unbuffered request body, chunked transfer-encoding |
| 122 | |
| 123 | $f = get_body('/chunked'); |
| 124 | ok($f->{headers}, 'chunked'); |
| 125 | is($f->{upload}('01234', body_more => 1), '5' . CRLF . '01234' . CRLF, |
| 126 | 'chunked - part'); |
| 127 | is($f->{upload}('56789'), '5' . CRLF . '56789' . CRLF . '0' . CRLF . CRLF, |
| 128 | 'chunked - part 2'); |
| 129 | is($f->{http_end}(), 200, 'chunked - response'); |
| 130 | |
| 131 | $f = get_body('/chunked'); |
| 132 | ok($f->{headers}, 'chunked buffer'); |
| 133 | is($f->{upload}('0123' x 128, body_more => 1), |
| 134 | '200' . CRLF . '0123' x 128 . CRLF, 'chunked buffer - below'); |
| 135 | is($f->{upload}('4567' x 128, body_more => 1), |
| 136 | '200' . CRLF . '4567' x 128 . CRLF, 'chunked buffer - equal'); |
| 137 | is($f->{upload}('89AB' x 128), |
| 138 | '200' . CRLF . '89AB' x 128 . CRLF . '0' . CRLF . CRLF, |
| 139 | 'chunked buffer - above'); |
| 140 | is($f->{http_end}(), 200, 'chunked buffer - response'); |
| 141 | |
| 142 | $f = get_body('/chunked'); |
| 143 | ok($f->{headers}, 'chunked many'); |
| 144 | is($f->{upload}('01234many', body_split => [ 5 ], body_more => 1), |
| 145 | '9' . CRLF . '01234many' . CRLF, 'chunked many - part'); |
| 146 | is($f->{upload}('56789many', body_split => [ 5 ]), |
| 147 | '9' . CRLF . '56789many' . CRLF . '0' . CRLF . CRLF, |
| 148 | 'chunked many - part 2'); |
| 149 | is($f->{http_end}(), 200, 'chunked many - response'); |
| 150 | |
| 151 | $f = get_body('/chunked'); |
| 152 | ok($f->{headers}, 'chunked empty'); |
Sergey Kandaurov | 42e2414 | 2016-06-15 13:27:56 +0300 | [diff] [blame] | 153 | is($f->{upload}('', body_more => 1, wait => 0.2), '', 'chunked empty - part'); |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 154 | is($f->{upload}(''), '0' . CRLF . CRLF, 'chunked empty - part 2'); |
| 155 | is($f->{http_end}(), 200, 'chunked empty - response'); |
| 156 | |
| 157 | $f = get_body('/chunked'); |
| 158 | ok($f->{headers}, 'chunked split'); |
Sergey Kandaurov | 3f302bc | 2018-01-12 17:23:22 +0300 | [diff] [blame] | 159 | is(http_content($f->{upload}('0123456789', split => [ 14 ])), |
| 160 | '0123456789', 'chunked split'); |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 161 | is($f->{http_end}(), 200, 'chunked split - response'); |
| 162 | |
Sergey Kandaurov | ece1523 | 2016-07-06 15:57:26 +0300 | [diff] [blame] | 163 | # unbuffered request body, chunked transfer-encoding |
| 164 | # client sends partial DATA frame and closes connection |
| 165 | |
Sergey Kandaurov | ece1523 | 2016-07-06 15:57:26 +0300 | [diff] [blame] | 166 | my $s = Test::Nginx::HTTP2->new(); |
| 167 | my $s2 = Test::Nginx::HTTP2->new(); |
| 168 | |
| 169 | $s->new_stream({ path => '/abort', body_more => 1 }); |
| 170 | $s->h2_body('TEST', { split => [ 9 ], abort => 1 }); |
| 171 | |
| 172 | close $s->{socket}; |
| 173 | |
| 174 | $s2->h2_ping('PING'); |
| 175 | isnt(@{$s2->read()}, 0, 'chunked abort'); |
| 176 | |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 177 | ############################################################################### |
| 178 | |
Sergey Kandaurov | 3f302bc | 2018-01-12 17:23:22 +0300 | [diff] [blame] | 179 | sub http_content { |
| 180 | my ($body) = @_; |
| 181 | my $content = ''; |
| 182 | |
| 183 | while ($body =~ /\G\x0d?\x0a?([0-9a-f]+)\x0d\x0a?/gcmsi) { |
| 184 | my $len = hex($1); |
| 185 | $content .= substr($body, pos($body), $len); |
| 186 | pos($body) += $len; |
| 187 | } |
| 188 | |
| 189 | return $content; |
| 190 | } |
| 191 | |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 192 | sub get_body { |
| 193 | my ($url, %extra) = @_; |
| 194 | my ($server, $client, $f); |
| 195 | |
| 196 | $server = IO::Socket::INET->new( |
| 197 | Proto => 'tcp', |
| 198 | LocalHost => '127.0.0.1', |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 199 | LocalPort => port(8081), |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 200 | Listen => 5, |
| 201 | Timeout => 3, |
| 202 | Reuse => 1 |
| 203 | ) |
| 204 | or die "Can't create listening socket: $!\n"; |
| 205 | |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 206 | my $s = Test::Nginx::HTTP2->new(); |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 207 | my $sid = exists $extra{'content-length'} |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 208 | ? $s->new_stream({ headers => [ |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 209 | { name => ':method', value => 'GET' }, |
| 210 | { name => ':scheme', value => 'http' }, |
| 211 | { name => ':path', value => $url, }, |
| 212 | { name => ':authority', value => 'localhost' }, |
| 213 | { name => 'content-length', |
| 214 | value => $extra{'content-length'} }], |
| 215 | body_more => 1 }) |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 216 | : $s->new_stream({ path => $url, body_more => 1 }); |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 217 | |
| 218 | $client = $server->accept() or return; |
| 219 | |
| 220 | log2c("(new connection $client)"); |
| 221 | |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 222 | $f->{headers} = backend_read($client); |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 223 | |
| 224 | my $chunked = $f->{headers} =~ /chunked/; |
| 225 | |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 226 | $f->{upload} = sub { |
| 227 | my ($body, %extra) = @_; |
| 228 | my $len = length($body); |
| 229 | my $wait = $extra{wait}; |
| 230 | |
| 231 | $s->h2_body($body, { %extra }); |
| 232 | |
| 233 | $body = ''; |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 234 | |
| 235 | for (1 .. 10) { |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 236 | my $buf = backend_read($client, $wait) or return ''; |
| 237 | $body .= $buf; |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 238 | |
| 239 | my $got = 0; |
| 240 | $got += $chunked ? hex $_ : $_ for $chunked |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 241 | ? $body =~ /(\w+)\x0d\x0a?\w+\x0d\x0a?/g |
| 242 | : length($body); |
Sergey Kandaurov | 64f8f6b | 2019-10-10 18:15:02 +0300 | [diff] [blame] | 243 | next if $chunked && !$extra{body_more} |
| 244 | && $buf !~ /^0\x0d\x0a?/m; |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 245 | last if $got >= $len; |
| 246 | } |
| 247 | |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 248 | return $body; |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 249 | }; |
| 250 | $f->{http_end} = sub { |
| 251 | $client->write(<<EOF); |
| 252 | HTTP/1.1 200 OK |
| 253 | Connection: close |
| 254 | |
| 255 | EOF |
| 256 | |
| 257 | $client->close; |
| 258 | |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 259 | my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 260 | my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; |
| 261 | return $frame->{headers}->{':status'}; |
| 262 | }; |
| 263 | return $f; |
| 264 | } |
| 265 | |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 266 | sub backend_read { |
| 267 | my ($s, $timo) = @_; |
| 268 | my $buf = ''; |
| 269 | |
Sergey Kandaurov | 4aa9b91 | 2018-12-24 14:24:51 +0300 | [diff] [blame] | 270 | if (IO::Select->new($s)->can_read($timo || 8)) { |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 271 | $s->sysread($buf, 16384) or return; |
| 272 | log2i($buf); |
| 273 | } |
| 274 | return $buf; |
| 275 | } |
| 276 | |
Sergey Kandaurov | ece2019 | 2016-03-23 20:08:20 +0300 | [diff] [blame] | 277 | sub log2i { Test::Nginx::log_core('|| <<', @_); } |
| 278 | sub log2o { Test::Nginx::log_core('|| >>', @_); } |
| 279 | sub log2c { Test::Nginx::log_core('||', @_); } |
| 280 | |
| 281 | ############################################################################### |