blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/URL.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 100 CC 5 SCAR 30.9
CC
5
Cov
0%
init
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 22.0
1 package Dwarf::Plugin::URL;
2 use Dwarf::Pragma;
3 use Dwarf::Util qw/add_method/;
4  
5 sub init {
6 my ($class, $c, $conf) = @_;
7  
8 $conf //= {};
9 $conf->{want_ssl_callback} //= sub { my ($self, $host, $path) = @_; $self->redirect("https://$host$path") };
10 $conf->{do_not_want_ssl_callback} //= sub { my ($self, $host, $path) = @_; $self->redirect("http://$host$path") },
11  
12 add_method($c, can_use_ssl => sub {
13 my $self = shift;
14 return $self->conf("ssl");
15 });
16  
17 add_method($c, base_url => sub {
18 my $self = shift;
19 return $self->can_use_ssl ? $self->conf('url')->{ssl_base} : $self->conf('url')->{base};
20 });
21  
22 add_method($c, is_ssl => sub {
23 my $self = shift;
24 return (($self->env->{HTTPS}//'') eq 'on' or ($self->env->{HTTP_X_FORWARDED_PROTO}//'') eq 'https') ? 1 : 0;
25 });
26  
27 add_method($c, want_ssl => sub {
28 my ($self, $path) = @_;
29 $path //= $self->env->{REQUEST_URI}//'';
30 if ($self->can_use_ssl and not $self->is_ssl) {
31 my $host = $self->env->{HTTP_X_FORWARDED_HOST} || $self->env->{HTTP_HOST};
32 $conf->{want_ssl_callback}->($self, $host, $path);
33 }
34 });
35  
36 add_method($c, do_not_want_ssl => sub {
37 my ($self, $path) = @_;
38 $path //= $self->env->{REQUEST_URI}//'';
39 if ($self->can_use_ssl) {
40 my $host = $self->env->{HTTP_X_FORWARDED_HOST} || $self->env->{HTTP_HOST};
41 $conf->{do_not_want_ssl_callback}->($self, $host, $path);
42 }
43 });
44 }
45  
46 1;