]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-corner.psgi
localize $/ in more places to avoid potential problems
[public-inbox.git] / t / httpd-corner.psgi
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 # 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         }
64
65         [ $code, $h, $body ]
66 };
67
68 builder {
69         enable 'ContentLength';
70         enable 'Head';
71         $app;
72 }