File Coverage

blib/lib/Catalyst/ActionRole/Scheme.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             package Catalyst::ActionRole::Scheme;
2              
3 2     2   1957 use Moose::Role;
  2         11  
  2         19  
4              
5             requires 'match', 'match_captures', 'list_extra_info';
6              
7             around ['match','match_captures'] => sub {
8             my ($orig, $self, $ctx, @args) = @_;
9             my $request_scheme = lc($ctx->req->env->{'psgi.url_scheme'});
10             my $match_scheme = lc($self->scheme||'');
11              
12             return $request_scheme eq $match_scheme ? $self->$orig($ctx, @args) : 0;
13             };
14              
15             around 'list_extra_info' => sub {
16             my ($orig, $self, @args) = @_;
17             return {
18             %{ $self->$orig(@args) },
19             Scheme => $self->attributes->{Scheme}[0]||'',
20             };
21             };
22              
23             1;
24              
25             =head1 NAME
26              
27             Catalyst::ActionRole::Scheme - Match on HTTP Request Scheme
28              
29             =head1 SYNOPSIS
30              
31             package MyApp::Web::Controller::MyController;
32              
33             use base 'Catalyst::Controller';
34              
35             sub is_http :Path(scheme) Scheme(http) Args(0) {
36             my ($self, $c) = @_;
37             Test::More::is $c->action->scheme, 'http';
38             $c->response->body("is_http");
39             }
40              
41             sub is_https :Path(scheme) Scheme(https) Args(0) {
42             my ($self, $c) = @_;
43             Test::More::is $c->action->scheme, 'https';
44             $c->response->body("is_https");
45             }
46              
47             1;
48              
49             =head1 DESCRIPTION
50              
51             This is an action role that lets your L<Catalyst::Action> match on the scheme
52             type of the request. Typically this is C<http> or C<https> but other common
53             schemes that L<Catalyst> can handle include C<ws> and C<wss> (web socket and web
54             socket secure).
55              
56             This also ensures that if you use C<uri_for> on an action that specifies a
57             match scheme, that the generated L<URI> object sets its scheme to that automatically
58             (rather than the scheme of the current request object, which is and remains the
59             default behavior.)
60              
61             For matching purposes, we match strings but the casing is insensitive.
62              
63             =head1 REQUIRES
64              
65             This role requires the following methods in the consuming class.
66              
67             =head2 match
68              
69             =head2 match_captures
70              
71             Returns 1 if the action matches the existing request and zero if not.
72              
73             =head1 METHODS
74              
75             This role defines the following methods
76              
77             =head2 match
78              
79             =head2 match_captures
80              
81             Around method modifier that return 1 if the scheme matches
82              
83             =head2 list_extra_info
84              
85             Add the scheme declaration if present to the debug screen.
86              
87             =head1 AUTHORS
88              
89             Catalyst Contributors, see L<Catalyst>
90              
91             =head1 COPYRIGHT
92              
93             See L<Catalyst>
94              
95             =cut