| 1 | package Eve::HttpResource; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 9 | ▶ | use parent qw(Eve::Class); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 9 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 9 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 9 | ▶ | use Eve::Exception; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | =head1 NAME | ||||||||||||||
| 11 | |||||||||||||||
| 12 | B<Eve::HttpResource> - a base class for HTTP resource controllers. | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||
| 15 | |||||||||||||||
| 16 | package Eve::HttpResource::SomeResource; | ||||||||||||||
| 17 | |||||||||||||||
| 18 | use parent qw(Eve::HttpResource); | ||||||||||||||
| 19 | |||||||||||||||
| 20 | sub _get { | ||||||||||||||
| 21 | # some implementation here | ||||||||||||||
| 22 | } | ||||||||||||||
| 23 | |||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||
| 25 | |||||||||||||||
| 26 | B<Eve::HttpResource> is a class encapsulating all the actual | ||||||||||||||
| 27 | processing of an HTTP request. C<_get()>, C<_post()> and C<_delete()> | ||||||||||||||
| 28 | methods can be overriden by the class derivatives. If not overriden | ||||||||||||||
| 29 | this methods throw C<Eve::Exception::Http::405MethodNotAllowed>. | ||||||||||||||
| 30 | |||||||||||||||
| 31 | Inside the described above methods class attributes C<_request>, | ||||||||||||||
| 32 | C<_response>, C<_session_constructor> and C<_dispatcher> can be found. | ||||||||||||||
| 33 | |||||||||||||||
| 34 | =head3 Constructor arguments | ||||||||||||||
| 35 | |||||||||||||||
| 36 | =over 4 | ||||||||||||||
| 37 | |||||||||||||||
| 38 | =item C<response> | ||||||||||||||
| 39 | |||||||||||||||
| 40 | an HTTP response object | ||||||||||||||
| 41 | |||||||||||||||
| 42 | =item C<session_constructor> | ||||||||||||||
| 43 | |||||||||||||||
| 44 | a reference to a subroutine accepting the session C<id> argument and | ||||||||||||||
| 45 | returning a session object | ||||||||||||||
| 46 | |||||||||||||||
| 47 | =item C<dispatcher> | ||||||||||||||
| 48 | |||||||||||||||
| 49 | an HTTP dispatcher object. | ||||||||||||||
| 50 | |||||||||||||||
| 51 | =back | ||||||||||||||
| 52 | |||||||||||||||
| 53 | =head1 METHODS | ||||||||||||||
| 54 | |||||||||||||||
| 55 | =head2 B<init()> | ||||||||||||||
| 56 | |||||||||||||||
| 57 | =cut | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 0 | ▶ | sub init { | ||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 60 | 0 | my ($self, %arg_hash) = @_; | |||||||||||||
| 61 | 0 | Eve::Support::arguments( | |||||||||||||
| 62 | \%arg_hash, | ||||||||||||||
| 63 | my ($response, $session_constructor, $dispatcher), | ||||||||||||||
| 64 | my $session_cookie_domain = \undef); | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 0 | $self->{'_response'} = $response; | |||||||||||||
| 67 | 0 | $self->{'_session_constructor'} = $session_constructor; | |||||||||||||
| 68 | 0 | $self->{'_dispatcher'} = $dispatcher; | |||||||||||||
| 69 | 0 | $self->{'_session_cookie_domain'} = $session_cookie_domain; | |||||||||||||
| 70 | |||||||||||||||
| 71 | $self->{'_method_map'} = { | ||||||||||||||
| 72 | 0 | ▶ | 'GET' => sub { return $self->_get(@_); }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 73 | 0 | ▶ | 'POST' => sub { return $self->_post(@_); }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 74 | 0 | ▶ | 'DELETE' => sub { return $self->_delete(@_); }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 75 | 0 | ▶ | 'PUT' => sub { return $self->_post(@_); }}; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 76 | |||||||||||||||
| 77 | 0 | return; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | =head2 B<process()> | ||||||||||||||
| 81 | |||||||||||||||
| 82 | Processes an HTTP request delegating control to the appropriate HTTP method | ||||||||||||||
| 83 | implementation. | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =head3 Arguments | ||||||||||||||
| 86 | |||||||||||||||
| 87 | =over 4 | ||||||||||||||
| 88 | |||||||||||||||
| 89 | =item C<matches_hash> | ||||||||||||||
| 90 | |||||||||||||||
| 91 | a hash containing pattern matches from the URL. | ||||||||||||||
| 92 | |||||||||||||||
| 93 | =item C<request> | ||||||||||||||
| 94 | |||||||||||||||
| 95 | an HTTP request object. | ||||||||||||||
| 96 | |||||||||||||||
| 97 | =back | ||||||||||||||
| 98 | |||||||||||||||
| 99 | =head3 Throws | ||||||||||||||
| 100 | |||||||||||||||
| 101 | =over 4 | ||||||||||||||
| 102 | |||||||||||||||
| 103 | =item C<Eve::Exception::Http::405MethodNotAllowed> | ||||||||||||||
| 104 | |||||||||||||||
| 105 | when a not allowed HTTP method specified. | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =back | ||||||||||||||
| 108 | |||||||||||||||
| 109 | =head3 Returns | ||||||||||||||
| 110 | |||||||||||||||
| 111 | a ready HTTP response object. | ||||||||||||||
| 112 | |||||||||||||||
| 113 | =cut | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 0 | ▶ | sub process { | ||||||||||||
|
Subroutine
process: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||
| 116 | 0 | my ($self, %arg_hash) = @_; | |||||||||||||
| 117 | 0 | Eve::Support::arguments(\%arg_hash, my ($matches_hash, $request)); | |||||||||||||
| 118 | |||||||||||||||
| 119 | 0 | $self->{'_request'} = $request; | |||||||||||||
| 120 | |||||||||||||||
| 121 | 0 | $self->{'_response'} = $self->{'_response'}->new(); | |||||||||||||
| 122 | |||||||||||||||
| 123 | 0 | $self->{'_session'} = $self->_session_constructor->( | |||||||||||||
| 124 | id => $self->_request->get_cookie(name => 'session_id')); | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 0 | ▶ | if (!($self->_session->get_id() ~~ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 127 | $self->_request->get_cookie(name => 'session_id'))) { | ||||||||||||||
| 128 | 0 | ▶ | $self->_response->set_cookie( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | name => 'session_id', | ||||||||||||||
| 130 | value => $self->_session->get_id(), | ||||||||||||||
| 131 | expires => time + $self->_session->expiration_interval, | ||||||||||||||
| 132 | (defined $self->_session_cookie_domain ? | ||||||||||||||
| 133 | ('domain' => $self->_session_cookie_domain) : ())); | ||||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 0 | eval { | |||||||||||||
| 137 | $self->{'_method_map'}->{$self->_request->get_method()}->(%{ | ||||||||||||||
| 138 | 0 | $matches_hash}); | |||||||||||||
| 139 | }; | ||||||||||||||
| 140 | |||||||||||||||
| 141 | 0 | my $e; | |||||||||||||
| 142 | 0 | ▶ | if ($e = Eve::Exception::Privilege->caught()) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 143 | #print STDERR "HttpResource::process: caught privilege exception!\n"; | ||||||||||||||
| 144 | 0 | Eve::Exception::Http::403Forbidden->throw(message => $e->message); | |||||||||||||
| 145 | } elsif ($e = Exception::Class->caught()) { | ||||||||||||||
| 146 | 0 | ▶ | ref $e ? $e->rethrow() : die $e; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | 0 | return $self->{'_response'}; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | =head2 B<get_method_list()> | ||||||||||||||
| 153 | |||||||||||||||
| 154 | Returns a list of supported HTTP methods. | ||||||||||||||
| 155 | |||||||||||||||
| 156 | =head3 Returns | ||||||||||||||
| 157 | |||||||||||||||
| 158 | A list reference. | ||||||||||||||
| 159 | |||||||||||||||
| 160 | =cut | ||||||||||||||
| 161 | |||||||||||||||
| 162 | 0 | ▶ | sub get_method_list { | ||||||||||||
|
Subroutine
get_method_list: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 163 | 0 | my $self = shift; | |||||||||||||
| 164 | |||||||||||||||
| 165 | 0 | return [keys %{$self->{'_method_map'}}]; | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | |||||||||||||||
| 168 | 0 | ▶ | sub _get { | ||||||||||||
|
Subroutine
_get: not called CC=1 SCAR=6.9
| |||||||||||||||
| 169 | 0 | Eve::Exception::Http::405MethodNotAllowed->throw(); | |||||||||||||
| 170 | } | ||||||||||||||
| 171 | |||||||||||||||
| 172 | 0 | ▶ | sub _post { | ||||||||||||
|
Subroutine
_post: not called CC=1 SCAR=6.9
| |||||||||||||||
| 173 | 0 | Eve::Exception::Http::405MethodNotAllowed->throw(); | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | |||||||||||||||
| 176 | 0 | ▶ | sub _delete { | ||||||||||||
|
Subroutine
_delete: not called CC=1 SCAR=6.9
| |||||||||||||||
| 177 | 0 | Eve::Exception::Http::405MethodNotAllowed->throw(); | |||||||||||||
| 178 | } | ||||||||||||||
| 179 | |||||||||||||||
| 180 | =head1 SEE ALSO | ||||||||||||||
| 181 | |||||||||||||||
| 182 | =over 4 | ||||||||||||||
| 183 | |||||||||||||||
| 184 | =item L<Eve::Class> | ||||||||||||||
| 185 | |||||||||||||||
| 186 | =item L<Eve::Exception> | ||||||||||||||
| 187 | |||||||||||||||
| 188 | =back | ||||||||||||||
| 189 | |||||||||||||||
| 190 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||
| 191 | |||||||||||||||
| 192 | Copyright 2012 Igor Zinovyev. | ||||||||||||||
| 193 | |||||||||||||||
| 194 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||
| 195 | under the terms of either: the GNU General Public License as published | ||||||||||||||
| 196 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||
| 197 | |||||||||||||||
| 198 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||
| 199 | |||||||||||||||
| 200 | |||||||||||||||
| 201 | =head1 AUTHOR | ||||||||||||||
| 202 | |||||||||||||||
| 203 | =over 4 | ||||||||||||||
| 204 | |||||||||||||||
| 205 | =item L<Sergey Konoplev|mailto:gray.ru@gmail.com> | ||||||||||||||
| 206 | |||||||||||||||
| 207 | =item L<Igor Zinovyev|mailto:zinigor@gmail.com> | ||||||||||||||
| 208 | |||||||||||||||
| 209 | =back | ||||||||||||||
| 210 | |||||||||||||||
| 211 | =cut | ||||||||||||||
| 212 | |||||||||||||||
| 213 | 9 | 1; | |||||||||||||