<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Plack::Handler::CGI;
use strict;
use warnings;
use IO::Handle;

# copied from HTTP::Status
my %StatusCode = (
    100 =&gt; 'Continue',
    101 =&gt; 'Switching Protocols',
    102 =&gt; 'Processing',                      # RFC 2518 (WebDAV)
    103 =&gt; 'Early Hints',
    200 =&gt; 'OK',
    201 =&gt; 'Created',
    202 =&gt; 'Accepted',
    203 =&gt; 'Non-Authoritative Information',
    204 =&gt; 'No Content',
    205 =&gt; 'Reset Content',
    206 =&gt; 'Partial Content',
    207 =&gt; 'Multi-Status',                    # RFC 2518 (WebDAV)
    300 =&gt; 'Multiple Choices',
    301 =&gt; 'Moved Permanently',
    302 =&gt; 'Found',
    303 =&gt; 'See Other',
    304 =&gt; 'Not Modified',
    305 =&gt; 'Use Proxy',
    307 =&gt; 'Temporary Redirect',
    400 =&gt; 'Bad Request',
    401 =&gt; 'Unauthorized',
    402 =&gt; 'Payment Required',
    403 =&gt; 'Forbidden',
    404 =&gt; 'Not Found',
    405 =&gt; 'Method Not Allowed',
    406 =&gt; 'Not Acceptable',
    407 =&gt; 'Proxy Authentication Required',
    408 =&gt; 'Request Timeout',
    409 =&gt; 'Conflict',
    410 =&gt; 'Gone',
    411 =&gt; 'Length Required',
    412 =&gt; 'Precondition Failed',
    413 =&gt; 'Request Entity Too Large',
    414 =&gt; 'Request-URI Too Large',
    415 =&gt; 'Unsupported Media Type',
    416 =&gt; 'Request Range Not Satisfiable',
    417 =&gt; 'Expectation Failed',
    422 =&gt; 'Unprocessable Entity',            # RFC 2518 (WebDAV)
    423 =&gt; 'Locked',                          # RFC 2518 (WebDAV)
    424 =&gt; 'Failed Dependency',               # RFC 2518 (WebDAV)
    425 =&gt; 'No code',                         # WebDAV Advanced Collections
    426 =&gt; 'Upgrade Required',                # RFC 2817
    449 =&gt; 'Retry with',                      # unofficial Microsoft
    500 =&gt; 'Internal Server Error',
    501 =&gt; 'Not Implemented',
    502 =&gt; 'Bad Gateway',
    503 =&gt; 'Service Unavailable',
    504 =&gt; 'Gateway Timeout',
    505 =&gt; 'HTTP Version Not Supported',
    506 =&gt; 'Variant Also Negotiates',         # RFC 2295
    507 =&gt; 'Insufficient Storage',            # RFC 2518 (WebDAV)
    509 =&gt; 'Bandwidth Limit Exceeded',        # unofficial
    510 =&gt; 'Not Extended',                    # RFC 2774
);

sub new { bless {}, shift }

sub run {
    my ($self, $app) = @_;

    my $env = $self-&gt;setup_env();

    my $res = $app-&gt;($env);
    if (ref $res eq 'ARRAY') {
        $self-&gt;_handle_response($res);
    }
    elsif (ref $res eq 'CODE') {
        $res-&gt;(sub {
            $self-&gt;_handle_response($_[0]);
        });
    }
    else {
        die "Bad response $res";
    }
}

sub setup_env {
    my ( $self, $override_env ) = @_;

    $override_env ||= {};

    binmode STDIN;
    binmode STDERR;

    my $env = {
        %ENV,
        'psgi.version'    =&gt; [ 1, 1 ],
        'psgi.url_scheme' =&gt; ($ENV{HTTPS}||'off') =~ /^(?:on|1)$/i ? 'https' : 'http',
        'psgi.input'      =&gt; *STDIN,
        'psgi.errors'     =&gt; *STDERR,
        'psgi.multithread'  =&gt; 0,
        'psgi.multiprocess' =&gt; 1,
        'psgi.run_once'     =&gt; 1,
        'psgi.streaming'    =&gt; 1,
        'psgi.nonblocking'  =&gt; 1,
        %{ $override_env },
    };

    delete $env-&gt;{HTTP_CONTENT_TYPE};
    delete $env-&gt;{HTTP_CONTENT_LENGTH};
    $env-&gt;{'HTTP_COOKIE'} ||= $ENV{COOKIE}; # O'Reilly server bug

    if (!exists $env-&gt;{PATH_INFO}) {
        $env-&gt;{PATH_INFO} = '';
    }

    if ($env-&gt;{SCRIPT_NAME} eq '/') {
        $env-&gt;{SCRIPT_NAME} = '';
        $env-&gt;{PATH_INFO}   = '/' . $env-&gt;{PATH_INFO};
    }

    return $env;
}



sub _handle_response {
    my ($self, $res) = @_;

    *STDOUT-&gt;autoflush(1);
    binmode STDOUT;

    my $hdrs;
    my $message = $StatusCode{$res-&gt;[0]};
    $hdrs = "Status: $res-&gt;[0] $message\015\012";

    my $headers = $res-&gt;[1];
    while (my ($k, $v) = splice(@$headers, 0, 2)) {
        $hdrs .= "$k: $v\015\012";
    }
    $hdrs .= "\015\012";

    print STDOUT $hdrs;

    my $body = $res-&gt;[2];
    my $cb = sub { print STDOUT $_[0] };

    # inline Plack::Util::foreach here
    if (ref $body eq 'ARRAY') {
        for my $line (@$body) {
            $cb-&gt;($line) if length $line;
        }
    }
    elsif (defined $body) {
        local $/ = \65536 unless ref $/;
        while (defined(my $line = $body-&gt;getline)) {
            $cb-&gt;($line) if length $line;
        }
        $body-&gt;close;
    }
    else {
        return Plack::Handler::CGI::Writer-&gt;new;
    }
}

package Plack::Handler::CGI::Writer;
sub new   { bless \do { my $x }, $_[0] }
sub write { print STDOUT $_[1] }
sub close { }

package Plack::Handler::CGI;

1;
__END__

=head1 NAME

Plack::Handler::CGI - CGI handler for Plack

=head1 SYNOPSIS

Want to run PSGI application as a CGI script? Rename .psgi to .cgi and
change the shebang line like:

  #!/usr/bin/env plackup
  # rest of the file can be the same as other .psgi file

You can alternatively create a .cgi file that contains something like:

  #!/usr/bin/perl
  use Plack::Loader;
  my $app = Plack::Util::load_psgi("/path/to/app.psgi");
  Plack::Loader-&gt;auto-&gt;run($app);

This will auto-recognize the CGI environment variable to load this class.

If you really want to explicitly load the CGI handler, you can. For instance
you might do this when you want to embed a PSGI application server built into
CGI-compatible perl-based web server:

  use Plack::Handler::CGI;
  Plack::Handler::CGI-&gt;new-&gt;run($app);

=head1 DESCRIPTION

This is a handler module to run any PSGI application as a CGI script.

=head1 UTILITY METHODS

=head2 setup_env()

  my $env = Plack::Handler::CGI-&gt;setup_env();
  my $env = Plack::Handler::CGI-&gt;setup_env(\%override_env);

Sets up the PSGI environment hash for a CGI request from C&lt;&lt; %ENV &gt;&gt;&gt; and returns it.
You can provide a hashref of key/value pairs to override the defaults if you would like.

=head1 SEE ALSO

L&lt;Plack&gt;

=cut


</pre></body></html>