| 1 | package Mojolicious::Command::proxy; | ||||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojolicious::Command'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use Mojo::Util qw(getopt url_escape); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use Mojo::URL; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Mojolicious::Routes; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 1 | our $VERSION = '0.005'; | |||||||||||
| 8 | |||||||||||||
| 9 | 1 | has description => 'Proxy web requests elsewhere'; | |||||||||||
| 10 | 0 | ▶ | has usage => sub { shift->extract_usage . "\n" }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 11 | |||||||||||||
| 12 | 0 | ▶ | sub run { | ||||||||||
|
Subroutine
run: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 13 | 0 | my ($self, @args) = @_; | |||||||||||
| 14 | 0 | getopt \@args, [qw(no_permute pass_through)], | |||||||||||
| 15 | 'f|from=s' => \my $from; | ||||||||||||
| 16 | 0 | ▶ | $from ||= ''; | ||||||||||
|
Condition0%0/2
$from ||= ""
MC/DC0%0/1
$from ||= ""
$from
Truth table0%0/2
A
A= $from
| |||||||||||||
| 17 | 0 | my $to = shift @args; | |||||||||||
| 18 | 0 | ▶ | die $self->usage . "No to" if !$to; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 19 | 0 | ▶ | die $self->usage . "from must be blank or start '/', then something" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | if $from !~ m#^(?:$|/.)#; | ||||||||||||
| 21 | 0 | my $app = $self->app; | |||||||||||
| 22 | 0 | ▶ | $app->routes(Mojolicious::Routes->new) if ref $app eq 'Mojo::HelloWorld'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | 0 | $self->proxy($app, $from, $to); | |||||||||||
| 24 | 0 | $app->start(@args); | |||||||||||
| 25 | } | ||||||||||||
| 26 | |||||||||||||
| 27 | 2 | ▶ | sub proxy { | ||||||||||
|
Subroutine
proxy: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 28 | 2 | my ($self, $app, $from, $to) = @_; | |||||||||||
| 29 | 2 | ▶ | $from ||= '/'; | ||||||||||
|
Condition100%2/2
$from ||= "/"
MC/DC100%1/1
$from ||= "/"
$from
Truth table100%2/2
A
A= $from
| |||||||||||||
| 30 | 6 | ▶ | $app->routes->any("$from*proxy_path" => { proxy_path => "" } => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 31 | 6 | my ($c) = @_; | |||||||||||
| 32 | 6 | my $req = $c->req; | |||||||||||
| 33 | 6 | my $path = $c->stash('proxy_path'); | |||||||||||
| 34 | 6 | $path = url_escape $path, '^A-Za-z0-9\-._~/'; # route in unescapes, escaping normal stuff except "/" seems reasonable | |||||||||||
| 35 | 6 | my $query = $c->req->url->query->to_string; | |||||||||||
| 36 | 6 | $path = join '?', $path, grep length, $query; | |||||||||||
| 37 | 6 | $c->app->log->debug("Proxying '$path'"); | |||||||||||
| 38 | 6 | ▶ | $path = '/' . $path if $from eq '/'; # weird special behaviour by router | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | 6 | my $onward_url = $to . $path; | |||||||||||
| 40 | 6 | my $onward_tx = $app->ua->build_tx($req->method => $onward_url); | |||||||||||
| 41 | 6 | $onward_tx->req->content($req->content); # headers and body | |||||||||||
| 42 | 6 | $c->proxy->start_p($onward_tx); | |||||||||||
| 43 | 2 | }, 'proxy'); | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | 1 | 1; | |||||||||||
| 47 | |||||||||||||
| 48 | =encoding utf8 | ||||||||||||
| 49 | |||||||||||||
| 50 | =head1 NAME | ||||||||||||
| 51 | |||||||||||||
| 52 | Mojolicious::Command::proxy - Proxy web requests elsewhere | ||||||||||||
| 53 | |||||||||||||
| 54 | =begin markdown | ||||||||||||
| 55 | |||||||||||||
| 56 | # PROJECT STATUS | ||||||||||||
| 57 | |||||||||||||
| 58 | | OS | Build status | | ||||||||||||
| 59 | |:-------:|--------------:| | ||||||||||||
| 60 | | Linux | [](https://travis-ci.org/mohawk2/Mojolicious-Command-proxy) | | ||||||||||||
| 61 | |||||||||||||
| 62 | [](https://metacpan.org/pod/Mojolicious::Command::proxy) | ||||||||||||
| 63 | |||||||||||||
| 64 | =end markdown | ||||||||||||
| 65 | |||||||||||||
| 66 | =head1 SYNOPSIS | ||||||||||||
| 67 | |||||||||||||
| 68 | Usage: APPLICATION proxy [--from route_prefix] to_url | ||||||||||||
| 69 | |||||||||||||
| 70 | mojo proxy http://example.com/subdir daemon -l http://*:3000 | ||||||||||||
| 71 | mojo proxy -f /proxy http://example.com/subdir get /proxy/hi | ||||||||||||
| 72 | |||||||||||||
| 73 | Options: | ||||||||||||
| 74 | -f, --from Proxying route prefix | ||||||||||||
| 75 | |||||||||||||
| 76 | =head1 DESCRIPTION | ||||||||||||
| 77 | |||||||||||||
| 78 | L<Mojolicious::Command::proxy> is a command line interface for | ||||||||||||
| 79 | making an app that proxies some or all incoming requests elsewhere. | ||||||||||||
| 80 | Having done so, it then passes the rest of its arguments to the app's | ||||||||||||
| 81 | C<start> method, as illustrated in the synopsis above. | ||||||||||||
| 82 | |||||||||||||
| 83 | One major reason for this is to be able to point your browser at | ||||||||||||
| 84 | e.g. C<localhost:3000> (see first example in synopsis). This relaxes | ||||||||||||
| 85 | restrictions on e.g. Service Workers and push notifications, which | ||||||||||||
| 86 | normally demand TLS, so you can test functionality even if your real | ||||||||||||
| 87 | development server is running elsewhere. | ||||||||||||
| 88 | |||||||||||||
| 89 | =head1 ATTRIBUTES | ||||||||||||
| 90 | |||||||||||||
| 91 | =head2 description | ||||||||||||
| 92 | |||||||||||||
| 93 | $str = $self->description; | ||||||||||||
| 94 | |||||||||||||
| 95 | =head2 usage | ||||||||||||
| 96 | |||||||||||||
| 97 | $str = $self->usage; | ||||||||||||
| 98 | |||||||||||||
| 99 | =head1 METHODS | ||||||||||||
| 100 | |||||||||||||
| 101 | =head2 run | ||||||||||||
| 102 | |||||||||||||
| 103 | $get->run(@ARGV); | ||||||||||||
| 104 | |||||||||||||
| 105 | Run this command. It will add a L</proxy> route as below. If not supplied, | ||||||||||||
| 106 | the C<$from> will be empty-string. | ||||||||||||
| 107 | |||||||||||||
| 108 | Command-line arguments will only be parsed at the start of the | ||||||||||||
| 109 | command-line. This allows you to pass option through to e.g. C<daemon>. | ||||||||||||
| 110 | |||||||||||||
| 111 | As a special case, if the C<app> attribute is exactly a | ||||||||||||
| 112 | L<Mojo::HelloWorld> app, it will replace its C<routes> attribute with an | ||||||||||||
| 113 | empty one first, since the C<whatever> route clashes with the proxy route, | ||||||||||||
| 114 | being also a match-everything wildcard route. This makes the C<mojo proxy> | ||||||||||||
| 115 | invocation function as expected. | ||||||||||||
| 116 | |||||||||||||
| 117 | =head2 proxy | ||||||||||||
| 118 | |||||||||||||
| 119 | Mojolicious::Command::proxy->proxy($app, $from_prefix, $to_prefix); | ||||||||||||
| 120 | |||||||||||||
| 121 | Add a route to the given app, with the given prefix, named C<proxy>. It | ||||||||||||
| 122 | will transparently proxy all matching requests to the give C<$to>, | ||||||||||||
| 123 | with all the same headers both ways. | ||||||||||||
| 124 | |||||||||||||
| 125 | It operates by simply appending everything after the C<$from_prefix>, | ||||||||||||
| 126 | which I<can> be an empty string (which is treated the same as solitary | ||||||||||||
| 127 | C</>, doing what you'd expect), to the C<$to_prefix>. E.g.: | ||||||||||||
| 128 | |||||||||||||
| 129 | $cmd->proxy($app, '', '/subdir'); # /2 -> /subdir/2, / -> /subdir/ i.e. all | ||||||||||||
| 130 | $cmd->proxy($app, '/proxy', '/subdir'); # /proxy/2 -> /subdir/2 | ||||||||||||
| 131 | |||||||||||||
| 132 | C<$to> can be a path as well as a full URL, so you can also use this to | ||||||||||||
| 133 | route internally. However, the author can see no good reason to do this | ||||||||||||
| 134 | outside of testing. | ||||||||||||
| 135 | |||||||||||||
| 136 | It uses L<Mojolicious::Plugin::DefaultHelpers/proxy-E<gt>start_p> but | ||||||||||||
| 137 | adds the full header-proxying behaviour. | ||||||||||||
| 138 | |||||||||||||
| 139 | =head1 AUTHOR | ||||||||||||
| 140 | |||||||||||||
| 141 | Ed J | ||||||||||||
| 142 | |||||||||||||
| 143 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 144 | |||||||||||||
| 145 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 146 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 147 | |||||||||||||
| 148 | =cut | ||||||||||||