1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # 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 $maindir = "$tmpdir/main.git";
17 my $group = 'test-httpd';
18 my $addr = $group . '@example.com';
19 my $cfgpfx = "publicinbox.$group";
20 my $sock = tcp_server();
22 use_ok 'PublicInbox::Git';
23 use_ok 'PublicInbox::Import';
25 local $ENV{HOME} = $home;
26 my $cmd = [ '-init', $group, $maindir, 'http://example.com/', $addr ];
27 ok(run_script($cmd), 'init ran properly');
29 # ensure successful message delivery
31 my $mime = PublicInbox::Eml->new(<<EOF);
32 From: Me <me\@example.com>
33 To: You <you\@example.com>
35 Message-Id: <nntp\@example.com>
37 Date: Thu, 01 Jan 1970 06:06:06 +0000
42 my $git = PublicInbox::Git->new($maindir);
43 my $im = PublicInbox::Import->new($git, 'test', $addr);
47 ok($sock, 'sock created');
48 $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
49 $td = start_script($cmd, undef, { 3 => $sock });
50 my $host = $sock->sockhost;
51 my $port = $sock->sockport;
53 my $bad = tcp_connect($sock);
54 print $bad "GETT / HTTP/1.0\r\n\r\n" or die;
55 like(<$bad>, qr!\AHTTP/1\.[01] 405\b!, 'got 405 on bad req');
57 my $conn = tcp_connect($sock);
58 ok($conn, 'connected');
59 ok($conn->write("GET / HTTP/1.0\r\n\r\n"), 'wrote data to socket');
62 ok($conn->read($buf, 4096), 'read some bytes');
63 like($buf, qr!\AHTTP/1\.[01] 404\b!, 'got 404 response');
64 is($conn->read($buf, 1), 0, "EOF");
67 is(xsys(qw(git clone -q --mirror),
68 "http://$host:$port/$group", "$tmpdir/clone.git"),
69 0, 'smart clone successful');
71 # ensure dumb cloning works, too:
72 is(xsys('git', "--git-dir=$maindir",
73 qw(config http.uploadpack false)),
74 0, 'disable http.uploadpack');
75 is(xsys(qw(git clone -q --mirror),
76 "http://$host:$port/$group", "$tmpdir/dumb.git"),
77 0, 'clone successful');
79 ok($td->kill, 'killed httpd');
82 is(xsys('git', "--git-dir=$tmpdir/clone.git",
83 qw(fsck --no-verbose)), 0,
84 'fsck on cloned directory successful');
88 skip 'TCP_DEFER_ACCEPT is Linux-only', 1 if $^O ne 'linux';
89 my $var = eval { Socket::TCP_DEFER_ACCEPT() } // 9;
90 defined(my $x = getsockopt($sock, IPPROTO_TCP, $var)) or die;
91 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set');
94 skip 'SO_ACCEPTFILTER is FreeBSD-only', 1 if $^O ne 'freebsd';
95 if (system('kldstat -m accf_http >/dev/null') != 0) {
96 skip 'accf_http not loaded: kldload accf_http', 1;
98 require PublicInbox::Daemon;
99 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
100 my $x = getsockopt($sock, SOL_SOCKET, $var);
101 like($x, qr/\Ahttpready\0+\z/, 'got httpready accf for HTTP');