blib/lib/Plack/Handler/Mongrel2.pm untested

filter: Statementstmt 0.0% 0 / 212 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 328 CC 28 SCAR 33.2
CC
28
Cov
0%
mongrel2_req_to_psgi
72.0
reply
56.0
prepare_zmq
30.0
CRAP 27.8
1 package Plack::Handler::Mongrel2;
2 use strict;
3 use base qw(Plack::Handler);
4 our $VERSION = '0.01000_04';
5 use ZeroMQ::Raw;
6 use ZeroMQ::Constants qw(
7 ZMQ_POLLIN
8 ZMQ_PULL
9 ZMQ_PUB
10 ZMQ_IDENTITY
11 ZMQ_RCVMORE
12 ZMQ_LINGER
13 );
14 use JSON qw(decode_json);
15 use HTTP::Status qw(status_message);
16 use Plack::Util ();
17 use Plack::Util::Accessor
18 qw(send_spec send_ident recv_spec recv_ident max_workers max_reqs_per_child);
19 use URI::Escape ();
20 use constant DEBUG => $ENV{PLACK_HANDLER_MONGREL2_DEBUG} ? 1 : 0;
21  
22 # TODO
23 # - check and fix what the correct way is to handle content-length
24 # (mongrel2 chokes on simple requests if you don't responed with
25 # content-length -- as of 8/20/2010)
26  
27 sub _parse_netstring {
28 my ($len, $rest) = split /:/, $_[0], 2;
29 my $data = substr $rest, 0, $len, '';
30 $rest =~ s/^,//;
31 return ($data, $rest);
32 }
33  
34 sub _parse_headers {
35 my $headers = shift;
36  
37 my %hdrs;
38  
39 if (DEBUG()) {
40 print STDERR "[Mongrel2.pm] Decoding JSON '$headers'\n";
41 }
42 return decode_json $headers;
43 }
44  
45 sub _parse_pattern {
46 my $pattern = shift;
47 if (DEBUG()) {
48 print STDERR "[Mongrel2.pm] Decoding PATTERN '$pattern'\n";
49 }
50 my ($name) = split /\(/, $pattern, 2;
51 $name =~ s{/+$}{};
52 $name;
53 }
54  
55 sub _parse_path {
56 my ($path, $script_name) = @_;
57 if (DEBUG()) {
58 print STDERR "[Mongrel2.pm] Decoding PATH '$path ($script_name)'\n";
59 }
60 $path =~ s/^$script_name//;
61 URI::Escape::uri_unescape($path);
62 }
63  
64  
65 sub mongrel2_req_to_psgi {
66 my ($self, $data) = @_;
67 my ($rest, $headers, $body);
68  
69 if (DEBUG()) {
70 print STDERR "[Mongrel2.pm] Deparsing zmq message:\n",
71 " data: $data\n";
72 }
73  
74 my %env = (
75 'psgi.version' => [ 1, 1 ],
76 'psgi.url_scheme' => 'http', # XXX TODO
77 'psgi.errors' => *STDERR,
78 'psgi.multithread' => 0,
79 'psgi.multiprocess' => 0,
80 'psgi.run_once' => 0,
81 'psgi.streaming' => 0,
82 'psgi.nonblocking' => 0,
83 );
84  
85 ($env{MONGREL2_SENDER_ID}, $env{MONGREL2_CONN_ID}, undef, $rest) =
86 split / /, $data, 4;
87  
88 ($headers, $rest) = _parse_netstring($rest);
89  
90 my $hdrs = _parse_headers($headers);
91  
92 my $script_name = _parse_pattern(delete $hdrs->{PATTERN});
93  
94 $env{QUERY_STRING} = delete $hdrs->{QUERY} || '';
95 $env{REQUEST_METHOD} = delete $hdrs->{METHOD};
96 $env{REQUEST_URI} = delete $hdrs->{URI};
97 $env{SCRIPT_NAME} = $script_name;
98 $env{PATH_INFO} = _parse_path(delete $hdrs->{PATH}, $script_name);
99 $env{SERVER_PROTOCOL} = delete $hdrs->{VERSION};
100 ($env{SERVER_NAME}, $env{SERVER_PORT}) = split /:/, delete $hdrs->{host}, 2;
101 $env{SERVER_PORT} ||= 80;
102  
103 foreach my $key (keys %$hdrs) {
104 my $new_key = uc $key;
105 $new_key =~ s/-/_/g;
106 if ($new_key !~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/) {
107 $new_key = "HTTP_$new_key";
108 }
109  
110 # XXX See parse_headers for why this is necessary
111 my $value = $hdrs->{$key};
112 if (ref $value eq 'ARRAY') {
113 $value = join(', ', @$value);
114 }
115  
116 if (exists $env{$new_key}) {
117 $env{$new_key} .= ", $value";
118 } else {
119 $env{$new_key} = $value;
120 }
121 }
122  
123 ($body) = _parse_netstring($rest);
124  
125 if ( $env{REQUEST_METHOD} eq 'JSON' ) {
126 my $json_body = decode_json $body;
127 if ($json_body->{type} eq 'disconnect') {
128 # noop
129 } else {
130 # noop
131 }
132 if (DEBUG()) {
133 print STDERR "[Mongrel2.pm] Method is JSON, which is internal. Dropping request...\n";
134 }
135 return ();
136 }
137  
138 my $buf = Plack::TempBuffer->new( $env{CONTENT_LENGTH} );
139 $buf->print($body);
140 $env{'psgi.input'} = $buf->rewind;
141  
142 return \%env;
143 }
144  
145 sub new {
146 my ($class, %opts) = @_;
147  
148 $opts{max_workers} ||= 10;
149 $opts{max_reqs_per_child} ||= 1000;
150  
151 bless { %opts }, $class;
152 }
153  
154 sub run {
155 my ($self, $app) = @_;
156  
157 foreach my $field qw(send_spec send_ident recv_spec recv_ident) {
158 if (length $self->$field == 0) {
159 die "Argument $field is required";
160 }
161 }
162  
163 my $max_workers = $self->max_workers;
164 if ($max_workers > 1) {
165 require Parallel::Prefork;
166 my $pm = Parallel::Prefork->new({
167 max_workers => $max_workers,
168 trap_signals => {
169 HUP => 'TERM',
170 },
171 });
172  
173 local $SIG{INT} = sub {
174 $pm->signal_received('TERM');
175 $pm->signal_all_children('INT');
176 };
177 local $SIG{TERM} = $SIG{INT};
178  
179 while ($pm->signal_received ne 'TERM') {
180 $pm->start and next;
181 local $SIG{TERM} = sub {
182 warn "child $$ received signal";
183 $pm->finish;
184 };
185 my @zmq = $self->prepare_zmq();
186 $self->accept_loop($app, @zmq);
187 $self->finalize_zmq(@zmq);
188 $pm->finish;
189 }
190 $pm->wait_all_children;
191 } else {
192 if (DEBUG()) {
193 print STDERR "[Mongrel2.pm] mac_workers = 1, so not loading Parallel::Prefork\n";
194 }
195 my @zmq = $self->prepare_zmq();
196 while (1) {
197 $self->accept_loop($app, @zmq);
198 }
199 $self->finalize_zmq(@zmq);
200 }
201 }
202  
203 sub prepare_zmq {
204 my ($self) = @_;
205 my $ctxt = zmq_init();
206 my $incoming = zmq_socket( $ctxt, ZMQ_PULL );
207 my $outgoing = zmq_socket( $ctxt, ZMQ_PUB );
208  
209 zmq_connect( $incoming, $self->send_spec );
210 if (DEBUG()) {
211 print STDERR "[Mongrel2.pm] Connected incoming socket to ",
212 $self->send_spec, "\n";
213 }
214 zmq_setsockopt( $incoming, ZMQ_IDENTITY, $self->send_ident );
215 if (DEBUG()) {
216 print STDERR "[Mongrel2.pm] outgoing socket sets identity to ",
217 $self->send_ident, "\n";
218 }
219  
220 zmq_connect( $outgoing, $self->recv_spec );
221 if (DEBUG()) {
222 print STDERR "[Mongrel2.pm] Connected outgoing socket to ",
223 $self->recv_spec, "\n";
224 }
225 zmq_setsockopt( $outgoing, ZMQ_LINGER, 1 );
226 zmq_setsockopt( $outgoing, ZMQ_IDENTITY, $self->recv_ident );
227 if (DEBUG()) {
228 print STDERR "[Mongrel2.pm] outgoing socket sets identity to ",
229 $self->recv_ident, "\n";
230 }
231  
232 return ($ctxt, $incoming, $outgoing);
233 }
234  
235 sub finalize_zmq {
236 my ($self, $ctxt, $incoming, $outgoing) = @_;
237 zmq_close( $incoming );
238 zmq_close( $outgoing );
239 zmq_term( $ctxt );
240 }
241  
242 sub accept_loop {
243 my ($self, $app, $ctxt, $incoming, $outgoing) = @_;
244  
245 my $proc_req_count = 0;
246 my $max_reqs_per_child = $self->max_reqs_per_child;
247 my $loop = 1;
248  
249 local $SIG{ INT } = sub {
250 $loop = 0;
251 zmq_close($incoming);
252 zmq_close($outgoing);
253 exit 0;
254 };
255 local $SIG{TERM} = $SIG{INT};
256 while ( $loop && (!defined $max_reqs_per_child || $proc_req_count < $max_reqs_per_child ) ) {
257 if (DEBUG()) {
258 print STDERR "[Mongrel2.pm] Waiting for next request\n";
259 }
260 zmq_poll( [
261 {
262 socket => $incoming,
263 events => ZMQ_POLLIN,
264 callback => sub {
265 while ( my $msg = zmq_recv( $incoming, ZMQ_RCVMORE ) ) {
266 my $env = $self->mongrel2_req_to_psgi( zmq_msg_data( $msg ) );
267 if ( DEBUG() ) {
268 require Data::Dumper;
269 print STDERR Data::Dumper::Dumper($env);
270 }
271 next unless $env;
272  
273 eval {
274 my $res = $app->( $env );
275 $self->reply( $outgoing, $env, $res );
276 };
277 if ($@) {
278 $self->reply( $outgoing, $env, [ 500, [ "Content-Type" => "text/plain" ], [ "Internal Server Error" ] ] );
279 }
280 $proc_req_count++;
281 }
282 }
283 }
284 ], 1000000 );
285 }
286 }
287  
288 sub reply {
289 my ($self, $outgoing, $env, $res) = @_;
290  
291 my ($status, $hdrs, $body) = @$res;
292 if (ref $body eq 'ARRAY') {
293 $body = join '', @$body;
294 } elsif ( defined $body) {
295 local $/ = \65536 unless ref $/;
296 my $x = '';
297 while ( defined( my $line = $body->getline ) ) {
298 $x .= $line;
299 }
300 $body->close;
301 $body = $x;
302 } else {
303 die "unimplmented";
304 }
305  
306 if ( ! Plack::Util::status_with_no_entity_body($status) ) {
307 push @$hdrs, "Content-Length", length $body;
308 }
309  
310 my $mongrel_header = sprintf( "%s %d:%s, %s %d %s\r\n%s\r\n\r\n",
311 $env->{MONGREL2_SENDER_ID},
312 length $env->{MONGREL2_CONN_ID},
313 $env->{MONGREL2_CONN_ID},
314 $env->{SERVER_PROTOCOL},
315 $status,
316 status_message($status),
317 join("\r\n", map { sprintf( '%s: %s', $hdrs->[$_ * 2], $hdrs->[$_ * 2 + 1] ) } (0.. (@$hdrs/2 - 1) ) ),
318 );
319  
320 if (DEBUG) {
321 print STDERR "[Mongrel2.pm]: Sending\n";
322 print STDERR
323 join "\\r\\n\n", map { " $_" } (split /\r\n/, $mongrel_header);
324 print STDERR "$body\n";
325 }
326  
327 my %hdr = @$hdrs;
328 if ($hdr{'Content-Type'} =~ /^text.*utf-8/i) {
329 # Allow conversion to UTF-8
330 zmq_send( $outgoing, $mongrel_header . $body);
331 }
332 else {
333 # Ensure body is not converted to UTF-8
334 use bytes;
335 zmq_send( $outgoing, $mongrel_header . $body);
336 }
337  
338 }
339  
340 1;
341  
342 __END__