ASP4-PSGI
view release on metacpan or search on metacpan
lib/ASP4/PSGI.pm view on Meta::CPAN
package ASP4::PSGI;
use strict;
use warnings 'all';
use ASP4::SimpleCGI;
use Plack::Request;
our $VERSION = '0.004';
sub app
{
return sub {
my $env = shift;
my $preq = Plack::Request->new( $env );
local %ENV = %$env;
require ASP4::API;
my $api = ASP4::API->new();
# Parse cookies:
foreach my $cookie ( split /;\s*/, ($ENV{HTTP_COOKIE}||'') )
{
my ($k,$v) = map { ASP4::SimpleCGI->unescape($_) } split /\=/, $cookie;
$api->ua->add_cookie( $k => $v );
}# end foreach()
# Execute the request:
my $method = lc( $ENV{REQUEST_METHOD} );
my $res = do {
# Is it a GET, POST or Upload?
if( $method eq 'get' )
{
# GET
$api->ua->get( $env->{REQUEST_URI} );
}
else
{
if( $ENV{CONTENT_TYPE} =~ m{^multipart/form\-data;} )
{
# Upload:
my @pairs = $preq->parameters->flatten;
# Prepare the upload:
foreach my $up ( keys %{ $preq->uploads } )
{
my $upload = $preq->uploads->{$up};
push @pairs, $up => [
$upload->{tempname}, $upload->{filename},
'content-type' => $upload->{'content-type'}
];
}# end foreach()
# Now we can upload:
$api->ua->upload( $env->{REQUEST_URI}, \@pairs );
}
else
{
# POST:
$api->ua->post( $env->{REQUEST_URI}, [ $preq->parameters->flatten ] );
}# end if()
}# end if()
};
# Check for a 404 response. If we got one, then see if we've got a /404.asp:
my ($status) = $res->status_line =~ m{^(\d+)};
if( $status eq 404 && -f $api->config->web->www_root . '/404.asp' )
{
# Try to do the right thing:
$res = $api->ua->get( '/404.asp' );
}# end if()
# Return a PSGI-compliant response:
return [
$status,
[
%{ $res->headers }
],
[
$res->content
]
];
};
}# end app()
1;# return true:
=pod
=head1 NAME
ASP4::PSGI - Run your ASP4 web application under PSGI/Plack.
=head1 SYNOPSIS
In your C<app.psgi> file:
use ASP4::PSGI;
ASP4::PSGI->app;
( run in 0.625 second using v1.01-cache-2.11-cpan-39bf76dae61 )