Threaded/Socket.pm untested

filter: Statementstmt 0.0% 0 / 170 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 278 CC 16 SCAR 25.7
CC
16
Cov
0%
send_file_header
42.0
send_file_response
42.0
handleSocketEvent
6.0
CRAP 13.1
1 =pod
2  
3 =begin classdoc
4  
5  
6 Enhances HTTP::Daemon::ClientConn with the ability to directly invoke
7 a object-specific event handler for the I/O handle whenever a
8 HTTP::Daemon::Threaded::IOSelector object detects an event on the handle. Also
9 provides interfaces to manage the handle's assignment to the
10 read, write, and exception selectors within the HTTP::Daemon::Threaded::IOSelector
11 object, as well as managing the removal from the selectors when
12 the I/O handle is closed.
13 <p>
14 Copyright&copy 2006-2008, Dean Arnold, Presicient Corp., USA<br>
15 All rights reserved.
16 <p>
17 Licensed under the Academic Free License version 3.0, as specified at
18 <a href='http://www.opensource.org/licenses/afl-3.0.php'>OpenSource.org</a>.
19  
20 @author D. Arnold
21 @since 2005-12-01
22 @self $self
23  
24  
25  
26 =end classdoc
27  
28 =cut
29 package HTTP::Daemon::Threaded::Socket;
30  
31 use HTTP::Status;
32 use HTTP::Daemon;
33 use HTTP::Date qw(time2str);
34 use LWP::MediaTypes qw(guess_media_type);
35 use base qw(HTTP::Daemon::ClientConn);
36  
37 use strict;
38 use warnings;
39  
40 our $VERSION = '0.91';
41  
42 our $CRLF = "\015\012"; # "\r\n" is not portable
43  
44 =pod
45  
46 =begin classdoc
47  
48 Handle an event on the socket. Invokes the handleSocketEvent()
49 method on any registered context object.
50  
51 @param $eventmask bitmask indicating read, write, or exception event
52  
53 @return the context object
54  
55  
56 =end classdoc
57  
58 =cut
59 sub handleSocketEvent {
60 my $self = shift;
61 warn "Unimplemented handleSocketEvent() method!\n",
62 return undef
63 unless exists ${*$self}{_httpd_context};
64 return ${*$self}{_httpd_context}->handleSocketEvent($self, @_);
65 }
66  
67 =pod
68  
69 =begin classdoc
70  
71 Close the socket. Removes the socket from the registered
72 HTTP::Daemon::Threaded::IOSelector, closes the handle, and deletes any registered
73 context and selector objects.
74  
75 @return 1
76  
77  
78 =end classdoc
79  
80 =cut
81 sub close {
82 my $self = shift;
83 #
84 # now support IO::Select removal
85 #
86 ${*$self}{_select_context}->removeAll($self)
87 if exists ${*$self}{_select_context};
88 $self->SUPER::close();
89 delete ${*$self}{_httpd_context};
90 delete ${*$self}{_select_context};
91 return 1;
92 }
93 #
94 # our method additions to manage
95 # context objects
96 #
97 =pod
98  
99 =begin classdoc
100  
101 Set the context object. The registered object should implement
102 a handleSocketEvent() method. Also gets a printable peer IP address
103  
104 @param $context the registered object
105 @param $getpeer boolean; true means get printable peer address
106  
107 @return HTTP::Daemon::Threaded::Socket object
108  
109  
110 =end classdoc
111  
112 =cut
113 sub setContext {
114 my ($self, $context, $getpeer) = @_;
115 ${*$self}{_httpd_context} = $context;
116 if ($getpeer) {
117 my ($port, $addr) = sockaddr_in(getpeername(*$self));
118 ${*$self}{_peer_addr} = inet_ntoa($addr);
119 }
120 return $self;
121 }
122  
123 =pod
124  
125 =begin classdoc
126  
127 Returns the current context object.
128  
129 @return an object
130  
131  
132 =end classdoc
133  
134 =cut
135 sub getContext {
136 my $self = shift;
137 return ${*$self}{_httpd_context};
138 }
139  
140 =pod
141  
142 =begin classdoc
143  
144 Remove the context object.
145  
146 @return HTTP::Daemon::Threaded::Socket object
147  
148  
149 =end classdoc
150  
151 =cut
152 sub removeContext {
153 my $self = shift;
154 delete ${*$self}{_httpd_context};
155 delete ${*$self}{_peer_addr};
156 return $self;
157 }
158 #
159 # because of a catch-22 situation w/ IO::Select(),
160 # we have to register the selectors here, so we
161 # can remove ourselves on close()
162 # NOTE: we assume the selector is a HTTP::Daemon::Threaded::IOSelector
163 #
164 =pod
165  
166 =begin classdoc
167  
168 Set the HTTP::Daemon::Threaded::IOSelector object.
169  
170 @param $selector the HTTP::Daemon::Threaded::IOSelector object
171  
172 @return HTTP::Daemon::Threaded::Socket object
173  
174  
175 =end classdoc
176  
177 =cut
178 sub setSelector {
179 my $self = shift;
180 ${*$self}{_select_context} = shift;
181 return $self;
182 }
183  
184 =pod
185  
186 =begin classdoc
187  
188 Return the current selector.
189  
190 @return HTTP::Daemon::Threaded::IOSelector object
191  
192  
193 =end classdoc
194  
195 =cut
196 sub getSelector {
197 my $self = shift;
198 return ${*$self}{_select_context};
199 }
200  
201 =pod
202  
203 =begin classdoc
204  
205 Remove selector object.
206  
207 @return HTTP::Daemon::Threaded::Socket object
208  
209  
210 =end classdoc
211  
212 =cut
213 sub removeSelector {
214 my $self = shift;
215 delete ${*$self}{_select_context};
216 return $self;
217 }
218 ########################################
219 #
220 # provide Selector i/fs thru ourselves
221 # to simplify the code
222 #
223 ########################################
224 =pod
225  
226 =begin classdoc
227  
228 Add ourself to the selector's read selector
229  
230 @return HTTP::Daemon::Threaded::IOSelector object
231  
232  
233 =end classdoc
234  
235 =cut
236 sub addRead {
237 my $self = shift;
238 return ${*$self}{_select_context}->addRead($self);
239 }
240  
241 =pod
242  
243 =begin classdoc
244  
245 Add ourself to the selector's write selector
246  
247 @return HTTP::Daemon::Threaded::IOSelector object
248  
249  
250 =end classdoc
251  
252 =cut
253 sub addWrite {
254 my $self = shift;
255 return ${*$self}{_select_context}->addWrite($self);
256 }
257 =pod
258  
259 =begin classdoc
260  
261 Add ourself to the selector's exception selector
262  
263 @return HTTP::Daemon::Threaded::IOSelector object
264  
265  
266 =end classdoc
267  
268 =cut
269 sub addExcept {
270 my $self = shift;
271 return ${*$self}{_select_context}->addExcept($self);
272 }
273 =pod
274  
275 =begin classdoc
276  
277 Add ourself to the selector's read and exception selectors
278  
279 @return HTTP::Daemon::Threaded::IOSelector object
280  
281  
282 =end classdoc
283  
284 =cut
285 sub addNoWrite {
286 my $self = shift;
287 return ${*$self}{_select_context}->addNoWrite($self);
288 }
289 =pod
290  
291 =begin classdoc
292  
293 Add ourself to all the selector's selectors
294  
295 @return HTTP::Daemon::Threaded::IOSelector object
296  
297  
298 =end classdoc
299  
300 =cut
301 sub addAll {
302 my $self = shift;
303 return ${*$self}{_select_context}->addAll($self);
304 }
305  
306 =pod
307  
308 =begin classdoc
309  
310 Remove ourself from the selector's read selector
311  
312 @return HTTP::Daemon::Threaded::IOSelector object
313  
314  
315 =end classdoc
316  
317 =cut
318 sub removeRead {
319 my $self = shift;
320 return ${*$self}{_select_context}->removeRead($self);
321 }
322  
323 =pod
324  
325 =begin classdoc
326  
327 Remove ourself from the selector's write selector
328  
329 @return HTTP::Daemon::Threaded::IOSelector object
330  
331  
332 =end classdoc
333  
334 =cut
335 sub removeWrite {
336 my $self = shift;
337 return ${*$self}{_select_context}->removeWrite($self);
338 }
339 =pod
340  
341 =begin classdoc
342  
343 Remove ourself from the selector's exception selector
344  
345 @return HTTP::Daemon::Threaded::IOSelector object
346  
347  
348 =end classdoc
349  
350 =cut
351 sub removeExcept {
352 my $self = shift;
353 return ${*$self}{_select_context}->removeExcept($self);
354 }
355 =pod
356  
357 =begin classdoc
358  
359 Remove ourself from the selector's read and exception selectors
360  
361 @return HTTP::Daemon::Threaded::IOSelector object
362  
363  
364 =end classdoc
365  
366 =cut
367 sub removeNoWrite {
368 my $self = shift;
369 return ${*$self}{_select_context}->removeNoWrite($self);
370 }
371 =pod
372  
373 =begin classdoc
374  
375 Remove ourself from all the selector's selectors
376  
377 @return HTTP::Daemon::Threaded::IOSelector object
378  
379  
380 =end classdoc
381  
382 =cut
383 sub removeAll {
384 my $self = shift;
385 return ${*$self}{_select_context}->removeAll($self);
386 }
387 =pod
388  
389 =begin classdoc
390  
391 Return response to a HEAD request for a file.
392 Borrowed from HTTP::Daemon::ClientConn::send_file_response(),
393 but with content omitted. Automatically write weblog entry
394 if a WebLogger is configured.
395  
396 @params $file path of file to be returned
397  
398 @return HTTP status of the response
399  
400  
401 =end classdoc
402  
403 =cut
404 sub send_file_header
405 {
406 my ($self, $file) = @_;
407  
408 return $self->send_error(RC_NOT_IMPLEMENTED)
409 if (-d $file);
410  
411 return $self->send_error(RC_NOT_FOUND)
412 unless (-f _);
413  
414 my $f;
415 sysopen($f, $file, 0) or
416 return $self->send_error(RC_FORBIDDEN);
417  
418 binmode($f);
419 my ($ct, $ce) = guess_media_type($file);
420 my ($size, $mtime) = (stat _)[7,9];
421 ${*$self}{_httpd_context}->logRequest(${*$self}{_peer_addr}, RC_OK, $size);
422  
423 $self->send_basic_header;
424 print $self "Content-Type: $ct$CRLF";
425 print $self "Content-Encoding: $ce$CRLF"
426 if $ce;
427 print $self "Content-Length: $size$CRLF"
428 if $size;
429 print $self "Last-Modified: ", time2str($mtime), "$CRLF"
430 if $mtime;
431 print $self $CRLF;
432 $self->flush();
433 return RC_OK;
434 }
435  
436 =pod
437  
438 =begin classdoc
439  
440 Return response to a GET or POST request for a file.
441 Overrides HTTP::Daemon::ClientConn::send_file_response().
442 Automatically write weblog entry if a WebLogger is configured.
443  
444 @params $file path of file to be returned
445  
446 @return HTTP status of the response
447  
448  
449 =end classdoc
450  
451 =cut
452 sub send_file_response
453 {
454 my ($self, $file) = @_;
455  
456 return $self->send_error(RC_NOT_IMPLEMENTED)
457 if (-d $file);
458  
459 return $self->send_error(RC_NOT_FOUND)
460 unless (-f _);
461  
462 my $f;
463 sysopen($f, $file, 0) or
464 return $self->send_error(RC_FORBIDDEN);
465  
466 binmode($f);
467 my ($ct, $ce) = guess_media_type($file);
468 my ($size, $mtime) = (stat _)[7,9];
469 ${*$self}{_httpd_context}->logRequest(${*$self}{_peer_addr}, RC_OK, $size);
470  
471 $self->send_basic_header;
472 print $self "Content-Type: $ct$CRLF";
473 print $self "Content-Encoding: $ce$CRLF"
474 if $ce;
475 print $self "Content-Length: $size$CRLF"
476 if $size;
477 print $self "Last-Modified: ", time2str($mtime), "$CRLF"
478 if $mtime;
479 print $self $CRLF;
480 $self->send_file($f);
481 $self->flush();
482 return RC_OK;
483 }
484 =pod
485  
486 =begin classdoc
487  
488 Return parent WebClient object.
489 Overrides HTTP::Daemon::ClientConn::daemon to return
490  
491 @return parent HTTP::Daemon::Threaded::WebClient object
492  
493  
494 =end classdoc
495  
496 =cut
497 sub daemon {
498 my $self = shift;
499 return ${*$self}{_httpd_context};
500 }
501 =pod
502  
503 =begin classdoc
504  
505 Return response to a GET or POST request.
506 Overrides HTTP::Daemon::ClientConn::send_response().
507 Automatically writes weblog entry if a WebLogger is configured.
508  
509 @params $res HTTP::Response object to generate response; alternately,
510 may be simple raw content, from which an HTTP::Response object
511 will be generated.
512  
513 @return HTTP status of the response
514  
515  
516 =end classdoc
517  
518 =cut
519 sub send_response
520 {
521 my $self = shift;
522 my $res = shift;
523  
524 $res ||= RC_OK,
525 $res = HTTP::Response->new($res, @_)
526 unless (ref $res);
527  
528 ${*$self}{_httpd_context}->logRequest(
529 ${*$self}{_peer_addr}, $res->code(), $res->content_length());
530 $self->SUPER::send_response($res, @_);
531 $self->flush();
532 return $res->code();
533 }
534  
535 =pod
536  
537 =begin classdoc
538  
539 Return error response to a request.
540 Overrides HTTP::Daemon::ClientConn::send_error().
541 Automatically writes weblog entry if a WebLogger is configured.
542  
543 @param $status <i>(optional)</i> HTTP status code of the error; default 400.
544 @param $errormsg <i>(optional)</i> Error message text to be included body of response.
545  
546 @return HTTP status of the response
547  
548  
549 =end classdoc
550  
551 =cut
552 sub send_error
553 {
554 my $self = shift;
555 my $status = shift;
556 $status ||= RC_BAD_REQUEST;
557 ${*$self}{_httpd_context}->logRequest(${*$self}{_peer_addr}, $status);
558 $self->SUPER::send_error($status, @_);
559 $self->flush();
560 return $status
561 }
562 =pod
563  
564 =begin classdoc
565  
566 Return redirect response to a request.
567 Overrides HTTP::Daemon::ClientConn::send_redirect().
568 Automatically writes weblog entry if a WebLogger is configured.
569  
570 @param $loc URL of new target location
571 @param $status <i>(optional)</i> redirect status code (default 301).
572 @param $content <i>(optional)</i> Any alternate content to be returned to client.
573  
574 @return HTTP status of the response
575  
576  
577 =end classdoc
578  
579 =cut
580 sub send_redirect
581 {
582 my $self = shift;
583 my ($loc, $status, $content) = @_;
584 $status ||= RC_MOVED_PERMANENTLY;
585 ${*$self}{_httpd_context}->logRequest(${*$self}{_peer_addr}, $status);
586 return $self->SUPER::send_redirect($loc, $status, $content);
587 }
588  
589 =pod
590  
591 =begin classdoc
592  
593 Get client request.
594 Overrides HTTP::Daemon::ClientConn::get_request().
595 Automatically extract weblog entry fragments if a WebLogger is configured.
596  
597 @param $only_headers boolean; true => only retrieve HTTP headers
598  
599 @return HTTP::Request object
600  
601  
602 =end classdoc
603  
604 =cut
605 sub get_request
606 {
607 my $self = shift;
608 my $req = $self->SUPER::get_request(@_);
609 ${*$self}{_httpd_context}->scanForLogging($req)
610 if $req;
611 return $req;
612 }
613  
614 1;