1 # Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 foreach my $mod (qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status)) {
9 plan skip_all => "$mod missing for httpd.t" if $@;
11 use Socket qw(IPPROTO_TCP SOL_SOCKET);
12 require './t/common.perl';
14 # FIXME: too much setup
15 my ($tmpdir, $for_destroy) = tmpdir();
16 my $home = "$tmpdir/pi-home";
17 my $err = "$tmpdir/stderr.log";
18 my $out = "$tmpdir/stdout.log";
19 my $maindir = "$tmpdir/main.git";
20 my $group = 'test-httpd';
21 my $addr = $group . '@example.com';
22 my $cfgpfx = "publicinbox.$group";
23 my $sock = tcp_server();
25 use_ok 'PublicInbox::Git';
26 use_ok 'PublicInbox::Import';
29 local $ENV{HOME} = $home;
30 my $cmd = [ '-init', $group, $maindir, 'http://example.com/', $addr ];
31 ok(run_script($cmd), 'init ran properly');
33 # ensure successful message delivery
35 my $mime = Email::MIME->new(<<EOF);
36 From: Me <me\@example.com>
37 To: You <you\@example.com>
39 Message-Id: <nntp\@example.com>
41 Date: Thu, 01 Jan 1970 06:06:06 +0000
46 my $git = PublicInbox::Git->new($maindir);
47 my $im = PublicInbox::Import->new($git, 'test', $addr);
51 ok($sock, 'sock created');
52 $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err" ];
53 $td = start_script($cmd, undef, { 3 => $sock });
54 my $host = $sock->sockhost;
55 my $port = $sock->sockport;
56 my $conn = tcp_connect($sock);
57 ok($conn, 'connected');
58 ok($conn->write("GET / HTTP/1.0\r\n\r\n"), 'wrote data to socket');
61 ok($conn->read($buf, 4096), 'read some bytes');
62 like($buf, qr!\AHTTP/1\.[01] 404\b!, 'got 404 response');
63 is($conn->read($buf, 1), 0, "EOF");
66 is(system(qw(git clone -q --mirror),
67 "http://$host:$port/$group", "$tmpdir/clone.git"),
68 0, 'smart clone successful');
70 # ensure dumb cloning works, too:
71 is(system('git', "--git-dir=$maindir",
72 qw(config http.uploadpack false)),
73 0, 'disable http.uploadpack');
74 is(system(qw(git clone -q --mirror),
75 "http://$host:$port/$group", "$tmpdir/dumb.git"),
76 0, 'clone successful');
78 ok($td->kill, 'killed httpd');
81 is(system('git', "--git-dir=$tmpdir/clone.git",
82 qw(fsck --no-verbose)), 0,
83 'fsck on cloned directory successful');
87 skip 'TCP_DEFER_ACCEPT is Linux-only', 1 if $^O ne 'linux';
88 my $var = Socket::TCP_DEFER_ACCEPT();
89 defined(my $x = getsockopt($sock, IPPROTO_TCP, $var)) or die;
90 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set');
93 skip 'SO_ACCEPTFILTER is FreeBSD-only', 1 if $^O ne 'freebsd';
94 if (system('kldstat -m accf_http >/dev/null') != 0) {
95 skip 'accf_http not loaded: kldload accf_http', 1;
97 require PublicInbox::Daemon;
98 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
99 my $x = getsockopt($sock, SOL_SOCKET, $var);
100 like($x, qr/\Ahttpready\0+\z/, 'got httpready accf for HTTP');