]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-corner.t
initial public-inbox-httpd implemenation
[public-inbox.git] / t / httpd-corner.t
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # note: our HTTP server should be standalone and capable of running
4 # generic Rack apps.
5 use strict;
6 use warnings;
7 use Test::More;
8
9 foreach my $mod (qw(Plack::Util Plack::Request Plack::Builder Danga::Socket
10                         HTTP::Parser::XS HTTP::Date HTTP::Status)) {
11         eval "require $mod";
12         plan skip_all => "$mod missing for httpd-corner.t" if $@;
13 }
14
15 use Digest::SHA qw(sha1_hex);
16 use File::Temp qw/tempdir/;
17 use Cwd qw/getcwd/;
18 use IO::Socket;
19 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
20 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
21 my $tmpdir = tempdir(CLEANUP => 1);
22 my $err = "$tmpdir/stderr.log";
23 my $out = "$tmpdir/stdout.log";
24 my $httpd = 'blib/script/public-inbox-httpd';
25 my $psgi = getcwd()."/t/httpd-corner.psgi";
26 my %opts = (
27         LocalAddr => '127.0.0.1',
28         ReuseAddr => 1,
29         Proto => 'tcp',
30         Type => SOCK_STREAM,
31         Listen => 1024,
32 );
33 my $sock = IO::Socket::INET->new(%opts);
34 my $pid;
35 END { kill 'TERM', $pid if defined $pid };
36 {
37         ok($sock, 'sock created');
38         $! = 0;
39         my $fl = fcntl($sock, F_GETFD, 0);
40         ok(! $!, 'no error from fcntl(F_GETFD)');
41         is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
42         $pid = fork;
43         if ($pid == 0) {
44                 use POSIX qw(dup2);
45                 # pretend to be systemd
46                 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
47                 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
48                 $ENV{LISTEN_PID} = $$;
49                 $ENV{LISTEN_FDS} = 1;
50                 exec $httpd, '-W0', "--stdout=$out", "--stderr=$err", $psgi;
51                 die "FAIL: $!\n";
52         }
53         ok(defined $pid, 'forked httpd process successfully');
54         $! = 0;
55         fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
56         ok(! $!, 'no error from fcntl(F_SETFD)');
57 }
58
59 sub conn_for {
60         my ($sock, $msg) = @_;
61         my $conn = IO::Socket::INET->new(
62                                 PeerAddr => $sock->sockhost,
63                                 PeerPort => $sock->sockport,
64                                 Proto => 'tcp',
65                                 Type => SOCK_STREAM);
66         ok($conn, "connected for $msg");
67         $conn->autoflush(1);
68         setsockopt($conn, IPPROTO_TCP, TCP_NODELAY, 1);
69         return $conn;
70 }
71
72 sub delay { select(undef, undef, undef, shift || rand(0.02)) }
73
74 my $str = 'abcdefghijklmnopqrstuvwxyz';
75 my $len = length $str;
76 is($len, 26, 'got the alphabet');
77 my $check_self = sub {
78         my ($conn) = @_;
79         $conn->read(my $buf, 4096);
80         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
81         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
82         is($body, sha1_hex($str), 'read expected body');
83 };
84
85 {
86         my $conn = conn_for($sock, '1.1 pipeline together');
87         $conn->write("PUT /sha1 HTTP/1.1\r\nUser-agent: hello\r\n\r\n" .
88                         "PUT /sha1 HTTP/1.1\r\n\r\n");
89         my $buf = '';
90         my @r;
91         until (scalar(@r) >= 2) {
92                 my $r = $conn->sysread(my $tmp, 4096);
93                 die $! unless defined $r;
94                 die "EOF <$buf>" unless $r;
95                 $buf .= $tmp;
96                 @r = ($buf =~ /\r\n\r\n([a-f0-9]{40})/g);
97         }
98         is(2, scalar @r, 'got 2 responses');
99         my $i = 3;
100         foreach my $hex (@r) {
101                 is($hex, sha1_hex(''), "read expected body $i");
102                 $i++;
103         }
104 }
105
106 # various DoS attacks against the chunk parser:
107 {
108         local $SIG{PIPE} = 'IGNORE';
109         my $conn = conn_for($sock, '1.1 chunk header excessive');
110         $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding:chunked\r\n\r\n");
111         my $n = 0;
112         my $w;
113         while ($w = $conn->write('ffffffff')) {
114                 $n += $w;
115         }
116         ok($!, 'got error set in $!');
117         is($w, undef, 'write error happened');
118         ok($n > 0, 'was able to write');
119         my $r = $conn->read(my $buf, 66666);
120         ok($r > 0, 'got non-empty response');
121         like($buf, qr!HTTP/1\.\d 400 !, 'got 400 response');
122
123         $conn = conn_for($sock, '1.1 chunk trailer excessive');
124         $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding:chunked\r\n\r\n");
125         is($conn->syswrite("1\r\na"), 4, 'wrote first header + chunk');
126         delay();
127         $n = 0;
128         while ($w = $conn->write("\r")) {
129                 $n += $w;
130         }
131         ok($!, 'got error set in $!');
132         ok($n > 0, 'wrote part of chunk end (\r)');
133         $r = $conn->read($buf, 66666);
134         ok($r > 0, 'got non-empty response');
135         like($buf, qr!HTTP/1\.\d 400 !, 'got 400 response');
136 }
137
138 {
139         my $conn = conn_for($sock, '1.1 chunked close trickle');
140         $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
141         $conn->write("Transfer-encoding: chunked\r\n\r\n");
142         foreach my $x ('a'..'z') {
143                 delay();
144                 $conn->write('1');
145                 delay();
146                 $conn->write("\r");
147                 delay();
148                 $conn->write("\n");
149                 delay();
150                 $conn->write($x);
151                 delay();
152                 $conn->write("\r");
153                 delay();
154                 $conn->write("\n");
155         }
156         $conn->write('0');
157         delay();
158         $conn->write("\r");
159         delay();
160         $conn->write("\n");
161         delay();
162         $conn->write("\r");
163         delay();
164         $conn->write("\n");
165         delay();
166         $check_self->($conn);
167 }
168
169 {
170         my $conn = conn_for($sock, '1.1 chunked close');
171         $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
172         my $xlen = sprintf('%x', $len);
173         $conn->write("Transfer-Encoding: chunked\r\n\r\n$xlen\r\n" .
174                 "$str\r\n0\r\n\r\n");
175         $check_self->($conn);
176 }
177
178 {
179         my $conn = conn_for($sock, 'chunked body + pipeline');
180         $conn->write("PUT /sha1 HTTP/1.1\r\n" .
181                         "Transfer-Encoding: chunked\r\n");
182         delay();
183         $conn->write("\r\n1\r\n");
184         delay();
185         $conn->write('a');
186         delay();
187         $conn->write("\r\n0\r\n\r\nPUT /sha1 HTTP/1.1\r\n");
188         delay();
189
190         my $buf = '';
191         until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
192                 $conn->sysread(my $tmp, 4096);
193                 $buf .= $tmp;
194         }
195         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
196         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
197         is($body, sha1_hex('a'), 'read expected body');
198
199         $conn->write("Connection: close\r\n");
200         $conn->write("Content-Length: $len\r\n\r\n$str");
201         $check_self->($conn);
202 }
203
204 {
205         my $conn = conn_for($sock, 'trickle header, one-shot body + pipeline');
206         $conn->write("PUT /sha1 HTTP/1.0\r\n" .
207                         "Connection: keep-alive\r\n");
208         delay();
209         $conn->write("Content-Length: $len\r\n\r\n${str}PUT");
210         my $buf = '';
211         until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
212                 $conn->sysread(my $tmp, 4096);
213                 $buf .= $tmp;
214         }
215         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
216         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
217         is($body, sha1_hex($str), 'read expected body');
218
219         $conn->write(" /sha1 HTTP/1.0\r\nContent-Length: $len\r\n\r\n$str");
220         $check_self->($conn);
221 }
222
223 {
224         my $conn = conn_for($sock, 'trickle body');
225         $conn->write("PUT /sha1 HTTP/1.0\r\n");
226         $conn->write("Content-Length: $len\r\n\r\n");
227         my $beg = substr($str, 0, 10);
228         my $end = substr($str, 10);
229         is($beg . $end, $str, 'substr setup correct');
230         delay();
231         $conn->write($beg);
232         delay();
233         $conn->write($end);
234         $check_self->($conn);
235 }
236
237 {
238         my $conn = conn_for($sock, 'one-shot write');
239         $conn->write("PUT /sha1 HTTP/1.0\r\n" .
240                         "Content-Length: $len\r\n\r\n$str");
241         $check_self->($conn);
242 }
243
244 {
245         my $conn = conn_for($sock, 'trickle header, one-shot body');
246         $conn->write("PUT /sha1 HTTP/1.0\r\n");
247         delay();
248         $conn->write("Content-Length: $len\r\n\r\n$str");
249         $check_self->($conn);
250 }
251
252 {
253         my $conn = conn_for($sock, '1.1 Connnection: close');
254         $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
255         delay();
256         $conn->write("Content-Length: $len\r\n\r\n$str");
257         $check_self->($conn);
258 }
259
260 {
261         my $conn = conn_for($sock, '1.1 pipeline start');
262         $conn->write("PUT /sha1 HTTP/1.1\r\n\r\nPUT");
263         my $buf = '';
264         until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
265                 $conn->sysread(my $tmp, 4096);
266                 $buf .= $tmp;
267         }
268         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
269         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
270         is($body, sha1_hex(''), 'read expected body');
271
272         # 2nd request
273         $conn->write(" /sha1 HTTP/1.1\r\n\r\n");
274         $buf = '';
275         until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
276                 $conn->sysread(my $tmp, 4096);
277                 $buf .= $tmp;
278         }
279         ($head, $body) = split(/\r\n\r\n/, $buf, 2);
280         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
281         is($body, sha1_hex(''), 'read expected body #2');
282 }
283
284 done_testing();
285
286 1;