2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # note: our HTTP server should be standalone and capable of running
5 # generic PSGI/Plack apps.
6 use v5.12; use PublicInbox::TestCommon;
7 use Time::HiRes qw(gettimeofday tv_interval);
8 use autodie qw(getsockopt seek setsockopt);
9 use PublicInbox::Spawn qw(spawn popen_rd);
10 require_mods '-httpd';
11 use PublicInbox::SHA qw(sha1_hex);
15 use Socket qw(IPPROTO_TCP TCP_NODELAY SOL_SOCKET);
18 my ($tmpdir, $for_destroy) = tmpdir();
19 my $fifo = "$tmpdir/fifo";
20 ok(defined mkfifo($fifo, 0777), 'created FIFO');
21 my $err = "$tmpdir/stderr.log";
22 my $out = "$tmpdir/stdout.log";
23 my $psgi = "./t/httpd-corner.psgi";
24 my $sock = tcp_server();
25 my @zmods = qw(PublicInbox::GzipFilter IO::Uncompress::Gunzip);
27 # Make sure we don't clobber socket options set by systemd or similar
28 # using socket activation:
29 my ($defer_accept_val, $accf_arg, $TCP_DEFER_ACCEPT);
31 skip 'TCP_DEFER_ACCEPT is Linux-only', 1 if $^O ne 'linux';
32 $TCP_DEFER_ACCEPT = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
33 setsockopt($sock, IPPROTO_TCP, $TCP_DEFER_ACCEPT, 5);
34 my $x = getsockopt($sock, IPPROTO_TCP, $TCP_DEFER_ACCEPT);
35 $defer_accept_val = unpack('i', $x);
36 ok($defer_accept_val > 0, 'TCP_DEFER_ACCEPT val non-zero') or
37 xbail "unexpected TCP_DEFER_ACCEPT value: $defer_accept_val";
40 require_mods '+accf_data';
41 require PublicInbox::Daemon;
42 my $var = $PublicInbox::Daemon::SO_ACCEPTFILTER;
43 $accf_arg = pack('a16a240', 'dataready', '');
44 setsockopt($sock, SOL_SOCKET, $var, $accf_arg);
48 my $s = IO::Socket::UNIX->new(
50 Type => Socket::SOCK_STREAM(),
52 ) or BAIL_OUT "bind + listen $_[0]: $!";
57 my $upath = "$tmpdir/s";
58 my $unix = unix_server($upath);
59 my $alt = tcp_server();
61 my $spawn_httpd = sub {
63 my $x = tcp_host_port($alt);
64 my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi,
65 '-l', "http://$x/?psgi=t/alt.psgi,env.PI_CONFIG=/path/to/alt".
66 ",err=$tmpdir/alt.err" ];
67 my $env = { PI_CONFIG => '/dev/null' };
68 $td = start_script($cmd, $env, { 3 => $sock, 4 => $unix, 5 => $alt });
73 my $conn = conn_for($alt, 'alt PSGI path');
74 $conn->write("GET / HTTP/1.0\r\n\r\n");
75 $conn->read(my $buf, 4096);
76 like($buf, qr!^/path/to/alt\z!sm,
77 'alt.psgi loaded on alt socket with correct env');
79 $conn = conn_for($sock, 'default PSGI path');
80 $conn->write("GET /PI_CONFIG HTTP/1.0\r\n\r\n");
81 $conn->read($buf, 4096);
82 like($buf, qr!^/dev/null\z!sm,
83 'default PSGI on original socket');
84 my $log = capture("$tmpdir/alt.err");
85 ok(grep(/ALT/, @$log), 'alt psgi.errors written to');
87 ok(!grep(/ALT/, @$log), 'STDERR not written to');
88 is(unlink($err, "$tmpdir/alt.err"), 2, 'unlinked stderr and alt.err');
90 $td->kill('USR1'); # trigger reopen_logs
93 if ('test worker death') {
94 my $conn = conn_for($sock, 'killed worker');
95 $conn->write("GET /pid HTTP/1.1\r\nHost:example.com\r\n\r\n");
97 while (defined(my $line = $conn->getline)) {
98 next unless $line eq "\r\n";
99 chomp($pid = $conn->getline);
102 like($pid, qr/\A[0-9]+\z/, '/pid response');
103 is(kill('KILL', $pid), 1, 'killed worker');
104 is($conn->getline, undef, 'worker died and EOF-ed client');
106 $conn = conn_for($sock, 'respawned worker');
107 $conn->write("GET /pid HTTP/1.0\r\n\r\n");
108 ok($conn->read(my $buf, 8192), 'read response');
109 my ($head, $body) = split(/\r\n\r\n/, $buf);
111 like($body, qr/\A[0-9]+\z/, '/pid response');
112 isnt($body, $pid, 'respawned worker');
114 { # check on prior USR1 signal
115 ok(-e $err, 'stderr recreated after USR1');
116 ok(-e "$tmpdir/alt.err", 'alt.err recreated after USR1');
119 my $conn = conn_for($sock, 'Header spaces bogus');
120 $conn->write("GET /empty HTTP/1.1\r\nSpaced-Out : 3\r\n\r\n");
121 $conn->read(my $buf, 4096);
122 like($buf, qr!\AHTTP/1\.[0-9] 400 !, 'got 400 response on bad request');
125 my $conn = conn_for($sock, 'streaming callback');
126 $conn->write("GET /callback HTTP/1.0\r\n\r\n");
127 ok($conn->read(my $buf, 8192), 'read response');
128 my ($head, $body) = split(/\r\n\r\n/, $buf);
129 is($body, "hello world\n", 'callback body matches expected');
133 my $conn = conn_for($sock, 'getline-die');
134 $conn->write("GET /getline-die HTTP/1.1\r\nHost: example.com\r\n\r\n");
135 ok($conn->read(my $buf, 8192), 'read some response');
136 like($buf, qr!HTTP/1\.1 200\b[^\r]*\r\n!, 'got some sort of header');
137 is($conn->read(my $nil, 8192), 0, 'read EOF');
139 my $after = capture($err);
140 is(scalar(grep(/GETLINE FAIL/, @$after)), 1, 'failure logged');
141 is(scalar(grep(/CLOSE FAIL/, @$after)), 1, 'body->close not called');
145 my $conn = conn_for($sock, 'close-die');
146 $conn->write("GET /close-die HTTP/1.1\r\nHost: example.com\r\n\r\n");
147 ok($conn->read(my $buf, 8192), 'read some response');
148 like($buf, qr!HTTP/1\.1 200\b[^\r]*\r\n!, 'got some sort of header');
149 is($conn->read(my $nil, 8192), 0, 'read EOF');
151 my $after = capture($err);
152 is(scalar(grep(/GETLINE FAIL/, @$after)), 0, 'getline not failed');
153 is(scalar(grep(/CLOSE FAIL/, @$after)), 1, 'body->close not called');
158 my $r = $conn->read(my $buf, 8192);
159 # ECONNRESET and $r==0 are both observed on FreeBSD 11.2
161 ok($!{ECONNRESET}, 'ECONNRESET on read (BSD sometimes)');
163 like($buf, qr!\AHTTP/1\.\d 400 !, 'got 400 response');
165 is($r, 0, 'got EOF (BSD sometimes)');
167 close($conn); # ensure we don't get SIGPIPE later
171 local $SIG{PIPE} = 'IGNORE';
172 my $conn = conn_for($sock, 'excessive header');
173 $conn->write("GET /callback HTTP/1.0\r\n");
174 foreach my $i (1..500000) {
175 last unless $conn->write("X-xxxxxJunk-$i: omg\r\n");
177 ok(!$conn->write("\r\n"), 'broken request');
182 my $conn = conn_for($sock, 'excessive body Content-Length');
183 my $n = (10 * 1024 * 1024) + 1;
184 $conn->write("PUT /sha1 HTTP/1.0\r\nContent-Length: $n\r\n\r\n");
185 my $r = $conn->read(my $buf, 8192);
186 ok($r > 0, 'read response');
187 my ($head, $body) = split(/\r\n\r\n/, $buf);
188 like($head, qr/\b413\b/, 'got 413 response');
192 my $conn = conn_for($sock, 'excessive body chunked');
193 my $n = (10 * 1024 * 1024) + 1;
194 $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding: chunked\r\n");
195 $conn->write("\r\n".sprintf("%x\r\n", $n));
196 my $r = $conn->read(my $buf, 8192);
197 ok($r > 0, 'read response');
198 my ($head, $body) = split(/\r\n\r\n/, $buf);
199 like($head, qr/\b413\b/, 'got 413 response');
203 my $conn = conn_for($sock, '1.1 Transfer-Encoding bogus');
204 $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding: bogus\r\n\r\n");
205 $conn->read(my $buf, 4096);
206 like($buf, qr!\AHTTP/1\.[0-9] 400 !, 'got 400 response on bogus TE');
209 my $conn = conn_for($sock, '1.1 Content-Length bogus');
210 $conn->write("PUT /sha1 HTTP/1.1\r\nContent-Length: 3.3\r\n\r\n");
211 $conn->read(my $buf, 4096);
212 like($buf, qr!\AHTTP/1\.[0-9] 400 !, 'got 400 response on bad length');
216 my $req = "PUT /sha1 HTTP/1.1\r\nContent-Length: 3\r\n" .
217 "Content-Length: 3\r\n\r\n";
218 # this is stricter than it needs to be. Due to the way
219 # Plack::HTTPParser, PSGI specs, and how hash tables work in common
220 # languages; it's not possible to tell the difference between folded
221 # and intentionally bad commas (e.g. "Content-Length: 3, 3")
223 require Plack::HTTPParser; # XS or pure Perl
224 require Data::Dumper;
225 Plack::HTTPParser::parse_http_request($req, my $env = {});
226 diag Data::Dumper::Dumper($env); # "Content-Length: 3, 3"
228 my $conn = conn_for($sock, '1.1 Content-Length dupe');
230 $conn->read(my $buf, 4096);
231 like($buf, qr!\AHTTP/1\.[0-9] 400 !, 'got 400 response on dupe length');
235 my $conn = conn_for($sock, 'chunk with pipeline');
237 my $payload = 'b'x$n;
238 $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding: chunked\r\n");
239 $conn->write("\r\n".sprintf("%x\r\n", $n));
240 $conn->write($payload . "\r\n0\r\n\r\nGET /empty HTTP/1.0\r\n\r\n");
241 $conn->read(my $buf, 4096);
243 $lim++ while ($conn->read($buf, 4096, length($buf)) && $lim < 9);
244 my $exp = sha1_hex($payload);
245 like($buf, qr!\r\n\r\n${exp}HTTP/1\.0 200 OK\r\n!s,
246 'chunk parser can handled pipelined requests');
249 # Unix domain sockets
251 my $u = IO::Socket::UNIX->new(Type => SOCK_STREAM, Peer => $upath);
252 ok($u, 'unix socket connected');
253 $u->write("GET /host-port HTTP/1.0\r\n\r\n");
254 $u->read(my $buf, 4096);
255 like($buf, qr!\r\n\r\n127\.0\.0\.1 0\z!,
256 'set REMOTE_ADDR and REMOTE_PORT for Unix socket');
260 my ($dest, $msg) = @_;
261 my $conn = tcp_connect($dest);
262 ok($conn, "connected for $msg");
263 setsockopt($conn, IPPROTO_TCP, TCP_NODELAY, 1);
268 my $conn = conn_for($sock, 'host-port');
269 $conn->write("GET /host-port HTTP/1.0\r\n\r\n");
270 $conn->read(my $buf, 4096);
271 my ($head, $body) = split(/\r\n\r\n/, $buf);
272 my ($addr, $port) = split(/ /, $body);
273 is($addr, (tcp_host_port($conn))[0], 'host matches addr');
274 is($port, $conn->sockport, 'port matches');
277 # graceful termination
279 my $conn = conn_for($sock, 'graceful termination via slow header');
280 $conn->write("GET /slow-header HTTP/1.0\r\n" .
281 "X-Check-Fifo: $fifo\r\n\r\n");
282 open my $f, '>', $fifo or die "open $fifo: $!\n";
284 ok(print($f "hello\n"), 'wrote something to fifo');
285 is($td->kill, 1, 'started graceful shutdown');
286 ok(print($f "world\n"), 'wrote else to fifo');
287 close $f or die "close fifo: $!\n";
288 $conn->read(my $buf, 8192);
289 my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
290 like($head, qr!\AHTTP/1\.[01] 200 OK!, 'got 200 for slow-header');
291 is($body, "hello\nworld\n", 'read expected body');
293 is($?, 0, 'no error');
294 $spawn_httpd->('-W0');
298 my $conn = conn_for($sock, 'graceful termination via slow-body');
299 $conn->write("GET /slow-body HTTP/1.0\r\n" .
300 "X-Check-Fifo: $fifo\r\n\r\n");
301 open my $f, '>', $fifo or die "open $fifo: $!\n";
304 $conn->sysread($buf, 8192);
305 like($buf, qr!\AHTTP/1\.[01] 200 OK!, 'got 200 for slow-body');
306 like($buf, qr!\r\n\r\n!, 'finished HTTP response header');
308 foreach my $c ('a'..'c') {
310 ok(print($f $c), 'wrote line to fifo');
311 $conn->sysread($buf, 8192);
312 is($buf, $c, 'got trickle for reading');
314 is($td->kill, 1, 'started graceful shutdown');
315 ok(print($f "world\n"), 'wrote else to fifo');
316 close $f or die "close fifo: $!\n";
317 $conn->sysread($buf, 8192);
318 is($buf, "world\n", 'read expected body');
319 is($conn->sysread($buf, 8192), 0, 'got EOF from server');
321 is($?, 0, 'no error');
322 $spawn_httpd->('-W0');
325 sub delay { tick(shift || rand(0.02)) }
327 my $str = 'abcdefghijklmnopqrstuvwxyz';
328 my $len = length $str;
329 is($len, 26, 'got the alphabet');
330 my $check_self = sub {
332 vec(my $rbits = '', fileno($conn), 1) = 1;
333 select($rbits, undef, undef, 30) or Carp::confess('timed out');
334 $conn->read(my $buf, 4096);
335 my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
336 like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
337 is($body, sha1_hex($str), 'read expected body');
341 my $curl = require_cmd('curl', 1) or skip('curl(1) missing', 4);
342 my $base = 'http://'.tcp_host_port($sock);
343 my $url = "$base/sha1";
345 pipe($r, $w) or die "pipe: $!";
346 my $cmd = [$curl, qw(--tcp-nodelay -T- -HExpect: -gsSN), $url];
347 open my $cout, '+>', undef or die;
348 open my $cerr, '>', undef or die;
349 my $rdr = { 0 => $r, 1 => $cout, 2 => $cerr };
350 my $pid = spawn($cmd, undef, $rdr);
351 close $r or die "close read pipe: $!";
352 foreach my $c ('a'..'z') {
353 print $w $c or die "failed to write to curl: $!";
356 close $w or die "close write pipe: $!";
358 is($?, 0, 'curl exited successfully');
359 is(-s $cerr, 0, 'no errors from curl');
360 seek($cout, 0, SEEK_SET);
361 is(<$cout>, sha1_hex($str), 'read expected body');
363 my $fh = popen_rd([$curl, '-gsS', "$base/async-big"]);
367 my $r = sysread($fh, my $buf, 4096) or last;
369 $buf =~ /\A\0+\z/ or $non_zero++;
371 $fh->close or die "close curl pipe: $!";
372 is($?, 0, 'curl succesful');
373 is($n, 30 * 1024 * 1024, 'got expected output from curl');
374 is($non_zero, 0, 'read all zeros');
376 require_mods(@zmods, 4);
377 my $buf = xqx([$curl, '-gsS', "$base/psgi-yield-gzip"]);
378 is($?, 0, 'curl succesful');
379 IO::Uncompress::Gunzip::gunzip(\$buf => \(my $out));
380 is($out, "hello world\n");
381 my $curl_rdr = { 2 => \(my $curl_err = '') };
382 $buf = xqx([$curl, qw(-gsSv --compressed),
383 "$base/psgi-yield-compressible"], undef, $curl_rdr);
384 is($?, 0, 'curl --compressed successful');
385 is($buf, "goodbye world\n", 'gzipped response as expected');
386 like($curl_err, qr/\bContent-Encoding: gzip\b/,
387 'curl got gzipped response');
391 my $conn = conn_for($sock, 'psgi_yield ENOENT');
392 print $conn "GET /psgi-yield-enoent HTTP/1.1\r\n\r\n" or die;
394 sysread($conn, $buf, 16384, length($buf)) until $buf =~ /\r\n\r\n/;
395 like($buf, qr!HTTP/1\.[01] 500\b!, 'got 500 error on ENOENT');
399 my $conn = conn_for($sock, '1.1 pipeline together');
400 $conn->write("PUT /sha1 HTTP/1.1\r\nUser-agent: hello\r\n\r\n" .
401 "PUT /sha1 HTTP/1.1\r\n\r\n");
404 until (scalar(@r) >= 2) {
405 my $r = $conn->sysread(my $tmp, 4096);
406 die $! unless defined $r;
407 die "EOF <$buf>" unless $r;
409 @r = ($buf =~ /\r\n\r\n([a-f0-9]{40})/g);
411 is(2, scalar @r, 'got 2 responses');
413 foreach my $hex (@r) {
414 is($hex, sha1_hex(''), "read expected body $i");
420 my $conn = conn_for($sock, 'no TCP_CORK on empty body');
421 $conn->write("GET /empty HTTP/1.1\r\nHost:example.com\r\n\r\n");
423 my $t0 = [ gettimeofday ];
424 until ($buf =~ /\r\n\r\n/s) {
425 $conn->sysread($buf, 4096, length($buf));
427 my $elapsed = tv_interval($t0, [ gettimeofday ]);
428 ok($elapsed < 0.190, 'no 200ms TCP cork delay on empty body');
432 my $conn = conn_for($sock, 'graceful termination during slow request');
433 $conn->write("PUT /sha1 HTTP/1.0\r\nContent-Length: $len\r\n\r\n");
435 # XXX ugh, want a reliable and non-intrusive way to detect
436 # that the server has started buffering our partial request so we
437 # can reliably test graceful termination. Maybe making this and
438 # similar tests dependent on Linux strace is a possibility?
441 is($td->kill, 1, 'start graceful shutdown');
443 foreach my $c ('a'..'z') {
444 $n += $conn->write($c);
446 ok(kill(0, $td->{pid}), 'graceful shutdown did not kill httpd');
447 is($n, $len, 'wrote alphabet');
448 $check_self->($conn);
450 is($?, 0, 'no error');
451 $spawn_httpd->('-W0');
454 # various DoS attacks against the chunk parser:
456 local $SIG{PIPE} = 'IGNORE';
457 my $conn = conn_for($sock, '1.1 chunk header excessive');
458 $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding:chunked\r\n\r\n");
461 while ($w = $conn->write('ffffffff')) {
464 ok($!, 'got error set in $!');
465 is($w, undef, 'write error happened');
466 ok($n > 0, 'was able to write');
468 $conn = conn_for($sock, '1.1 chunk trailer excessive');
469 $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding:chunked\r\n\r\n");
470 is($conn->syswrite("1\r\na"), 4, 'wrote first header + chunk');
473 while ($w = $conn->write("\r")) {
476 ok($!, 'got error set in $!');
477 ok($n > 0, 'wrote part of chunk end (\r)');
482 my $conn = conn_for($sock, '1.1 chunked close trickle');
483 $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
484 $conn->write("Transfer-encoding: chunked\r\n\r\n");
485 foreach my $x ('a'..'z') {
509 $check_self->($conn);
513 my $conn = conn_for($sock, '1.1 chunked close');
514 $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
515 my $xlen = sprintf('%x', $len);
516 $conn->write("Transfer-Encoding: chunked\r\n\r\n$xlen\r\n" .
517 "$str\r\n0\r\n\r\n");
518 $check_self->($conn);
522 my $conn = conn_for($sock, 'chunked body + pipeline');
523 $conn->write("PUT /sha1 HTTP/1.1\r\n" .
524 "Transfer-Encoding: chunked\r\n");
526 $conn->write("\r\n1\r\n");
530 $conn->write("\r\n0\r\n\r\nPUT /sha1 HTTP/1.1\r\n");
534 until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
535 $conn->sysread(my $tmp, 4096);
538 my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
539 like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
540 is($body, sha1_hex('a'), 'read expected body');
542 $conn->write("Connection: close\r\n");
543 $conn->write("Content-Length: $len\r\n\r\n$str");
544 $check_self->($conn);
548 my $conn = conn_for($sock, 'trickle header, one-shot body + pipeline');
549 $conn->write("PUT /sha1 HTTP/1.0\r\n" .
550 "Connection: keep-alive\r\n");
552 $conn->write("Content-Length: $len\r\n\r\n${str}PUT");
554 until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
555 $conn->sysread(my $tmp, 4096);
558 my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
559 like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
560 is($body, sha1_hex($str), 'read expected body');
562 $conn->write(" /sha1 HTTP/1.0\r\nContent-Length: $len\r\n\r\n$str");
563 $check_self->($conn);
567 my $conn = conn_for($sock, 'trickle body');
568 $conn->write("PUT /sha1 HTTP/1.0\r\n");
569 $conn->write("Content-Length: $len\r\n\r\n");
570 my $beg = substr($str, 0, 10);
571 my $end = substr($str, 10);
572 is($beg . $end, $str, 'substr setup correct');
577 $check_self->($conn);
581 my $conn = conn_for($sock, 'one-shot write');
582 $conn->write("PUT /sha1 HTTP/1.0\r\n" .
583 "Content-Length: $len\r\n\r\n$str");
584 $check_self->($conn);
588 my $conn = conn_for($sock, 'trickle header, one-shot body');
589 $conn->write("PUT /sha1 HTTP/1.0\r\n");
591 $conn->write("Content-Length: $len\r\n\r\n$str");
592 $check_self->($conn);
596 my $conn = conn_for($sock, '1.1 Connection: close');
597 $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
599 $conn->write("Content-Length: $len\r\n\r\n$str");
600 $check_self->($conn);
604 my $conn = conn_for($sock, '1.1 pipeline start');
605 $conn->write("PUT /sha1 HTTP/1.1\r\n\r\nPUT");
607 until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
608 $conn->sysread(my $tmp, 4096);
611 my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
612 like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
613 is($body, sha1_hex(''), 'read expected body');
616 $conn->write(" /sha1 HTTP/1.1\r\n\r\n");
618 until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
619 $conn->sysread(my $tmp, 4096);
622 ($head, $body) = split(/\r\n\r\n/, $buf, 2);
623 like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
624 is($body, sha1_hex(''), 'read expected body #2');
628 skip 'TCP_DEFER_ACCEPT is Linux-only', 1 if $^O ne 'linux';
629 my $var = $TCP_DEFER_ACCEPT;
630 my $x = getsockopt($sock, IPPROTO_TCP, $var);
631 is(unpack('i', $x), $defer_accept_val,
632 'TCP_DEFER_ACCEPT unchanged if previously set');
635 require_mods '+accf_data';
636 my $var = $PublicInbox::Daemon::SO_ACCEPTFILTER;
637 my $x = getsockopt($sock, SOL_SOCKET, $var);
638 is($x, $accf_arg, 'SO_ACCEPTFILTER unchanged if previously set');
642 skip 'only testing /proc/PID/fd on Linux', 1 if $^O ne 'linux';
643 my $fd_dir = "/proc/$td->{pid}/fd";
644 -d $fd_dir or skip '/proc/$PID/fd missing', 1;
645 my @child = grep defined, map readlink, glob "$fd_dir/*";
646 my @d = grep /\(deleted\)/, @child;
647 is_deeply(\@d, [], 'no lingering deleted inputs') or diag explain(\@d);
649 # filter out pipes inherited from the parent
650 my @this = grep defined, map readlink, glob "/proc/$$/fd/*";
651 my $extract_inodes = sub { map { $_ => 1 } grep /\bpipe\b/, @_ };
652 my %child = $extract_inodes->(@child);
653 my %parent = $extract_inodes->(@this);
654 delete @child{(keys %parent)};
655 is_deeply([], [keys %child], 'no extra pipes with -W0') or
656 diag explain([child => \%child, parent => \%parent]);
659 # ensure compatibility with other PSGI servers
661 require_mods @zmods, 'psgi', 3;
663 open my $olderr, '>&', \*STDERR or die "dup stderr: $!";
664 open my $tmperr, '+>', undef or die;
665 open STDERR, '>&', $tmperr or die;
666 STDERR->autoflush(1);
667 my $app = require $psgi;
668 test_psgi($app, sub {
670 my $req = GET('http://example.com/psgi-yield-gzip');
671 my $res = $cb->($req);
672 my $buf = $res->content;
673 IO::Uncompress::Gunzip::gunzip(\$buf => \(my $out));
674 is($out, "hello world\n", 'got expected output');
676 $req = GET('http://example.com/psgi-yield-enoent');
678 is($res->code, 500, 'got error on ENOENT');
679 seek $tmperr, 0, SEEK_SET;
680 my $errbuf = do { local $/; <$tmperr> };
681 like($errbuf, qr/this-better-not-exist/,
682 'error logged about missing command');
684 open STDERR, '>&', $olderr or die "restore stderr: $!";
691 open my $fh, '+<', $f or die "failed to open $f: $!\n";
694 truncate($fh, 0) or die "truncate failed on $f: $!\n";