Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuho committed Oct 1, 2009
2 parents 8f85b10 + b71452a commit 901f782
Show file tree
Hide file tree
Showing 55 changed files with 1,735 additions and 772 deletions.
6 changes: 5 additions & 1 deletion AUTHORS
@@ -1,3 +1,7 @@
Tatsuhiko Miyagawa
Kazuhiro Osawa
Tokuhiro Matsuno
Tatsuhiko Miyagawa
Kazuho Oku
Daisuke Murase
Yuval Kogman
mala
5 changes: 4 additions & 1 deletion Makefile.PL
Expand Up @@ -3,15 +3,18 @@ sub author_tests { die "You need to install Module::Install::AuthorTests and Tes
use inc::Module::Install;
name 'Plack';
all_from 'lib/Plack.pm';
readme_from 'lib/Plack.pm';

requires 'Class::Accessor::Fast';
requires 'LWP'; # HTTP::Status, HTTP::Headers and HTTP::Request
requires 'HTTP::Body';
requires 'URI';
requires 'HTTP::Request::AsCGI';
requires 'HTTP::Parser::XS', 0.02;
requires 'Pod::Usage';
requires 'CGI::Simple::Cookie'; # Plack::Response
requires 'Pod::Usage'; # plackup

requires 'CGI::ExceptionManager'; # Middleware::DebugScreen
requires 'Path::Class'; # Middleware::Static
requires 'MIME::Types'; # Middleware::Static

Expand Down
76 changes: 76 additions & 0 deletions README
@@ -0,0 +1,76 @@
NAME
Plack - PSGI reference implementation and utilities

DESCRIPTION
Plack is a set of PSGI reference server implementations and helper
utilities for Web application frameworks, exactly like Ruby's Rack.

See PSGI for the PSGI specification.

MODULES AND UTILITIES
Plack::Server
Plack::Server is a namespace for PSGI server implementations. We have
Standalone, CGI, FCGI, Apache, AnyEvent, Coro, Danga::Socket and many
server environments that you can run PSGI applications on.

See Plack::Server how to write your own server implementation.

Plack::Loader
Plack::Loader is a loader to load one of Plack::Server backends and run
PSGI application code reference with it.

Plack::Util
Plack::Util contains a lot of utility functions for server implementors
as well as middleware authors.

.psgi files
PSGI application is a code reference but it's not easy to pass code
reference in the command line or configuration files, so Plack uses a
convention that you need a file named "app.psgi" or alike, which would
be loaded (via perl's core function "do") to return the PSGI application
code reference. See eg/dot-psgi directory for the example ".psgi" files.

plackup
plackup is a command line launcher to run PSGI applications from command
line using Plack::Loader to load PSGI backends. It can be used to run
standalone servers and FastCGI daemon processes. Other server backends
like Apache2 needs a separate configuration but ".psgi" application file
can still be the same.

Plack::Middleware
PSGI middleware is a PSGI application that wraps existent PSGI
application and plays both side of application and servers. From the
servers the wrapped code reference still looks like and behaves exactly
the same as PSGI applications.

Plack::Middleware gives you an easy way to wrap PSGI applications with a
clean API, and compatibility with Plack::Builder DSL.

Plack::Builder
Plack::Builder gives you a DSL that you can enable Middleware in ".psgi"
files to wrap existent PSGI applications.

Plack::Request, Plack::Response
Plack::Request gives you a nice wrapper API around PSGI $env hash to get
headers, cookies and query parameters much like Apache::Request in
mod_perl.

Plack::Response does the same to construct the response array reference.

Plack::Test
Plack::Test is an unified interface to test your PSGI application using
standard HTTP::Request and HTTP::Response pair with simple callbacks.

Plack::Test::Suite
Plack::Test::Suite is a test suite to test a new PSGI server backend.

AUTHOR
See "AUTHORS" file.

SEE ALSO
PSGI

LICENSE
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

7 changes: 0 additions & 7 deletions README.md

This file was deleted.

2 changes: 1 addition & 1 deletion lib/Perlbal/Plugin/PSGI.pm
Expand Up @@ -110,7 +110,7 @@ Perlbal::Plugin::PSGI - PSGI web server on Perlbal
=head1 DESCRIPTION
This is a Perlbal plugin to asllow any PSGI application run natively
This is a Perlbal plugin to allow any PSGI application run natively
inside Perlbal process.
=head1 AUTHOR
Expand Down
79 changes: 74 additions & 5 deletions lib/Plack.pm
Expand Up @@ -10,17 +10,86 @@ __END__
=head1 NAME
Plack -
=head1 SYNOPSIS
use Plack;
Plack - PSGI reference implementation and utilities
=head1 DESCRIPTION
Plack is a set of PSGI reference server implementations and helper
utilities for Web application frameworks, exactly like Ruby's Rack.
See L<PSGI> for the PSGI specification.
=head1 MODULES AND UTILITIES
=head2 Plack::Server
L<Plack::Server> is a namespace for PSGI server implementations. We
have Standalone, CGI, FCGI, Apache, AnyEvent, Coro, Danga::Socket and
many server environments that you can run PSGI applications on.
See L<Plack::Server> how to write your own server implementation.
=head2 Plack::Loader
L<Plack::Loader> is a loader to load one of Plack::Server backends and
run PSGI application code reference with it.
=head2 Plack::Util
L<Plack::Util> contains a lot of utility functions for server
implementors as well as middleware authors.
=head2 .psgi files
PSGI application is a code reference but it's not easy to pass code
reference in the command line or configuration files, so Plack uses a
convention that you need a file named C<app.psgi> or alike, which
would be loaded (via perl's core function C<do>) to return the PSGI
application code reference. See eg/dot-psgi directory for the example
C<.psgi> files.
=head2 plackup
L<plackup> is a command line launcher to run PSGI applications from
command line using L<Plack::Loader> to load PSGI backends. It can be
used to run standalone servers and FastCGI daemon processes. Other
server backends like Apache2 needs a separate configuration but
C<.psgi> application file can still be the same.
=head2 Plack::Middleware
PSGI middleware is a PSGI application that wraps existent PSGI
application and plays both side of application and servers. From the
servers the wrapped code reference still looks like and behaves
exactly the same as PSGI applications.
L<Plack::Middleware> gives you an easy way to wrap PSGI applications
with a clean API, and compatibility with L<Plack::Builder> DSL.
=head2 Plack::Builder
L<Plack::Builder> gives you a DSL that you can enable Middleware in
C<.psgi> files to wrap existent PSGI applications.
=head2 Plack::Request, Plack::Response
L<Plack::Request> gives you a nice wrapper API around PSGI C<$env>
hash to get headers, cookies and query parameters much like
L<Apache::Request> in mod_perl.
L<Plack::Response> does the same to construct the response array
reference.
=head2 Plack::Test
L<Plack::Test> is an unified interface to test your PSGI application
using standard L<HTTP::Request> and L<HTTP::Response> pair with simple
callbacks.
=head2 Plack::Test::Suite
L<Plack::Test::Suite> is a test suite to test a new PSGI server backend.
=head1 AUTHOR
See C<AUTHORS> file.
Expand Down
5 changes: 3 additions & 2 deletions lib/Plack/Middleware.pm
Expand Up @@ -10,7 +10,8 @@ sub import {
my($class, @subclasses) = @_;

for my $sub (@subclasses) {
eval "use Plack::Middleware::$sub";
my $subclass = $sub =~ s/^\+// ? $sub : "Plack::Middleware::$sub";
eval "use $subclass";
die $@ if $@;
}
}
Expand Down Expand Up @@ -72,7 +73,7 @@ Plack::Middleware is an utility base class to write PSGI
middleware. All you have to do is to inherit from Plack::Middleware
and then implement the callback C<call> method (or C<to_app> method
that would return the PSGI code reference) to do the actual work. You
can use C<< $self->app >> to call the original (wrapped) appilcation.
can use C<< $self->app >> to call the original (wrapped) application.
See L<Plack::Builder> how to actually enable them in your I<.psgi>
application file using the DSL. If you do not like our builder DSL,
Expand Down
156 changes: 156 additions & 0 deletions lib/Plack/Middleware/AccessLog.pm
@@ -0,0 +1,156 @@
package Plack::Middleware::AccessLog;
use strict;
use warnings;
use base qw( Plack::Middleware );

__PACKAGE__->mk_accessors(qw( logger format ));

use Carp ();

my %formats = (
common => "%h %l %u %t \"%r\" %>s %b",
combined => "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"",
);

use POSIX;

sub call {
my $self = shift;
my($env) = @_;

my $res = $self->app->($env);

my $logger = $self->logger || sub { $env->{'psgi.errors'}->print(@_) };

my $content_length = Plack::Util::content_length($res->[2]);
$logger->( $self->log_line($res->[0], $res->[1], $env, { content_length => $content_length }) );

return $res;
}

sub log_line {
my($self, $status, $headers, $env, $opts) = @_;

my $h = Plack::Util::headers($headers);

my $block_handler = sub {
my($block, $type) = @_;
if ($type eq 'i') {
$block =~ s/-/_/;
return $env->{"HTTP_" . uc($block)} || "-";
} elsif ($type eq 'o') {
return scalar $h->get($block) || "-";
} elsif ($type eq 't') {
return "[" . POSIX::strftime($block, localtime) . "]";
} else {
Carp::carp("{$block}$type not supported");
return "-";
}
};

my %char_handler = (
'%' => sub { '%' },
h => sub { $env->{HTTP_X_FORWARDED_FOR} || $env->{REMOTE_ADDR} || '-' },
l => sub { '-' },
u => sub { $env->{REMOTE_USER} || '-' },
t => sub { "[" . POSIX::strftime("%d/%b/%Y %H:%M:%S", localtime) . "]" },
r => sub { $env->{REQUEST_METHOD} . " " . $env->{PATH_INFO} .
(length $env->{QUERY_STRING} ? '?' . $env->{QUERY_STRING} : '') .
" " . $env->{SERVER_PROTOCOL} },
s => sub { $status },
b => sub { $opts->{content_length} || $h->get('Content-Length') || "-" },
T => sub { $opts->{time} ? int($opts->{time}) : "-" },
D => sub { $opts->{time} || "-" },
);

my $char_handler = sub {
my $char = shift;

my $cb = $char_handler{$char};
unless ($cb) {
Carp::carp "\%$char not supported.";
return "-";
}
$cb->($char);
};

my $fmt = $self->format || "combined";
$fmt = $formats{$fmt} if exists $formats{$fmt};

$fmt =~ s{
(?:
\%\{([\w\-]+)\}([a-z]) |
\%(?:[<>])?([a-z\%])
)
}{ $1 ? $block_handler->($1, $2) : $char_handler->($3) }egx;

return $fmt . "\n";
}

__END__
=for stopwords
LogFormat
=head1 NAME
Plack::Middleware::AccessLog - Logs requests like Apache's log format
=head1 SYNOPSIS
# in app.psgi
use Plack::Middleware qw(AccessLog);
use Plack::Builder;
builder {
enable Plack::Middleware::AccessLog format => "combined";
$app;
};
=head1 DESCRIPTION
Plack::Middleware::AccessLog forwards the request to the given app and
logs request and response details to the logger callback. The format
can be specified using Apache-like format strings (or C<combined> or
C<common> for the default formats).
This middleware uses calculable content-length by checking body type,
and can not log the time taken to serve requests. It also logs the
request B<before> the response is actually sent to the client. Use
L<Plack::Middleware::AccessLog::Timed> if you want to log details
B<after> the response is transmitted (more like a real web server) to
the client.
This middleware is enabled by default when you run L<plackup> as a
default C<development> environment.
=head1 CONFIGURATION
=over 4
=item format
enable Plack::Middleware::AccessLog
format => "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"";
Takes a format string (or a preset template C<combined> or C<custom>)
to specify the log format. This middleware implements subset of
Apache's LogFormat templates.
=item logger
my $logger = Log::Dispatch->new(...);
enable Plack::Middleware::AccessLog
logger => sub { $logger->log(debug => @_) };
Sets a callback to print log message to. It prints to C<psgi.errors>
output stream by default.
=back
=head1 SEE ALSO
L<http://httpd.apache.org/docs/2.2/mod/mod_log_config.html> Rack::CustomLogger
=cut

0 comments on commit 901f782

Please sign in to comment.