Commit | Line | Data |
342d2169 |
1 | package Catalyst::ActionRole::Scheme; |
2 | |
3 | use Moose::Role; |
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 { |
2eb1c5fc |
18 | %{ $self->$orig(@args) }, |
342d2169 |
19 | Scheme => $self->attributes->{Scheme}[0]||'', |
20 | }; |
21 | }; |
22 | |
23 | 1; |
24 | |
25 | =head1 NAME |
26 | |
2eb1c5fc |
27 | Catalyst::ActionRole::Scheme - Match on HTTP Request Scheme |
342d2169 |
28 | |
29 | =head1 SYNOPSIS |
30 | |
31 | package MyApp::Web::Controller::MyController; |
32 | |
33 | use base 'Catalyst::Controller'; |
34 | |
1380fba8 |
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 | } |
342d2169 |
40 | |
1380fba8 |
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 | } |
342d2169 |
46 | |
1380fba8 |
47 | 1; |
342d2169 |
48 | |
49 | =head1 DESCRIPTION |
50 | |
2eb1c5fc |
51 | This is an action role that lets your L<Catalyst::Action> match on the scheme |
1380fba8 |
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). |
342d2169 |
55 | |
1380fba8 |
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.) |
342d2169 |
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 | |
1380fba8 |
81 | Around method modifier that return 1 if the scheme matches |
342d2169 |
82 | |
83 | =head2 list_extra_info |
84 | |
473078ff |
85 | Add the scheme declaration if present to the debug screen. |
342d2169 |
86 | |
87 | =head1 AUTHORS |
88 | |
1380fba8 |
89 | Catalyst Contributors, see L<Catalyst> |
342d2169 |
90 | |
91 | =head1 COPYRIGHT |
92 | |
1380fba8 |
93 | See L<Catalyst> |
342d2169 |
94 | |
95 | =cut |