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 IO::Socket;
11use Test::More tests => 18;
12use LightyTest;
13
14my $tf = LightyTest->new();
15my $t;
16
17ok($tf->start_proc == 0, "Starting lighttpd") or die();
18
19# mod-cgi
20#
21$t->{REQUEST}  = ( <<EOF
22GET /cgi.pl HTTP/1.0
23EOF
24 );
25$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
26ok($tf->handle_http($t) == 0, 'perl via cgi');
27
28$t->{REQUEST}  = ( <<EOF
29GET /cgi.pl/foo HTTP/1.0
30EOF
31 );
32$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/cgi.pl' } ];
33ok($tf->handle_http($t) == 0, 'perl via cgi + pathinfo');
34
35$t->{REQUEST}  = ( <<EOF
36GET /cgi-pathinfo.pl/foo HTTP/1.0
37EOF
38 );
39$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/foo' } ];
40ok($tf->handle_http($t) == 0, 'perl via cgi + pathinfo');
41
42$t->{REQUEST}  = ( <<EOF
43GET /nph-status.pl?30 HTTP/1.0
44EOF
45 );
46$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
47ok($tf->handle_http($t) == 0, 'NPH + perl, invalid status-code (#14)');
48
49$t->{REQUEST}  = ( <<EOF
50GET /nph-status.pl?304 HTTP/1.0
51EOF
52 );
53$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 304 } ];
54ok($tf->handle_http($t) == 0, 'NPH + perl, setting status-code (#1125)');
55
56$t->{REQUEST}  = ( <<EOF
57GET /nph-status.pl?200 HTTP/1.0
58EOF
59 );
60$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
61ok($tf->handle_http($t) == 0, 'NPH + perl, setting status-code');
62
63$t->{REQUEST} = ( <<EOF
64GET /get-header.pl?GATEWAY_INTERFACE HTTP/1.0
65EOF
66 );
67$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'CGI/1.1' } ];
68ok($tf->handle_http($t) == 0, 'cgi-env: GATEWAY_INTERFACE');
69
70$t->{REQUEST} = ( <<EOF
71GET /get-header.pl?QUERY_STRING HTTP/1.0
72EOF
73 );
74$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'QUERY_STRING' } ];
75ok($tf->handle_http($t) == 0, 'cgi-env: QUERY_STRING');
76
77$t->{REQUEST} = ( <<EOF
78GET /get-header.pl?GATEWAY_INTERFACE HTTP/1.0
79EOF
80 );
81$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'CGI/1.1' } ];
82ok($tf->handle_http($t) == 0, 'cgi-env: GATEWAY_INTERFACE');
83
84$t->{REQUEST} = ( <<EOF
85GET /get-header.pl?HTTP_HOST HTTP/1.0
86Host: www.example.org
87EOF
88 );
89$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
90ok($tf->handle_http($t) == 0, 'cgi-env: HTTP_HOST');
91
92$t->{REQUEST}  = ( <<EOF
93GET /get-header.pl?HTTP_XX_YY123 HTTP/1.0
94xx-yy123: foo
95EOF
96 );
97$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'foo' } ];
98ok($tf->handle_http($t) == 0, 'cgi-env: quoting headers with numbers');
99
100$t->{REQUEST}  = ( <<EOF
101GET /get-header.pl?HTTP_HOST HTTP/1.0
102Host: www.example.org
103EOF
104 );
105$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
106ok($tf->handle_http($t) == 0, 'cgi-env: HTTP_HOST');
107
108$t->{REQUEST}  = ( <<EOF
109GET /get-header.pl?HTTP_HOST HTTP/1.0
110Host: www.example.org
111EOF
112 );
113$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
114ok($tf->handle_http($t) == 0, 'cgi-env: HTTP_HOST');
115
116$t->{REQUEST}  = ( <<EOF
117GET /get-header.pl?HTTP_HOST HTTP/1.0
118Host: www.example.org
119EOF
120 );
121$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Type' => 'text/plain' } ];
122ok($tf->handle_http($t) == 0, 'cgi-env: HTTP_HOST');
123
124$t->{REQUEST}  = ( <<EOF
125GET /get-header.pl?HTTP_HOST HTTP/1.1
126Host: www.example.org
127Connection: close
128EOF
129 );
130$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.1', 'HTTP-Status' => 200, '+Content-Length' => '' } ];
131ok($tf->handle_http($t) == 0, 'cgi-env: HTTP_HOST');
132
133# broken header crash
134$t->{REQUEST}  = ( <<EOF
135GET /crlfcrash.pl HTTP/1.0
136EOF
137 );
138$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 302, 'Location' => 'http://www.example.org/' } ];
139ok($tf->handle_http($t) == 0, 'broken header via perl cgi');
140
141ok($tf->stop_proc == 0, "Stopping lighttpd");
142
143