line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::App::REST; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
114804
|
use 5.008_005; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
95
|
|
4
|
3
|
|
|
3
|
|
11
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
71
|
|
5
|
3
|
|
|
3
|
|
9
|
use warnings FATAL => 'all'; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
124
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.02'; # Set automatically by milla |
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
351
|
use parent qw( Plack::Component ); |
|
3
|
|
|
|
|
199
|
|
|
3
|
|
|
|
|
13
|
|
10
|
3
|
|
|
3
|
|
10926
|
use HTTP::Exception; |
|
3
|
|
|
|
|
10295
|
|
|
3
|
|
|
|
|
14
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub call { |
13
|
7
|
|
|
7
|
1
|
34658
|
my($self, $env) = @_; |
14
|
|
|
|
|
|
|
|
15
|
7
|
|
|
|
|
11
|
my $method = $env->{REQUEST_METHOD}; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
### Throw an exception if method is not defined |
18
|
7
|
100
|
|
|
|
46
|
if (!$self->can($method)){ |
19
|
1
|
|
|
|
|
10
|
return [405, ['Content-Type', 'text/plain'], ['Method Not Allowed']]; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
### Set params of path |
23
|
6
|
|
|
|
|
14
|
my $id = _get_param($env); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
### Set ref to env |
26
|
6
|
|
|
|
|
21
|
$env->{'REST.class'} = ref $self; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# compatibility with Plack::Middleware::ParseContent |
29
|
6
|
50
|
|
|
|
14
|
my $data = $env->{'parsecontent.data'} if exists $env->{'parsecontent.data'}; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
### Call method |
32
|
6
|
|
|
|
|
8
|
my ($ret, $h) = eval{ $self->$method($env, $id, $data) }; |
|
6
|
|
|
|
|
18
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
### Parse output |
35
|
6
|
100
|
|
|
|
1576
|
if ( my $e = HTTP::Exception->caught ) { |
|
|
100
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
2
|
|
|
|
|
50
|
my @headers = ('Content-Type', 'text/plain'); |
38
|
2
|
|
|
|
|
5
|
my $code = $e->code; |
39
|
|
|
|
|
|
|
|
40
|
2
|
50
|
33
|
|
|
8
|
if ( $code =~ /^3/ && (my $loc = eval{$e->location}) ) { |
|
0
|
|
|
|
|
0
|
|
41
|
0
|
|
|
|
|
0
|
push( @headers, Location => $loc ); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
2
|
|
|
|
|
11
|
$env->{'psgi.errors'}->print( $e ); |
45
|
2
|
|
|
|
|
2816
|
return [ $code, \@headers, [$e->message] ]; |
46
|
|
|
|
|
|
|
}elsif($@){ |
47
|
1
|
|
|
|
|
22
|
$env->{'psgi.errors'}->print( $e ); |
48
|
1
|
|
|
|
|
12
|
return [ 500, ['Content-Type', 'text/plain'], [$@] ]; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
3
|
|
50
|
|
|
85
|
return [200, ($h||[]), $ret]; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
### Get last requested path |
55
|
|
|
|
|
|
|
sub _get_param { |
56
|
6
|
|
|
6
|
|
9
|
my $env = shift; |
57
|
6
|
|
|
|
|
9
|
my $p = $env->{PATH_INFO}; |
58
|
6
|
100
|
100
|
|
|
33
|
return if !$p or $p eq '/'; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# get param of uri |
61
|
3
|
|
|
|
|
12
|
(my $r = $p) =~ s/\+/ /g; |
62
|
3
|
|
|
|
|
12
|
$r =~ m!/(?:([^/]*))!g; |
63
|
3
|
|
|
|
|
9
|
return $1; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
1; |
67
|
|
|
|
|
|
|
__END__ |