line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Web::Machine; |
2
|
|
|
|
|
|
|
# ABSTRACT: A Perl port of Webmachine |
3
|
|
|
|
|
|
|
|
4
|
10
|
|
|
10
|
|
399809
|
use strict; |
|
10
|
|
|
|
|
16
|
|
|
10
|
|
|
|
|
397
|
|
5
|
10
|
|
|
10
|
|
43
|
use warnings; |
|
10
|
|
|
|
|
15
|
|
|
10
|
|
|
|
|
398
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.16'; |
8
|
|
|
|
|
|
|
|
9
|
10
|
|
|
10
|
|
1495
|
use Try::Tiny; |
|
10
|
|
|
|
|
3524
|
|
|
10
|
|
|
|
|
567
|
|
10
|
10
|
|
|
10
|
|
56
|
use Carp qw[ confess ]; |
|
10
|
|
|
|
|
70
|
|
|
10
|
|
|
|
|
457
|
|
11
|
10
|
|
|
10
|
|
53
|
use Scalar::Util qw[ blessed ]; |
|
10
|
|
|
|
|
16
|
|
|
10
|
|
|
|
|
665
|
|
12
|
10
|
|
|
10
|
|
5066
|
use Module::Runtime qw[ use_package_optimistically ]; |
|
10
|
|
|
|
|
13767
|
|
|
10
|
|
|
|
|
51
|
|
13
|
|
|
|
|
|
|
|
14
|
10
|
|
|
10
|
|
4551
|
use Plack::Request; |
|
10
|
|
|
|
|
327203
|
|
|
10
|
|
|
|
|
323
|
|
15
|
10
|
|
|
10
|
|
4239
|
use Plack::Response; |
|
10
|
|
|
|
|
27164
|
|
|
10
|
|
|
|
|
298
|
|
16
|
|
|
|
|
|
|
|
17
|
10
|
|
|
10
|
|
3659
|
use Web::Machine::Util qw[ inflate_headers ]; |
|
10
|
|
|
|
|
26
|
|
|
10
|
|
|
|
|
122
|
|
18
|
10
|
|
|
10
|
|
6272
|
use Web::Machine::FSM; |
|
10
|
|
|
|
|
27
|
|
|
10
|
|
|
|
|
331
|
|
19
|
|
|
|
|
|
|
|
20
|
10
|
|
|
10
|
|
67
|
use parent 'Plack::Component'; |
|
10
|
|
|
|
|
235
|
|
|
10
|
|
|
|
|
82
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new { |
23
|
19
|
|
|
19
|
1
|
10904
|
my ($class, %args) = @_; |
24
|
|
|
|
|
|
|
|
25
|
19
|
50
|
33
|
|
|
260
|
(exists $args{'resource'} |
|
|
|
33
|
|
|
|
|
26
|
|
|
|
|
|
|
&& (not blessed $args{'resource'}) |
27
|
|
|
|
|
|
|
&& use_package_optimistically($args{'resource'})->isa('Web::Machine::Resource')) |
28
|
|
|
|
|
|
|
|| confess 'You must pass in a resource for this Web::Machine'; |
29
|
|
|
|
|
|
|
|
30
|
19
|
100
|
|
|
|
5020
|
if (exists $args{'request_class'}) { |
31
|
4
|
100
|
|
|
|
9
|
use_package_optimistically($args{'request_class'})->isa('Plack::Request') |
32
|
|
|
|
|
|
|
|| confess 'The request_class class must inherit from Plack::Request'; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
else { |
35
|
15
|
|
|
|
|
45
|
$args{'request_class'} = 'Plack::Request'; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
16
|
|
|
|
|
423
|
$class->SUPER::new( \%args ); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub inflate_request { |
42
|
34
|
|
|
34
|
1
|
70
|
my ($self, $env) = @_; |
43
|
34
|
|
|
|
|
525
|
inflate_headers( $self->{request_class}->new( $env ) ); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub create_fsm { |
47
|
32
|
|
|
32
|
1
|
45
|
my $self = shift; |
48
|
32
|
|
|
|
|
223
|
Web::Machine::FSM->new( tracing => $self->{'tracing'} ) |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub create_resource { |
52
|
32
|
|
|
32
|
1
|
39
|
my ($self, $request) = @_; |
53
|
32
|
50
|
|
|
|
755
|
$self->{'resource'}->new( |
54
|
|
|
|
|
|
|
request => $request, |
55
|
|
|
|
|
|
|
response => $request->new_response, |
56
|
32
|
|
|
|
|
143
|
@{ $self->{'resource_args'} || [] }, |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub finalize_response { |
61
|
33
|
|
|
33
|
1
|
63
|
my ($self, $response) = @_; |
62
|
33
|
|
|
|
|
90
|
$response->finalize; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub call { |
66
|
33
|
|
|
33
|
1
|
104522
|
my ($self, $env) = @_; |
67
|
|
|
|
|
|
|
|
68
|
33
|
|
|
33
|
|
189
|
my $request = try { $self->inflate_request($env) }; |
|
33
|
|
|
|
|
490
|
|
69
|
|
|
|
|
|
|
|
70
|
33
|
100
|
|
|
|
70237
|
return $self->finalize_response( Plack::Response->new( 400 ) ) |
71
|
|
|
|
|
|
|
unless defined $request; |
72
|
|
|
|
|
|
|
|
73
|
32
|
|
|
|
|
87
|
my $resource = $self->create_resource( $request ); |
74
|
32
|
|
|
|
|
76
|
my $fsm = $self->create_fsm; |
75
|
|
|
|
|
|
|
|
76
|
32
|
100
|
|
|
|
107
|
if ($self->{'streaming'}) { |
77
|
|
|
|
|
|
|
return sub { |
78
|
10
|
|
|
10
|
|
2168
|
my $responder = shift; |
79
|
|
|
|
|
|
|
|
80
|
10
|
|
|
|
|
37
|
my $response = $self->finalize_response( $fsm->run( $resource ) ); |
81
|
|
|
|
|
|
|
|
82
|
10
|
100
|
|
|
|
1724
|
if (my $cb = $env->{'web.machine.streaming_push'}) { |
83
|
4
|
|
|
|
|
5
|
pop @$response; |
84
|
4
|
|
|
|
|
11
|
my $writer = $responder->($response); |
85
|
4
|
|
|
|
|
77
|
$cb->($writer); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
else { |
88
|
6
|
|
|
|
|
20
|
$responder->($response); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
10
|
|
|
|
|
66
|
} |
92
|
|
|
|
|
|
|
else { |
93
|
22
|
|
|
|
|
82
|
my $response = $self->finalize_response( $fsm->run( $resource ) ); |
94
|
|
|
|
|
|
|
|
95
|
22
|
100
|
|
|
|
3883
|
if ($env->{'web.machine.streaming_push'}) { |
96
|
1
|
|
|
|
|
13
|
die "Can't do a streaming push response " |
97
|
|
|
|
|
|
|
. "unless the 'streaming' option was set"; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
else { |
100
|
21
|
|
|
|
|
303
|
return $response; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
1; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
__END__ |