forked from plack/Plack
/
CGI.pm
65 lines (51 loc) · 1.53 KB
/
CGI.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package Plack::Server::CGI;
use strict;
use warnings;
use IO::Handle;
use Plack::Util;
sub new { bless {}, shift }
sub run {
my ($self, $app) = @_;
my %env;
while (my ($k, $v) = each %ENV) {
next unless $k =~ qr/^(?:REQUEST_METHOD|SCRIPT_NAME|PATH_INFO|QUERY_STRING|SERVER_NAME|SERVER_PORT|SERVER_PROTOCOL|CONTENT_LENGTH|CONTENT_TYPE|REMOTE_ADDR|REQUEST_URI)$|^HTTP_/;
$env{$k} = $v;
}
$env{'HTTP_COOKIE'} ||= $ENV{COOKIE};
$env{'psgi.version'} = [ 1, 0 ];
$env{'psgi.url_scheme'} = ($ENV{HTTPS}||'off') =~ /^(?:on|1)$/i ? 'https' : 'http';
$env{'psgi.input'} = *STDIN;
$env{'psgi.errors'} = *STDERR;
$env{'psgi.multithread'} = Plack::Util::FALSE;
$env{'psgi.multiprocess'} = Plack::Util::TRUE;
$env{'psgi.run_once'} = Plack::Util::TRUE;
my $res = $app->(\%env);
print "Status: $res->[0]\n";
my $headers = $res->[1];
while (my ($k, $v) = splice(@$headers, 0, 2)) {
print "$k: $v\n";
}
print "\n";
my $body = $res->[2];
my $cb = sub { print STDOUT $_[0] };
Plack::Util::foreach($body, $cb);
}
1;
__END__
=head1 SYNOPSIS
## in your .cgi
#!/usr/bin/perl
use Plack::Server::CGI;
# or Plack::Util::load_psgi("/path/to/app.psgi");
my $app = sub {
my $env = shift;
return [
200,
[ 'Content-Type' => 'text/plain', 'Content-Length' => 13 ],
'Hello, world!',
];
};
Plack::Server::CGI->new->run($app);
=head1 SEE ALSO
L<Plack::Server::Base>
=cut