| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Eve::HttpResource; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 8 |  |  | 8 |  | 4191 | use parent qw(Eve::Class); | 
|  | 8 |  |  |  |  | 16 |  | 
|  | 8 |  |  |  |  | 44 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 8 |  |  | 8 |  | 586 | use strict; | 
|  | 8 |  |  |  |  | 18 |  | 
|  | 8 |  |  |  |  | 224 |  | 
| 6 | 8 |  |  | 8 |  | 39 | use warnings; | 
|  | 8 |  |  |  |  | 15 |  | 
|  | 8 |  |  |  |  | 224 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 8 |  |  | 8 |  | 45 | use Eve::Exception; | 
|  | 8 |  |  |  |  | 15 |  | 
|  | 8 |  |  |  |  | 6246 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | =head1 NAME | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | B - 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 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. | 
| 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 | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | an HTTP response object | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | =item C | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | a reference to a subroutine accepting the session C argument and | 
| 45 |  |  |  |  |  |  | returning a session object | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | =item C | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | an HTTP dispatcher object. | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =back | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | =head1 METHODS | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =head2 B | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | =cut | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub init { | 
| 60 | 0 |  |  | 0 | 1 |  | 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 |  |  | 0 |  |  | 'GET' => sub { return $self->_get(@_); }, | 
| 73 | 0 |  |  | 0 |  |  | 'POST' => sub { return $self->_post(@_); }, | 
| 74 | 0 |  |  | 0 |  |  | 'DELETE' => sub { return $self->_delete(@_); }, | 
| 75 | 0 |  |  | 0 |  |  | 'PUT' => sub { return $self->_post(@_); }}; | 
|  | 0 |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 0 |  |  |  |  |  | return; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | =head2 B | 
| 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 | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | a hash containing pattern matches from the URL. | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =item C | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | an HTTP request object. | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =back | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =head3 Throws | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =over 4 | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =item C | 
| 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 |  |  |  |  |  |  | sub process { | 
| 116 | 0 |  |  | 0 | 1 |  | 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 | 0 |  |  |  |  | if (!($self->_session->get_id() ~~ | 
| 127 |  |  |  |  |  |  | $self->_request->get_cookie(name => 'session_id'))) { | 
| 128 | 0 | 0 |  |  |  |  | $self->_response->set_cookie( | 
| 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 | 0 |  |  |  |  |  | $self->{'_method_map'}->{$self->_request->get_method()}->(%{ | 
| 138 | 0 |  |  |  |  |  | $matches_hash}); | 
| 139 |  |  |  |  |  |  | }; | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 0 |  |  |  |  |  | my $e; | 
| 142 | 0 | 0 |  |  |  |  | if ($e = Eve::Exception::Privilege->caught()) { | 
|  |  | 0 |  |  |  |  |  | 
| 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 | 0 |  |  |  |  | ref $e ? $e->rethrow() : die $e; | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 0 |  |  |  |  |  | return $self->{'_response'}; | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =head2 B | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | Returns a list of supported HTTP methods. | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =head3 Returns | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | A list reference. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | =cut | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | sub get_method_list { | 
| 163 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 0 |  |  |  |  |  | return [keys %{$self->{'_method_map'}}]; | 
|  | 0 |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | } | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | sub _get { | 
| 169 | 0 |  |  | 0 |  |  | Eve::Exception::Http::405MethodNotAllowed->throw(); | 
| 170 |  |  |  |  |  |  | } | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | sub _post { | 
| 173 | 0 |  |  | 0 |  |  | Eve::Exception::Http::405MethodNotAllowed->throw(); | 
| 174 |  |  |  |  |  |  | } | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | sub _delete { | 
| 177 | 0 |  |  | 0 |  |  | Eve::Exception::Http::405MethodNotAllowed->throw(); | 
| 178 |  |  |  |  |  |  | } | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | =over 4 | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =item L | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | =item L | 
| 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 | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | =item L | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | =back | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | =cut | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | 1; |