2 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
8 use Socket qw(IPPROTO_TCP SOL_SOCKET);
9 require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status));
11 # FIXME: too much setup
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $home = "$tmpdir/pi-home";
14 my $err = "$tmpdir/stderr.log";
15 my $out = "$tmpdir/stdout.log";
16 my $inboxdir = "$tmpdir/i.git";
17 my $group = 'test-httpd';
18 my $addr = $group . '@example.com';
19 my $sock = tcp_server();
22 create_inbox 'test', tmpdir => $inboxdir, sub {
24 $im->add(PublicInbox::Eml->new(<<EOF)) or BAIL_OUT;
25 From: Me <me\@example.com>
26 To: You <you\@example.com>
28 Message-Id: <nntp\@example.com>
30 Date: Thu, 01 Jan 1970 06:06:06 +0000
35 my $i2 = create_inbox 'test-2', sub {
37 $im->add(eml_load('t/plack-qp.eml')) or xbail '->add';
39 local $ENV{HOME} = $home;
40 my $cmd = [ '-init', $group, $inboxdir, 'http://example.com/', $addr ];
41 ok(run_script($cmd), 'init ran properly');
42 $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
43 $td = start_script($cmd, undef, { 3 => $sock });
44 my $http_pfx = 'http://'.tcp_host_port($sock);
46 my $bad = tcp_connect($sock);
47 print $bad "GETT / HTTP/1.0\r\n\r\n" or die;
48 like(<$bad>, qr!\AHTTP/1\.[01] 405\b!, 'got 405 on bad req');
50 my $conn = tcp_connect($sock);
51 ok($conn->write("GET / HTTP/1.0\r\n\r\n"), 'wrote data to socket');
54 ok($conn->read($buf, 4096), 'read some bytes');
55 like($buf, qr!\AHTTP/1\.[01] 404\b!, 'got 404 response');
56 is($conn->read($buf, 1), 0, "EOF");
59 is(xsys(qw(git clone -q --mirror),
60 "$http_pfx/$group", "$tmpdir/clone.git"),
61 0, 'smart clone successful');
63 # ensure dumb cloning works, too:
64 is(xsys('git', "--git-dir=$inboxdir",
65 qw(config http.uploadpack false)),
66 0, 'disable http.uploadpack');
67 is(xsys(qw(git clone -q --mirror),
68 "$http_pfx/$group", "$tmpdir/dumb.git"),
69 0, 'clone successful');
72 my $cfg = "$home/.public-inbox/config";
73 open my $fh, '>>', $cfg or xbail "open: $!";
74 print $fh <<EOM or xbail "print $!";
75 [publicinbox "test-2"]
76 inboxdir = $i2->{inboxdir}
77 address = test-2\@example.com
78 url = https://example.com/test-2
80 close $fh or xbail "close $!";
81 $td->kill('HUP') or BAIL_OUT "failed to kill -httpd: $!";
82 tick; # wait for HUP to take effect
84 my $c2 = tcp_connect($sock);
85 $c2->write("GET /test-2/qp\@example.com/raw HTTP/1.0\r\n\r\n")
86 or xbail "c2 write: $!";
90 like($buf, qr!\AHTTP/1\.0 200\b!s, 'got 200 after reload for test-2');
92 ok($td->kill, 'killed httpd');
95 is(xsys('git', "--git-dir=$tmpdir/clone.git",
96 qw(fsck --no-verbose)), 0,
97 'fsck on cloned directory successful');
101 skip 'TCP_DEFER_ACCEPT is Linux-only', 1 if $^O ne 'linux';
102 my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
103 defined(my $x = getsockopt($sock, IPPROTO_TCP, $var)) or die;
104 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set');
107 skip 'SO_ACCEPTFILTER is FreeBSD-only', 1 if $^O ne 'freebsd';
108 if (system('kldstat -m accf_http >/dev/null') != 0) {
109 skip 'accf_http not loaded: kldload accf_http', 1;
111 require PublicInbox::Daemon;
112 ok(defined($PublicInbox::Daemon::SO_ACCEPTFILTER),
113 'SO_ACCEPTFILTER defined');
114 my $x = getsockopt($sock, SOL_SOCKET,
115 $PublicInbox::Daemon::SO_ACCEPTFILTER);
116 like($x, qr/\Ahttpready\0+\z/, 'got httpready accf for HTTP');