2 # Copyright (C) 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);
10 require_git_http_backend;
12 # FIXME: too much setup
13 my ($tmpdir, $for_destroy) = tmpdir();
14 my $home = "$tmpdir/pi-home";
15 my $err = "$tmpdir/stderr.log";
16 my $out = "$tmpdir/stdout.log";
17 my $inboxdir = "$tmpdir/i.git";
18 my $group = 'test-httpd';
19 my $addr = $group . '@example.com';
20 my $sock = tcp_server();
23 create_inbox 'test', tmpdir => $inboxdir, sub {
25 $im->add(PublicInbox::Eml->new(<<EOF)) or BAIL_OUT;
26 From: Me <me\@example.com>
27 To: You <you\@example.com>
29 Message-Id: <nntp\@example.com>
31 Date: Thu, 01 Jan 1970 06:06:06 +0000
36 my $i2 = create_inbox 'test-2', sub {
38 $im->add(eml_load('t/plack-qp.eml')) or xbail '->add';
40 local $ENV{HOME} = $home;
41 my $cmd = [ '-init', $group, $inboxdir, 'http://example.com/', $addr ];
42 ok(run_script($cmd), 'init ran properly');
43 $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
44 $td = start_script($cmd, undef, { 3 => $sock });
45 my $http_pfx = 'http://'.tcp_host_port($sock);
47 my $bad = tcp_connect($sock);
48 print $bad "GETT / HTTP/1.0\r\n\r\n" or die;
49 like(<$bad>, qr!\AHTTP/1\.[01] 405\b!, 'got 405 on bad req');
51 my $conn = tcp_connect($sock);
52 ok($conn->write("GET / HTTP/1.0\r\n\r\n"), 'wrote data to socket');
55 ok($conn->read($buf, 4096), 'read some bytes');
56 like($buf, qr!\AHTTP/1\.[01] 404\b!, 'got 404 response');
57 is($conn->read($buf, 1), 0, "EOF");
60 is(xsys(qw(git clone -q --mirror),
61 "$http_pfx/$group", "$tmpdir/clone.git"),
62 0, 'smart clone successful');
64 # ensure dumb cloning works, too:
65 is(xsys('git', "--git-dir=$inboxdir",
66 qw(config http.uploadpack false)),
67 0, 'disable http.uploadpack');
68 is(xsys(qw(git clone -q --mirror),
69 "$http_pfx/$group", "$tmpdir/dumb.git"),
70 0, 'clone successful');
73 my $cfg = "$home/.public-inbox/config";
74 open my $fh, '>>', $cfg or xbail "open: $!";
75 print $fh <<EOM or xbail "print $!";
76 [publicinbox "test-2"]
77 inboxdir = $i2->{inboxdir}
78 address = test-2\@example.com
79 url = https://example.com/test-2
81 close $fh or xbail "close $!";
82 $td->kill('HUP') or BAIL_OUT "failed to kill -httpd: $!";
83 tick; # wait for HUP to take effect
85 my $c2 = tcp_connect($sock);
86 $c2->write("GET /test-2/qp\@example.com/raw HTTP/1.0\r\n\r\n")
87 or xbail "c2 write: $!";
91 like($buf, qr!\AHTTP/1\.0 200\b!s, 'got 200 after reload for test-2');
93 ok($td->kill, 'killed httpd');
96 is(xsys('git', "--git-dir=$tmpdir/clone.git",
97 qw(fsck --no-verbose)), 0,
98 'fsck on cloned directory successful');
102 skip 'TCP_DEFER_ACCEPT is Linux-only', 1 if $^O ne 'linux';
103 my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
104 defined(my $x = getsockopt($sock, IPPROTO_TCP, $var)) or die;
105 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set');
108 require_mods '+accf_http';
109 require PublicInbox::Daemon;
110 ok(defined($PublicInbox::Daemon::SO_ACCEPTFILTER),
111 'SO_ACCEPTFILTER defined');
112 my $x = getsockopt($sock, SOL_SOCKET,
113 $PublicInbox::Daemon::SO_ACCEPTFILTER);
114 like($x, qr/\Ahttpready\0+\z/, 'got httpready accf for HTTP');