Container-Builder
view release on metacpan or search on metacpan
examples/fatpacked.plackup view on Meta::CPAN
=over 4
=item authenticator
A callback function that takes username, password and PSGI environment
supplied and returns whether the authentication succeeds. Required.
Authenticator can also be an object that responds to C<authenticate>
method that takes username and password and returns boolean, so
backends for L<Authen::Simple> is perfect to use:
use Authen::Simple::LDAP;
enable "Auth::Basic", authenticator => Authen::Simple::LDAP->new(...);
=item realm
Realm name to display in the basic authentication dialog. Defaults to I<restricted area>.
=back
=head1 LIMITATIONS
This middleware expects that the application has a full access to the
headers sent by clients in PSGI environment. That is normally the case
with standalone Perl PSGI web servers such as L<Starman> or
L<HTTP::Server::Simple::PSGI>.
However, in a web server configuration where you can't achieve this
(i.e. using your application via Apache's mod_cgi), this middleware
does not work since your application can't know the value of
C<Authorization:> header.
If you use Apache as a web server and CGI to run your PSGI
application, you can either a) compile Apache with
C<-DSECURITY_HOLE_PASS_AUTHORIZATION> option, or b) use mod_rewrite to
pass the Authorization header to the application with the rewrite rule
like following.
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
=head1 AUTHOR
Tatsuhiko Miyagawa
=head1 SEE ALSO
L<Plack>
=cut
PLACK_MIDDLEWARE_AUTH_BASIC
$fatpacked{"Plack/Middleware/BufferedStreaming.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'PLACK_MIDDLEWARE_BUFFEREDSTREAMING';
package Plack::Middleware::BufferedStreaming;
use strict;
no warnings;
use Carp;
use Plack::Util;
use Plack::Util::Accessor qw(force);
use Scalar::Util qw(weaken);
use parent qw(Plack::Middleware);
sub call {
my ( $self, $env ) = @_;
my $caller_supports_streaming = $env->{'psgi.streaming'};
$env->{'psgi.streaming'} = Plack::Util::TRUE;
my $res = $self->app->($env);
return $res if $caller_supports_streaming && !$self->force;
if ( ref($res) eq 'CODE' ) {
my $ret;
$res->(sub {
my $write = shift;
if ( @$write == 2 ) {
my @body;
$ret = [ @$write, \@body ];
return Plack::Util::inline_object(
write => sub { push @body, $_[0] },
close => sub { },
);
} else {
$ret = $write;
return;
}
});
return $ret;
} else {
return $res;
}
}
1;
__END__
=head1 NAME
Plack::Middleware::BufferedStreaming - Enable buffering for non-streaming aware servers
=head1 SYNOPSIS
enable "BufferedStreaming";
=head1 DESCRIPTION
Plack::Middleware::BufferedStreaming is a PSGI middleware component
that wraps the application that uses C<psgi.streaming> interface to
run on the servers that do not support the interface, by buffering the
writer output to a temporary buffer.
This middleware doesn't do anything and bypass the application if the
server supports C<psgi.streaming> interface, unless you set C<force>
option (see below).
examples/fatpacked.plackup view on Meta::CPAN
=head1 SEE ALSO
L<HTTP::Headers>
=cut
PLACK_MIDDLEWARE_REARRANGEHEADERS
$fatpacked{"Plack/Middleware/Recursive.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'PLACK_MIDDLEWARE_RECURSIVE';
package Plack::Middleware::Recursive;
use strict;
use parent qw(Plack::Middleware);
use Try::Tiny;
use Scalar::Util qw(blessed);
open my $null_io, "<", \"";
sub call {
my($self, $env) = @_;
$env->{'plack.recursive.include'} = $self->recurse_callback($env, 1);
my $res = try {
$self->app->($env);
} catch {
if (blessed $_ && $_->isa('Plack::Recursive::ForwardRequest')) {
return $self->recurse_callback($env)->($_->path);
} else {
die $_; # rethrow
}
};
return $res if ref $res eq 'ARRAY';
return sub {
my $respond = shift;
my $writer;
try {
$res->(sub { return $writer = $respond->(@_) });
} catch {
if (!$writer && blessed $_ && $_->isa('Plack::Recursive::ForwardRequest')) {
$res = $self->recurse_callback($env)->($_->path);
return ref $res eq 'CODE' ? $res->($respond) : $respond->($res);
} else {
die $_;
}
};
};
}
sub recurse_callback {
my($self, $env, $include) = @_;
my $old_path_info = $env->{PATH_INFO};
return sub {
my $new_path_info = shift;
my($path, $query) = split /\?/, $new_path_info, 2;
Scalar::Util::weaken($env);
$env->{PATH_INFO} = $path;
$env->{QUERY_STRING} = $query;
$env->{REQUEST_METHOD} = 'GET';
$env->{CONTENT_LENGTH} = 0;
$env->{CONTENT_TYPE} = '';
$env->{'psgi.input'} = $null_io;
push @{$env->{'plack.recursive.old_path_info'}}, $old_path_info;
$include ? $self->app->($env) : $self->call($env);
};
}
package Plack::Recursive::ForwardRequest;
use overload q("") => \&as_string, fallback => 1;
sub new {
my($class, $path) = @_;
bless { path => $path }, $class;
}
sub path { $_[0]->{path} }
sub throw {
my($class, @args) = @_;
die $class->new(@args);
}
sub as_string {
my $self = shift;
return "Forwarding to $self->{path}: Your application should be wrapped with Plack::Middleware::Recursive.";
}
package Plack::Middleware::Recursive;
1;
__END__
=head1 NAME
Plack::Middleware::Recursive - Allows PSGI apps to include or forward requests recursively
=head1 SYNOPSIS
# with Builder
enable "Recursive";
# in apps
my $res = $env->{'plack.recursive.include'}->("/new_path");
# Or, use exceptions
my $app = sub {
# ...
Plack::Recursive::ForwardRequest->throw("/new_path");
};
=head1 DESCRIPTION
Plack::Middleware::Recursive allows PSGI applications to recursively
examples/fatpacked.plackup view on Meta::CPAN
}
1;
__END__
=head1 NAME
Plack::Middleware::SimpleContentFilter - Filters response content
=head1 SYNOPSIS
use Plack::Builder;
my $app = sub {
return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
};
builder {
enable "Plack::Middleware::SimpleContentFilter",
filter => sub { s/Foo/Bar/g; };
$app;
};
=head1 DESCRIPTION
B<This middleware should be considered as a demo. Running this against
your application might break your HTML unless you code the filter
callback carefully>.
Plack::Middleware::SimpleContentFilter is a simple content text filter
to run against response body. This middleware is only enabled against
responses with C<text/*> Content-Type.
=head1 AUTHOR
Tatsuhiko Miyagawa
=cut
PLACK_MIDDLEWARE_SIMPLECONTENTFILTER
$fatpacked{"Plack/Middleware/SimpleLogger.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'PLACK_MIDDLEWARE_SIMPLELOGGER';
package Plack::Middleware::SimpleLogger;
use strict;
use parent qw(Plack::Middleware);
use Config ();
use Plack::Util::Accessor qw(level);
use POSIX ();
use Scalar::Util ();
# Should this be in Plack::Util?
my $i = 0;
my %level_numbers = map { $_ => $i++ } qw(debug info warn error fatal);
sub call {
my($self, $env) = @_;
my $min = $level_numbers{ $self->level || "debug" };
my $env_ref = $env;
Scalar::Util::weaken($env_ref);
$env->{'psgix.logger'} = sub {
my $args = shift;
if ($level_numbers{$args->{level}} >= $min) {
$env_ref->{'psgi.errors'}->print($self->format_message($args->{level}, $args->{message}));
}
};
$self->app->($env);
}
sub format_time {
my $old_locale;
if ( $Config::config{d_setlocale} ) {
$old_locale = POSIX::setlocale(&POSIX::LC_ALL);
POSIX::setlocale(&POSIX::LC_ALL, 'C');
}
my $out = POSIX::strftime(@_);
if ( $Config::config{d_setlocale} ) {
POSIX::setlocale(&POSIX::LC_ALL, $old_locale);
};
return $out;
}
sub format_message {
my($self, $level, $message) = @_;
my $time = format_time("%Y-%m-%dT%H:%M:%S", localtime);
sprintf "%s [%s #%d] %s: %s\n", uc substr($level, 0, 1), $time, $$, uc $level, $message;
}
1;
__END__
=head1 NAME
Plack::Middleware::SimpleLogger - Simple logger that prints to psgi.errors
=head1 SYNOPSIS
enable "SimpleLogger", level => "warn";
=head1 DESCRIPTION
SimpleLogger is a middleware component that formats the log message
with information such as the time and PID and prints them to
I<psgi.errors> stream, which is mostly STDERR or server log output.
=head1 SEE ALSO
L<Plack::Middleware::LogErrors>, essentially the opposite of this module
=head1 AUTHOR
Tatsuhiko Miyagawa
=cut
PLACK_MIDDLEWARE_SIMPLELOGGER
( run in 2.059 seconds using v1.01-cache-2.11-cpan-600a1bdf6e4 )