| 1 | package Mojolicious::Controller; | ||||||||||||||||||||||||||||||||||||
| 2 | 21 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||
| 4 | # No imports, for security reasons! | ||||||||||||||||||||||||||||||||||||
| 5 | 21 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 21 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 21 | ▶ | use Mojo::ByteStream; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 21 | ▶ | use Mojo::DynamicMethods -dispatch; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 21 | ▶ | use Mojo::URL; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 21 | ▶ | use Mojo::Util; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | 21 | ▶ | use Mojolicious::Routes::Match; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | 21 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | 21 | my $ABSOLUTE = qr!^(?:[^:/?#]+:|//|#)!; | |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | 21 | has [qw(app tx)] => undef, weak => 1; | |||||||||||||||||||||||||||||||||||
| 17 | 155 | ▶ | has match => sub { Mojolicious::Routes::Match->new(root => shift->app->routes) }; | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||
| 19 | 1271 | ▶ | sub BUILD_DYNAMIC { | ||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD_DYNAMIC: called CC=1 SCAR=2.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 20 | 1271 | my ($class, $method, $dyn_methods) = @_; | |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
email_field: not called CC=2 SCAR=12.4
Subroutine
javascript: called CC=2 SCAR=12.4
| |||||||||||||||||||||||||||||||||||||
| 23 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 24 | 0 | my $dynamic = $dyn_methods->{$self->{app}{renderer}}{$method}; | |||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | return $self->$dynamic(@_) if $dynamic; | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 26 | 0 | my $package = ref $self; | |||||||||||||||||||||||||||||||||||
| 27 | 0 | Carp::croak qq{Can't locate object method "$method" via package "$package"}; | |||||||||||||||||||||||||||||||||||
| 28 | 1271 | }; | |||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||
| 31 | 2 | ▶ | sub continue { $_[0]->app->routes->continue($_[0]) } | ||||||||||||||||||||||||||||||||||
|
Subroutine
continue: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | sub cookie { | ||||||||||||||||||||||||||||||||||
|
Subroutine
cookie: called CC=5 SCAR=17.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 34 | 2 | my ($self, $name) = (shift, shift); | |||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||
| 36 | # Response cookie | ||||||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||
| 39 | # Cookie too big | ||||||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | my $cookie = {name => $name, value => shift, %{shift // {}}}; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() // {}
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||
| 41 | 2 | ▶ | $self->helpers->log->error(qq{Cookie "$name" is bigger than 4KiB}) if length $cookie->{value} > 4096; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||
| 43 | 2 | $self->res->cookies($cookie); | |||||||||||||||||||||||||||||||||||
| 44 | 2 | return $self; | |||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||
| 47 | # Request cookies | ||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | return undef unless my $cookie = $self->req->cookie($name); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 49 | 0 | return $cookie->value; | |||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub encrypted_cookie { | ||||||||||||||||||||||||||||||||||
|
Subroutine
encrypted_cookie: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 53 | 0 | my ($self, $name, $value, $options) = @_; | |||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||
| 55 | # Request cookie | ||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | return $self->every_encrypted_cookie($name)->[-1] unless defined $value; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | # Response cookie | ||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $app = $self->app; | |||||||||||||||||||||||||||||||||||
| 60 | 0 | my $secret = $app->secrets->[0]; | |||||||||||||||||||||||||||||||||||
| 61 | 0 | my $moniker = $app->moniker; | |||||||||||||||||||||||||||||||||||
| 62 | 0 | return $self->cookie($name, Mojo::Util::encrypt_cookie($value, $secret, $moniker), $options); | |||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub every_cookie { [map { $_->value } @{shift->req->every_cookie(shift)}] } | ||||||||||||||||||||||||||||||||||
|
Subroutine
every_cookie: called CC=1 SCAR=0.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub every_encrypted_cookie { | ||||||||||||||||||||||||||||||||||
|
Subroutine
every_encrypted_cookie: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 68 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $app = $self->app; | |||||||||||||||||||||||||||||||||||
| 71 | 0 | my $secrets = $app->secrets; | |||||||||||||||||||||||||||||||||||
| 72 | 0 | my $moniker = $app->moniker; | |||||||||||||||||||||||||||||||||||
| 73 | 0 | my @results; | |||||||||||||||||||||||||||||||||||
| 74 | 0 | for my $value (@{$self->every_cookie($name)}) { | |||||||||||||||||||||||||||||||||||
| 75 | 0 | my $decrypted; | |||||||||||||||||||||||||||||||||||
| 76 | 0 | for my $secret (@$secrets) { | |||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | last if defined($decrypted = Mojo::Util::decrypt_cookie($value, $secret, $moniker)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if (defined $decrypted) { push @results, $decrypted } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||
| 81 | 0 | else { $self->helpers->log->trace(qq{Cookie "$name" is not encrypted}) } | |||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||
| 84 | 0 | return \@results; | |||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||
| 87 | 72 | ▶ | sub every_param { | ||||||||||||||||||||||||||||||||||
|
Subroutine
every_param: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 88 | 72 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||
| 90 | # Captured unreserved values | ||||||||||||||||||||||||||||||||||||
| 91 | 72 | ▶ | my $captures = $self->stash->{'mojo.captures'} //= {}; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->stash->{"mojo.captures"} //= {}
MC/DC100%1/1
$self->stash->{"mojo.captures"} //= {}
$self->stash->{"mojo.captures"}
Truth table100%2/2
A
A= $self->stash->{"mojo.captures"}
| |||||||||||||||||||||||||||||||||||||
| 92 | 72 | ▶ | if (exists $captures->{$name} && !$self->app->routes->is_reserved($name)) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $captures->{$name} and not $self->app->routes->is_reserved($name)
MC/DC100%2/2
exists $captures->{$name} and not $self->app->routes->is_reserved($name)
exists $captures->{$name}
not $self->app->routes->is_reserved($name)
Truth table100%3/3
A and not(B)
A= exists $captures->{$name}B= not $self->app->routes->is_reserved($name)
| |||||||||||||||||||||||||||||||||||||
| 93 | 16 | my $value = $captures->{$name}; | |||||||||||||||||||||||||||||||||||
| 94 | 16 | ▶ | return ref $value eq 'ARRAY' ? $value : [$value]; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||
| 97 | # Uploads or param values | ||||||||||||||||||||||||||||||||||||
| 98 | 56 | my $req = $self->req; | |||||||||||||||||||||||||||||||||||
| 99 | 56 | my $uploads = $req->every_upload($name); | |||||||||||||||||||||||||||||||||||
| 100 | 56 | ▶ | return @$uploads ? $uploads : $req->every_param($name); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||
| 103 | 23 | ▶ | sub every_signed_cookie { | ||||||||||||||||||||||||||||||||||
|
Subroutine
every_signed_cookie: called CC=7 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 104 | 23 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | 23 | my $secrets = $self->app->secrets; | |||||||||||||||||||||||||||||||||||
| 107 | 23 | my @results; | |||||||||||||||||||||||||||||||||||
| 108 | 23 | for my $value (@{$self->every_cookie($name)}) { | |||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | # Check signature with rotating secrets | ||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ($value =~ s/--([^\-]+)$//) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $signature = $1; | |||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $valid; | |||||||||||||||||||||||||||||||||||
| 115 | 0 | for my $secret (@$secrets) { | |||||||||||||||||||||||||||||||||||
| 116 | 0 | my $check = Digest::SHA::hmac_sha256_hex("$name=$value", $secret); | |||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | ++$valid and last if Mojo::Util::secure_compare($signature, $check); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
Mojo::Util::secure_compare($signature, $check) && (++$valid && (last))
++$valid and last
MC/DC0%0/2
Mojo::Util::secure_compare($signature, $check) && (++$valid && (last))
Mojo::Util::secure_compare($signature, $check)
++$valid
Truth table0%0/3
A && B
A= Mojo::Util::secure_compare($signature, $check)B= ++$valid
| |||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ($valid) { push @results, $value } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||
| 121 | 0 | else { $self->helpers->log->trace(qq{Cookie "$name" has bad signature}) } | |||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||
| 124 | 0 | else { $self->helpers->log->trace(qq{Cookie "$name" is not signed}) } | |||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||
| 127 | 23 | return \@results; | |||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | sub finish { | ||||||||||||||||||||||||||||||||||
|
Subroutine
finish: called CC=8 SCAR=29.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 131 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | # WebSocket | ||||||||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | my $tx = $self->tx || Carp::croak 'Transaction already destroyed'; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->tx || Carp::croak('Transaction already destroyed')
MC/DC100%1/1
$self->tx || Carp::croak('Transaction already destroyed')
$self->tx
Truth table100%2/2
A
A= $self->tx
| |||||||||||||||||||||||||||||||||||||
| 135 | 1 | ▶ | $tx->finish(@_) and return $tx->established ? $self : $self->rendered(101) if $tx->is_websocket; | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition20%1/5
$tx->is_websocket && ($tx->finish(@_) && (return $tx->established ? $self : $self->rendered(101)))
$tx->finish(@_) and return $tx->established ? $self : $self->rendered(101)
MC/DC0%0/2
$tx->is_websocket && ($tx->finish(@_) && (return $tx->established ? $self : $self->rendered(101)))
$tx->is_websocket
$tx->finish(@_)
Truth table33%1/3
A && B
A= $tx->is_websocketB= $tx->finish(@_)
| |||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||
| 137 | # Chunked stream | ||||||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | return @_ ? $self->write_chunk(@_)->write_chunk('') : $self->write_chunk('') if $tx->res->content->is_chunked; | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||
| 140 | # Normal stream | ||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | return @_ ? $self->write(@_)->write('') : $self->write(''); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||
| 144 | 859 | ▶ | sub helpers { $_[0]->app->renderer->get_helper('')->($_[0]) } | ||||||||||||||||||||||||||||||||||
|
Subroutine
helpers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||
| 146 | 3 | ▶ | sub on { | ||||||||||||||||||||||||||||||||||
|
Subroutine
on: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 147 | 3 | my ($self, $name, $cb) = @_; | |||||||||||||||||||||||||||||||||||
| 148 | 3 | ▶ | my $tx = $self->tx || Carp::croak 'Transaction already destroyed'; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->tx || Carp::croak('Transaction already destroyed')
MC/DC100%1/1
$self->tx || Carp::croak('Transaction already destroyed')
$self->tx
Truth table100%2/2
A
A= $self->tx
| |||||||||||||||||||||||||||||||||||||
| 149 | 2 | ▶ | $self->rendered(101) if $tx->is_websocket && !$tx->established; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tx->is_websocket and not $tx->established
MC/DC50%1/2
$tx->is_websocket and not $tx->established
$tx->is_websocket
not $tx->established
Truth table66%2/3
A and not(B)
A= $tx->is_websocketB= not $tx->established
| |||||||||||||||||||||||||||||||||||||
| 150 | 2 | ▶ | return $tx->on($name => sub { shift; $self->$cb(@_) }); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||
| 153 | 83 | ▶ | sub param { | ||||||||||||||||||||||||||||||||||
|
Subroutine
param: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 154 | 83 | my ($self, $name) = (shift, shift); | |||||||||||||||||||||||||||||||||||
| 155 | 83 | ▶ | return $self->every_param($name)->[-1] unless @_; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 156 | 25 | ▶ | $self->stash->{'mojo.captures'}{$name} = @_ > 1 ? [@_] : $_[0]; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 157 | 25 | return $self; | |||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||
| 160 | 217 | ▶ | sub render { | ||||||||||||||||||||||||||||||||||
|
Subroutine
render: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 161 | 217 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||
| 163 | # Template may be first argument | ||||||||||||||||||||||||||||||||||||
| 164 | 217 | ▶ | my ($template, $args) = (@_ % 2 ? shift : undef, {@_}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 165 | 217 | ▶ | $args->{template} = $template if $template; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 166 | 217 | my $app = $self->app; | |||||||||||||||||||||||||||||||||||
| 167 | 217 | my $plugins = $app->plugins->emit_hook(before_render => $self, $args); | |||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||
| 169 | # Localize "extends" and "layout" to allow argument overrides | ||||||||||||||||||||||||||||||||||||
| 170 | 217 | my ($maybe, $ts) = @{$args}{'mojo.maybe', 'mojo.string'}; | |||||||||||||||||||||||||||||||||||
| 171 | 217 | my $stash = $self->stash; | |||||||||||||||||||||||||||||||||||
| 172 | 217 | ▶ | local $stash->{layout} = $stash->{layout} if exists $stash->{layout}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 173 | 217 | ▶ | local $stash->{extends} = $stash->{extends} if exists $stash->{extends}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||
| 175 | # Rendering to string | ||||||||||||||||||||||||||||||||||||
| 176 | 217 | ▶ | local @{$stash}{keys %$args} if $ts || $maybe; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ts or $maybe
MC/DC100%2/2
$ts or $maybe
$ts
$maybe
Truth table100%3/3
A or B
A= $tsB= $maybe
| |||||||||||||||||||||||||||||||||||||
| 177 | 217 | ▶ | delete @{$stash}{qw(layout extends)} if $ts; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||
| 179 | # All other arguments just become part of the stash | ||||||||||||||||||||||||||||||||||||
| 180 | 217 | @$stash{keys %$args} = values %$args; | |||||||||||||||||||||||||||||||||||
| 181 | 217 | my $renderer = $app->renderer; | |||||||||||||||||||||||||||||||||||
| 182 | 217 | my ($output, $format) = $renderer->render($self); | |||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||
| 184 | # Maybe no 404 | ||||||||||||||||||||||||||||||||||||
| 185 | 214 | ▶ | return defined $output ? Mojo::ByteStream->new($output) : undef if $ts; | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 186 | 203 | ▶ | return $maybe ? undef : Carp::croak("Could not render a response") unless defined $output; | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||
| 188 | 144 | $plugins->emit_hook(after_render => $self, \$output, $format); | |||||||||||||||||||||||||||||||||||
| 189 | 144 | return $renderer->respond($self, $output, $format, $stash->{status}); | |||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||
| 192 | 171 | ▶ | sub render_later { shift->stash('mojo.rendered' => 1) } | ||||||||||||||||||||||||||||||||||
|
Subroutine
render_later: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||
| 194 | 145 | ▶ | sub render_maybe { shift->render(@_, 'mojo.maybe' => 1) } | ||||||||||||||||||||||||||||||||||
|
Subroutine
render_maybe: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||
| 196 | 11 | ▶ | sub render_to_string { shift->render(@_, 'mojo.string' => 1) } | ||||||||||||||||||||||||||||||||||
|
Subroutine
render_to_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||
| 198 | 186 | ▶ | sub rendered { | ||||||||||||||||||||||||||||||||||
|
Subroutine
rendered: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 199 | 186 | my ($self, $status) = @_; | |||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||
| 201 | # Make sure we have a status | ||||||||||||||||||||||||||||||||||||
| 202 | 186 | ▶ | $self->res->code($status) if $status; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||
| 204 | # Finish transaction | ||||||||||||||||||||||||||||||||||||
| 205 | 186 | my $stash = $self->stash; | |||||||||||||||||||||||||||||||||||
| 206 | 186 | ▶ | if (!$stash->{'mojo.finished'} && ++$stash->{'mojo.finished'}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $stash->{"mojo.finished"} and ++$stash->{"mojo.finished"}
MC/DC50%1/2
not $stash->{"mojo.finished"} and ++$stash->{"mojo.finished"}
not $stash->{"mojo.finished"}
++$stash->{"mojo.finished"}
Truth table66%2/3
not(A) and B
A= not $stash->{"mojo.finished"}B= ++$stash->{"mojo.finished"}
| |||||||||||||||||||||||||||||||||||||
| 207 | 171 | my $res = $self->res; | |||||||||||||||||||||||||||||||||||
| 208 | 171 | ▶ | $res->code(200) if !$status && !$res->code; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$status or $res->code
MC/DC100%2/2
$status or $res->code
$status
$res->code
Truth table100%3/3
A or B
A= $statusB= $res->code
| |||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||
| 210 | # Disable auto rendering and stop timer | ||||||||||||||||||||||||||||||||||||
| 211 | 171 | my $app = $self->render_later->app; | |||||||||||||||||||||||||||||||||||
| 212 | 10 | ▶ | $self->helpers->log->trace(sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.0
| |||||||||||||||||||||||||||||||||||||
| 213 | 10 | my $timing = $self->helpers->timing; | |||||||||||||||||||||||||||||||||||
| 214 | 10 | ▶ | my $elapsed = $timing->elapsed('mojo.timer') // 0; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$timing->elapsed("mojo.timer") // 0
MC/DC0%0/1
$timing->elapsed("mojo.timer") // 0
$timing->elapsed("mojo.timer")
Truth table50%1/2
A
A= $timing->elapsed("mojo.timer")
| |||||||||||||||||||||||||||||||||||||
| 215 | 10 | ▶ | my $rps = $timing->rps($elapsed) // '??'; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$timing->rps($elapsed) // "??"
MC/DC0%0/1
$timing->rps($elapsed) // "??"
$timing->rps($elapsed)
Truth table50%1/2
A
A= $timing->rps($elapsed)
| |||||||||||||||||||||||||||||||||||||
| 216 | 10 | my $code = $res->code; | |||||||||||||||||||||||||||||||||||
| 217 | 10 | ▶ | my $msg = $res->message || $res->default_message($code); | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$res->message || $res->default_message($code)
MC/DC0%0/2
$res->message || $res->default_message($code)
$res->message
$res->default_message($code)
Truth table33%1/3
A || B
A= $res->messageB= $res->default_message($code)
| |||||||||||||||||||||||||||||||||||||
| 218 | 10 | return "$code $msg (${elapsed}s, $rps/s)"; | |||||||||||||||||||||||||||||||||||
| 219 | 171 | ▶ | }) unless $stash->{'mojo.static'}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||
| 221 | 171 | $app->plugins->emit_hook_reverse(after_dispatch => $self); | |||||||||||||||||||||||||||||||||||
| 222 | 171 | $app->sessions->store($self); | |||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||
| 224 | 186 | $self->tx->resume; | |||||||||||||||||||||||||||||||||||
| 225 | 186 | return $self; | |||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||
| 228 | 913 | ▶ | sub req { (shift->tx || Carp::croak 'Transaction already destroyed')->req } | ||||||||||||||||||||||||||||||||||
|
Subroutine
req: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
(shift())->tx || Carp::croak('Transaction already destroyed')
MC/DC100%1/1
(shift())->tx || Carp::croak('Transaction already destroyed')
(shift())->tx
Truth table100%2/2
A
A= (shift())->tx
| |||||||||||||||||||||||||||||||||||||
| 229 | 661 | ▶ | sub res { (shift->tx || Carp::croak 'Transaction already destroyed')->res } | ||||||||||||||||||||||||||||||||||
|
Subroutine
res: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
(shift())->tx || Carp::croak('Transaction already destroyed')
MC/DC100%1/1
(shift())->tx || Carp::croak('Transaction already destroyed')
(shift())->tx
Truth table100%2/2
A
A= (shift())->tx
| |||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||
| 231 | 3 | ▶ | sub send { | ||||||||||||||||||||||||||||||||||
|
Subroutine
send: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 232 | 3 | my ($self, $msg, $cb) = @_; | |||||||||||||||||||||||||||||||||||
| 233 | 3 | ▶ | my $tx = $self->tx || Carp::croak 'Transaction already destroyed'; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->tx || Carp::croak('Transaction already destroyed')
MC/DC100%1/1
$self->tx || Carp::croak('Transaction already destroyed')
$self->tx
Truth table100%2/2
A
A= $self->tx
| |||||||||||||||||||||||||||||||||||||
| 234 | 2 | ▶ | Carp::croak 'No WebSocket connection to send message to' unless $tx->is_websocket; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | $tx->send($msg, $cb ? sub { shift; $self->$cb(@_) } : ()); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 236 | 1 | ▶ | return $tx->established ? $self : $self->rendered(101); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||
| 239 | 23 | ▶ | sub session { | ||||||||||||||||||||||||||||||||||
|
Subroutine
session: called CC=7 SCAR=20.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 240 | 23 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||
| 242 | 23 | my $stash = $self->stash; | |||||||||||||||||||||||||||||||||||
| 243 | 23 | ▶ | $self->app->sessions->load($self) unless exists $stash->{'mojo.active_session'}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||
| 245 | # Hash | ||||||||||||||||||||||||||||||||||||
| 246 | 23 | ▶ | my $session = $stash->{'mojo.session'} //= {}; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$stash->{"mojo.session"} //= {}
MC/DC100%1/1
$stash->{"mojo.session"} //= {}
$stash->{"mojo.session"}
Truth table100%2/2
A
A= $stash->{"mojo.session"}
| |||||||||||||||||||||||||||||||||||||
| 247 | 23 | ▶ | return $session unless @_; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||
| 249 | # Get | ||||||||||||||||||||||||||||||||||||
| 250 | 1 | ▶ | return $session->{$_[0]} unless @_ > 1 || ref $_[0]; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ > 1 or ref $_[0]
MC/DC0%0/2
@_ > 1 or ref $_[0]
@_ > 1
ref $_[0]
Truth table33%1/3
A or B
A= @_ > 1B= ref $_[0]
| |||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||
| 252 | # Set | ||||||||||||||||||||||||||||||||||||
| 253 | 1 | ▶ | my $values = ref $_[0] ? $_[0] : {@_}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 254 | 1 | @$session{keys %$values} = values %$values; | |||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||
| 256 | 1 | return $self; | |||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||
| 259 | 24 | ▶ | sub signed_cookie { | ||||||||||||||||||||||||||||||||||
|
Subroutine
signed_cookie: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 260 | 24 | my ($self, $name, $value, $options) = @_; | |||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||
| 262 | # Request cookie | ||||||||||||||||||||||||||||||||||||
| 263 | 24 | ▶ | return $self->every_signed_cookie($name)->[-1] unless defined $value; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||
| 265 | # Response cookie | ||||||||||||||||||||||||||||||||||||
| 266 | 1 | my $sum = Digest::SHA::hmac_sha256_hex("$name=$value", $self->app->secrets->[0]); | |||||||||||||||||||||||||||||||||||
| 267 | 1 | return $self->cookie($name, "$value--$sum", $options); | |||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||
| 270 | 3479 | ▶ | sub stash { Mojo::Util::_stash(stash => @_) } | ||||||||||||||||||||||||||||||||||
|
Subroutine
stash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||
| 272 | 158 | ▶ | sub url_for { | ||||||||||||||||||||||||||||||||||
|
Subroutine
url_for: called CC=12 SCAR=25.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 273 | 158 | ▶ | my ($self, $target) = (shift, shift // ''); | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() // ""
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||
| 275 | # Absolute URL | ||||||||||||||||||||||||||||||||||||
| 276 | 158 | ▶ | return $target if Scalar::Util::blessed $target && $target->isa('Mojo::URL'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($target) and $target->isa("Mojo::URL")
MC/DC50%1/2
builtin::blessed($target) and $target->isa("Mojo::URL")
builtin::blessed($target)
$target->isa("Mojo::URL")
Truth table66%2/3
A and B
A= builtin::blessed($target)B= $target->isa("Mojo::URL")
| |||||||||||||||||||||||||||||||||||||
| 277 | 152 | ▶ | return Mojo::URL->new($target) if $target =~ $ABSOLUTE; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||
| 279 | # Base | ||||||||||||||||||||||||||||||||||||
| 280 | 152 | my $url = Mojo::URL->new; | |||||||||||||||||||||||||||||||||||
| 281 | 152 | my $req = $self->req; | |||||||||||||||||||||||||||||||||||
| 282 | 152 | my $base = $url->base($req->url->base->clone)->base->userinfo(undef); | |||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||
| 284 | # Relative URL | ||||||||||||||||||||||||||||||||||||
| 285 | 152 | my $path = $url->path; | |||||||||||||||||||||||||||||||||||
| 286 | 152 | ▶ | if ($target =~ m!^/!) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 287 | 134 | ▶ | if (defined(my $prefix = $self->stash->{path})) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 288 | 0 | my $real = $req->url->path->to_route; | |||||||||||||||||||||||||||||||||||
| 289 | 0 | $real =~ s!/?\Q$prefix\E$!$target!; | |||||||||||||||||||||||||||||||||||
| 290 | 0 | $target = $real; | |||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||
| 292 | 134 | $url->parse($target); | |||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||
| 295 | # Route | ||||||||||||||||||||||||||||||||||||
| 296 | else { | ||||||||||||||||||||||||||||||||||||
| 297 | 18 | my $generated = $self->match->path_for($target, @_); | |||||||||||||||||||||||||||||||||||
| 298 | 18 | ▶ | $path->parse($generated->{path}) if $generated->{path}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 299 | 18 | ▶ | $base->scheme($base->protocol eq 'https' ? 'wss' : 'ws') if $generated->{websocket}; | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||
| 302 | # Make path absolute (skip parsing if there is no base path and nothing would need to be decoded) | ||||||||||||||||||||||||||||||||||||
| 303 | 152 | my $base_path = $base->path; | |||||||||||||||||||||||||||||||||||
| 304 | 152 | ▶ | return $url if $base_path->to_string eq '' && index($path->to_string, '%') < 0; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$base_path->to_string eq "" and index($path->to_string, "%") == -1
MC/DC50%1/2
$base_path->to_string eq "" and index($path->to_string, "%") == -1
$base_path->to_string eq ""
index($path->to_string, "%") == -1
Truth table66%2/3
A and B
A= $base_path->to_string eq ""B= index($path->to_string, "%") == -1
| |||||||||||||||||||||||||||||||||||||
| 305 | 4 | unshift @{$path->parts}, @{$base_path->parts}; | |||||||||||||||||||||||||||||||||||
| 306 | 4 | $base_path->parts([])->trailing_slash(0); | |||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||
| 308 | 4 | return $url; | |||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | sub url_for_asset { | ||||||||||||||||||||||||||||||||||
|
Subroutine
url_for_asset: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 312 | 0 | my ($self, $asset) = @_; | |||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | return $self->url_for($asset) if $asset =~ $ABSOLUTE; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||
| 315 | # Bundlers can rebuild assets with new names at any time during development | ||||||||||||||||||||||||||||||||||||
| 316 | 0 | my $app = $self->app; | |||||||||||||||||||||||||||||||||||
| 317 | 0 | ▶ | $app->static->warmup if $app->mode eq 'development'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 318 | 0 | return $self->url_for($app->static->asset_path($asset)); | |||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||
| 321 | 128 | ▶ | sub url_for_file { | ||||||||||||||||||||||||||||||||||
|
Subroutine
url_for_file: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 322 | 128 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||
| 323 | 128 | ▶ | return $self->url_for($file =~ $ABSOLUTE ? $file : $self->app->static->file_path($file)); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | sub write { | ||||||||||||||||||||||||||||||||||
|
Subroutine
write: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 327 | 0 | my ($self, $chunk, $cb) = @_; | |||||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | $self->res->content->write($chunk, $cb ? sub { shift; $self->$cb(@_) } : ()); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 329 | 0 | return $self->rendered; | |||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||
| 332 | 18 | ▶ | sub write_chunk { | ||||||||||||||||||||||||||||||||||
|
Subroutine
write_chunk: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 333 | 18 | my ($self, $chunk, $cb) = @_; | |||||||||||||||||||||||||||||||||||
| 334 | 18 | ▶ | $self->res->content->write_chunk($chunk, $cb ? sub { shift; $self->$cb(@_) } : ()); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 335 | 18 | return $self->rendered; | |||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | sub write_sse { | ||||||||||||||||||||||||||||||||||
|
Subroutine
write_sse: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 339 | 0 | my ($self, $event, $cb) = @_; | |||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | $self->res->content->write_sse($event, $cb ? sub { shift; $self->$cb(@_) } : ()); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 341 | 0 | return $self->rendered; | |||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||
| 344 | 21 | 1; | |||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||
| 346 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||
| 348 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||
| 350 | Mojolicious::Controller - Controller base class | ||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||
| 352 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||
| 354 | # Controller | ||||||||||||||||||||||||||||||||||||
| 355 | package MyApp::Controller::Foo; | ||||||||||||||||||||||||||||||||||||
| 356 | use Mojo::Base 'Mojolicious::Controller', -signatures; | ||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||
| 358 | # Action | ||||||||||||||||||||||||||||||||||||
| 359 | sub bar ($self) { | ||||||||||||||||||||||||||||||||||||
| 360 | my $name = $self->param('name'); | ||||||||||||||||||||||||||||||||||||
| 361 | $self->res->headers->cache_control('max-age=1, no-cache'); | ||||||||||||||||||||||||||||||||||||
| 362 | $self->render(json => {hello => $name}); | ||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||
| 365 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||
| 367 | L<Mojolicious::Controller> is the base class for your L<Mojolicious> controllers. It is also the default controller | ||||||||||||||||||||||||||||||||||||
| 368 | class unless you set L<Mojolicious/"controller_class">. | ||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||
| 370 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||
| 372 | L<Mojolicious::Controller> implements the following attributes. | ||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||
| 374 | =head2 app | ||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||
| 376 | my $app = $c->app; | ||||||||||||||||||||||||||||||||||||
| 377 | $c = $c->app(Mojolicious->new); | ||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||
| 379 | A reference back to the application that dispatched to this controller, usually a L<Mojolicious> object. Note that this | ||||||||||||||||||||||||||||||||||||
| 380 | attribute is weakened. | ||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||
| 382 | # Use application logger | ||||||||||||||||||||||||||||||||||||
| 383 | $c->app->log->debug('Hello Mojo'); | ||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||
| 385 | # Generate path | ||||||||||||||||||||||||||||||||||||
| 386 | my $path = $c->app->home->child('templates', 'foo', 'bar.html.ep'); | ||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||
| 388 | =head2 match | ||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||
| 390 | my $m = $c->match; | ||||||||||||||||||||||||||||||||||||
| 391 | $c = $c->match(Mojolicious::Routes::Match->new); | ||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||
| 393 | Router results for the current request, defaults to a L<Mojolicious::Routes::Match> object. | ||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||
| 395 | # Introspect | ||||||||||||||||||||||||||||||||||||
| 396 | my $name = $c->match->endpoint->name; | ||||||||||||||||||||||||||||||||||||
| 397 | my $foo = $c->match->endpoint->pattern->defaults->{foo}; | ||||||||||||||||||||||||||||||||||||
| 398 | my $action = $c->match->stack->[-1]{action}; | ||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||
| 400 | =head2 tx | ||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||
| 402 | my $tx = $c->tx; | ||||||||||||||||||||||||||||||||||||
| 403 | $c = $c->tx(Mojo::Transaction::HTTP->new); | ||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||
| 405 | The transaction that is currently being processed, usually a L<Mojo::Transaction::HTTP> or | ||||||||||||||||||||||||||||||||||||
| 406 | L<Mojo::Transaction::WebSocket> object. Note that this attribute is weakened. So the object needs to be referenced | ||||||||||||||||||||||||||||||||||||
| 407 | elsewhere as well when you're performing non-blocking operations and the underlying connection might get closed early. | ||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||
| 409 | # Check peer information | ||||||||||||||||||||||||||||||||||||
| 410 | my $address = $c->tx->remote_address; | ||||||||||||||||||||||||||||||||||||
| 411 | my $port = $c->tx->remote_port; | ||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||
| 413 | # Increase size limit for WebSocket messages to 16MiB | ||||||||||||||||||||||||||||||||||||
| 414 | $c->tx->max_websocket_size(16777216) if $c->tx->is_websocket; | ||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||
| 416 | # Perform non-blocking operation without knowing the connection status | ||||||||||||||||||||||||||||||||||||
| 417 | my $tx = $c->tx; | ||||||||||||||||||||||||||||||||||||
| 418 | Mojo::IOLoop->timer(2 => sub { | ||||||||||||||||||||||||||||||||||||
| 419 | $c->app->log->debug($tx->is_finished ? 'Finished' : 'In progress'); | ||||||||||||||||||||||||||||||||||||
| 420 | }); | ||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||
| 422 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||
| 424 | L<Mojolicious::Controller> inherits all methods from L<Mojo::Base> and implements the following new ones. | ||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||
| 426 | =head2 continue | ||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||
| 428 | $c->continue; | ||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||
| 430 | Continue dispatch chain from an intermediate destination with L<Mojolicious::Routes/"continue">. | ||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||
| 432 | =head2 cookie | ||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||
| 434 | my $value = $c->cookie('foo'); | ||||||||||||||||||||||||||||||||||||
| 435 | $c = $c->cookie(foo => 'bar'); | ||||||||||||||||||||||||||||||||||||
| 436 | $c = $c->cookie(foo => 'bar', {path => '/'}); | ||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||
| 438 | Access request cookie values and create new response cookies. If there are multiple values sharing the same name, and | ||||||||||||||||||||||||||||||||||||
| 439 | you want to access more than just the last one, you can use L</"every_cookie">. | ||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||
| 441 | # Create response cookie with domain and expiration date | ||||||||||||||||||||||||||||||||||||
| 442 | $c->cookie(user => 'sri', {domain => 'example.com', expires => time + 60}); | ||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||
| 444 | # Create secure response cookie | ||||||||||||||||||||||||||||||||||||
| 445 | $c->cookie(secret => 'I <3 Mojolicious', {secure => 1, httponly => 1}); | ||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||
| 447 | =head2 encrypted_cookie | ||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||
| 449 | my $value = $c->encrypted_cookie('foo'); | ||||||||||||||||||||||||||||||||||||
| 450 | $c = $c->encrypted_cookie(foo => 'bar'); | ||||||||||||||||||||||||||||||||||||
| 451 | $c = $c->encrypted_cookie(foo => 'bar', {path => '/'}); | ||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||
| 453 | Access encrypted request cookie values and create new encrypted response cookies. If there are multiple values sharing | ||||||||||||||||||||||||||||||||||||
| 454 | the same name, and you want to access more than just the last one, you can use L</"every_encrypted_cookie">. Cookies | ||||||||||||||||||||||||||||||||||||
| 455 | are encrypted with ChaCha20-Poly1305, to prevent tampering, and the ones failing decryption will be automatically | ||||||||||||||||||||||||||||||||||||
| 456 | discarded. | ||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||
| 458 | =head2 every_cookie | ||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||
| 460 | my $values = $c->every_cookie('foo'); | ||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||
| 462 | Similar to L</"cookie">, but returns all request cookie values sharing the same name as an array reference. | ||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||
| 464 | $ Get first cookie value | ||||||||||||||||||||||||||||||||||||
| 465 | my $first = $c->every_cookie('foo')->[0]; | ||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||
| 467 | =head2 every_encrypted_cookie | ||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||
| 469 | my $values = $c->every_encrypted_cookie('foo'); | ||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||
| 471 | Similar to L</"encrypted_cookie">, but returns all encrypted request cookie values sharing the same name as an array | ||||||||||||||||||||||||||||||||||||
| 472 | reference. | ||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||
| 474 | # Get first encrypted cookie value | ||||||||||||||||||||||||||||||||||||
| 475 | my $first = $c->every_encrypted_cookie('foo')->[0]; | ||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||
| 477 | =head2 every_param | ||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||
| 479 | my $values = $c->every_param('foo'); | ||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||
| 481 | Similar to L</"param">, but returns all values sharing the same name as an array reference. | ||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||
| 483 | # Get first value | ||||||||||||||||||||||||||||||||||||
| 484 | my $first = $c->every_param('foo')->[0]; | ||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||
| 486 | =head2 every_signed_cookie | ||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||
| 488 | my $values = $c->every_signed_cookie('foo'); | ||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||
| 490 | Similar to L</"signed_cookie">, but returns all signed request cookie values sharing the same name as an array | ||||||||||||||||||||||||||||||||||||
| 491 | reference. | ||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||
| 493 | # Get first signed cookie value | ||||||||||||||||||||||||||||||||||||
| 494 | my $first = $c->every_signed_cookie('foo')->[0]; | ||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||
| 496 | =head2 finish | ||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||
| 498 | $c = $c->finish; | ||||||||||||||||||||||||||||||||||||
| 499 | $c = $c->finish(1000); | ||||||||||||||||||||||||||||||||||||
| 500 | $c = $c->finish(1003 => 'Cannot accept data!'); | ||||||||||||||||||||||||||||||||||||
| 501 | $c = $c->finish('Bye!'); | ||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||
| 503 | Close WebSocket connection or long poll stream gracefully. This method will automatically respond to WebSocket | ||||||||||||||||||||||||||||||||||||
| 504 | handshake requests with a C<101> response status, to establish the WebSocket connection. | ||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||
| 506 | =head2 helpers | ||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||
| 508 | my $helpers = $c->helpers; | ||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||
| 510 | Return a proxy object containing the current controller object and on which helpers provided by L</"app"> can be | ||||||||||||||||||||||||||||||||||||
| 511 | called. This includes all helpers from L<Mojolicious::Plugin::DefaultHelpers> and L<Mojolicious::Plugin::TagHelpers>. | ||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||
| 513 | # Make sure to use the "title" helper and not the controller method | ||||||||||||||||||||||||||||||||||||
| 514 | $c->helpers->title('Welcome!'); | ||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||
| 516 | # Use a nested helper instead of the "reply" controller method | ||||||||||||||||||||||||||||||||||||
| 517 | $c->helpers->reply->not_found; | ||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||
| 519 | =head2 on | ||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||
| 521 | my $cb = $c->on(finish => sub {...}); | ||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||
| 523 | Subscribe to events of L</"tx">, which is usually a L<Mojo::Transaction::HTTP> or L<Mojo::Transaction::WebSocket> | ||||||||||||||||||||||||||||||||||||
| 524 | object. This method will automatically respond to WebSocket handshake requests with a C<101> response status, to | ||||||||||||||||||||||||||||||||||||
| 525 | establish the WebSocket connection. | ||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||
| 527 | # Do something after the transaction has been finished | ||||||||||||||||||||||||||||||||||||
| 528 | $c->on(finish => sub ($c) { | ||||||||||||||||||||||||||||||||||||
| 529 | $c->app->log->debug('All data has been sent'); | ||||||||||||||||||||||||||||||||||||
| 530 | }); | ||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||
| 532 | # Receive WebSocket message | ||||||||||||||||||||||||||||||||||||
| 533 | $c->on(message => sub ($c, $msg) { | ||||||||||||||||||||||||||||||||||||
| 534 | $c->app->log->debug("Message: $msg"); | ||||||||||||||||||||||||||||||||||||
| 535 | }); | ||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||
| 537 | # Receive JSON object via WebSocket message | ||||||||||||||||||||||||||||||||||||
| 538 | $c->on(json => sub ($c, $hash) { | ||||||||||||||||||||||||||||||||||||
| 539 | $c->app->log->debug("Test: $hash->{test}"); | ||||||||||||||||||||||||||||||||||||
| 540 | }); | ||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||
| 542 | # Receive WebSocket "Binary" message | ||||||||||||||||||||||||||||||||||||
| 543 | $c->on(binary => sub ($c, $bytes) { | ||||||||||||||||||||||||||||||||||||
| 544 | my $len = length $bytes; | ||||||||||||||||||||||||||||||||||||
| 545 | $c->app->log->debug("Received $len bytes"); | ||||||||||||||||||||||||||||||||||||
| 546 | }); | ||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||
| 548 | =head2 param | ||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||
| 550 | my $value = $c->param('foo'); | ||||||||||||||||||||||||||||||||||||
| 551 | $c = $c->param(foo => 'ba;r'); | ||||||||||||||||||||||||||||||||||||
| 552 | $c = $c->param(foo => 'ba;r', 'baz'); | ||||||||||||||||||||||||||||||||||||
| 553 | $c = $c->param(foo => ['ba;r', 'baz']); | ||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||
| 555 | Access route placeholder values that are not reserved stash values, file uploads as well as C<GET> and C<POST> | ||||||||||||||||||||||||||||||||||||
| 556 | parameters extracted from the query string and C<application/x-www-form-urlencoded> or C<multipart/form-data> message | ||||||||||||||||||||||||||||||||||||
| 557 | body, in that order. If there are multiple values sharing the same name, and you want to access more than just the last | ||||||||||||||||||||||||||||||||||||
| 558 | one, you can use L</"every_param">. Parts of the request body need to be loaded into memory to parse C<POST> | ||||||||||||||||||||||||||||||||||||
| 559 | parameters, so you have to make sure it is not excessively large. There's a 16MiB limit for requests by default. | ||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||
| 561 | # Get first value | ||||||||||||||||||||||||||||||||||||
| 562 | my $first = $c->every_param('foo')->[0]; | ||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||
| 564 | For more control you can also access request information directly. | ||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||
| 566 | # Only GET parameters | ||||||||||||||||||||||||||||||||||||
| 567 | my $foo = $c->req->query_params->param('foo'); | ||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||
| 569 | # Only POST parameters | ||||||||||||||||||||||||||||||||||||
| 570 | my $foo = $c->req->body_params->param('foo'); | ||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||
| 572 | # Only GET and POST parameters | ||||||||||||||||||||||||||||||||||||
| 573 | my $foo = $c->req->param('foo'); | ||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||
| 575 | # Only file uploads | ||||||||||||||||||||||||||||||||||||
| 576 | my $foo = $c->req->upload('foo'); | ||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||
| 578 | =head2 render | ||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||
| 580 | my $bool = $c->render; | ||||||||||||||||||||||||||||||||||||
| 581 | my $bool = $c->render(foo => 'bar', baz => 23); | ||||||||||||||||||||||||||||||||||||
| 582 | my $bool = $c->render(template => 'foo/index'); | ||||||||||||||||||||||||||||||||||||
| 583 | my $bool = $c->render(template => 'index', format => 'html'); | ||||||||||||||||||||||||||||||||||||
| 584 | my $bool = $c->render(data => $bytes); | ||||||||||||||||||||||||||||||||||||
| 585 | my $bool = $c->render(text => 'Hello!'); | ||||||||||||||||||||||||||||||||||||
| 586 | my $bool = $c->render(json => {foo => 'bar'}); | ||||||||||||||||||||||||||||||||||||
| 587 | my $bool = $c->render(handler => 'something'); | ||||||||||||||||||||||||||||||||||||
| 588 | my $bool = $c->render('foo/index'); | ||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||
| 590 | Render content with L<Mojolicious/"renderer"> and emit hooks L<Mojolicious/"before_render"> as well as | ||||||||||||||||||||||||||||||||||||
| 591 | L<Mojolicious/"after_render">, or dies if no response could be generated. All additional key/value pairs get merged into | ||||||||||||||||||||||||||||||||||||
| 592 | the L</"stash">. | ||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||
| 594 | # Render characters | ||||||||||||||||||||||||||||||||||||
| 595 | $c->render(text => 'I ♥ Mojolicious!'); | ||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||
| 597 | # Render characters (alternative) | ||||||||||||||||||||||||||||||||||||
| 598 | $c->stash(text => 'I ♥ Mojolicious!')->render; | ||||||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||||||
| 600 | # Render binary data | ||||||||||||||||||||||||||||||||||||
| 601 | use Mojo::JSON qw(encode_json); | ||||||||||||||||||||||||||||||||||||
| 602 | $c->render(data => encode_json({test => 'I ♥ Mojolicious!'})); | ||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||
| 604 | # Render JSON | ||||||||||||||||||||||||||||||||||||
| 605 | $c->render(json => {test => 'I ♥ Mojolicious!'}); | ||||||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||||||
| 607 | # Render inline template | ||||||||||||||||||||||||||||||||||||
| 608 | $c->render(inline => '<%= 1 + 1 %>'); | ||||||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||||||
| 610 | # Render template "foo/bar.html.ep" | ||||||||||||||||||||||||||||||||||||
| 611 | $c->render(template => 'foo/bar', format => 'html', handler => 'ep'); | ||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||
| 613 | # Render template "test.*.*" with arbitrary values "foo" and "bar" | ||||||||||||||||||||||||||||||||||||
| 614 | $c->render(template => 'test', foo => 'test', bar => 23); | ||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||
| 616 | # Render template "test.xml.*" | ||||||||||||||||||||||||||||||||||||
| 617 | $c->render(template => 'test', format => 'xml'); | ||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||
| 619 | # Render template "test.xml.*" (alternative) | ||||||||||||||||||||||||||||||||||||
| 620 | $c->render('test', format => 'xml'); | ||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||
| 622 | =head2 render_later | ||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||
| 624 | $c = $c->render_later; | ||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||
| 626 | Disable automatic rendering to delay response generation, only necessary if automatic rendering would result in a | ||||||||||||||||||||||||||||||||||||
| 627 | response. | ||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||
| 629 | # Delayed rendering | ||||||||||||||||||||||||||||||||||||
| 630 | $c->render_later; | ||||||||||||||||||||||||||||||||||||
| 631 | Mojo::IOLoop->timer(2 => sub { $c->render(text => 'Delayed by 2 seconds!') }); | ||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||
| 633 | =head2 render_maybe | ||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||
| 635 | my $bool = $c->render_maybe; | ||||||||||||||||||||||||||||||||||||
| 636 | my $bool = $c->render_maybe(foo => 'bar', baz => 23); | ||||||||||||||||||||||||||||||||||||
| 637 | my $bool = $c->render_maybe('foo/index', format => 'html'); | ||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||
| 639 | Try to render content, but do not call L<Mojolicious::Plugin::DefaultHelpers/"reply-E<gt>not_found"> if no response | ||||||||||||||||||||||||||||||||||||
| 640 | could be generated, all arguments get localized automatically and are only available during this render operation, | ||||||||||||||||||||||||||||||||||||
| 641 | takes the same arguments as L</"render">. | ||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||
| 643 | # Render template "index_local" only if it exists | ||||||||||||||||||||||||||||||||||||
| 644 | $c->render_maybe('index_local') or $c->render('index'); | ||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||
| 646 | =head2 render_to_string | ||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||
| 648 | my $output = $c->render_to_string('foo/index', format => 'pdf'); | ||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||
| 650 | Try to render content and return it wrapped in a L<Mojo::ByteStream> object or return C<undef>, all arguments get | ||||||||||||||||||||||||||||||||||||
| 651 | localized automatically and are only available during this render operation, takes the same arguments as L</"render">. | ||||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||||
| 653 | # Render inline template | ||||||||||||||||||||||||||||||||||||
| 654 | my $two = $c->render_to_string(inline => '<%= 1 + 1 %>'); | ||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||
| 656 | =head2 rendered | ||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||
| 658 | $c = $c->rendered; | ||||||||||||||||||||||||||||||||||||
| 659 | $c = $c->rendered(302); | ||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||
| 661 | Finalize response and emit hook L<Mojolicious/"after_dispatch">, defaults to using a C<200> response code. | ||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||
| 663 | # Custom response | ||||||||||||||||||||||||||||||||||||
| 664 | $c->res->headers->content_type('text/plain'); | ||||||||||||||||||||||||||||||||||||
| 665 | $c->res->body('Hello World!'); | ||||||||||||||||||||||||||||||||||||
| 666 | $c->rendered(200); | ||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||
| 668 | =head2 req | ||||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||
| 670 | my $req = $c->req; | ||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||
| 672 | Get L<Mojo::Message::Request> object from L</"tx">. | ||||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||||
| 674 | # Longer version | ||||||||||||||||||||||||||||||||||||
| 675 | my $req = $c->tx->req; | ||||||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||||||
| 677 | # Extract request information | ||||||||||||||||||||||||||||||||||||
| 678 | my $id = $c->req->request_id; | ||||||||||||||||||||||||||||||||||||
| 679 | my $method = $c->req->method; | ||||||||||||||||||||||||||||||||||||
| 680 | my $url = $c->req->url->to_abs; | ||||||||||||||||||||||||||||||||||||
| 681 | my $info = $c->req->url->to_abs->userinfo; | ||||||||||||||||||||||||||||||||||||
| 682 | my $host = $c->req->url->to_abs->host; | ||||||||||||||||||||||||||||||||||||
| 683 | my $agent = $c->req->headers->user_agent; | ||||||||||||||||||||||||||||||||||||
| 684 | my $custom = $c->req->headers->header('Custom-Header'); | ||||||||||||||||||||||||||||||||||||
| 685 | my $bytes = $c->req->body; | ||||||||||||||||||||||||||||||||||||
| 686 | my $str = $c->req->text; | ||||||||||||||||||||||||||||||||||||
| 687 | my $hash = $c->req->params->to_hash; | ||||||||||||||||||||||||||||||||||||
| 688 | my $all = $c->req->uploads; | ||||||||||||||||||||||||||||||||||||
| 689 | my $value = $c->req->json; | ||||||||||||||||||||||||||||||||||||
| 690 | my $foo = $c->req->json('/23/foo'); | ||||||||||||||||||||||||||||||||||||
| 691 | my $dom = $c->req->dom; | ||||||||||||||||||||||||||||||||||||
| 692 | my $bar = $c->req->dom('div.bar')->first->text; | ||||||||||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||||||||||
| 694 | =head2 res | ||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||
| 696 | my $res = $c->res; | ||||||||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||||||
| 698 | Get L<Mojo::Message::Response> object from L</"tx">. | ||||||||||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||||||||||
| 700 | # Longer version | ||||||||||||||||||||||||||||||||||||
| 701 | my $res = $c->tx->res; | ||||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||||
| 703 | # Force file download by setting a response header | ||||||||||||||||||||||||||||||||||||
| 704 | $c->res->headers->content_disposition('attachment; filename=foo.png;'); | ||||||||||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||||||||||
| 706 | # Use a custom response header | ||||||||||||||||||||||||||||||||||||
| 707 | $c->res->headers->header('Custom-Header' => 'whatever'); | ||||||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||||||
| 709 | # Make sure response is cached correctly | ||||||||||||||||||||||||||||||||||||
| 710 | $c->res->headers->cache_control('public, max-age=300'); | ||||||||||||||||||||||||||||||||||||
| 711 | $c->res->headers->append(Vary => 'Accept-Encoding'); | ||||||||||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||||||||||
| 713 | =head2 send | ||||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||||
| 715 | $c = $c->send({binary => $bytes}); | ||||||||||||||||||||||||||||||||||||
| 716 | $c = $c->send({text => $bytes}); | ||||||||||||||||||||||||||||||||||||
| 717 | $c = $c->send({json => {test => [1, 2, 3]}}); | ||||||||||||||||||||||||||||||||||||
| 718 | $c = $c->send([$fin, $rsv1, $rsv2, $rsv3, $op, $payload]); | ||||||||||||||||||||||||||||||||||||
| 719 | $c = $c->send($chars); | ||||||||||||||||||||||||||||||||||||
| 720 | $c = $c->send($chars => sub ($c) {...}); | ||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||
| 722 | Send message or frame non-blocking via WebSocket, the optional drain callback will be executed once all data has been | ||||||||||||||||||||||||||||||||||||
| 723 | written. This method will automatically respond to WebSocket handshake requests with a C<101> response status, to | ||||||||||||||||||||||||||||||||||||
| 724 | establish the WebSocket connection. | ||||||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||||
| 726 | # Send "Text" message | ||||||||||||||||||||||||||||||||||||
| 727 | $c->send('I ♥ Mojolicious!'); | ||||||||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||||||
| 729 | # Send JSON object as "Text" message | ||||||||||||||||||||||||||||||||||||
| 730 | $c->send({json => {test => 'I ♥ Mojolicious!'}}); | ||||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||||
| 732 | # Send JSON object as "Binary" message | ||||||||||||||||||||||||||||||||||||
| 733 | use Mojo::JSON qw(encode_json); | ||||||||||||||||||||||||||||||||||||
| 734 | $c->send({binary => encode_json({test => 'I ♥ Mojolicious!'})}); | ||||||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||||||
| 736 | # Send "Ping" frame | ||||||||||||||||||||||||||||||||||||
| 737 | use Mojo::WebSocket qw(WS_PING); | ||||||||||||||||||||||||||||||||||||
| 738 | $c->send([1, 0, 0, 0, WS_PING, 'Hello World!']); | ||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||
| 740 | # Make sure the first message has been written before continuing | ||||||||||||||||||||||||||||||||||||
| 741 | $c->send('First message!' => sub ($c) { $c->send('Second message!') }); | ||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||
| 743 | For mostly idle WebSockets you might also want to increase the inactivity timeout with | ||||||||||||||||||||||||||||||||||||
| 744 | L<Mojolicious::Plugin::DefaultHelpers/"inactivity_timeout">, which usually defaults to C<30> seconds. | ||||||||||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||||||||||
| 746 | # Increase inactivity timeout for connection to 300 seconds | ||||||||||||||||||||||||||||||||||||
| 747 | $c->inactivity_timeout(300); | ||||||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||||||
| 749 | =head2 session | ||||||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||||||
| 751 | my $session = $c->session; | ||||||||||||||||||||||||||||||||||||
| 752 | my $foo = $c->session('foo'); | ||||||||||||||||||||||||||||||||||||
| 753 | $c = $c->session({foo => 'bar'}); | ||||||||||||||||||||||||||||||||||||
| 754 | $c = $c->session(foo => 'bar'); | ||||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||||
| 756 | Persistent data storage for the next few requests, all session data gets serialized with L<Mojo::JSON> and stored | ||||||||||||||||||||||||||||||||||||
| 757 | Base64 encoded in HMAC-SHA256 signed cookies, to prevent tampering. Note that cookies usually have a C<4096> byte | ||||||||||||||||||||||||||||||||||||
| 758 | (4KiB) limit, depending on browser. | ||||||||||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||||||||||
| 760 | # Manipulate session | ||||||||||||||||||||||||||||||||||||
| 761 | $c->session->{foo} = 'bar'; | ||||||||||||||||||||||||||||||||||||
| 762 | my $foo = $c->session->{foo}; | ||||||||||||||||||||||||||||||||||||
| 763 | delete $c->session->{foo}; | ||||||||||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||||||||||
| 765 | # Expiration date in seconds from now (persists between requests) | ||||||||||||||||||||||||||||||||||||
| 766 | $c->session(expiration => 604800); | ||||||||||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||||||||||
| 768 | # Expiration date as absolute epoch time (only valid for one request) | ||||||||||||||||||||||||||||||||||||
| 769 | $c->session(expires => time + 604800); | ||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||
| 771 | # Delete whole session by setting an expiration date in the past | ||||||||||||||||||||||||||||||||||||
| 772 | $c->session(expires => 1); | ||||||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||||||
| 774 | =head2 signed_cookie | ||||||||||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||||||||||
| 776 | my $value = $c->signed_cookie('foo'); | ||||||||||||||||||||||||||||||||||||
| 777 | $c = $c->signed_cookie(foo => 'bar'); | ||||||||||||||||||||||||||||||||||||
| 778 | $c = $c->signed_cookie(foo => 'bar', {path => '/'}); | ||||||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||||
| 780 | Access signed request cookie values and create new signed response cookies. If there are multiple values sharing the | ||||||||||||||||||||||||||||||||||||
| 781 | same name, and you want to access more than just the last one, you can use L</"every_signed_cookie">. Cookies are | ||||||||||||||||||||||||||||||||||||
| 782 | cryptographically signed with HMAC-SHA256, to prevent tampering, and the ones failing signature verification will be | ||||||||||||||||||||||||||||||||||||
| 783 | automatically discarded. | ||||||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||||||
| 785 | =head2 stash | ||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||
| 787 | my $hash = $c->stash; | ||||||||||||||||||||||||||||||||||||
| 788 | my $foo = $c->stash('foo'); | ||||||||||||||||||||||||||||||||||||
| 789 | $c = $c->stash({foo => 'bar', baz => 23}); | ||||||||||||||||||||||||||||||||||||
| 790 | $c = $c->stash(foo => 'bar', baz => 23); | ||||||||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||||||
| 792 | Non-persistent data storage and exchange for the current request, application wide default values can be set with | ||||||||||||||||||||||||||||||||||||
| 793 | L<Mojolicious/"defaults">. Some stash values have a special meaning and are reserved, the full list is currently | ||||||||||||||||||||||||||||||||||||
| 794 | C<action>, C<app>, C<cb>, C<controller>, C<data>, C<extends>, C<format>, C<handler>, C<inline>, C<json>, C<layout>, | ||||||||||||||||||||||||||||||||||||
| 795 | C<namespace>, C<path>, C<status>, C<template>, C<text> and C<variant>. Note that all stash values with a C<mojo.*> | ||||||||||||||||||||||||||||||||||||
| 796 | prefix are reserved for internal use. | ||||||||||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||||||||||
| 798 | # Remove value | ||||||||||||||||||||||||||||||||||||
| 799 | my $foo = delete $c->stash->{foo}; | ||||||||||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||||||||||
| 801 | # Assign multiple values at once | ||||||||||||||||||||||||||||||||||||
| 802 | $c->stash(foo => 'test', bar => 23); | ||||||||||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||||||||||
| 804 | =head2 url_for | ||||||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||||||
| 806 | my $url = $c->url_for; | ||||||||||||||||||||||||||||||||||||
| 807 | my $url = $c->url_for(name => 'sebastian'); | ||||||||||||||||||||||||||||||||||||
| 808 | my $url = $c->url_for({name => 'sebastian'}); | ||||||||||||||||||||||||||||||||||||
| 809 | my $url = $c->url_for('test', name => 'sebastian'); | ||||||||||||||||||||||||||||||||||||
| 810 | my $url = $c->url_for('test', {name => 'sebastian'}); | ||||||||||||||||||||||||||||||||||||
| 811 | my $url = $c->url_for('/index.html'); | ||||||||||||||||||||||||||||||||||||
| 812 | my $url = $c->url_for('//example.com/index.html'); | ||||||||||||||||||||||||||||||||||||
| 813 | my $url = $c->url_for('http://example.com/index.html'); | ||||||||||||||||||||||||||||||||||||
| 814 | my $url = $c->url_for('mailto:sri@example.com'); | ||||||||||||||||||||||||||||||||||||
| 815 | my $url = $c->url_for('#whatever'); | ||||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||||
| 817 | Generate a portable L<Mojo::URL> object with base for a path, URL or route. | ||||||||||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||||||||||
| 819 | # Rebuild URL for the current route | ||||||||||||||||||||||||||||||||||||
| 820 | $c->url_for; | ||||||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||||
| 822 | # Rebuild URL for the current route, but replace the "name" placeholder value | ||||||||||||||||||||||||||||||||||||
| 823 | $c->url_for(name => 'sebastian'); | ||||||||||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||||||||||
| 825 | # Absolute URL for the current route | ||||||||||||||||||||||||||||||||||||
| 826 | $c->url_for->to_abs; | ||||||||||||||||||||||||||||||||||||
| 827 | |||||||||||||||||||||||||||||||||||||
| 828 | # Build URL for route "test" with two placeholder values | ||||||||||||||||||||||||||||||||||||
| 829 | $c->url_for('test', name => 'sebastian', foo => 'bar'); | ||||||||||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||||||||||
| 831 | # "http://127.0.0.1:3000/index.html" if application was started with Morbo | ||||||||||||||||||||||||||||||||||||
| 832 | $c->url_for('/index.html')->to_abs; | ||||||||||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||||||||||
| 834 | # "https://127.0.0.1:443/index.html" if application was started with Morbo | ||||||||||||||||||||||||||||||||||||
| 835 | $c->url_for('/index.html')->to_abs->scheme('https')->port(443); | ||||||||||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||||||||||
| 837 | # "/index.html?foo=bar" if application is deployed under "/" | ||||||||||||||||||||||||||||||||||||
| 838 | $c->url_for('/index.html')->query(foo => 'bar'); | ||||||||||||||||||||||||||||||||||||
| 839 | |||||||||||||||||||||||||||||||||||||
| 840 | # "/myapp/index.html?foo=bar" if application is deployed under "/myapp" | ||||||||||||||||||||||||||||||||||||
| 841 | $c->url_for('/index.html')->query(foo => 'bar'); | ||||||||||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||||||||||
| 843 | You can also use the helper L<Mojolicious::Plugin::DefaultHelpers/"url_with"> to inherit query parameters from the | ||||||||||||||||||||||||||||||||||||
| 844 | current request. | ||||||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||||||
| 846 | # "/list?q=mojo&page=2" if current request was for "/list?q=mojo&page=1" | ||||||||||||||||||||||||||||||||||||
| 847 | $c->url_with->query({page => 2}); | ||||||||||||||||||||||||||||||||||||
| 848 | |||||||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||||||
| 850 | =head2 url_for_asset | ||||||||||||||||||||||||||||||||||||
| 851 | |||||||||||||||||||||||||||||||||||||
| 852 | my $url = $c->url_for_asset('/app.js'); | ||||||||||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||||||||||
| 854 | Generate a portable L<Mojo::URL> object with base for a static asset. In C<development> mode the asset directories are | ||||||||||||||||||||||||||||||||||||
| 855 | scanned again every time, so assets that have just been rebuilt with a different name are found too. | ||||||||||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||||||||||
| 857 | =head2 url_for_file | ||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||
| 859 | my $url = $c->url_for_file('/index.html'); | ||||||||||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||||||||||
| 861 | Generate a portable L<Mojo::URL> object with base for a static file. | ||||||||||||||||||||||||||||||||||||
| 862 | |||||||||||||||||||||||||||||||||||||
| 863 | =head2 write | ||||||||||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||||||||||
| 865 | $c = $c->write; | ||||||||||||||||||||||||||||||||||||
| 866 | $c = $c->write(''); | ||||||||||||||||||||||||||||||||||||
| 867 | $c = $c->write($bytes); | ||||||||||||||||||||||||||||||||||||
| 868 | $c = $c->write($bytes => sub ($c) {...}); | ||||||||||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||||||||||
| 870 | Write dynamic content non-blocking, the optional drain callback will be executed once all data has been written. | ||||||||||||||||||||||||||||||||||||
| 871 | Calling this method without a chunk of data will finalize the response headers and allow for dynamic content to be | ||||||||||||||||||||||||||||||||||||
| 872 | written later. | ||||||||||||||||||||||||||||||||||||
| 873 | |||||||||||||||||||||||||||||||||||||
| 874 | # Keep connection alive (with Content-Length header) | ||||||||||||||||||||||||||||||||||||
| 875 | $c->res->headers->content_length(6); | ||||||||||||||||||||||||||||||||||||
| 876 | $c->write('Hel' => sub ($c) { $c->write('lo!') }); | ||||||||||||||||||||||||||||||||||||
| 877 | |||||||||||||||||||||||||||||||||||||
| 878 | # Close connection when finished (without Content-Length header) | ||||||||||||||||||||||||||||||||||||
| 879 | $c->write('Hel' => sub ($c) { | ||||||||||||||||||||||||||||||||||||
| 880 | $c->write('lo!' => sub ($c) { $c->finish }); | ||||||||||||||||||||||||||||||||||||
| 881 | }); | ||||||||||||||||||||||||||||||||||||
| 882 | |||||||||||||||||||||||||||||||||||||
| 883 | You can call L</"finish"> or write an empty chunk of data at any time to end the stream. | ||||||||||||||||||||||||||||||||||||
| 884 | |||||||||||||||||||||||||||||||||||||
| 885 | HTTP/1.1 200 OK | ||||||||||||||||||||||||||||||||||||
| 886 | Date: Sat, 13 Sep 2014 16:48:29 GMT | ||||||||||||||||||||||||||||||||||||
| 887 | Content-Length: 6 | ||||||||||||||||||||||||||||||||||||
| 888 | Server: Mojolicious (Perl) | ||||||||||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||||||||||
| 890 | Hello! | ||||||||||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||||||||||
| 892 | HTTP/1.1 200 OK | ||||||||||||||||||||||||||||||||||||
| 893 | Connection: close | ||||||||||||||||||||||||||||||||||||
| 894 | Date: Sat, 13 Sep 2014 16:48:29 GMT | ||||||||||||||||||||||||||||||||||||
| 895 | Server: Mojolicious (Perl) | ||||||||||||||||||||||||||||||||||||
| 896 | |||||||||||||||||||||||||||||||||||||
| 897 | Hello! | ||||||||||||||||||||||||||||||||||||
| 898 | |||||||||||||||||||||||||||||||||||||
| 899 | For Comet (long polling) you might also want to increase the inactivity timeout with | ||||||||||||||||||||||||||||||||||||
| 900 | L<Mojolicious::Plugin::DefaultHelpers/"inactivity_timeout">, which usually defaults to C<30> seconds. | ||||||||||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||||||||||
| 902 | # Increase inactivity timeout for connection to 300 seconds | ||||||||||||||||||||||||||||||||||||
| 903 | $c->inactivity_timeout(300); | ||||||||||||||||||||||||||||||||||||
| 904 | |||||||||||||||||||||||||||||||||||||
| 905 | =head2 write_chunk | ||||||||||||||||||||||||||||||||||||
| 906 | |||||||||||||||||||||||||||||||||||||
| 907 | $c = $c->write_chunk; | ||||||||||||||||||||||||||||||||||||
| 908 | $c = $c->write_chunk(''); | ||||||||||||||||||||||||||||||||||||
| 909 | $c = $c->write_chunk($bytes); | ||||||||||||||||||||||||||||||||||||
| 910 | $c = $c->write_chunk($bytes => sub ($c) {...}); | ||||||||||||||||||||||||||||||||||||
| 911 | |||||||||||||||||||||||||||||||||||||
| 912 | Write dynamic content non-blocking with chunked transfer encoding, the optional drain callback will be executed once | ||||||||||||||||||||||||||||||||||||
| 913 | all data has been written. Calling this method without a chunk of data will finalize the response headers and allow for | ||||||||||||||||||||||||||||||||||||
| 914 | dynamic content to be written later. | ||||||||||||||||||||||||||||||||||||
| 915 | |||||||||||||||||||||||||||||||||||||
| 916 | # Make sure previous chunk has been written before continuing | ||||||||||||||||||||||||||||||||||||
| 917 | $c->write_chunk('H' => sub ($c) { | ||||||||||||||||||||||||||||||||||||
| 918 | $c->write_chunk('ell' => sub ($c) { $c->finish('o!') }); | ||||||||||||||||||||||||||||||||||||
| 919 | }); | ||||||||||||||||||||||||||||||||||||
| 920 | |||||||||||||||||||||||||||||||||||||
| 921 | You can call L</"finish"> or write an empty chunk of data at any time to end the stream. | ||||||||||||||||||||||||||||||||||||
| 922 | |||||||||||||||||||||||||||||||||||||
| 923 | HTTP/1.1 200 OK | ||||||||||||||||||||||||||||||||||||
| 924 | Date: Sat, 13 Sep 2014 16:48:29 GMT | ||||||||||||||||||||||||||||||||||||
| 925 | Transfer-Encoding: chunked | ||||||||||||||||||||||||||||||||||||
| 926 | Server: Mojolicious (Perl) | ||||||||||||||||||||||||||||||||||||
| 927 | |||||||||||||||||||||||||||||||||||||
| 928 | 1 | ||||||||||||||||||||||||||||||||||||
| 929 | H | ||||||||||||||||||||||||||||||||||||
| 930 | 3 | ||||||||||||||||||||||||||||||||||||
| 931 | ell | ||||||||||||||||||||||||||||||||||||
| 932 | 2 | ||||||||||||||||||||||||||||||||||||
| 933 | o! | ||||||||||||||||||||||||||||||||||||
| 934 | 0 | ||||||||||||||||||||||||||||||||||||
| 935 | |||||||||||||||||||||||||||||||||||||
| 936 | =head2 write_sse | ||||||||||||||||||||||||||||||||||||
| 937 | |||||||||||||||||||||||||||||||||||||
| 938 | $c = $c->write_sse; | ||||||||||||||||||||||||||||||||||||
| 939 | $c = $c->write_sse($event); | ||||||||||||||||||||||||||||||||||||
| 940 | $c = $c->write_sse($event => sub ($c) {...}); | ||||||||||||||||||||||||||||||||||||
| 941 | |||||||||||||||||||||||||||||||||||||
| 942 | Write Server-Sent Event (SSE) non-blocking, the optional drain callback will be executed once all data has been | ||||||||||||||||||||||||||||||||||||
| 943 | written. Calling this method without an event will finalize the response headers and allow for events to be written | ||||||||||||||||||||||||||||||||||||
| 944 | later. Note that this method is B<EXPERIMENTAL> and might change without warning! | ||||||||||||||||||||||||||||||||||||
| 945 | |||||||||||||||||||||||||||||||||||||
| 946 | # Send event with default "message" type | ||||||||||||||||||||||||||||||||||||
| 947 | $c->write_sse({text => 'Hello World!'}); | ||||||||||||||||||||||||||||||||||||
| 948 | |||||||||||||||||||||||||||||||||||||
| 949 | # You can send comments in regular intervals to keep the connection alive | ||||||||||||||||||||||||||||||||||||
| 950 | $c->write_sse({comment => 'Keep connection alive!'}); | ||||||||||||||||||||||||||||||||||||
| 951 | |||||||||||||||||||||||||||||||||||||
| 952 | # Make sure previous event has been written before continuing (id and type are optional) | ||||||||||||||||||||||||||||||||||||
| 953 | $c->write_sse({id => 23, type => 'greeting', text => 'Hello World!'} => sub ($c) { | ||||||||||||||||||||||||||||||||||||
| 954 | $c->write_sse({id => 24, type => 'farewell', text => 'Goodbye World!'} => sub ($c) { $c->finish }); | ||||||||||||||||||||||||||||||||||||
| 955 | }); | ||||||||||||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||||||||||
| 957 | =head1 HELPERS | ||||||||||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||||||||||
| 959 | In addition to the L</"ATTRIBUTES"> and L</"METHODS"> above you can also call helpers provided by L</"app"> on | ||||||||||||||||||||||||||||||||||||
| 960 | L<Mojolicious::Controller> objects. This includes all helpers from L<Mojolicious::Plugin::DefaultHelpers> and | ||||||||||||||||||||||||||||||||||||
| 961 | L<Mojolicious::Plugin::TagHelpers>. | ||||||||||||||||||||||||||||||||||||
| 962 | |||||||||||||||||||||||||||||||||||||
| 963 | # Call helpers | ||||||||||||||||||||||||||||||||||||
| 964 | $c->layout('green'); | ||||||||||||||||||||||||||||||||||||
| 965 | $c->title('Welcome!'); | ||||||||||||||||||||||||||||||||||||
| 966 | |||||||||||||||||||||||||||||||||||||
| 967 | # Longer version | ||||||||||||||||||||||||||||||||||||
| 968 | $c->helpers->layout('green'); | ||||||||||||||||||||||||||||||||||||
| 969 | |||||||||||||||||||||||||||||||||||||
| 970 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||
| 971 | |||||||||||||||||||||||||||||||||||||
| 972 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||||||||||
| 973 | |||||||||||||||||||||||||||||||||||||
| 974 | =cut | ||||||||||||||||||||||||||||||||||||