line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Web::Machine; |
2
|
|
|
|
|
|
|
# ABSTRACT: A Perl port of Webmachine |
3
|
|
|
|
|
|
|
|
4
|
10
|
|
|
10
|
|
301440
|
use strict; |
|
10
|
|
|
|
|
14
|
|
|
10
|
|
|
|
|
238
|
|
5
|
10
|
|
|
10
|
|
30
|
use warnings; |
|
10
|
|
|
|
|
10
|
|
|
10
|
|
|
|
|
350
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.17'; |
8
|
|
|
|
|
|
|
|
9
|
10
|
|
|
10
|
|
1249
|
use Try::Tiny; |
|
10
|
|
|
|
|
2870
|
|
|
10
|
|
|
|
|
438
|
|
10
|
10
|
|
|
10
|
|
36
|
use Carp qw[ confess ]; |
|
10
|
|
|
|
|
11
|
|
|
10
|
|
|
|
|
331
|
|
11
|
10
|
|
|
10
|
|
33
|
use Scalar::Util qw[ blessed ]; |
|
10
|
|
|
|
|
10
|
|
|
10
|
|
|
|
|
501
|
|
12
|
10
|
|
|
10
|
|
3687
|
use Module::Runtime qw[ use_package_optimistically ]; |
|
10
|
|
|
|
|
10754
|
|
|
10
|
|
|
|
|
44
|
|
13
|
|
|
|
|
|
|
|
14
|
10
|
|
|
10
|
|
3518
|
use Plack::Request; |
|
10
|
|
|
|
|
245821
|
|
|
10
|
|
|
|
|
231
|
|
15
|
10
|
|
|
10
|
|
2861
|
use Plack::Response; |
|
10
|
|
|
|
|
19723
|
|
|
10
|
|
|
|
|
228
|
|
16
|
|
|
|
|
|
|
|
17
|
10
|
|
|
10
|
|
3042
|
use Web::Machine::Util qw[ inflate_headers ]; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
72
|
|
18
|
10
|
|
|
10
|
|
4968
|
use Web::Machine::FSM; |
|
10
|
|
|
|
|
21
|
|
|
10
|
|
|
|
|
283
|
|
19
|
|
|
|
|
|
|
|
20
|
10
|
|
|
10
|
|
44
|
use parent 'Plack::Component'; |
|
10
|
|
|
|
|
154
|
|
|
10
|
|
|
|
|
59
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new { |
23
|
19
|
|
|
19
|
1
|
11020
|
my ($class, %args) = @_; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
(exists $args{'resource'} |
26
|
|
|
|
|
|
|
&& (not blessed $args{'resource'}) |
27
|
19
|
50
|
33
|
|
|
214
|
&& use_package_optimistically($args{'resource'})->isa('Web::Machine::Resource')) |
|
|
|
33
|
|
|
|
|
28
|
|
|
|
|
|
|
|| confess 'You must pass in a resource for this Web::Machine'; |
29
|
|
|
|
|
|
|
|
30
|
19
|
100
|
|
|
|
290851
|
if (exists $args{'request_class'}) { |
31
|
4
|
100
|
|
|
|
7
|
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
|
|
|
|
|
36
|
$args{'request_class'} = 'Plack::Request'; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
16
|
|
|
|
|
362
|
$class->SUPER::new( \%args ); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub inflate_request { |
42
|
34
|
|
|
34
|
1
|
53
|
my ($self, $env) = @_; |
43
|
34
|
|
|
|
|
337
|
inflate_headers( $self->{request_class}->new( $env ) ); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub create_fsm { |
47
|
32
|
|
|
32
|
1
|
32
|
my $self = shift; |
48
|
32
|
|
|
|
|
203
|
Web::Machine::FSM->new( tracing => $self->{'tracing'} ) |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub create_resource { |
52
|
32
|
|
|
32
|
1
|
36
|
my ($self, $request) = @_; |
53
|
|
|
|
|
|
|
$self->{'resource'}->new( |
54
|
|
|
|
|
|
|
request => $request, |
55
|
|
|
|
|
|
|
response => $request->new_response, |
56
|
32
|
50
|
|
|
|
105
|
@{ $self->{'resource_args'} || [] }, |
|
32
|
|
|
|
|
717
|
|
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub finalize_response { |
61
|
33
|
|
|
33
|
1
|
54
|
my ($self, $response) = @_; |
62
|
33
|
|
|
|
|
73
|
$response->finalize; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub call { |
66
|
33
|
|
|
33
|
1
|
86426
|
my ($self, $env) = @_; |
67
|
|
|
|
|
|
|
|
68
|
33
|
|
|
33
|
|
183
|
my $request = try { $self->inflate_request($env) }; |
|
33
|
|
|
|
|
416
|
|
69
|
|
|
|
|
|
|
|
70
|
33
|
100
|
|
|
|
58439
|
return $self->finalize_response( Plack::Response->new( 400 ) ) |
71
|
|
|
|
|
|
|
unless defined $request; |
72
|
|
|
|
|
|
|
|
73
|
32
|
|
|
|
|
68
|
my $resource = $self->create_resource( $request ); |
74
|
32
|
|
|
|
|
64
|
my $fsm = $self->create_fsm; |
75
|
|
|
|
|
|
|
|
76
|
32
|
100
|
|
|
|
83
|
if ($self->{'streaming'}) { |
77
|
|
|
|
|
|
|
return sub { |
78
|
10
|
|
|
10
|
|
2389
|
my $responder = shift; |
79
|
|
|
|
|
|
|
|
80
|
10
|
|
|
|
|
30
|
my $response = $self->finalize_response( $fsm->run( $resource ) ); |
81
|
|
|
|
|
|
|
|
82
|
10
|
100
|
|
|
|
2174
|
if (my $cb = $env->{'web.machine.streaming_push'}) { |
83
|
4
|
|
|
|
|
4
|
pop @$response; |
84
|
4
|
|
|
|
|
8
|
my $writer = $responder->($response); |
85
|
4
|
|
|
|
|
68
|
$cb->($writer); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
else { |
88
|
6
|
|
|
|
|
12
|
$responder->($response); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
10
|
|
|
|
|
52
|
} |
92
|
|
|
|
|
|
|
else { |
93
|
22
|
|
|
|
|
62
|
my $response = $self->finalize_response( $fsm->run( $resource ) ); |
94
|
|
|
|
|
|
|
|
95
|
22
|
100
|
|
|
|
3552
|
if ($env->{'web.machine.streaming_push'}) { |
96
|
1
|
|
|
|
|
9
|
die "Can't do a streaming push response " |
97
|
|
|
|
|
|
|
. "unless the 'streaming' option was set"; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
else { |
100
|
21
|
|
|
|
|
261
|
return $response; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
1; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
__END__ |