comparison proxy_request_buffering_ssl.t @ 543:a36757f66c75

Tests: unbuffered request body to ssl backend.
author Sergey Kandaurov <pluknet@nginx.com>
date Mon, 06 Apr 2015 18:02:39 +0300
parents
children dbf8fb0f3d30
comparison
equal deleted inserted replaced
542:e7e3ced702f5 543:a36757f66c75
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4 # (C) Sergey Kandaurov
5 # (C) Nginx, Inc.
6
7 # Tests for unbuffered request body to ssl backend.
8
9 ###############################################################################
10
11 use warnings;
12 use strict;
13
14 use Test::More;
15 use Socket qw/ CRLF /;
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 my $t = Test::Nginx->new()->has(qw/http http_ssl proxy rewrite/)
28 ->has_daemon('openssl');
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 {
43 listen 127.0.0.1:8080;
44 server_name localhost;
45
46 client_header_buffer_size 1k;
47 proxy_request_buffering off;
48
49 location / {
50 client_body_buffer_size 2k;
51 add_header X-Body "$request_body";
52 proxy_pass https://127.0.0.1:8081;
53 }
54 location /single {
55 client_body_in_single_buffer on;
56 add_header X-Body "$request_body";
57 proxy_pass https://127.0.0.1:8081;
58 }
59 location /discard {
60 return 200 "TEST\n";
61 }
62 location /preread {
63 proxy_pass https://127.0.0.1:8081;
64 }
65 location /error_page {
66 proxy_pass https://127.0.0.1:8081/404;
67 error_page 404 /404;
68 proxy_intercept_errors on;
69 }
70 location /404 {
71 return 200 "$request_body\n";
72 }
73 }
74
75 server {
76 listen 127.0.0.1:8081 ssl;
77 server_name localhost;
78
79 ssl_certificate_key localhost.key;
80 ssl_certificate localhost.crt;
81 proxy_request_buffering off;
82
83 location /preread {
84 client_body_buffer_size 2k;
85 add_header X-Body "$request_body";
86 proxy_pass http://127.0.0.1:8082/;
87 }
88
89 location / {
90 return 204;
91 }
92 location /404 { }
93 }
94 }
95
96 EOF
97
98 $t->write_file('openssl.conf', <<EOF);
99 [ req ]
100 default_bits = 2048
101 encrypt_key = no
102 distinguished_name = req_distinguished_name
103 [ req_distinguished_name ]
104 EOF
105
106 my $d = $t->testdir();
107
108 foreach my $name ('localhost') {
109 system('openssl req -x509 -new '
110 . "-config '$d/openssl.conf' -subj '/CN=$name/' "
111 . "-out '$d/$name.crt' -keyout '$d/$name.key' "
112 . ">>$d/openssl.out 2>&1") == 0
113 or die "Can't create certificate for $name: $!\n";
114 }
115
116 $t->try_run('no proxy_request_buffering')->plan(18);
117
118 ###############################################################################
119
120 unlike(http_get('/'), qr/X-Body:/ms, 'no body');
121
122 like(http_get_body('/', '0123456789'),
123 qr/X-Body: 0123456789\x0d?$/ms, 'body');
124
125 like(http_get_body('/', '0123456789' x 128),
126 qr/X-Body: (0123456789){128}\x0d?$/ms, 'body in two buffers');
127
128 like(http_get_body('/single', '0123456789' x 128),
129 qr/X-Body: (0123456789){128}\x0d?$/ms, 'body in single buffer');
130
131 like(http_get_body('/error_page', '0123456789'),
132 qr/^0123456789$/m, 'body in error page');
133
134 # pipelined requests
135
136 like(http_get_body('/', '0123456789', '0123456789' x 128, '0123456789' x 512,
137 'foobar'), qr/X-Body: foobar\x0d?$/ms, 'body pipelined');
138 like(http_get_body('/', '0123456789' x 128, '0123456789' x 512, '0123456789',
139 'foobar'), qr/X-Body: foobar\x0d?$/ms, 'body pipelined 2');
140
141 like(http_get_body('/discard', '0123456789', '0123456789' x 128,
142 '0123456789' x 512, 'foobar'), qr/(TEST.*){4}/ms,
143 'body discard');
144 like(http_get_body('/discard', '0123456789' x 128, '0123456789' x 512,
145 '0123456789', 'foobar'), qr/(TEST.*){4}/ms,
146 'body discard 2');
147
148 # interactive tests
149
150 my $s = get_body('/preread', 8082, 10);
151 ok($s, 'no preread');
152
153 SKIP: {
154 skip 'no preread failed', 3 unless $s;
155
156 TODO: {
157 local $TODO = 'not yet';
158
159 is($s->{upload}('01234'), '01234', 'no preread - body part');
160 is($s->{upload}('56789'), '56789', 'no preread - body part 2');
161
162 }
163
164 like($s->{http_end}(), qr/200 OK/, 'no preread - response');
165
166 }
167
168 $s = get_body('/preread', 8082, 15, '01234');
169 ok($s, 'preread');
170
171 SKIP: {
172 skip 'preread failed', 3 unless $s;
173
174 is($s->{preread}, '01234', 'preread - preread');
175
176 TODO: {
177 local $TODO = 'not yet';
178
179 is($s->{upload}('56789'), '56789', 'preread - body part');
180 is($s->{upload}('abcde'), 'abcde', 'preread - body part 2');
181
182 }
183
184 like($s->{http_end}(), qr/200 OK/, 'preread - response');
185
186 }
187
188 ###############################################################################
189
190 sub http_get_body {
191 my $uri = shift;
192 my $last = pop;
193 return http( join '', (map {
194 my $body = $_;
195 "GET $uri HTTP/1.1" . CRLF
196 . "Host: localhost" . CRLF
197 . "Content-Length: " . (length $body) . CRLF . CRLF
198 . $body
199 } @_),
200 "GET $uri HTTP/1.1" . CRLF
201 . "Host: localhost" . CRLF
202 . "Connection: close" . CRLF
203 . "Content-Length: " . (length $last) . CRLF . CRLF
204 . $last
205 );
206 }
207
208 sub get_body {
209 my ($url, $port, $length, $body) = @_;
210 my ($server, $client, $s);
211
212 $server = IO::Socket::INET->new(
213 Proto => 'tcp',
214 LocalHost => '127.0.0.1',
215 LocalPort => $port,
216 Listen => 5,
217 Reuse => 1
218 )
219 or die "Can't create listening socket: $!\n";
220
221 my $r = <<EOF;
222 GET $url HTTP/1.1
223 Host: localhost
224 Connection: close
225 Content-Length: $length
226
227 EOF
228
229 if (defined $body) {
230 $r .= $body;
231 }
232
233 $s = http($r, start => 1);
234
235 eval {
236 local $SIG{ALRM} = sub { die "timeout\n" };
237 local $SIG{PIPE} = sub { die "sigpipe\n" };
238 alarm(5);
239
240 $client = $server->accept();
241
242 alarm(0);
243 };
244 alarm(0);
245 if ($@) {
246 log_in("died: $@");
247 return undef;
248 }
249
250 $client->sysread(my $buf, 1024);
251 $buf =~ s/.*?\x0d\x0a?\x0d\x0a?(.*)/$1/ms;
252
253 my $f = { preread => $buf };
254 $f->{upload} = sub {
255 my $buf = shift;
256
257 eval {
258 local $SIG{ALRM} = sub { die "timeout\n" };
259 local $SIG{PIPE} = sub { die "sigpipe\n" };
260 alarm(5);
261
262 $s->write($buf);
263 $client->sysread($buf, 1024);
264
265 alarm(0);
266 };
267 alarm(0);
268 if ($@) {
269 log_in("died: $@");
270 return undef;
271 }
272
273 return $buf;
274 };
275 $f->{http_end} = sub {
276 my $buf = '';
277
278 $client->write(<<EOF);
279 HTTP/1.1 200 OK
280 Connection: close
281 X-Port: $port
282
283 OK
284 EOF
285
286 $client->close;
287
288 eval {
289 local $SIG{ALRM} = sub { die "timeout\n" };
290 local $SIG{PIPE} = sub { die "sigpipe\n" };
291 alarm(5);
292
293 $s->sysread($buf, 1024);
294
295 alarm(0);
296 };
297 alarm(0);
298 if ($@) {
299 log_in("died: $@");
300 return undef;
301 }
302
303 return $buf;
304 };
305 return $f;
306 }
307
308 ###############################################################################