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