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 => 21;
12use LightyTest;
13
14my $tf = LightyTest->new();
15my $t;
16
17ok($tf->start_proc == 0, "Starting lighttpd") or die();
18
19$t->{REQUEST}  = ( <<EOF
20GET / HTTP/1.0
21EOF
22 );
23$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
24ok($tf->handle_http($t) == 0, 'Valid HTTP/1.0 Request') or die();
25
26$t->{REQUEST}  = ( <<EOF
27GET /
28EOF
29 );
30$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } ];
31ok($tf->handle_http($t) == 0, 'missing Protocol');
32
33$t->{REQUEST}  = ( <<EOF
34GET / HTTP/01.01
35Host: foo
36Connection: close
37EOF
38 );
39$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.1', 'HTTP-Status' => 200 } ];
40ok($tf->handle_http($t) == 0, 'zeros in protocol version');
41
42$t->{REQUEST}  = ( <<EOF
43GET / HTTP/.01
44EOF
45 );
46$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } ];
47ok($tf->handle_http($t) == 0, 'missing major version');
48
49$t->{REQUEST}  = ( <<EOF
50GET / HTTP/01.
51EOF
52 );
53$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } ];
54ok($tf->handle_http($t) == 0, 'missing minor version');
55
56$t->{REQUEST}  = ( <<EOF
57GET / HTTP/a.b
58EOF
59 );
60$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } ];
61ok($tf->handle_http($t) == 0, 'strings as version');
62
63$t->{REQUEST}  = ( <<EOF
64BC /
65EOF
66 );
67$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } ];
68ok($tf->handle_http($t) == 0, 'missing protocol + unknown method');
69
70$t->{REQUEST}  = ( <<EOF
71ABC
72EOF
73 );
74$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } ];
75ok($tf->handle_http($t) == 0, 'missing protocol + unknown method + missing URI');
76
77$t->{REQUEST}  = ( <<EOF
78ABC / HTTP/1.0
79EOF
80 );
81$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 501 } ];
82ok($tf->handle_http($t) == 0, 'unknown method');
83
84$t->{REQUEST}  = ( <<EOF
85GET / HTTP/1.3
86EOF
87 );
88$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 505 } ];
89ok($tf->handle_http($t) == 0, 'unknown protocol');
90
91$t->{REQUEST}  = ( <<EOF
92GET http://www.example.org/ HTTP/1.0
93EOF
94 );
95$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
96ok($tf->handle_http($t) == 0, 'absolute URI');
97
98print "\nLow-Level Request-Header Parsing\n";
99$t->{REQUEST}  = ( <<EOF
100GET / HTTP/1.0
101ABC : foo
102EOF
103 );
104$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
105ok($tf->handle_http($t) == 0, 'whitespace after key');
106
107$t->{REQUEST}  = ( <<EOF
108GET / HTTP/1.0
109ABC a: foo
110EOF
111 );
112$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } ];
113ok($tf->handle_http($t) == 0, 'whitespace with-in key');
114
115$t->{REQUEST}  = ( <<EOF
116GET / HTTP/1.0
117ABC:foo
118EOF
119 );
120$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
121ok($tf->handle_http($t) == 0, 'no whitespace');
122
123$t->{REQUEST}  = ( <<EOF
124GET / HTTP/1.0
125ABC:foo
126  bc
127EOF
128 );
129$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
130ok($tf->handle_http($t) == 0, 'line-folding');
131
132print "\nLow-Level Request-Header Parsing - URI\n";
133$t->{REQUEST}  = ( <<EOF
134GET /index%2ehtml HTTP/1.0
135EOF
136 );
137$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
138ok($tf->handle_http($t) == 0, 'URL-encoding');
139
140$t->{REQUEST}  = ( <<EOF
141GET /index.html%00 HTTP/1.0
142EOF
143 );
144$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
145ok($tf->handle_http($t) == 0, 'URL-encoding, %00');
146
147$t->{REQUEST}  = ( <<EOF
148OPTIONS * HTTP/1.0
149EOF
150 );
151$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
152ok($tf->handle_http($t) == 0, 'OPTIONS');
153
154$t->{REQUEST}  = ( <<EOF
155OPTIONS / HTTP/1.1
156Host: www.example.org
157Connection: close
158EOF
159 );
160$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.1', 'HTTP-Status' => 200 } ];
161ok($tf->handle_http($t) == 0, 'OPTIONS');
162
163
164
165ok($tf->stop_proc == 0, "Stopping lighttpd");
166
167