]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-corner.psgi
5fab2ba48d1e39e05cc6bcff9c0005673661cb2f
[public-inbox.git] / t / httpd-corner.psgi
1 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # corner case tests for the generic PSGI server
4 # Usage: plackup [OPTIONS] /path/to/this/file
5 use strict;
6 use warnings;
7 use Plack::Builder;
8 require Digest::SHA;
9 my $app = sub {
10         my ($env) = @_;
11         my $path = $env->{PATH_INFO};
12         my $in = $env->{'psgi.input'};
13         my $actual = -s $in;
14         my $code = 500;
15         my $h = [ 'Content-Type' => 'text/plain' ];
16         my $body = [];
17         if ($path eq '/sha1') {
18                 my $sha1 = Digest::SHA->new('SHA-1');
19                 my $buf;
20                 while (1) {
21                         my $r = $in->read($buf, 4096);
22                         die "read err: $!" unless defined $r;
23                         last if $r == 0;
24                         $sha1->add($buf);
25                 }
26                 $code = 200;
27                 push @$body, $sha1->hexdigest;
28         } elsif (my $fifo = $env->{HTTP_X_CHECK_FIFO}) {
29                 if ($path eq '/slow-header') {
30                         return sub {
31                                 open my $f, '<', $fifo or
32                                                 die "open $fifo: $!\n";
33                                 local $/ = "\n";
34                                 my @r = <$f>;
35                                 $_[0]->([200, $h, \@r ]);
36                         };
37                 } elsif ($path eq '/slow-body') {
38                         return sub {
39                                 my $fh = $_[0]->([200, $h]);
40                                 open my $f, '<', $fifo or
41                                                 die "open $fifo: $!\n";
42                                 local $/ = "\n";
43                                 while (defined(my $l = <$f>)) {
44                                         $fh->write($l);
45                                 }
46                                 $fh->close;
47                         };
48                 }
49         } elsif ($path eq '/host-port') {
50                 $code = 200;
51                 push @$body, "$env->{REMOTE_ADDR} $env->{REMOTE_PORT}";
52         } elsif ($path eq '/callback') {
53                 return sub {
54                         my ($res) = @_;
55                         my $buf = "hello world\n";
56                         push @$h, 'Content-Length', length($buf);
57                         my $fh = $res->([200, $h]);
58                         $fh->write($buf);
59                         $fh->close;
60                 }
61         } elsif ($path eq '/empty') {
62                 $code = 200;
63         } elsif ($path eq '/getline-die') {
64                 $code = 200;
65                 $body = Plack::Util::inline_object(
66                         getline => sub { die 'GETLINE FAIL' },
67                         close => sub { die 'CLOSE FAIL' },
68                 );
69         } elsif ($path eq '/close-die') {
70                 $code = 200;
71                 $body = Plack::Util::inline_object(
72                         getline => sub { undef },
73                         close => sub { die 'CLOSE FAIL' },
74                 );
75         } elsif ($path eq '/async-big') {
76                 require PublicInbox::Qspawn;
77                 open my $null, '>', '/dev/null' or die;
78                 my $rdr = { 2 => fileno($null) };
79                 my $cmd = [qw(dd if=/dev/zero count=30 bs=1024k)];
80                 my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
81                 return $qsp->psgi_return($env, undef, sub {
82                         my ($r, $bref) = @_;
83                         # make $rd_hdr retry sysread + $parse_hdr in Qspawn:
84                         return until length($$bref) > 8000;
85                         close $null;
86                         [ 200, [ qw(Content-Type application/octet-stream) ]];
87                 });
88         } elsif ($path eq '/psgi-return-gzip') {
89                 require PublicInbox::Qspawn;
90                 require PublicInbox::GzipFilter;
91                 my $cmd = [qw(echo hello world)];
92                 my $qsp = PublicInbox::Qspawn->new($cmd);
93                 $env->{'qspawn.filter'} = PublicInbox::GzipFilter->new;
94                 return $qsp->psgi_return($env, undef, sub {
95                         [ 200, [ qw(Content-Type application/octet-stream)]]
96                 });
97         } elsif ($path eq '/psgi-return-compressible') {
98                 require PublicInbox::Qspawn;
99                 my $cmd = [qw(echo goodbye world)];
100                 my $qsp = PublicInbox::Qspawn->new($cmd);
101                 return $qsp->psgi_return($env, undef, sub {
102                         [200, [qw(Content-Type text/plain)]]
103                 });
104         } elsif ($path eq '/psgi-return-enoent') {
105                 require PublicInbox::Qspawn;
106                 my $cmd = [ 'this-better-not-exist-in-PATH'.rand ];
107                 my $qsp = PublicInbox::Qspawn->new($cmd);
108                 return $qsp->psgi_return($env, undef, sub {
109                         [ 200, [ qw(Content-Type application/octet-stream)]]
110                 });
111         } elsif ($path eq '/pid') {
112                 $code = 200;
113                 push @$body, "$$\n";
114         }
115
116         [ $code, $h, $body ]
117 };
118
119 builder {
120         enable 'ContentLength';
121         enable 'Head';
122         $app;
123 }