Debian Codegraph @

t/04cgi.t

raw
1 # -*- perl -*-
2
3 use Test::More;
4 use Socket;
5 use strict;
6
7 my $PORT = 40000 + int(rand(10000));
8
9 my $host = gethostbyaddr(inet_aton('localhost'), AF_INET);
10
11 my %methods=(
12 url => "url: http://$host:".$PORT,
13 path_info => 'path_info: /cgitest/path_info',
14 server_name => "server_name: $host",
15 server_port => 'server_port: '.$PORT,
16 server_software => 'server_software: HTTP::Server::Simple/\d+.\d+',
17 request_method => 'request_method: GET',
18 raw_cookie => undef, # do not test
19 );
20
21 my %envvars=(
22 SERVER_URL => "SERVER_URL: http://$host:".$PORT.'/',
23 SERVER_PORT => 'SERVER_PORT: '.$PORT,
24 REQUEST_METHOD => 'REQUEST_METHOD: GET',
25 REQUEST_URI => 'REQUEST_URI: /cgitest/REQUEST_URI',
26 SERVER_PROTOCOL => 'SERVER_PROTOCOL: HTTP/1.1',
27 SERVER_NAME => "SERVER_NAME: $host",
28 SERVER_SOFTWARE => 'SERVER_SOFTWARE: HTTP::Server::Simple/\d+.\d+',
29 REMOTE_ADDR => 'REMOTE_ADDR: 127.0.0.1',
30 QUERY_STRING => 'QUERY_STRING: ',
31 PATH_INFO => 'PATH_INFO: /cgitest/PATH_INFO',
32 );
33
34 if ($^O eq 'freebsd' && `sysctl -n security.jail.jailed` == 1) {
35 delete @methods{qw(url server_name)};
36 delete @envvars{qw(SERVER_URL SERVER_NAME REMOTE_ADDR)};
37 plan tests => 47;
38 }
39 else {
40 plan tests => 62;
41 }
42
43 {
44 my $server=CGIServer->new($PORT);
45 is($server->port(),$PORT,'Constructor set port correctly');
46 sleep(3); # wait just a moment
47
48 my $pid=$server->background;
49
50 like($pid, '/^-?\d+$/', 'pid is numeric');
51
52 select(undef,undef,undef,0.2); # wait a sec
53 my @message_tests = (
54 [["GET / HTTP/1.1",""], '/NOFILE/', '[GET] no file'],
55 [["POST / HTTP/1.1","Content-Length: 0",""], '/NOFILE/', '[POST] no file'],
56 [["HEAD / HTTP/1.1",""], '/NOFILE/', '[HEAD] no file'],
57 [["PUT / HTTP/1.1","Content-Length: 0",""], '/NOFILE/', '[PUT] no file'],
58 [["DELETE / HTTP/1.1",""], '/NOFILE/', '[DELETE] no file'],
59 [["PATCH / HTTP/1.1","Content-Length: 0",""], '/NOFILE/', '[PATCH] no file'],
60 [["OPTIONS / HTTP/1.1","Content-Length: 0",""], '/NOFILE/', '[OPTIONS] no file'],
61 );
62 foreach my $message_test (@message_tests) {
63 my ($message, $expected, $description) = @$message_test;
64 like(fetch(@$message), $expected, $description);
65 select(undef,undef,undef,0.2); # wait a sec
66 }
67
68 foreach my $method (keys(%methods)) {
69 next unless defined $methods{$method};
70 like(
71 fetch("GET /cgitest/$method HTTP/1.1",""),
72 "/$methods{$method}/",
73 "method - $method"
74 );
75 select(undef,undef,undef,0.2); # wait a sec
76 }
77
78 foreach my $envvar (keys(%envvars)) {
79 like(
80 fetch("GET /cgitest/$envvar HTTP/1.1",""),
81 "/$envvars{$envvar}/",
82 "Environment - $envvar"
83 );
84 select(undef,undef,undef,0.2); # wait a sec
85 }
86
87 # extra tests for HTTP/1.1 absolute URLs
88
89 foreach my $verb ('GET', 'HEAD') {
90 foreach my $method (keys(%methods)) {
91 next unless defined $methods{$method};
92
93 my $method_value = $methods{$method};
94 $method_value =~ s/\bGET\b/$verb/;
95
96 like(
97 fetch("$verb http://localhost/cgitest/$method HTTP/1.1",""),
98 "/$method_value/",
99 "method (absolute URL) - $method"
100 );
101 select(undef,undef,undef,0.2); # wait a sec
102 }
103
104 foreach my $envvar (keys(%envvars)) {
105 (my $envvar_value = $envvars{$envvar});
106 $envvar_value =~ s/\bGET\b/$verb/;
107
108 like(
109 fetch("$verb http://localhost/cgitest/$envvar HTTP/1.1",""),
110 "/$envvar_value/",
111 "Environment (absolute URL) - $envvar"
112 );
113 select(undef,undef,undef,0.2); # wait a sec
114 }
115 }
116
117 like(
118 fetch("GET /cgitest/REQUEST_URI?foo%3Fbar HTTP/1.0",""),
119 qr/foo%3Fbar/,
120 "Didn't decode already"
121 );
122
123 like(
124 fetch("GET /cgitest/foo%2Fbar/PATH_INFO HTTP/1.0",""),
125 qr|foo/bar|,
126 "Did decode already"
127 );
128
129 like(
130 fetch("GET /cgitest/raw_cookie HTTP/1.0","Cookie: foo=bar",""),
131 qr|foo=bar|,
132 "uses HTTP_COOKIE",
133 );
134
135 like(
136 fetch("GET /cgitest/raw_cookie HTTP/1.0",
137 "Cookie: foo=bar\r\nCookie: baz=qux",""),
138 qr|foo=bar[;,] baz=qux|,
139 "combines multiple cookies into HTTP_COOKIE"
140 );
141
142 is(kill(9,$pid),1,'Signaled 1 process successfully');
143 wait or die "counldn't wait for sub-process completion";
144 }
145
146
147 sub fetch {
148 my $hostname = "localhost";
149 my $port = $PORT;
150 my $message = join "", map { "$_\015\012" } @_;
151 my $timeout = 5;
152 my $response;
153
154 eval {
155 local $SIG{ALRM} = sub { die "early exit - SIGALRM caught" };
156 alarm $timeout*2; #twice longer than timeout used later by select()
157
158 my $iaddr = inet_aton($hostname) || die "inet_aton: $!";
159 my $paddr = sockaddr_in($port, $iaddr) || die "sockaddr_in: $!";
160 my $proto = getprotobyname('tcp') || die "getprotobyname: $!";
161 socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
162 connect(SOCK, $paddr) || die "connect: $!";
163 (send SOCK, $message, 0) || die "send: $!";
164
165 my $rvec = '';
166 vec($rvec, fileno(SOCK), 1) = 1;
167 die "vec(): $!" unless $rvec;
168
169 $response = '';
170 for (;;) {
171 my $r = select($rvec, undef, undef, $timeout);
172 die "select: timeout - no data to read from server" unless ($r > 0);
173 my $l = sysread(SOCK, $response, 1024, length($response));
174 die "sysread: $!" unless defined($l);
175 last if ($l == 0);
176 }
177 $response =~ s/\015\012/\n/g;
178 (close SOCK) || die "close(): $!";
179 alarm 0;
180 };
181 if ($@) {
182 return "[ERROR] $@";
183 }
184 else {
185 return $response;
186 }
187 }
188
189 {
190 package CGIServer;
191 use base qw(HTTP::Server::Simple::CGI);
192
193 sub handle_request {
194 my $self=shift;
195 my $cgi=shift;
196
197
198 my $file=(split('/',$cgi->path_info))[-1]||'NOFILE';
199 $file=~s/\s+//g;
200 $file||='NOFILE';
201 print "HTTP/1.0 200 OK\r\n"; # probably OK by now
202 print "Content-Type: text/html\r\nContent-Length: ";
203 my $response;
204 if(exists $methods{$file}) {
205 $response = "$file: ".$cgi->$file();
206 } elsif($envvars{$file}) {
207 $response="$file: $ENV{$file}";
208 } else {
209 $response=$file;
210 }
211 print length($response), "\r\n\r\n", $response;
212 }
213 }
214
215