use HTTP::Headers;
use URI::QueryParam;
use Moose::Util::TypeConstraints;
+use Plack::Loader;
+use Plack::Middleware::Conditional;
+use Plack::Middleware::ReverseProxy;
use namespace::clean -except => 'meta';
has _prepared_write => (is => 'rw');
has _response_cb => (
- is => 'ro',
- isa => 'CodeRef',
- writer => '_set_response_cb',
+ is => 'ro',
+ isa => 'CodeRef',
+ writer => '_set_response_cb',
+ clearer => '_clear_response_cb',
);
has _writer => (
- is => 'ro',
- isa => duck_type([qw(write close)]),
- writer => '_set_writer',
+ is => 'ro',
+ isa => duck_type([qw(write close)]),
+ writer => '_set_writer',
+ clearer => '_clear_writer',
);
# Amount of data to read from input on each pass
}
$self->_writer->close;
+ $self->_clear_writer;
+ $self->_clear_env;
return;
}
$ctx->response->headers->scan(sub { push @headers, @_ });
$self->_set_writer($self->_response_cb->([ $ctx->response->status, \@headers ]));
+ $self->_clear_response_cb;
return;
}
$request->protocol( $env->{SERVER_PROTOCOL} );
$request->remote_user( $env->{REMOTE_USER} );
$request->method( $env->{REQUEST_METHOD} );
- $request->secure( $env->{'psgi.url_scheme'} eq 'https' );
+ $request->secure( $env->{'psgi.url_scheme'} eq 'https' ? 1 : 0 );
return;
}
# set the request URI
my $req_uri = $env->{REQUEST_URI};
$req_uri =~ s/\?.*$//;
- my $path = $self->unescape_uri($req_uri);
+ my $path = $req_uri;
$path =~ s{^/+}{};
# Using URI directly is way too slow, so we construct the URLs manually
$host .= ":$port";
}
- # Escape the path
- $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
- $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
-
my $query = $env->{QUERY_STRING} ? '?' . $env->{QUERY_STRING} : '';
my $uri = $scheme . '://' . $host . '/' . $path . $query;
- $ctx->request->uri( bless \$uri, $uri_class );
+ $ctx->request->uri( (bless \$uri, $uri_class)->canonical );
# set the base URI
# base must end in a slash
my $rc = $self->read_chunk( $c, my $buffer, $readlen );
if ( defined $rc ) {
if (0 == $rc) { # Nothing more to read even though Content-Length
- # said there should be. FIXME - Warn in the log here?
+ # said there should be.
$self->finalize_read;
return;
}
=cut
-sub read_chunk { }
+sub read_chunk {
+ my ($self, $ctx) = (shift, shift);
+ return $self->env->{'psgi.input'}->read(@_);
+}
=head2 $self->read_length
=cut
sub run {
- my ($self, $app) = @_;
+ my ($self, $app, $server, @args) = @_;
+ # FIXME - Do something sensible with the options we're passed
+ $server->run($self->_build_psgi_app($app, @args));
+}
- return sub {
+sub _build_psgi_app {
+ my ($self, $app, @args) = @_;
+
+ my $psgi_app = sub {
my ($env) = @_;
return sub {
$app->handle_request(env => $env);
};
};
+
+ $psgi_app = Plack::Middleware::Conditional->wrap(
+ $psgi_app,
+ condition => sub {
+ my ($env) = @_;
+ return if $app->config->{ignore_frontend_proxy};
+ return $env->{REMOTE_ADDR} eq '127.0.0.1' || $app->config->{using_frontend_proxy};
+ },
+ builder => sub { Plack::Middleware::ReverseProxy->wrap($_[0]) },
+ );
+
+ return $psgi_app;
}
=head2 $self->write($c, $buffer)