1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # note: our HTTP server should be standalone and capable of running
9 foreach my $mod (qw(Plack::Util Plack::Request Plack::Builder Danga::Socket
10 HTTP::Parser::XS HTTP::Date HTTP::Status)) {
12 plan skip_all => "$mod missing for httpd-corner.t" if $@;
15 use Digest::SHA qw(sha1_hex);
16 use File::Temp qw/tempdir/;
20 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD :seek);
21 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
22 use POSIX qw(dup2 mkfifo :sys_wait_h);
23 my $tmpdir = tempdir('httpd-corner-XXXXXX', TMPDIR => 1, CLEANUP => 1);
24 my $fifo = "$tmpdir/fifo";
25 ok(defined mkfifo($fifo, 0777), 'created FIFO');
26 my $err = "$tmpdir/stderr.log";
27 my $out = "$tmpdir/stdout.log";
28 my $httpd = 'blib/script/public-inbox-httpd';
29 my $psgi = getcwd()."/t/httpd-corner.psgi";
31 LocalAddr => '127.0.0.1',
37 my $sock = IO::Socket::INET->new(%opts);
38 my $upath = "$tmpdir/s";
39 my $unix = IO::Socket::UNIX->new(
44 ok($unix, 'UNIX socket created');
46 END { kill 'TERM', $pid if defined $pid };
47 my $spawn_httpd = sub {
50 my $fl = fcntl($sock, F_GETFD, 0);
51 ok(! $!, 'no error from fcntl(F_GETFD)');
52 is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
55 # pretend to be systemd
56 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
57 dup2(fileno($unix), 4) or die "dup2 failed: $!\n";
58 my $t = IO::Handle->new_from_fd(3, 'r');
59 $t->fcntl(F_SETFD, 0);
60 my $u = IO::Handle->new_from_fd(4, 'r');
61 $u->fcntl(F_SETFD, 0);
62 $ENV{LISTEN_PID} = $$;
64 exec $httpd, @args, "--stdout=$out", "--stderr=$err", $psgi;
67 ok(defined $pid, 'forked httpd process successfully');
71 ok($sock, 'sock created');
73 my $fl = fcntl($sock, F_GETFD, 0);
74 ok(! $!, 'no error from fcntl(F_GETFD)');
75 is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
76 $spawn_httpd->('-W0');
80 my $conn = conn_for($sock, 'streaming callback');
81 $conn->write("GET /callback HTTP/1.0\r\n\r\n");
82 ok($conn->read(my $buf, 8192), 'read response');
83 my ($head, $body) = split(/\r\n\r\n/, $buf);
84 is($body, "hello world\n", 'callback body matches expected');
90 my $u = IO::Socket::UNIX->new(Type => SOCK_STREAM, Peer => $upath);
91 ok($u, 'unix socket connected');
92 $u->write("GET /host-port HTTP/1.0\r\n\r\n");
93 $u->read(my $buf, 4096);
94 like($buf, qr!\r\n\r\n127\.0\.0\.1:0\z!,
95 'set REMOTE_ADDR and REMOTE_PORT for Unix socket');
99 my ($sock, $msg) = @_;
100 my $conn = IO::Socket::INET->new(
101 PeerAddr => $sock->sockhost,
102 PeerPort => $sock->sockport,
104 Type => SOCK_STREAM);
105 ok($conn, "connected for $msg");
107 setsockopt($conn, IPPROTO_TCP, TCP_NODELAY, 1);
112 my $conn = conn_for($sock, 'host-port');
113 $conn->write("GET /host-port HTTP/1.0\r\n\r\n");
114 $conn->read(my $buf, 4096);
115 my ($head, $body) = split(/\r\n\r\n/, $buf);
116 my ($addr, $port) = split(/:/, $body);
117 is($addr, $conn->sockhost, 'host matches addr');
118 is($port, $conn->sockport, 'port matches');
121 # graceful termination
123 my $conn = conn_for($sock, 'graceful termination via slow header');
124 $conn->write("GET /slow-header HTTP/1.0\r\n" .
125 "X-Check-Fifo: $fifo\r\n\r\n");
126 open my $f, '>', $fifo or die "open $fifo: $!\n";
128 ok(print($f "hello\n"), 'wrote something to fifo');
131 is(kill('TERM', $kpid), 1, 'started graceful shutdown');
132 ok(print($f "world\n"), 'wrote else to fifo');
133 close $f or die "close fifo: $!\n";
134 $conn->read(my $buf, 8192);
135 my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
136 like($head, qr!\AHTTP/1\.[01] 200 OK!, 'got 200 for slow-header');
137 is($body, "hello\nworld\n", 'read expected body');
138 is(waitpid($kpid, 0), $kpid, 'reaped httpd');
139 is($?, 0, 'no error');
140 $spawn_httpd->('-W0');
144 my $conn = conn_for($sock, 'graceful termination via slow-body');
145 $conn->write("GET /slow-body HTTP/1.0\r\n" .
146 "X-Check-Fifo: $fifo\r\n\r\n");
147 open my $f, '>', $fifo or die "open $fifo: $!\n";
150 $conn->sysread($buf, 8192);
151 like($buf, qr!\AHTTP/1\.[01] 200 OK!, 'got 200 for slow-body');
152 like($buf, qr!\r\n\r\n!, 'finished HTTP response header');
154 foreach my $c ('a'..'c') {
156 ok(print($f $c), 'wrote line to fifo');
157 $conn->sysread($buf, 8192);
158 is($buf, $c, 'got trickle for reading');
162 is(kill('TERM', $kpid), 1, 'started graceful shutdown');
163 ok(print($f "world\n"), 'wrote else to fifo');
164 close $f or die "close fifo: $!\n";
165 $conn->sysread($buf, 8192);
166 is($buf, "world\n", 'read expected body');
167 is($conn->sysread($buf, 8192), 0, 'got EOF from server');
168 is(waitpid($kpid, 0), $kpid, 'reaped httpd');
169 is($?, 0, 'no error');
170 $spawn_httpd->('-W0');
173 sub delay { select(undef, undef, undef, shift || rand(0.02)) }
175 my $str = 'abcdefghijklmnopqrstuvwxyz';
176 my $len = length $str;
177 is($len, 26, 'got the alphabet');
178 my $check_self = sub {
180 $conn->read(my $buf, 4096);
181 my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
182 like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
183 is($body, sha1_hex($str), 'read expected body');
190 foreach my $p (split(':', $ENV{PATH})) {
191 -x "$p/curl" or next;
196 $have_curl or skip('curl(1) missing', $ntest);
197 my $url = 'http://' . $sock->sockhost . ':' . $sock->sockport . '/sha1';
199 pipe($r, $w) or die "pipe: $!";
200 my $tout = IO::File->new_tmpfile or die "new_tmpfile: $!";
202 defined $pid or die "fork: $!";
203 my @cmd = (qw(curl --tcp-nodelay --no-buffer -T- -HExpect: -sS), $url);
205 dup2(fileno($r), 0) or die "redirect stdin failed: $!\n";
206 dup2(fileno($tout), 1) or die "redirect stdout failed: $!\n";
207 exec(@cmd) or die 'exec `' . join(' '). "' failed: $!\n";
210 foreach my $c ('a'..'z') {
211 print $w $c or die "failed to write to curl: $!";
214 close $w or die "close write pipe: $!";
215 close $r or die "close read pipe: $!";
216 my $kid = waitpid $pid, 0;
217 is($?, 0, 'curl exited successfully');
218 $tout->sysseek(0, SEEK_SET);
219 $tout->sysread(my $buf, 100);
220 is($buf, sha1_hex($str), 'read expected body');
224 my $conn = conn_for($sock, '1.1 pipeline together');
225 $conn->write("PUT /sha1 HTTP/1.1\r\nUser-agent: hello\r\n\r\n" .
226 "PUT /sha1 HTTP/1.1\r\n\r\n");
229 until (scalar(@r) >= 2) {
230 my $r = $conn->sysread(my $tmp, 4096);
231 die $! unless defined $r;
232 die "EOF <$buf>" unless $r;
234 @r = ($buf =~ /\r\n\r\n([a-f0-9]{40})/g);
236 is(2, scalar @r, 'got 2 responses');
238 foreach my $hex (@r) {
239 is($hex, sha1_hex(''), "read expected body $i");
245 my $conn = conn_for($sock, 'graceful termination during slow request');
246 $conn->write("PUT /sha1 HTTP/1.0\r\n");
248 $conn->write("Content-Length: $len\r\n");
250 $conn->write("\r\n");
253 is(kill('TERM', $kpid), 1, 'started graceful shutdown');
256 foreach my $c ('a'..'z') {
257 $n += $conn->write($c);
259 is($n, $len, 'wrote alphabet');
260 $check_self->($conn);
261 is(waitpid($kpid, 0), $kpid, 'reaped httpd');
262 is($?, 0, 'no error');
263 $spawn_httpd->('-W0');
266 # various DoS attacks against the chunk parser:
268 local $SIG{PIPE} = 'IGNORE';
269 my $conn = conn_for($sock, '1.1 chunk header excessive');
270 $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding:chunked\r\n\r\n");
273 while ($w = $conn->write('ffffffff')) {
276 ok($!, 'got error set in $!');
277 is($w, undef, 'write error happened');
278 ok($n > 0, 'was able to write');
279 my $r = $conn->read(my $buf, 66666);
280 ok($r > 0, 'got non-empty response');
281 like($buf, qr!HTTP/1\.\d 400 !, 'got 400 response');
283 $conn = conn_for($sock, '1.1 chunk trailer excessive');
284 $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding:chunked\r\n\r\n");
285 is($conn->syswrite("1\r\na"), 4, 'wrote first header + chunk');
288 while ($w = $conn->write("\r")) {
291 ok($!, 'got error set in $!');
292 ok($n > 0, 'wrote part of chunk end (\r)');
293 $r = $conn->read($buf, 66666);
294 ok($r > 0, 'got non-empty response');
295 like($buf, qr!HTTP/1\.\d 400 !, 'got 400 response');
299 my $conn = conn_for($sock, '1.1 chunked close trickle');
300 $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
301 $conn->write("Transfer-encoding: chunked\r\n\r\n");
302 foreach my $x ('a'..'z') {
326 $check_self->($conn);
330 my $conn = conn_for($sock, '1.1 chunked close');
331 $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
332 my $xlen = sprintf('%x', $len);
333 $conn->write("Transfer-Encoding: chunked\r\n\r\n$xlen\r\n" .
334 "$str\r\n0\r\n\r\n");
335 $check_self->($conn);
339 my $conn = conn_for($sock, 'chunked body + pipeline');
340 $conn->write("PUT /sha1 HTTP/1.1\r\n" .
341 "Transfer-Encoding: chunked\r\n");
343 $conn->write("\r\n1\r\n");
347 $conn->write("\r\n0\r\n\r\nPUT /sha1 HTTP/1.1\r\n");
351 until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
352 $conn->sysread(my $tmp, 4096);
355 my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
356 like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
357 is($body, sha1_hex('a'), 'read expected body');
359 $conn->write("Connection: close\r\n");
360 $conn->write("Content-Length: $len\r\n\r\n$str");
361 $check_self->($conn);
365 my $conn = conn_for($sock, 'trickle header, one-shot body + pipeline');
366 $conn->write("PUT /sha1 HTTP/1.0\r\n" .
367 "Connection: keep-alive\r\n");
369 $conn->write("Content-Length: $len\r\n\r\n${str}PUT");
371 until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
372 $conn->sysread(my $tmp, 4096);
375 my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
376 like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
377 is($body, sha1_hex($str), 'read expected body');
379 $conn->write(" /sha1 HTTP/1.0\r\nContent-Length: $len\r\n\r\n$str");
380 $check_self->($conn);
384 my $conn = conn_for($sock, 'trickle body');
385 $conn->write("PUT /sha1 HTTP/1.0\r\n");
386 $conn->write("Content-Length: $len\r\n\r\n");
387 my $beg = substr($str, 0, 10);
388 my $end = substr($str, 10);
389 is($beg . $end, $str, 'substr setup correct');
394 $check_self->($conn);
398 my $conn = conn_for($sock, 'one-shot write');
399 $conn->write("PUT /sha1 HTTP/1.0\r\n" .
400 "Content-Length: $len\r\n\r\n$str");
401 $check_self->($conn);
405 my $conn = conn_for($sock, 'trickle header, one-shot body');
406 $conn->write("PUT /sha1 HTTP/1.0\r\n");
408 $conn->write("Content-Length: $len\r\n\r\n$str");
409 $check_self->($conn);
413 my $conn = conn_for($sock, '1.1 Connnection: close');
414 $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
416 $conn->write("Content-Length: $len\r\n\r\n$str");
417 $check_self->($conn);
421 my $conn = conn_for($sock, '1.1 pipeline start');
422 $conn->write("PUT /sha1 HTTP/1.1\r\n\r\nPUT");
424 until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
425 $conn->sysread(my $tmp, 4096);
428 my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
429 like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
430 is($body, sha1_hex(''), 'read expected body');
433 $conn->write(" /sha1 HTTP/1.1\r\n\r\n");
435 until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
436 $conn->sysread(my $tmp, 4096);
439 ($head, $body) = split(/\r\n\r\n/, $buf, 2);
440 like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
441 is($body, sha1_hex(''), 'read expected body #2');