Mojolicious-Plugin-PlackMiddleware
view release on metacpan or search on metacpan
lib/Mojolicious/Plugin/PlackMiddleware.pm view on Meta::CPAN
my $env = shift;
my $req = Mojo::Message::Request->new->parse($env);
$req->reverse_proxy($env->{MOJO_REVERSE_PROXY});
# Request body
my $len = $env->{CONTENT_LENGTH};
while (!$req->is_finished) {
my $chunk = ($len && $len < CHUNK_SIZE) ? $len : CHUNK_SIZE;
my $read = $env->{'psgi.input'}->read(my $buffer, $chunk, 0);
last unless $read;
$req->parse($buffer);
$len -= $read;
last if $len <= 0;
}
return $req;
}
### ---
### convert mojo tx to psgi env
### ---
sub mojo_req_to_psgi_env {
my $mojo_req = shift;
my $url = $mojo_req->url;
my $base = $url->base;
my $body =
Mojolicious::Plugin::PlackMiddleware::_PSGIInput->new($mojo_req->build_body);
my %headers_org = %{$mojo_req->headers->to_hash};
my %headers;
for my $key (keys %headers_org) {
my $value = $headers_org{$key};
$key =~ s{-}{_}g;
$key = uc $key;
$key = "HTTP_$key" if ($key !~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/);
$headers{$key} = $value;
}
return {
%ENV,
%headers,
'SERVER_PROTOCOL' => $base->protocol. '/'. $mojo_req->version,
'SERVER_NAME' => $base->host,
'SERVER_PORT' => $base->port,
'REQUEST_METHOD' => $mojo_req->method,
'SCRIPT_NAME' => '',
'PATH_INFO' => $url->path->to_string,
'REQUEST_URI' => $url->to_string,
'QUERY_STRING' => $url->query->to_string,
'psgi.url_scheme' => $base->scheme,
'psgi.version' => [1,1],
'psgi.errors' => *STDERR,
'psgi.input' => $body,
'psgi.multithread' => Plack::Util::FALSE,
'psgi.multiprocess' => Plack::Util::TRUE,
'psgi.run_once' => Plack::Util::FALSE,
'psgi.streaming' => Plack::Util::TRUE,
'psgi.nonblocking' => Plack::Util::FALSE,
};
}
### ---
### convert psgi res to mojo res
### ---
sub psgi_res_to_mojo_res {
my $psgi_res = shift;
my $mojo_res = Mojo::Message::Response->new;
$mojo_res->code($psgi_res->[0]);
my $headers = $mojo_res->headers;
while (scalar @{$psgi_res->[1]}) {
$headers->add(shift @{$psgi_res->[1]} => shift @{$psgi_res->[1]});
}
$headers->remove('Content-Length'); # should be set by mojolicious later
my $asset = $mojo_res->content->asset;
Plack::Util::foreach($psgi_res->[2], sub {$asset->add_chunk($_[0])});
weaken($psgi_res);
return $mojo_res;
}
### ---
### convert mojo res to psgi res
### ---
sub mojo_res_to_psgi_res {
my $mojo_res = shift;
my $status = $mojo_res->code;
my $headers = $mojo_res->content->headers;
my @headers;
push @headers, $_ => $headers->header($_) for (@{$headers->names});
my @body;
my $offset = 0;
# don't know why but this block makes long polling tests to pass
if ($mojo_res->content->is_dynamic && $mojo_res->content->{delay}) {
$mojo_res->get_body_chunk(0);
}
while (length(my $chunk = $mojo_res->get_body_chunk($offset))) {
push(@body, $chunk);
$offset += length $chunk;
}
return [$status, \@headers, \@body];
}
### ---
### load mw class
### ---
sub _load_class {
my($class, $prefix) = @_;
if ($prefix) {
unless ($class =~ s/^\+// || $class =~ /^$prefix/) {
$class = "$prefix\::$class";
}
}
if ($class->can('call')) {
( run in 0.853 second using v1.01-cache-2.11-cpan-d8267643d1d )