| 1 | package Mojolicious::Plugin::WebAPI::Proxy; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Proxy for WebAPI integration | ||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Mojo::Base 'Mojolicious'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Plack::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | has 'script'; | |||||||||
| 9 | 1 | has 'app'; | |||||||||
| 10 | 1 | has 'base'; | |||||||||
| 11 | |||||||||||
| 12 | 0 | ▶ | sub handler { | ||||||||
|
Subroutine
handler: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 13 | 0 | my ($self, $c) = @_; | |||||||||
| 14 | |||||||||||
| 15 | 0 | ▶ | if(!defined $self->app) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 0 | $self->app(Plack::Util::load_psgi($self->home->rel_file($self->script))); | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | 0 | my $plack_env = $self->_mojo_req_to_psgi_env($c->req); | |||||||||
| 20 | 0 | $plack_env->{'MOJO.CONTROLLER'} = $c; | |||||||||
| 21 | |||||||||||
| 22 | 0 | my $plack_res = $self->app->($plack_env); | |||||||||
| 23 | 0 | my $mojo_res = _psgi_res_to_mojo_res($plack_res); | |||||||||
| 24 | |||||||||||
| 25 | 0 | $c->tx->res($mojo_res); | |||||||||
| 26 | 0 | $c->rendered; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | sub _mojo_req_to_psgi_env { | ||||||||
|
Subroutine
_mojo_req_to_psgi_env: not called CC=4 SCAR=30.0
| |||||||||||
| 30 | 0 | my $self = shift; | |||||||||
| 31 | 0 | my $mojo_req = shift; | |||||||||
| 32 | |||||||||||
| 33 | 0 | my $url = $mojo_req->url; | |||||||||
| 34 | 0 | my $base = $url->base; | |||||||||
| 35 | 0 | my $body = Mojolicious::Plugin::WebAPI::_PSGIInput->new($mojo_req->body); | |||||||||
| 36 | |||||||||||
| 37 | 0 | my %headers = %{$mojo_req->headers->to_hash}; | |||||||||
| 38 | 0 | for my $key (keys %headers) { | |||||||||
| 39 | 0 | my $value = $headers{$key}; | |||||||||
| 40 | 0 | delete $headers{$key}; | |||||||||
| 41 | 0 | $key =~ s{-}{_}; | |||||||||
| 42 | 0 | $headers{'HTTP_'. uc $key} = $value; | |||||||||
| 43 | 0 | $headers{uc $key} = $value; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 0 | my $path = $url->path->to_string; | |||||||||
| 47 | 0 | ▶ | if ( $self->base ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | ▶ | $path = '/' . $path if index( $path, '/' ) != 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | |||||||||||
| 50 | 0 | my $base = $self->base; | |||||||||
| 51 | 0 | $path =~ s{ \A $base }{}xms; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | return { | ||||||||||
| 55 | 0 | %ENV, | |||||||||
| 56 | %headers, | ||||||||||
| 57 | 'SERVER_PROTOCOL' => 'HTTP/'. $mojo_req->version, | ||||||||||
| 58 | 'SERVER_NAME' => $base->host, | ||||||||||
| 59 | 'SERVER_PORT' => $base->port, | ||||||||||
| 60 | 'REQUEST_METHOD' => $mojo_req->method, | ||||||||||
| 61 | 'SCRIPT_NAME' => '', | ||||||||||
| 62 | 'PATH_INFO' => $path, | ||||||||||
| 63 | 'REQUEST_URI' => $url->to_string, | ||||||||||
| 64 | 'QUERY_STRING' => $url->query->to_string, | ||||||||||
| 65 | 'psgi.url_scheme' => $base->scheme, | ||||||||||
| 66 | 'psgi.multithread' => Plack::Util::FALSE, | ||||||||||
| 67 | 'psgi.version' => [1,1], | ||||||||||
| 68 | 'psgi.errors' => *STDERR, | ||||||||||
| 69 | 'psgi.input' => $body, | ||||||||||
| 70 | 'psgi.multithread' => Plack::Util::FALSE, | ||||||||||
| 71 | 'psgi.multiprocess' => Plack::Util::TRUE, | ||||||||||
| 72 | 'psgi.run_once' => Plack::Util::FALSE, | ||||||||||
| 73 | 'psgi.streaming' => Plack::Util::TRUE, | ||||||||||
| 74 | 'psgi.nonblocking' => Plack::Util::FALSE, | ||||||||||
| 75 | }; | ||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 0 | ▶ | sub _psgi_res_to_mojo_res { | ||||||||
|
Subroutine
_psgi_res_to_mojo_res: not called CC=2 SCAR=17.9
| |||||||||||
| 79 | 0 | my $psgi_res = shift; | |||||||||
| 80 | |||||||||||
| 81 | 0 | my $mojo_res = Mojo::Message::Response->new; | |||||||||
| 82 | 0 | $mojo_res->code($psgi_res->[0]); | |||||||||
| 83 | |||||||||||
| 84 | 0 | my $headers = $mojo_res->headers; | |||||||||
| 85 | 0 | ▶ | while (scalar @{$psgi_res->[1]}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | $headers->header(shift @{$psgi_res->[1]} => shift @{$psgi_res->[1]}); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 0 | $headers->remove('Content-Length'); # should be set by mojolicious later | |||||||||
| 90 | |||||||||||
| 91 | 0 | my $asset = $mojo_res->content->asset; | |||||||||
| 92 | 0 | ▶ | Plack::Util::foreach($psgi_res->[2], sub {$asset->add_chunk($_[0])}); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 93 | |||||||||||
| 94 | 0 | return $mojo_res; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | package Mojolicious::Plugin::WebAPI::_PSGIInput; | ||||||||||
| 98 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 99 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 100 | |||||||||||
| 101 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||
| 102 | 0 | my ($class, $content) = @_; | |||||||||
| 103 | 0 | return bless [$content, 0], $class; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | sub read { | ||||||||
|
Subroutine
read: not called CC=2 SCAR=17.9
| |||||||||||
| 107 | 0 | my $self = shift; | |||||||||
| 108 | 0 | ▶ | if ($_[0] = substr($self->[0], $self->[1], $_[1])) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 0 | $self->[1] += $_[1]; | |||||||||
| 110 | 0 | return 1; | |||||||||
| 111 | } | ||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | sub seek { | ||||||||
|
Subroutine
seek: not called CC=1 SCAR=6.9
| |||||||||||
| 115 | 0 | my $self = shift; | |||||||||
| 116 | |||||||||||
| 117 | 0 | $self->[1] = $_[0]; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 1 | 1; | |||||||||
| 121 | |||||||||||
| 122 | __END__ | ||||||||||