| 1 | package Mojolicious::Plugin::DefaultHelpers; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 18 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 18 | ▶ | use Mojo::Asset::File; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use Mojo::ByteStream; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use Mojo::Collection; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use Mojo::Exception; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 18 | ▶ | use Mojo::IOLoop; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 18 | ▶ | use Mojo::Promise; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 18 | ▶ | use Mojo::Util qw(dumper random_bytes); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 18 | ▶ | use Time::HiRes qw(gettimeofday tv_interval); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 18 | ▶ | use Scalar::Util qw(blessed weaken); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 83 | ▶ | sub register { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 83 | my ($self, $app) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # Controller alias helpers | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 83 | for my $name (qw(app param stash session url_for url_for_asset url_for_file)) { | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | 581 | ▶ | $app->helper($name => sub { shift->$name(@_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # Stash key shortcuts (should not generate log messages) | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 83 | for my $name (qw(extends layout title)) { | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 249 | ▶ | $app->helper($name => sub { shift->stash($name, @_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | $app->helper(accepts => sub { $_[0]->app->renderer->accepts(@_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | $app->helper(b => sub { shift; Mojo::ByteStream->new(@_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $app->helper(c => sub { shift; Mojo::Collection->new(@_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 83 | ▶ | $app->helper(config => sub { shift->app->config(@_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 83 | ▶ | $app->helper(content => sub { _content(0, 0, @_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | $app->helper(content_for => sub { _content(1, 0, @_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $app->helper(content_with => sub { _content(0, 1, @_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | $app->helper($_ => $self->can("_$_")) | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 83 | for qw(csrf_token current_route exception_format flash inactivity_timeout is_fresh), | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | qw(redirect_to respond_to url_with validation); | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 83 | ▶ | $app->helper(dumper => sub { shift; dumper @_ }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 83 | ▶ | $app->helper(include => sub { shift->render_to_string(@_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 83 | $app->helper(log => \&_log); | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $app->helper('proxy.get_p' => sub { _proxy_method_p('GET', @_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $app->helper('proxy.post_p' => sub { _proxy_method_p('POST', @_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 83 | $app->helper('proxy.start_p' => \&_proxy_start_p); | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 83 | $app->helper("reply.$_" => $self->can("_$_")) for qw(asset file static); | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 83 | ▶ | $app->helper('reply.exception', => sub { shift->helpers->reply->http_exception(@_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 83 | ▶ | $app->helper('reply.not_found', => sub { shift->helpers->reply->http_not_found() }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 83 | $app->helper('reply.http_exception', => \&_http_exception); | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 83 | $app->helper('reply.http_not_found', => \&_http_not_found); | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 83 | ▶ | $app->helper('reply.html_exception' => sub { _development('exception', @_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 83 | ▶ | $app->helper('reply.html_not_found' => sub { _development('not_found', @_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 83 | $app->helper('reply.json_exception', => \&_json_exception); | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | 83 | $app->helper('reply.json_not_found', => \&_json_not_found); | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | 83 | $app->helper('reply.txt_exception', => \&_txt_exception); | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | 83 | $app->helper('reply.txt_not_found', => \&_txt_not_found); | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 83 | $app->helper('timing.begin' => \&_timing_begin); | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 83 | $app->helper('timing.elapsed' => \&_timing_elapsed); | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | 83 | $app->helper('timing.rps' => \&_timing_rps); | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | 83 | $app->helper('timing.server_timing' => \&_timing_server_timing); | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $app->helper(ua => sub { shift->app->ua }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub _asset { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_asset: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $c->app->static->serve_asset($c, @_); | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $c->rendered; | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub _block { ref $_[0] eq 'CODE' ? $_[0]() : $_[0] } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_block: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 10 | ▶ | sub _content { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_content: called CC=8 SCAR=29.6
| |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 10 | my ($append, $replace, $c, $name, $content) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 10 | ▶ | $name ||= 'content'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$name ||= "content"
MC/DC0%0/1
$name ||= "content"
$name
Truth table50%1/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 10 | ▶ | my $hash = $c->stash->{'mojo.content'} //= {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$c->stash->{"mojo.content"} //= {}
MC/DC100%1/1
$c->stash->{"mojo.content"} //= {}
$c->stash->{"mojo.content"}
Truth table100%2/2
A
A= $c->stash->{"mojo.content"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 10 | ▶ | if (defined $content) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ($append) { $hash->{$name} .= _block($content) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ($replace) { $hash->{$name} = _block($content) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | else { $hash->{$name} //= _block($content) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$hash->{$name} //= _block($content)
MC/DC0%0/2
$hash->{$name} //= _block($content)
$hash->{$name}
_block($content)
Truth table0%0/3
A //= B
A= $hash->{$name}B= _block($content)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 10 | ▶ | return Mojo::ByteStream->new($hash->{$name} // ''); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$hash->{$name} // ""
MC/DC100%1/1
$hash->{$name} // ""
$hash->{$name}
Truth table100%2/2
A
A= $hash->{$name}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 25 | ▶ | sub _convert_to_exception { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_convert_to_exception: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 25 | my $e = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 25 | ▶ | return (blessed $e && $e->isa('Mojo::Exception')) ? $e : Mojo::Exception->new($e); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($e) and $e->isa("Mojo::Exception")
MC/DC0%0/2
builtin::blessed($e) and $e->isa("Mojo::Exception")
builtin::blessed($e)
$e->isa("Mojo::Exception")
Truth table33%1/3
A and B
A= builtin::blessed($e)B= $e->isa("Mojo::Exception")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub _csrf_token { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_csrf_token: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # Generate token and store it in the session | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $session = $c->session; | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $session->{csrf_token} ||= unpack "H*", random_bytes(20); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$session->{"csrf_token"} ||= unpack("H*", random_bytes(20))
MC/DC0%0/2
$session->{"csrf_token"} ||= unpack("H*", random_bytes(20))
$session->{"csrf_token"}
unpack("H*", random_bytes(20))
Truth table0%0/3
A ||= B
A= $session->{"csrf_token"}B= unpack("H*", random_bytes(20))
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # Mask token to protect against BREACH attacks | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my $mask = random_bytes(20); | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | return unpack("H*", $mask) . unpack("H*", $mask ^ pack("H*", $session->{csrf_token})); | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub _current_route { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_current_route: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | return '' unless my $route = shift->match->endpoint; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | return @_ ? $route->name eq shift : $route->name; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 48 | ▶ | sub _development { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_development: called CC=9 SCAR=22.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 48 | my ($page, $c, $e) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 48 | ▶ | $c->helpers->log->error(($e = _convert_to_exception($e))->inspect) if $page eq 'exception'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # Filtered stash snapshot | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 48 | my $stash = $c->stash; | |||||||||||||||||||||||||||||||||||||||||||||
| 122 | 48 | %{$stash->{snapshot} = {}} | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | 48 | ▶ | = map { $_ => $_ eq 'app' ? 'DUMMY' : $stash->{$_} } grep { !/^mojo\./ and defined $stash->{$_} } keys %$stash; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
!/^mojo\./u && defined $stash->{$_}
MC/DC50%1/2
!/^mojo\./u && defined $stash->{$_}
!/^mojo\./u
defined $stash->{$_}
Truth table66%2/3
not(A) && B
A= !/^mojo\./uB= defined $stash->{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 48 | ▶ | $stash->{exception} = $page eq 'exception' ? $e : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | # Render with fallbacks | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 48 | my $app = $c->app; | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | 48 | my $mode = $app->mode; | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | my $options = { | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 48 | ▶ | format => $stash->{format} || $app->renderer->default_format, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$stash->{'format'} || $app->renderer->default_format
MC/DC0%0/2
$stash->{'format'} || $app->renderer->default_format
$stash->{'format'}
$app->renderer->default_format
Truth table66%2/3
A || B
A= $stash->{'format'}B= $app->renderer->default_format
| |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | handler => undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | status => $page eq 'exception' ? 500 : 404, | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | template => "$page.$mode" | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 48 | ▶ | my $bundled = 'mojo/' . ($mode eq 'development' ? 'debug' : $page); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 48 | ▶ | return $c if _fallbacks($c, $options, $page, $bundled); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | _fallbacks($c, {%$options, format => 'html'}, $page, $bundled); | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | return $c; | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 48 | ▶ | sub _exception_format { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_exception_format: called CC=3 SCAR=13.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 48 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | 48 | my $stash = $c->stash; | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | 48 | ▶ | $stash->{'mojo.exception_format'} ||= $c->app->exception_format; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$stash->{"mojo.exception_format"} ||= $c->app->exception_format
MC/DC0%0/2
$stash->{"mojo.exception_format"} ||= $c->app->exception_format
$stash->{"mojo.exception_format"}
$c->app->exception_format
Truth table33%1/3
A ||= B
A= $stash->{"mojo.exception_format"}B= $c->app->exception_format
| |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 48 | ▶ | return $stash->{'mojo.exception_format'} unless @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | $stash->{'mojo.exception_format'} = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | return $c; | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 48 | ▶ | sub _fallbacks { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fallbacks: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 48 | my ($c, $options, $template, $bundled) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # Mode specific template | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 48 | ▶ | return 1 if $c->render_maybe(%$options); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # Normal template | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 39 | ▶ | return 1 if $c->render_maybe(%$options, template => $template); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # Inline template | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 17 | my $stash = $c->stash; | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | 17 | ▶ | return undef unless $options->{format} eq 'html'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 17 | delete @$stash{qw(extends layout)}; | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 17 | return $c->render_maybe($bundled, %$options, handler => 'ep'); | |||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub _file { _asset(shift, Mojo::Asset::File->new(path => shift)) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_file: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | sub _flash { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flash: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # Check old flash | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | my $session = $c->session; | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | return $session->{flash} ? $session->{flash}{$_[0]} : undef if @_ == 1 && !ref $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
@_ == 1 and not ref $_[0]
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1
not ref $_[0]
Truth table0%0/3
A and not(B)
A= @_ == 1B= not ref $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # Initialize new flash and merge values | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | my $values = ref $_[0] ? $_[0] : {@_}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | @{$session->{new_flash} //= {}}{keys %$values} = values %$values; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$session->{"new_flash"} //= {}
MC/DC0%0/1
$session->{"new_flash"} //= {}
$session->{"new_flash"}
Truth table0%0/2
A
A= $session->{"new_flash"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | return $c; | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 25 | ▶ | sub _http_exception { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_http_exception: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 25 | my ($c, $e) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 25 | my $format = $c->exception_format; | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | 25 | ▶ | return $c->helpers->reply->txt_exception($e) if $format eq 'txt'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 25 | ▶ | return $c->helpers->reply->json_exception($e) if $format eq 'json'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 25 | return $c->helpers->reply->html_exception($e); | |||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 23 | ▶ | sub _http_not_found { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_http_not_found: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 23 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | 23 | my $format = $c->exception_format; | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | 23 | ▶ | return $c->helpers->reply->txt_not_found if $format eq 'txt'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 23 | ▶ | return $c->helpers->reply->json_not_found if $format eq 'json'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 23 | return $c->helpers->reply->html_not_found; | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | sub _inactivity_timeout { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_inactivity_timeout: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | my ($c, $timeout) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | my $stream = Mojo::IOLoop->stream($c->tx->connection // ''); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$c->tx->connection // ""
MC/DC0%0/1
$c->tx->connection // ""
$c->tx->connection
Truth table0%0/2
A
A= $c->tx->connection
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | $stream->timeout($timeout) if $stream; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | return $c; | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | sub _is_fresh { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_fresh: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | my ($c, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | return $c->app->static->is_fresh($c, \%options); | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | sub _json_exception { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_json_exception: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | my ($c, $e) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | $c->stash->{exception} = _convert_to_exception($e); | |||||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | return $c->render(json => {error => $e}, status => 500) if $c->app->mode eq 'development'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | return $c->render(json => {error => 'Internal Server Error'}, status => 500); | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | sub _json_not_found { shift->render(json => {error => 'Not Found'}, status => 404) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_json_not_found: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 678 | ▶ | sub _log { $_[0]->stash->{'mojo.log'} ||= $_[0]->app->log->context('[' . $_[0]->req->request_id . ']') } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_log: called CC=2 SCAR=7.2
Condition66%2/3
$_[0]->stash->{"mojo.log"} ||= $_[0]->app->log->context('[' . $_[0]->req->request_id . ']')
MC/DC0%0/2
$_[0]->stash->{"mojo.log"} ||= $_[0]->app->log->context('[' . $_[0]->req->request_id . ']')
$_[0]->stash->{"mojo.log"}
$_[0]->app->log->context('[' . $_[0]->req->request_id . ']')
Truth table66%2/3
A ||= B
A= $_[0]->stash->{"mojo.log"}B= $_[0]->app->log->context('[' . $_[0]->req->request_id . ']')
| |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | sub _proxy_method_p { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_proxy_method_p: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | my ($method, $c) = (shift, shift); | |||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | return _proxy_start_p($c, $c->ua->build_tx($method, @_)); | |||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | sub _proxy_start_p { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_proxy_start_p: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | my ($c, $source_tx) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | my $tx = $c->render_later->tx; | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | my $promise = Mojo::Promise->new; | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | $source_tx->res->content->auto_upgrade(0)->auto_decompress(0)->once( | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | ▶ | body => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | my $source_content = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | my $source_res = $source_tx->res; | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | my $res = $tx->res; | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | my $content = $res->content; | |||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | $res->code($source_res->code)->message($source_res->message); | |||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | my $headers = $source_res->headers->clone->dehop; | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | $content->headers($headers); | |||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | $promise->resolve; | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | my $source_stream = Mojo::IOLoop->stream($source_tx->connection); | |||||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | return unless my $stream = Mojo::IOLoop->stream($tx->connection); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | my $write = $source_content->is_chunked ? 'write_chunk' : 'write'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | $source_content->unsubscribe('read')->on( | ||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | read => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | my $data = pop; | |||||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | $content->$write(length $data ? $data : ()) and $tx->resume; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | # Throttle transparently when backpressure rises | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | return if $stream->can_write; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | $source_stream->stop; | |||||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | ▶ | $stream->once(drain => sub { $source_stream->start }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | ▶ | $source_res->once(finish => sub { $content->$write('') and $tx->resume }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | weaken $source_tx; | |||||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | $source_tx->once(finish => sub { $promise->reject(_tx_error(@_)) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | $c->ua->start_p($source_tx)->catch(sub { }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | return $promise; | |||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | sub _redirect_to { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_redirect_to: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # Don't override 3xx status | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | my $res = $c->res; | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | $res->headers->location($c->url_for(@_)); | |||||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | return $c->rendered($res->is_redirect ? () : 302); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | sub _respond_to { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_respond_to: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | my ($c, $args) = (shift, ref $_[0] ? $_[0] : {@_}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | # Find target | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | my $target; | |||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | my $renderer = $c->app->renderer; | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | my @formats = @{$renderer->accepts($c)}; | |||||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | for my $format (@formats ? @formats : ($renderer->default_format)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | next unless $target = $args->{$format}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | $c->stash->{format} = $format; | |||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||
| 292 | # Fallback | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | unless ($target) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | return $c->rendered(204) unless $target = $args->{any}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | delete $c->stash->{format}; | |||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||
| 298 | # Dispatch | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | ref $target eq 'CODE' ? $target->($c) : $c->render(%$target); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | return $c; | |||||||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | ▶ | sub _static { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_static: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 1 | my ($c, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 306 | 1 | ▶ | croak qq{Static file "$file" not found} unless $c->app->static->serve($c, $file); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 1 | return $c->rendered; | |||||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 10 | ▶ | sub _timing_begin { shift->stash->{'mojo.timing'}{shift()} = [gettimeofday] } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_timing_begin: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 10 | ▶ | sub _timing_elapsed { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_timing_elapsed: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 10 | my ($c, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 314 | 10 | ▶ | return undef unless my $started = $c->stash->{'mojo.timing'}{$name}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 10 | return tv_interval($started, [gettimeofday()]); | |||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 10 | ▶ | sub _timing_rps { $_[1] == 0 ? undef : sprintf '%.3f', 1 / $_[1] } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_timing_rps: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | sub _timing_server_timing { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_timing_server_timing: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | my ($c, $metric, $desc, $dur) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | my $value = $metric; | |||||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | $value .= qq{;desc="$desc"} if defined $desc; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | $value .= ";dur=$dur" if defined $dur; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | $c->res->headers->append('Server-Timing' => $value); | |||||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | sub _tx_error { (shift->error // {})->{message} // 'Unknown error' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tx_error: not called CC=3 SCAR=24.8
Condition0%0/4
+((shift())->error // {})->{"message"} // "Unknown error"
(shift())->error // {}
MC/DC0%0/1
+((shift())->error // {})->{"message"} // "Unknown error"
(shift())->error
Truth table0%0/2
A
A= (shift())->error
| |||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | sub _txt_exception { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_txt_exception: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | my ($c, $e) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | $c->stash->{exception} = _convert_to_exception($e); | |||||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | return $c->render(text => $e, format => 'txt', status => 500) if $c->app->mode eq 'development'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | return $c->render(text => 'Internal Server Error', format => 'txt', status => 500); | |||||||||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | sub _txt_not_found { shift->render(text => 'Not Found', format => 'txt', status => 404) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_txt_not_found: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | sub _url_with { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_url_with: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | return $c->url_for(@_)->query($c->req->url->query->clone); | |||||||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 11 | ▶ | sub _validation { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validation: called CC=6 SCAR=20.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 11 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 11 | my $stash = $c->stash; | |||||||||||||||||||||||||||||||||||||||||||||
| 348 | 11 | ▶ | return $stash->{'mojo.validation'} if $stash->{'mojo.validation'}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 11 | my $req = $c->req; | |||||||||||||||||||||||||||||||||||||||||||||
| 351 | 11 | my $token = $c->session->{csrf_token}; | |||||||||||||||||||||||||||||||||||||||||||||
| 352 | 11 | my $header = $req->headers->header('X-CSRF-Token'); | |||||||||||||||||||||||||||||||||||||||||||||
| 353 | 11 | my $hash = $req->params->to_hash; | |||||||||||||||||||||||||||||||||||||||||||||
| 354 | 11 | ▶ | $hash->{csrf_token} //= $header if $token && $header; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
$token and $header
$hash->{"csrf_token"} //= $header
MC/DC0%0/2
$token and $header
$token
$header
MC/DC0%0/2
$hash->{"csrf_token"} //= $header
$hash->{"csrf_token"}
$header
Truth table33%1/3
A and B
A= $tokenB= $header
Truth table0%0/3
A //= B
A= $hash->{"csrf_token"}B= $header
| |||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | $hash->{$_} = $req->every_upload($_) for map { $_->name } @{$req->uploads}; | |||||||||||||||||||||||||||||||||||||||||||||
| 356 | 11 | my $v = $c->app->validator->validation->input($hash); | |||||||||||||||||||||||||||||||||||||||||||||
| 357 | 11 | return $stash->{'mojo.validation'} = $v->csrf_token($token); | |||||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 18 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||
| 362 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||
| 364 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||
| 366 | Mojolicious::Plugin::DefaultHelpers - Default helpers plugin | ||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||
| 368 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||
| 370 | # Mojolicious | ||||||||||||||||||||||||||||||||||||||||||||||
| 371 | $app->plugin('DefaultHelpers'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||
| 373 | # Mojolicious::Lite | ||||||||||||||||||||||||||||||||||||||||||||||
| 374 | plugin 'DefaultHelpers'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||
| 376 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||
| 378 | L<Mojolicious::Plugin::DefaultHelpers> is a collection of helpers for L<Mojolicious>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||||||
| 380 | This is a core plugin, that means it is always enabled and its code a good example for learning to build new plugins, | ||||||||||||||||||||||||||||||||||||||||||||||
| 381 | you're welcome to fork it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||||||
| 383 | See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available by default. | ||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||
| 385 | =head1 HELPERS | ||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||
| 387 | L<Mojolicious::Plugin::DefaultHelpers> implements the following helpers. | ||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||
| 389 | =head2 accepts | ||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||
| 391 | my $formats = $c->accepts; | ||||||||||||||||||||||||||||||||||||||||||||||
| 392 | my $format = $c->accepts('html', 'json', 'txt'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||
| 394 | Select best possible representation for resource from C<format> C<GET>/C<POST> parameter, C<format> stash value or | ||||||||||||||||||||||||||||||||||||||||||||||
| 395 | C<Accept> request header with L<Mojolicious::Renderer/"accepts">, defaults to returning the first extension if no | ||||||||||||||||||||||||||||||||||||||||||||||
| 396 | preference could be detected. | ||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||
| 398 | # Check if JSON is acceptable | ||||||||||||||||||||||||||||||||||||||||||||||
| 399 | $c->render(json => {hello => 'world'}) if $c->accepts('json'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||||
| 401 | # Check if JSON was specifically requested | ||||||||||||||||||||||||||||||||||||||||||||||
| 402 | $c->render(json => {hello => 'world'}) if $c->accepts('', 'json'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||
| 404 | # Unsupported representation | ||||||||||||||||||||||||||||||||||||||||||||||
| 405 | $c->render(data => '', status => 204) | ||||||||||||||||||||||||||||||||||||||||||||||
| 406 | unless my $format = $c->accepts('html', 'json'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||
| 408 | # Detected representations to select from | ||||||||||||||||||||||||||||||||||||||||||||||
| 409 | my @formats = @{$c->accepts}; | ||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||
| 411 | =head2 app | ||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||
| 413 | %= app->secrets->[0] | ||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||
| 415 | Alias for L<Mojolicious::Controller/"app">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||
| 417 | =head2 b | ||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||
| 419 | %= b('Joel is a slug')->slugify | ||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||
| 421 | Turn string into a L<Mojo::ByteStream> object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||
| 423 | =head2 c | ||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | %= c('a', 'b', 'c')->shuffle->join | ||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||
| 427 | Turn list into a L<Mojo::Collection> object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||
| 429 | =head2 config | ||||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||||
| 431 | %= config 'something' | ||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||
| 433 | Alias for L<Mojolicious/"config">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||||||
| 435 | =head2 content | ||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||
| 437 | %= content foo => begin | ||||||||||||||||||||||||||||||||||||||||||||||
| 438 | test | ||||||||||||||||||||||||||||||||||||||||||||||
| 439 | % end | ||||||||||||||||||||||||||||||||||||||||||||||
| 440 | %= content bar => 'Hello World!' | ||||||||||||||||||||||||||||||||||||||||||||||
| 441 | %= content 'foo' | ||||||||||||||||||||||||||||||||||||||||||||||
| 442 | %= content 'bar' | ||||||||||||||||||||||||||||||||||||||||||||||
| 443 | %= content | ||||||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||||||
| 445 | Store partial rendered content in a named buffer and retrieve it later, defaults to retrieving the named buffer | ||||||||||||||||||||||||||||||||||||||||||||||
| 446 | C<content>, which is used by the renderer for the C<layout> and C<extends> features. New content will be ignored if the | ||||||||||||||||||||||||||||||||||||||||||||||
| 447 | named buffer is already in use. | ||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||
| 449 | =head2 content_for | ||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||
| 451 | % content_for foo => begin | ||||||||||||||||||||||||||||||||||||||||||||||
| 452 | test | ||||||||||||||||||||||||||||||||||||||||||||||
| 453 | % end | ||||||||||||||||||||||||||||||||||||||||||||||
| 454 | %= content_for 'foo' | ||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||
| 456 | Same as L</"content">, but appends content to named buffers if they are already in use. | ||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||
| 458 | % content_for message => begin | ||||||||||||||||||||||||||||||||||||||||||||||
| 459 | Hello | ||||||||||||||||||||||||||||||||||||||||||||||
| 460 | % end | ||||||||||||||||||||||||||||||||||||||||||||||
| 461 | % content_for message => begin | ||||||||||||||||||||||||||||||||||||||||||||||
| 462 | world! | ||||||||||||||||||||||||||||||||||||||||||||||
| 463 | % end | ||||||||||||||||||||||||||||||||||||||||||||||
| 464 | %= content 'message' | ||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||
| 466 | =head2 content_with | ||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||
| 468 | % content_with foo => begin | ||||||||||||||||||||||||||||||||||||||||||||||
| 469 | test | ||||||||||||||||||||||||||||||||||||||||||||||
| 470 | % end | ||||||||||||||||||||||||||||||||||||||||||||||
| 471 | %= content_with 'foo' | ||||||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||||
| 473 | Same as L</"content">, but replaces content of named buffers if they are already in use. | ||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||
| 475 | % content message => begin | ||||||||||||||||||||||||||||||||||||||||||||||
| 476 | world! | ||||||||||||||||||||||||||||||||||||||||||||||
| 477 | % end | ||||||||||||||||||||||||||||||||||||||||||||||
| 478 | % content_with message => begin | ||||||||||||||||||||||||||||||||||||||||||||||
| 479 | Hello <%= content 'message' %> | ||||||||||||||||||||||||||||||||||||||||||||||
| 480 | % end | ||||||||||||||||||||||||||||||||||||||||||||||
| 481 | %= content 'message' | ||||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||
| 483 | =head2 csrf_token | ||||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||||
| 485 | %= csrf_token | ||||||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||||||
| 487 | Get CSRF token from L</"session">, and generate one if none exists. The token is masked with a fresh random value on | ||||||||||||||||||||||||||||||||||||||||||||||
| 488 | every call to protect against BREACH attacks. | ||||||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||||||
| 490 | =head2 current_route | ||||||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||||
| 492 | % if (current_route 'login') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 493 | Welcome to Mojolicious! | ||||||||||||||||||||||||||||||||||||||||||||||
| 494 | % } | ||||||||||||||||||||||||||||||||||||||||||||||
| 495 | %= current_route | ||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||
| 497 | Check or get name of current route. | ||||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||
| 499 | =head2 dumper | ||||||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||||||
| 501 | %= dumper {some => 'data'} | ||||||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||||||
| 503 | Dump a Perl data structure with L<Mojo::Util/"dumper">, very useful for debugging. | ||||||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||||
| 505 | =head2 exception_format | ||||||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||||||
| 507 | my $format = $c->exception_format; | ||||||||||||||||||||||||||||||||||||||||||||||
| 508 | $c = $c->exception_format('txt'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||||||
| 510 | Format for HTTP exceptions (C<html>, C<json>, or C<txt>), defaults to the value of L<Mojolicious/"exception_format">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||||||
| 512 | =head2 extends | ||||||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||||||
| 514 | % extends 'blue'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 515 | % extends 'blue', title => 'Blue!'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||||
| 517 | Set C<extends> stash value, all additional key/value pairs get merged into the L</"stash">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||
| 519 | =head2 flash | ||||||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||||||
| 521 | my $foo = $c->flash('foo'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 522 | $c = $c->flash({foo => 'bar'}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 523 | $c = $c->flash(foo => 'bar'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 524 | %= flash 'foo' | ||||||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||||||
| 526 | Data storage persistent only for the next request, stored in the L</"session">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||||
| 528 | # Show message after redirect | ||||||||||||||||||||||||||||||||||||||||||||||
| 529 | $c->flash(message => 'User created successfully!'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 530 | $c->redirect_to('show_user', id => 23); | ||||||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||||||
| 532 | =head2 inactivity_timeout | ||||||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||||||
| 534 | $c = $c->inactivity_timeout(3600); | ||||||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||||||
| 536 | Use L<Mojo::IOLoop/"stream"> to find the current connection and increase timeout if possible. | ||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||
| 538 | # Longer version | ||||||||||||||||||||||||||||||||||||||||||||||
| 539 | Mojo::IOLoop->stream($c->tx->connection)->timeout(3600); | ||||||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||||||
| 541 | =head2 include | ||||||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||||||
| 543 | %= include 'menubar' | ||||||||||||||||||||||||||||||||||||||||||||||
| 544 | %= include 'menubar', format => 'txt' | ||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||
| 546 | Alias for L<Mojolicious::Controller/"render_to_string">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||||||
| 548 | =head2 is_fresh | ||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||
| 550 | my $bool = $c->is_fresh; | ||||||||||||||||||||||||||||||||||||||||||||||
| 551 | my $bool = $c->is_fresh(etag => 'abc'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 552 | my $bool = $c->is_fresh(etag => 'W/"def"'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 553 | my $bool = $c->is_fresh(last_modified => $epoch); | ||||||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||||||
| 555 | Check freshness of request by comparing the C<If-None-Match> and C<If-Modified-Since> request headers to the C<ETag> | ||||||||||||||||||||||||||||||||||||||||||||||
| 556 | and C<Last-Modified> response headers with L<Mojolicious::Static/"is_fresh">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||||||
| 558 | # Add ETag/Last-Modified headers and check freshness before rendering | ||||||||||||||||||||||||||||||||||||||||||||||
| 559 | $c->is_fresh(etag => 'abc', last_modified => 1424985708) | ||||||||||||||||||||||||||||||||||||||||||||||
| 560 | ? $c->rendered(304) | ||||||||||||||||||||||||||||||||||||||||||||||
| 561 | : $c->render(text => 'I ♥ Mojolicious!'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||||||
| 563 | =head2 layout | ||||||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||||||
| 565 | % layout 'green'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 566 | % layout 'green', title => 'Green!'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||||||
| 568 | Set C<layout> stash value, all additional key/value pairs get merged into the L</"stash">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||||||
| 570 | =head2 log | ||||||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||||||
| 572 | my $log = $c->log; | ||||||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||||||
| 574 | Alternative to L<Mojolicious/"log"> that includes L<Mojo::Message::Request/"request_id"> with every log message. | ||||||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||||||
| 576 | # Log message with context | ||||||||||||||||||||||||||||||||||||||||||||||
| 577 | $c->log->debug('This is a log message with request id'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||||||
| 579 | # Pass logger with context to model | ||||||||||||||||||||||||||||||||||||||||||||||
| 580 | my $log = $c->log; | ||||||||||||||||||||||||||||||||||||||||||||||
| 581 | $c->some_model->create({foo => $foo}, $log); | ||||||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||||||
| 583 | =head2 param | ||||||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||||||
| 585 | %= param 'foo' | ||||||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||||||
| 587 | Alias for L<Mojolicious::Controller/"param">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||||||
| 589 | =head2 proxy->get_p | ||||||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||||||
| 591 | my $promise = $c->proxy->get_p('http://example.com' => {Accept => '*/*'}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||||||
| 593 | Perform non-blocking C<GET> request and forward response as efficiently as possible, takes the same arguments as | ||||||||||||||||||||||||||||||||||||||||||||||
| 594 | L<Mojo::UserAgent/"get"> and returns a L<Mojo::Promise> object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||||||
| 596 | # Forward with exception handling | ||||||||||||||||||||||||||||||||||||||||||||||
| 597 | $c->proxy->get_p('http://mojolicious.org')->catch(sub ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 598 | $c->log->debug("Proxy error: $err"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 599 | $c->render(text => 'Something went wrong!', status => 400); | ||||||||||||||||||||||||||||||||||||||||||||||
| 600 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||||||
| 602 | =head2 proxy->post_p | ||||||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||||||
| 604 | my $promise = $c->proxy->post_p('http://example.com' => {Accept => '*/*'}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||||||
| 606 | Perform non-blocking C<POST> request and forward response as efficiently as possible, takes the same arguments as | ||||||||||||||||||||||||||||||||||||||||||||||
| 607 | L<Mojo::UserAgent/"post"> and returns a L<Mojo::Promise> object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||||||
| 609 | # Forward with exception handling | ||||||||||||||||||||||||||||||||||||||||||||||
| 610 | $c->proxy->post_p('example.com' => form => {test => 'pass'})->catch(sub ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 611 | $c->log->debug("Proxy error: $err"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 612 | $c->render(text => 'Something went wrong!', status => 400); | ||||||||||||||||||||||||||||||||||||||||||||||
| 613 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||||||
| 615 | =head2 proxy->start_p | ||||||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||||||
| 617 | my $promise = $c->proxy->start_p(Mojo::Transaction::HTTP->new); | ||||||||||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||||||||||
| 619 | Perform non-blocking request for a custom L<Mojo::Transaction::HTTP> object and forward response as efficiently as | ||||||||||||||||||||||||||||||||||||||||||||||
| 620 | possible, returns a L<Mojo::Promise> object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||||||||||||
| 622 | # Forward with exception handling | ||||||||||||||||||||||||||||||||||||||||||||||
| 623 | my $tx = $c->ua->build_tx(GET => 'http://mojolicious.org'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 624 | $c->proxy->start_p($tx)->catch(sub ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 625 | $c->log->debug("Proxy error: $err"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 626 | $c->render(text => 'Something went wrong!', status => 400); | ||||||||||||||||||||||||||||||||||||||||||||||
| 627 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||||||
| 629 | # Forward with custom request and response headers | ||||||||||||||||||||||||||||||||||||||||||||||
| 630 | my $headers = $c->req->headers->clone->dehop; | ||||||||||||||||||||||||||||||||||||||||||||||
| 631 | $headers->header('X-Proxy' => 'Mojo'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 632 | my $tx = $c->ua->build_tx(GET => 'http://example.com' => $headers->to_hash); | ||||||||||||||||||||||||||||||||||||||||||||||
| 633 | $c->proxy->start_p($tx); | ||||||||||||||||||||||||||||||||||||||||||||||
| 634 | $tx->res->content->once(body => sub ($content) { $c->res->headers->header('X-Proxy' => 'Mojo') }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||||||||||||||
| 636 | =head2 redirect_to | ||||||||||||||||||||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||||||||||||||||||||
| 638 | $c = $c->redirect_to('named', foo => 'bar'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 639 | $c = $c->redirect_to('named', {foo => 'bar'}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 640 | $c = $c->redirect_to('/index.html'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 641 | $c = $c->redirect_to('http://example.com/index.html'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||||||||||||
| 643 | Prepare a C<302> (if the status code is not already C<3xx>) redirect response with C<Location> header, takes the same | ||||||||||||||||||||||||||||||||||||||||||||||
| 644 | arguments as L</"url_for">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||||||
| 646 | # Moved Permanently | ||||||||||||||||||||||||||||||||||||||||||||||
| 647 | $c->res->code(301); | ||||||||||||||||||||||||||||||||||||||||||||||
| 648 | $c->redirect_to('some_route'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||||||
| 650 | # Temporary Redirect | ||||||||||||||||||||||||||||||||||||||||||||||
| 651 | $c->res->code(307); | ||||||||||||||||||||||||||||||||||||||||||||||
| 652 | $c->redirect_to('some_route'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||||||||
| 654 | =head2 reply->asset | ||||||||||||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||||||||||||
| 656 | $c->reply->asset(Mojo::Asset::File->new); | ||||||||||||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||||||||||||
| 658 | Reply with a L<Mojo::Asset::File> or L<Mojo::Asset::Memory> object using L<Mojolicious::Static/"serve_asset">, and | ||||||||||||||||||||||||||||||||||||||||||||||
| 659 | perform content negotiation with C<Range>, C<If-Modified-Since> and C<If-None-Match> headers. | ||||||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||||||
| 661 | # Serve asset with custom modification time | ||||||||||||||||||||||||||||||||||||||||||||||
| 662 | my $asset = Mojo::Asset::Memory->new; | ||||||||||||||||||||||||||||||||||||||||||||||
| 663 | $asset->add_chunk('Hello World!')->mtime(784111777); | ||||||||||||||||||||||||||||||||||||||||||||||
| 664 | $c->res->headers->content_type('text/plain'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 665 | $c->reply->asset($asset); | ||||||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||||||
| 667 | # Serve static file if it exists | ||||||||||||||||||||||||||||||||||||||||||||||
| 668 | if (my $asset = $c->app->static->file('images/logo.png')) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 669 | $c->res->headers->content_type('image/png'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 670 | $c->reply->asset($asset); | ||||||||||||||||||||||||||||||||||||||||||||||
| 671 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||||||||||
| 673 | =head2 reply->exception | ||||||||||||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||||||||||||
| 675 | $c = $c->reply->exception('Oops!'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 676 | $c = $c->reply->exception(Mojo::Exception->new); | ||||||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||||||
| 678 | Render an exception response in the appropriate format by delegating to more specific exception helpers. | ||||||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||||||
| 680 | =head2 reply->file | ||||||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||||||
| 682 | $c->reply->file('/etc/passwd'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||||||||||||||||
| 684 | Reply with a static file from an absolute path anywhere on the file system using L<Mojolicious/"static">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||||||
| 686 | # Longer version | ||||||||||||||||||||||||||||||||||||||||||||||
| 687 | $c->reply->asset(Mojo::Asset::File->new(path => '/etc/passwd')); | ||||||||||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||||||||||
| 689 | # Serve file from an absolute path with a custom content type | ||||||||||||||||||||||||||||||||||||||||||||||
| 690 | $c->res->headers->content_type('application/myapp'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 691 | $c->reply->file('/home/sri/foo.txt'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||||||||||||||||
| 693 | # Serve file from a secret application directory | ||||||||||||||||||||||||||||||||||||||||||||||
| 694 | $c->reply->file($c->app->home->child('secret', 'file.txt')); | ||||||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||||||
| 696 | =head2 reply->html_exception | ||||||||||||||||||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||||||||||||||||
| 698 | $c = $c->reply->html_exception('Oops!'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 699 | $c = $c->reply->html_exception(Mojo::Exception->new); | ||||||||||||||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||||||||||||||
| 701 | Render the exception template C<exception.$mode.$format.*> or C<exception.$format.*> and set the response status code | ||||||||||||||||||||||||||||||||||||||||||||||
| 702 | to C<500>. Also sets the stash values C<exception> to a L<Mojo::Exception> object and C<snapshot> to a copy of the | ||||||||||||||||||||||||||||||||||||||||||||||
| 703 | L</"stash"> for use in the templates. | ||||||||||||||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||||||||||||||
| 705 | =head2 reply->html_not_found | ||||||||||||||||||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||||||||||||||||
| 707 | $c = $c->reply->html_not_found; | ||||||||||||||||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||||||||||||||||
| 709 | Render the not found template C<not_found.$mode.$format.*> or C<not_found.$format.*> and set the response status code | ||||||||||||||||||||||||||||||||||||||||||||||
| 710 | to C<404>. Also sets the stash value C<snapshot> to a copy of the L</"stash"> for use in the templates. | ||||||||||||||||||||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||||||||||||||||||||
| 712 | =head2 reply->json_exception | ||||||||||||||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||||||||||||||
| 714 | $c = $c->reply->json_exception('Oops!'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 715 | $c = $c->reply->json_exception(Mojo::Exception->new); | ||||||||||||||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||||||||||||||
| 717 | Render a JSON response and set the response status to C<500>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||||||
| 719 | =head2 reply->json_not_found | ||||||||||||||||||||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||||||||||||||||||||
| 721 | $c = $c->reply->json_not_found; | ||||||||||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||||||||||
| 723 | Render a JSON response and set the response status to C<404>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||||||||||||||||
| 725 | =head2 reply->not_found | ||||||||||||||||||||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||||||||||||||||||||
| 727 | $c = $c->reply->not_found; | ||||||||||||||||||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||||||||||||||||
| 729 | Render a not found response in the appropriate format by delegating to more specific exception helpers. | ||||||||||||||||||||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||||||||||||||||||||
| 731 | =head2 reply->static | ||||||||||||||||||||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||||||||||||||||||||
| 733 | $c->reply->static('images/logo.png'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 734 | $c->reply->static('../lib/MyApp.pm'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||||||||||||||||
| 736 | Reply with a static file using L<Mojolicious/"static">, usually from the C<public> directories or C<DATA> sections of | ||||||||||||||||||||||||||||||||||||||||||||||
| 737 | your application. Note that this helper uses a relative path, but does not protect from traversing to parent | ||||||||||||||||||||||||||||||||||||||||||||||
| 738 | directories. | ||||||||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||||||
| 740 | # Serve file from a relative path with a custom content type | ||||||||||||||||||||||||||||||||||||||||||||||
| 741 | $c->res->headers->content_type('application/myapp'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 742 | $c->reply->static('foo.txt'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||||||||||||||
| 744 | =head2 reply->txt_exception | ||||||||||||||||||||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||||||||||||||||||||
| 746 | $c = $c->reply->txt_exception('Oops!'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 747 | $c = $c->reply->txt_exception(Mojo::Exception->new); | ||||||||||||||||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||||||||||||||||
| 749 | Render a plain text response and set the response status to C<500>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||||||||||||||||
| 751 | =head2 reply->txt_not_found | ||||||||||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||||||||||
| 753 | $c = $c->reply->txt_not_found; | ||||||||||||||||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||||||||||||||||
| 755 | Render a plain text response and set the response status to C<404>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||||||||||||||||
| 757 | =head2 respond_to | ||||||||||||||||||||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||||||||||||||||||||
| 759 | $c = $c->respond_to( | ||||||||||||||||||||||||||||||||||||||||||||||
| 760 | json => {json => {message => 'Welcome!'}}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 761 | html => {template => 'welcome'}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 762 | any => sub {...} | ||||||||||||||||||||||||||||||||||||||||||||||
| 763 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||||||||||||||||||||
| 765 | Automatically select best possible representation for resource from C<format> C<GET>/C<POST> parameter, C<format> stash | ||||||||||||||||||||||||||||||||||||||||||||||
| 766 | value or C<Accept> request header, defaults to L<Mojolicious::Renderer/"default_format"> or rendering an empty C<204> | ||||||||||||||||||||||||||||||||||||||||||||||
| 767 | response. Each representation can be handled with a callback or a hash reference containing arguments to be passed to | ||||||||||||||||||||||||||||||||||||||||||||||
| 768 | L<Mojolicious::Controller/"render">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||||||||||||||||||||
| 770 | # Everything else than "json" and "xml" gets a 204 response | ||||||||||||||||||||||||||||||||||||||||||||||
| 771 | $c->respond_to( | ||||||||||||||||||||||||||||||||||||||||||||||
| 772 | json => sub { $c->render(json => {just => 'works'}) }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 773 | xml => {text => '<just>works</just>'}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 774 | any => {data => '', status => 204} | ||||||||||||||||||||||||||||||||||||||||||||||
| 775 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||||||||||||||||
| 777 | For more advanced negotiation logic you can also use L</"accepts">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||||||
| 779 | =head2 session | ||||||||||||||||||||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||||||||||||||||||||
| 781 | %= session 'foo' | ||||||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||||||
| 783 | Alias for L<Mojolicious::Controller/"session">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||||||||||||||||
| 785 | =head2 stash | ||||||||||||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||||||||||||
| 787 | %= stash 'foo' | ||||||||||||||||||||||||||||||||||||||||||||||
| 788 | % stash foo => 'bar'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||||||||||||||||
| 790 | Alias for L<Mojolicious::Controller/"stash">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||||||||||||||||
| 792 | %= stash('name') // 'Somebody' | ||||||||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||||||
| 794 | =head2 timing->begin | ||||||||||||||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||||||||||||||
| 796 | $c->timing->begin('foo'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||||||||||||||||||||
| 798 | Create named timestamp for L<"timing-E<gt>elapsed">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||||||||||||||||||||
| 800 | =head2 timing->elapsed | ||||||||||||||||||||||||||||||||||||||||||||||
| 801 | |||||||||||||||||||||||||||||||||||||||||||||||
| 802 | my $elapsed = $c->timing->elapsed('foo'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||||||||||||||||||||
| 804 | Return fractional amount of time in seconds since named timstamp has been created with L</"timing-E<gt>begin"> or | ||||||||||||||||||||||||||||||||||||||||||||||
| 805 | C<undef> if no such timestamp exists. | ||||||||||||||||||||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||||||||||||||||||||
| 807 | # Log timing information | ||||||||||||||||||||||||||||||||||||||||||||||
| 808 | $c->timing->begin('database_stuff'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 809 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 810 | my $elapsed = $c->timing->elapsed('database_stuff'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 811 | $c->app->log->debug("Database stuff took $elapsed seconds"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||||||||||||||||||||
| 813 | =head2 timing->rps | ||||||||||||||||||||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||||||||||||||||||||
| 815 | my $rps = $c->timing->rps('0.001'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||||||||||||||
| 817 | Return fractional number of requests that could be performed in one second if every singe one took the given amount of | ||||||||||||||||||||||||||||||||||||||||||||||
| 818 | time in seconds or C<undef> if the number is too low. | ||||||||||||||||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||||||||||||||||
| 820 | # Log more timing information | ||||||||||||||||||||||||||||||||||||||||||||||
| 821 | $c->timing->begin('web_stuff'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 822 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 823 | my $elapsed = $c->timing->elapsed('web_stuff'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 824 | my $rps = $c->timing->rps($elapsed); | ||||||||||||||||||||||||||||||||||||||||||||||
| 825 | $c->app->log->debug("Web stuff took $elapsed seconds ($rps per second)"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||||||||||||||||||||
| 827 | =head2 timing->server_timing | ||||||||||||||||||||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||||||||||||||||||||
| 829 | $c->timing->server_timing('metric'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 830 | $c->timing->server_timing('metric', 'Some Description'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 831 | $c->timing->server_timing('metric', 'Some Description', '0.001'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||||||||||||
| 833 | Create C<Server-Timing> header with optional description and duration. | ||||||||||||||||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||||||||||||||||
| 835 | # "Server-Timing: miss" | ||||||||||||||||||||||||||||||||||||||||||||||
| 836 | $c->timing->server_timing('miss'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||||||||||||||
| 838 | # "Server-Timing: dc;desc=atl" | ||||||||||||||||||||||||||||||||||||||||||||||
| 839 | $c->timing->server_timing('dc', 'atl'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||||||||||||||||
| 841 | # "Server-Timing: db;desc=Database;dur=0.0001" | ||||||||||||||||||||||||||||||||||||||||||||||
| 842 | $c->timing->begin('database_stuff'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 843 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 844 | my $elapsed = $c->timing->elapsed('database_stuff'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 845 | $c->timing->server_timing('db', 'Database', $elapsed); | ||||||||||||||||||||||||||||||||||||||||||||||
| 846 | |||||||||||||||||||||||||||||||||||||||||||||||
| 847 | # "Server-Timing: miss, dc;desc=atl" | ||||||||||||||||||||||||||||||||||||||||||||||
| 848 | $c->timing->server_timing('miss'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 849 | $c->timing->server_timing('dc', 'atl'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||||||
| 851 | =head2 title | ||||||||||||||||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||||||||||||||
| 853 | %= title | ||||||||||||||||||||||||||||||||||||||||||||||
| 854 | % title 'Welcome!'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 855 | % title 'Welcome!', foo => 'bar'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||||||||||||||||||||
| 857 | Get or set C<title> stash value, all additional key/value pairs get merged into the L</"stash">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||||||||||
| 859 | =head2 ua | ||||||||||||||||||||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||||||||||||||||||||
| 861 | %= ua->get('mojolicious.org')->result->dom->at('title')->text | ||||||||||||||||||||||||||||||||||||||||||||||
| 862 | |||||||||||||||||||||||||||||||||||||||||||||||
| 863 | Alias for L<Mojolicious/"ua">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||||||||||||||||||||
| 865 | =head2 url_for | ||||||||||||||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||||||||||||||
| 867 | %= url_for 'named', foo => 'bar', baz => 'yada' | ||||||||||||||||||||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||||||||||||||||||||
| 869 | Alias for L<Mojolicious::Controller/"url_for">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||||||||||||||||||||
| 871 | %= url_for('/index.html')->query(foo => 'bar') | ||||||||||||||||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||||||||||||||||
| 873 | =head2 url_for_asset | ||||||||||||||||||||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||||||||||||||||||||
| 875 | %= url_for_asset('/app.js'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||||||||||||||||||||
| 877 | Alias for L<Mojolicious::Controller/"url_for_asset">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||||||||||||||||||||
| 879 | =head2 url_for_file | ||||||||||||||||||||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||||||||||||||||||||
| 881 | %= url_for_file('/index.html'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 882 | |||||||||||||||||||||||||||||||||||||||||||||||
| 883 | Alias for L<Mojolicious::Controller/"url_for_file">. | ||||||||||||||||||||||||||||||||||||||||||||||
| 884 | |||||||||||||||||||||||||||||||||||||||||||||||
| 885 | =head2 url_with | ||||||||||||||||||||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||||||||||||||||||||
| 887 | %= url_with 'named', foo => 'bar', baz => 'yada' | ||||||||||||||||||||||||||||||||||||||||||||||
| 888 | |||||||||||||||||||||||||||||||||||||||||||||||
| 889 | Does the same as L</"url_for">, but inherits query parameters from the current request. | ||||||||||||||||||||||||||||||||||||||||||||||
| 890 | |||||||||||||||||||||||||||||||||||||||||||||||
| 891 | %= url_with->query({page => 2}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||||||||||||||||||||
| 893 | =head2 validation | ||||||||||||||||||||||||||||||||||||||||||||||
| 894 | |||||||||||||||||||||||||||||||||||||||||||||||
| 895 | my $v = $c->validation; | ||||||||||||||||||||||||||||||||||||||||||||||
| 896 | |||||||||||||||||||||||||||||||||||||||||||||||
| 897 | Get L<Mojolicious::Validator::Validation> object for current request to validate file uploads as well as C<GET> and | ||||||||||||||||||||||||||||||||||||||||||||||
| 898 | C<POST> parameters extracted from the query string and C<application/x-www-form-urlencoded> or C<multipart/form-data> | ||||||||||||||||||||||||||||||||||||||||||||||
| 899 | message body. Parts of the request body need to be loaded into memory to parse C<POST> parameters, so you have to make | ||||||||||||||||||||||||||||||||||||||||||||||
| 900 | sure it is not excessively large. There's a 16MiB limit for requests by default. | ||||||||||||||||||||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||||||||||||||||||||
| 902 | # Validate GET/POST parameter | ||||||||||||||||||||||||||||||||||||||||||||||
| 903 | my $v = $c->validation; | ||||||||||||||||||||||||||||||||||||||||||||||
| 904 | $v->required('title', 'trim')->size(3, 50); | ||||||||||||||||||||||||||||||||||||||||||||||
| 905 | my $title = $v->param('title'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 906 | |||||||||||||||||||||||||||||||||||||||||||||||
| 907 | # Validate file upload | ||||||||||||||||||||||||||||||||||||||||||||||
| 908 | my $v = $c->validation; | ||||||||||||||||||||||||||||||||||||||||||||||
| 909 | $v->required('tarball')->upload->size(1, 1048576); | ||||||||||||||||||||||||||||||||||||||||||||||
| 910 | my $tarball = $v->param('tarball'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 911 | |||||||||||||||||||||||||||||||||||||||||||||||
| 912 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||
| 913 | |||||||||||||||||||||||||||||||||||||||||||||||
| 914 | L<Mojolicious::Plugin::DefaultHelpers> inherits all methods from L<Mojolicious::Plugin> and implements the following | ||||||||||||||||||||||||||||||||||||||||||||||
| 915 | new ones. | ||||||||||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||||||||||
| 917 | =head2 register | ||||||||||||||||||||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||||||||||||||||||||
| 919 | $plugin->register(Mojolicious->new); | ||||||||||||||||||||||||||||||||||||||||||||||
| 920 | |||||||||||||||||||||||||||||||||||||||||||||||
| 921 | Register helpers in L<Mojolicious> application. | ||||||||||||||||||||||||||||||||||||||||||||||
| 922 | |||||||||||||||||||||||||||||||||||||||||||||||
| 923 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||
| 924 | |||||||||||||||||||||||||||||||||||||||||||||||
| 925 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 926 | |||||||||||||||||||||||||||||||||||||||||||||||
| 927 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||