line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Engine::Embeddable; |
2
|
3
|
|
|
3
|
|
270886
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
use URI; |
4
|
|
|
|
|
|
|
use HTTP::Body; |
5
|
|
|
|
|
|
|
use HTTP::Response; |
6
|
|
|
|
|
|
|
use namespace::autoclean; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
extends 'Catalyst::Engine'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.000003'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub prepare_request { |
13
|
|
|
|
|
|
|
my ($self, $c, $req, $res_ref, $err_ref) = @_; |
14
|
|
|
|
|
|
|
$c->req->{_engine_embeddable}{req} = $req; |
15
|
|
|
|
|
|
|
$c->req->{_engine_embeddable}{res} = $res_ref; |
16
|
|
|
|
|
|
|
$c->req->{_engine_embeddable}{err} = $err_ref; |
17
|
|
|
|
|
|
|
$c->req->method($req->method); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub prepare_headers { |
21
|
|
|
|
|
|
|
my ($self, $c) = @_; |
22
|
|
|
|
|
|
|
$c->req->{_engine_embeddable}{req}->scan |
23
|
|
|
|
|
|
|
(sub { |
24
|
|
|
|
|
|
|
my ($name, $value) = @_; |
25
|
|
|
|
|
|
|
$c->req->header($name, $value); |
26
|
|
|
|
|
|
|
}); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub prepare_path { |
30
|
|
|
|
|
|
|
my ($self, $c) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $uri = $c->req->{_engine_embeddable}{req}->uri(); |
33
|
|
|
|
|
|
|
my $base = $uri->clone; $base->path('/'); $base->path_query(''); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$c->req->uri($uri); |
36
|
|
|
|
|
|
|
$c->req->base($base); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub prepare_query_parameters { |
40
|
|
|
|
|
|
|
my ($self, $c) = @_; |
41
|
|
|
|
|
|
|
my %params = $c->req->{_engine_embeddable}{req}->uri->query_form; |
42
|
|
|
|
|
|
|
$c->req->query_parameters(\%params); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub prepare_body { |
46
|
|
|
|
|
|
|
my ($self, $c) = @_; |
47
|
|
|
|
|
|
|
my $req = $c->req->{_engine_embeddable}{req}; |
48
|
|
|
|
|
|
|
$req->content_length(0) unless $req->content_length; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$c->req->content_encoding($req->content_encoding); |
51
|
|
|
|
|
|
|
$c->req->content_type($req->content_type); |
52
|
|
|
|
|
|
|
$c->req->content_length($req->content_length); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $http_body = HTTP::Body->new($c->req->content_type, $c->req->content_length); |
55
|
|
|
|
|
|
|
$http_body->add($req->content()); |
56
|
|
|
|
|
|
|
$c->req->{_body} = $http_body; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub finalize_error { |
60
|
|
|
|
|
|
|
my ($self, $c) = @_; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$self->next::method($c, @_); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
@{ $c->req->{_engine_embeddable}{err} } = @{ $c->error } |
65
|
|
|
|
|
|
|
if $c->req->{_engine_embeddable}{err}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
$c->res->status(500); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub finalize_headers { |
71
|
|
|
|
|
|
|
my ($self, $c) = @_; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
my $response = HTTP::Response->new($c->res->status, |
74
|
|
|
|
|
|
|
'Catalyst-Engine-Embeddable', |
75
|
|
|
|
|
|
|
$c->res->headers); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
${$c->req->{_engine_embeddable}{res}} = $response; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub finalize_body { |
81
|
|
|
|
|
|
|
my ($self, $c) = @_; |
82
|
|
|
|
|
|
|
${$c->req->{_engine_embeddable}{res}}->content($c->res->body()); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
86
|
|
|
|
|
|
|
__END__ |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 NAME |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Catalyst::Engine::Embeddable - Use a Catalyst application as an object |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 SYNOPSIS |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# after creating the application using this engine, you can just |
95
|
|
|
|
|
|
|
my $http_response; |
96
|
|
|
|
|
|
|
my $response_code $app->handle_request( |
97
|
|
|
|
|
|
|
$http_request, \$http_response); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 ABSTRACT |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Enables a Catalyst application to be used as a standard Perl object. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 SUMMARY |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
This module provides a way to embed a Catalyst application in any |
106
|
|
|
|
|
|
|
other program using standard Perl Object Orientation to do a request |
107
|
|
|
|
|
|
|
and get the response. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
It works by using the arguments that are passed from the |
110
|
|
|
|
|
|
|
handle_request method in the Catalyst module to the prepare_request |
111
|
|
|
|
|
|
|
method in the engine, which will then handle the request as coming |
112
|
|
|
|
|
|
|
from the HTTP::Request object passed, instead of trying to fetch the |
113
|
|
|
|
|
|
|
elements from the ENV, like the CGI engine does. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
As the handle_request method only returns the response code and not |
116
|
|
|
|
|
|
|
the response object at all, in the case you want the complete response |
117
|
|
|
|
|
|
|
you need to pass a second argument which is a scalar ref where the |
118
|
|
|
|
|
|
|
HTTP::Response object will be stored in the "finalize" phase of the |
119
|
|
|
|
|
|
|
processing. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
This engine provides complete compatibility with any plugin the |
122
|
|
|
|
|
|
|
application may use, in a way that different embedded applications may |
123
|
|
|
|
|
|
|
use different plugins and still be used side-by-side. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
There's one important consideration regarding the URI in the |
126
|
|
|
|
|
|
|
request. For the means of the Catalyst processing, the base path for |
127
|
|
|
|
|
|
|
the script is always constructed as the "/" path of the same URI as |
128
|
|
|
|
|
|
|
the request. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 METHODS |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
The following methods were overriden from Catalyst::Engine. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=over |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item $engine->prepare_request($c, $http_request, $http_response_ret_ref) |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
This method is overriden in order to store the request and the |
139
|
|
|
|
|
|
|
response in $c as to continue the processing later. The scalar ref |
140
|
|
|
|
|
|
|
here will be used to set the response object, as there is no other way |
141
|
|
|
|
|
|
|
to obtain the response. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
This information will be stored as $c->req->{_engine_embeddable}{req} and |
144
|
|
|
|
|
|
|
$c->req->{_engine_embeddable}{res} for future usage. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item $engine->prepare_headers($c) |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This is where the headers are fetched from the HTTP::Request object |
149
|
|
|
|
|
|
|
and set into $c->req. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item $engine->prepare_path($c) |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Get the path info from the HTTP::Request object. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item $engine->prepare_query_parameters($c) |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Set the query params from the HTTP::Request to the catalyst request. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item $engine->prepare_body($c) |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Gets the body of the HTTP::Request, creates an HTTP::Body object, and |
162
|
|
|
|
|
|
|
set it in $c->req->{_body}, then being compatible with |
163
|
|
|
|
|
|
|
Catalyst::Engine from now on. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=item $engine->finalize_headers($c) |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Set the "Status" header in the response and store the headers from the |
168
|
|
|
|
|
|
|
catalyst response object to the HTTP::Response object. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item $engine->finalize_body($c) |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Copies the body from the catalyst response to the HTTP::Response |
173
|
|
|
|
|
|
|
object. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=back |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 SEE ALSO |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
L<Catalyst::Engine>, L<Catalyst::Engine::CGI>, L<HTTP::Request>, |
180
|
|
|
|
|
|
|
L<HTTP::Reponse>, L<Catalyst> |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 AUTHORS |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Daniel Ruoso C<daniel@ruoso.com> |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Currently maintained by Tomas Doran (t0m) C<bobtfish@bobtfish.net> |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 BUG REPORTS |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Please submit all bugs regarding C<Catalyst::Engine::Embeddable> to |
191
|
|
|
|
|
|
|
C<bug-catalyst-engine-embeddable@rt.cpan.org> |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 LICENSE |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
This library is free software, you can redistribute it and/or modify |
196
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=cut |
199
|
|
|
|
|
|
|
|