xref: /lighttpd1.4/tests/mod-fastcgi.t (revision 9570cc2f)
1#!/usr/bin/env perl
2BEGIN {
3	# add current source dir to the include-path
4	# we need this for make distcheck
5	(my $srcdir = $0) =~ s,/[^/]+$,/,;
6	unshift @INC, $srcdir;
7}
8
9use strict;
10use Test::More tests => 44;
11use LightyTest;
12
13my $tf = LightyTest->new();
14
15my $t;
16
17SKIP: {
18	skip "no php binary found", 29 unless $LightyTest::HAVE_PHP;
19
20	$tf->{CONFIGFILE} = 'fastcgi-10.conf';
21	ok($tf->start_proc == 0, "Starting lighttpd") or goto cleanup;
22
23	$t->{REQUEST} = ( <<EOF
24GET /phpinfo.php HTTP/1.0
25Host: www.example.org
26EOF
27 );
28	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
29	ok($tf->handle_http($t) == 0, 'valid request');
30
31	$t->{REQUEST}  = ( <<EOF
32GET /phpinfofoobar.php HTTP/1.0
33Host: www.example.org
34EOF
35 );
36	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
37	ok($tf->handle_http($t) == 0, 'file not found');
38
39	$t->{REQUEST}  = ( <<EOF
40GET /go/ HTTP/1.0
41Host: www.example.org
42EOF
43 );
44	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
45	ok($tf->handle_http($t) == 0, 'index-file handling');
46
47	$t->{REQUEST}  = ( <<EOF
48GET /redirect.php HTTP/1.0
49Host: www.example.org
50EOF
51 );
52	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 302, 'Location' => 'http://www.example.org:2048/' } ];
53	ok($tf->handle_http($t) == 0, 'Status + Location via FastCGI');
54
55	$t->{REQUEST}  = ( <<EOF
56GET /redirect.php/ HTTP/1.0
57Host: www.example.org
58EOF
59 );
60	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 302, 'Location' => 'http://www.example.org:2048/' } ];
61	ok($tf->handle_http($t) == 0, 'Trailing slash as path-info (#1989: workaround broken operating systems)');
62
63	$t->{REQUEST}  = ( <<EOF
64GET /get-server-env.php?env=PHP_SELF HTTP/1.0
65Host: www.example.org
66EOF
67 );
68	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
69	ok($tf->handle_http($t) == 0, '$_SERVER["PHP_SELF"]');
70
71	$t->{REQUEST}  = ( <<EOF
72GET /get-server-env.php/foo?env=SCRIPT_NAME HTTP/1.0
73Host: www.example.org
74EOF
75 );
76	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/get-server-env.php' } ];
77	ok($tf->handle_http($t) == 0, '$_SERVER["SCRIPT_NAME"]');
78
79	$t->{REQUEST}  = ( <<EOF
80GET /get-server-env.php/foo?env=PATH_INFO HTTP/1.0
81Host: www.example.org
82EOF
83 );
84	$t->{RESPONSE}  = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/foo' } ];
85	ok($tf->handle_http($t) == 0, '$_SERVER["PATH_INFO"]');
86
87	$t->{REQUEST}  = ( <<EOF
88GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
89Host: www.example.org
90EOF
91 );
92	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
93	ok($tf->handle_http($t) == 0, 'SERVER_NAME');
94
95	$t->{REQUEST}  = ( <<EOF
96GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
97Host: foo.example.org
98EOF
99 );
100	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
101	ok($tf->handle_http($t) == 0, 'SERVER_NAME');
102
103	$t->{REQUEST}  = ( <<EOF
104GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
105Host: vvv.example.org
106EOF
107 );
108	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
109	ok($tf->handle_http($t) == 0, 'SERVER_NAME');
110
111	$t->{REQUEST}  = ( <<EOF
112GET /cgi.php/abc HTTP/1.0
113EOF
114 );
115	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
116	ok($tf->handle_http($t) == 0, 'PATHINFO');
117
118    if ($^O ne "cygwin") {
119	$t->{REQUEST}  = ( <<EOF
120GET /cgi.php%20%20%20 HTTP/1.0
121EOF
122 );
123	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
124	ok($tf->handle_http($t) == 0, 'No source retrieval');
125    } else {
126	ok(1, 'No source retrieval; skipped on cygwin; see response.c');
127    }
128
129	$t->{REQUEST}  = ( <<EOF
130GET /www/abc/def HTTP/1.0
131EOF
132 );
133	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
134	ok($tf->handle_http($t) == 0, 'PATHINFO on a directory');
135
136	$t->{REQUEST}  = ( <<EOF
137GET /indexfile/ HTTP/1.0
138EOF
139 );
140	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/indexfile/index.php' } ];
141	ok($tf->handle_http($t) == 0, 'PHP_SELF + Indexfile, Bug #3');
142
143	$t->{REQUEST}  = ( <<EOF
144GET /prefix.fcgi?var=SCRIPT_NAME HTTP/1.0
145EOF
146 );
147	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/prefix.fcgi' } ];
148	ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
149
150	$t->{REQUEST}  = ( <<EOF
151GET /prefix.fcgi/foo/bar?var=SCRIPT_NAME HTTP/1.0
152EOF
153 );
154	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/prefix.fcgi' } ];
155	ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
156
157	$t->{REQUEST}  = ( <<EOF
158GET /prefix.fcgi/foo/bar?var=PATH_INFO HTTP/1.0
159EOF
160 );
161	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/foo/bar' } ];
162	ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
163
164	$t->{REQUEST}  = ( <<EOF
165GET /sendfile.php?range=0- HTTP/1.0
166EOF
167 );
168	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => 4348 } ];
169	ok($tf->handle_http($t) == 0, 'X-Sendfile2');
170
171	$t->{REQUEST}  = ( <<EOF
172GET /sendfile.php?range=0-4&range2=5- HTTP/1.0
173EOF
174 );
175	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => 4348 } ];
176	ok($tf->handle_http($t) == 0, 'X-Sendfile2');
177
178	$t->{REQUEST}  = ( <<EOF
179GET /get-server-env.php?env=REMOTE_USER HTTP/1.0
180Host: auth.example.org
181Authorization: Basic ZGVzOmRlcw==
182EOF
183 );
184	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'des' } ];
185	ok($tf->handle_http($t) == 0, '$_SERVER["REMOTE_USER"]');
186
187	$t->{REQUEST}  = ( <<EOF
188GET /get-server-env.php?env=AUTH_TYPE HTTP/1.0
189Host: auth.example.org
190Authorization: Basic ZGVzOmRlcw==
191EOF
192 );
193	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'Basic' } ];
194	ok($tf->handle_http($t) == 0, '$_SERVER["AUTH_TYPE"]');
195
196	$t->{REQUEST}  = ( <<EOF
197GET /indexfile/ HTTP/1.0
198Host: www.example.org
199EOF
200 );
201	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/indexfile/index.php' } ];
202	ok($tf->handle_http($t) == 0, 'Bug #6');
203
204	$t->{REQUEST}  = ( <<EOF
205POST /indexfile/abc HTTP/1.0
206Host: www.example.org
207Content-Length: 0
208EOF
209 );
210	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404, 'HTTP-Content' => '/indexfile/return-404.php' } ];
211	ok($tf->handle_http($t) == 0, 'Bug #12');
212
213	$t->{REQUEST}  = ( <<EOF
214GET /indexfile/index.php HTTP/1.0
215Host: bin-env.example.org
216EOF
217 );
218	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
219	ok($tf->handle_http($t) == 0, 'FastCGI + local spawning');
220
221	$t->{REQUEST} = ( <<EOF
222HEAD /indexfile/index.php HTTP/1.0
223Host: bin-env.example.org
224EOF
225 );
226	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, '-Content-Length' => '0' } ];
227	# Of course a valid content-length != 0 would be ok, but we assume for now that such one is not generated.
228	ok($tf->handle_http($t) == 0, 'Check for buggy content length with HEAD');
229
230	$t->{REQUEST}  = ( <<EOF
231GET /get-env.php?env=MAIL HTTP/1.0
232Host: bin-env.example.org
233EOF
234 );
235	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 , 'HTTP-Content' => '' } ];
236	ok($tf->handle_http($t) == 0, 'FastCGI + bin-copy-environment');
237
238	ok($tf->stop_proc == 0, "Stopping lighttpd");
239}
240
241SKIP: {
242	skip "no fcgi-auth, fcgi-responder found", 15
243	  unless (-x $tf->{BASEDIR}."/tests/fcgi-auth"      || -x $tf->{BASEDIR}."/tests/fcgi-auth.exe")
244	      && (-x $tf->{BASEDIR}."/tests/fcgi-responder" || -x $tf->{BASEDIR}."/tests/fcgi-responder.exe");
245
246	$tf->{CONFIGFILE} = 'fastcgi-responder.conf';
247	ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
248	$t->{REQUEST}  = ( <<EOF
249GET /index.html?ok HTTP/1.0
250Host: auth.example.org
251EOF
252 );
253	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
254	ok($tf->handle_http($t) == 0, 'FastCGI - Auth');
255
256	$t->{REQUEST}  = ( <<EOF
257GET /index.html?fail HTTP/1.0
258Host: auth.example.org
259EOF
260 );
261	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 403 } ];
262	ok($tf->handle_http($t) == 0, 'FastCGI - Auth');
263
264	$t->{REQUEST}  = ( <<EOF
265GET /expire/access.txt?ok HTTP/1.0
266Host: auth.example.org
267EOF
268 );
269	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
270	ok($tf->handle_http($t) == 0, 'FastCGI - Auth in subdirectory');
271
272	$t->{REQUEST}  = ( <<EOF
273GET /index.fcgi?varfail HTTP/1.0
274Host: auth.example.org
275EOF
276 );
277	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 403 } ];
278	ok($tf->handle_http($t) == 0, 'FastCGI - Auth Fail with FastCGI responder afterwards');
279
280	$t->{REQUEST}  = ( <<EOF
281GET /index.fcgi?var HTTP/1.0
282Host: auth.example.org
283EOF
284 );
285	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'LighttpdTestContent' } ];
286	ok($tf->handle_http($t) == 0, 'FastCGI - Auth Success with Variable- to Env expansion');
287
288	$t->{REQUEST}  = ( <<EOF
289GET /index.fcgi?lf HTTP/1.0
290Host: www.example.org
291EOF
292 );
293	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
294	ok($tf->handle_http($t) == 0, 'line-ending \n\n');
295
296	$t->{REQUEST}  = ( <<EOF
297GET /index.fcgi?crlf HTTP/1.0
298Host: www.example.org
299EOF
300 );
301	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
302	ok($tf->handle_http($t) == 0, 'line-ending \r\n\r\n');
303
304	$t->{REQUEST}  = ( <<EOF
305GET /index.fcgi?slow-lf HTTP/1.0
306Host: www.example.org
307EOF
308 );
309	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
310	ok($tf->handle_http($t) == 0, 'line-ending \n + \n');
311
312	$t->{REQUEST}  = ( <<EOF
313GET /index.fcgi?slow-crlf HTTP/1.0
314Host: www.example.org
315EOF
316 );
317	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
318	ok($tf->handle_http($t) == 0, 'line-ending \r\n + \r\n');
319
320	$t->{REQUEST}  = ( <<EOF
321GET /abc/def/ghi?path_info HTTP/1.0
322Host: wsgi.example.org
323EOF
324 );
325	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/abc/def/ghi' } ];
326	ok($tf->handle_http($t) == 0, 'PATH_INFO (wsgi)');
327
328	$t->{REQUEST}  = ( <<EOF
329GET /abc/def/ghi?script_name HTTP/1.0
330Host: wsgi.example.org
331EOF
332 );
333	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '' } ];
334	ok($tf->handle_http($t) == 0, 'SCRIPT_NAME (wsgi)');
335
336
337	$t->{REQUEST}  = ( <<EOF
338GET /index.fcgi?die-at-end HTTP/1.0
339Host: www.example.org
340EOF
341 );
342	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
343	ok($tf->handle_http($t) == 0, 'killing fastcgi and wait for restart');
344
345	# (might take lighttpd 1 sec to detect backend exit)
346	select(undef, undef, undef, .5);
347	for (my $c = 2*20; $c && 0 == $tf->listening_on(10000); --$c) {
348		select(undef, undef, undef, 0.05);
349	}
350	$t->{REQUEST}  = ( <<EOF
351GET /index.fcgi?crlf HTTP/1.0
352Host: www.example.org
353EOF
354 );
355	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
356	ok($tf->handle_http($t) == 0, 'regular response of after restart');
357
358
359	ok($tf->stop_proc == 0, "Stopping lighttpd");
360}
361
362exit 0;
363
364cleanup: ;
365
366die();
367