| 1 | =pod | ||
| 2 | |||
| 3 | =begin classdoc | ||
| 4 | |||
| 5 | Apartment threaded web server. Creates and maintains a pool of WebClient | ||
| 6 | apartment threaded objects. Permits application specific content and session | ||
| 7 | handler objects. | ||
| 8 | <p> | ||
| 9 | Copyright© 2006, Dean Arnold, Presicient Corp., USA<br> | ||
| 10 | All rights reserved. | ||
| 11 | <p> | ||
| 12 | Licensed under the Academic Free License version 3.0, as specified at | ||
| 13 | <a href='http://www.opensource.org/licenses/afl-3.0.php'>OpenSource.org</a>. | ||
| 14 | |||
| 15 | @author D. Arnold | ||
| 16 | @since 2006-08-21 | ||
| 17 | @self $self | ||
| 18 | @see HTTP::Daemon::Threaded::Listener | ||
| 19 | @see HTTP::Daemon::Threaded::Logable | ||
| 20 | @see HTTP::Daemon::Threaded::Logger | ||
| 21 | @see HTTP::Daemon::Threaded::WebClient | ||
| 22 | @see HTTP::Daemon::Threaded::Content | ||
| 23 | @see HTTP::Daemon::Threaded::ContentParams | ||
| 24 | @see HTTP::Daemon::Threaded::SessionCache | ||
| 25 | @see HTTP::Daemon::Threaded::Session | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | =end classdoc | ||
| 30 | |||
| 31 | =cut | ||
| 32 | package HTTP::Daemon::Threaded; | ||
| 33 | |||
| 34 | use threads; | ||
| 35 | use Thread::Apartment; | ||
| 36 | |||
| 37 | use base qw(Thread::Apartment); | ||
| 38 | |||
| 39 | use strict; | ||
| 40 | use warnings; | ||
| 41 | |||
| 42 | our $VERSION = '0.91'; | ||
| 43 | |||
| 44 | our $container; | ||
| 45 | our $timeout; | ||
| 46 | |||
| 47 | =pod | ||
| 48 | |||
| 49 | =begin classdoc | ||
| 50 | |||
| 51 | Set TQD timeout. | ||
| 52 | |||
| 53 | @static | ||
| 54 | @param $timeout TQD timeout in seconds | ||
| 55 | |||
| 56 | @return none | ||
| 57 | |||
| 58 | |||
| 59 | =end classdoc | ||
| 60 | |||
| 61 | =cut | ||
| 62 | sub setTimeout { $timeout = ($_[0] eq 'HTTP::Daemon::Threaded') ? $_[1] : $_[0]; } | ||
| 63 | |||
| 64 | =pod | ||
| 65 | |||
| 66 | =begin classdoc | ||
| 67 | |||
| 68 | Constructor. Provides facade for Thread::Apartment::new, | ||
| 69 | to create a TAS for HTTP::Daemon::Threaded::Listener. | ||
| 70 | Allocates or creates a thread, and installs a Listener in it. | ||
| 71 | <p> | ||
| 72 | Note that the following parameters are recognized by | ||
| 73 | HTTP::Daemon::Threaded, HTTP::Daemon::Threaded::Listener, and/or | ||
| 74 | HTTP::Daemon::Threaded::WebClient, but | ||
| 75 | applications may supply additional parameter key/value pairs | ||
| 76 | which will be provided to the constructor for any specified | ||
| 77 | HTTP::Daemon::Threaded::ContentParams class. | ||
| 78 | |||
| 79 | @param AptTimeout <i>(optional)</i> Thread::Apartment proxy return timeout | ||
| 80 | @param Port <i>(optional)</i> TCP listen port; default 80. | ||
| 81 | @param MaxClients <i>(optional)</i> max number of client handlers to spawn; | ||
| 82 | default 5 | ||
| 83 | @param LogLevel <i>(optional)</i> logging level; 1 => errors only; 2 => errors and warnings only; 3 => errors, warnings, | ||
| 84 | and info messages; default 1 | ||
| 85 | @param EventLogger <i>(optional)</i> Instance of a HTTP::Daemon::Threaded::Logger to receive | ||
| 86 | event notifications (except for web requests) | ||
| 87 | @param WebLogger <i>(optional)</i> Instance of a HTTP::Daemon::Threaded::Logger to receive | ||
| 88 | web request notifications | ||
| 89 | @param Handlers <i>(required)</i> URI handler map; arrayref mapping URI regex's to handler package names | ||
| 90 | @param UserAuth <i>(optional)</i> instance of a subclass of HTTP::Daemon::Threaded::Auth <i>(not yet supported)</i> | ||
| 91 | @param SessionCache <i>(optional)</i> instance of a subclass of HTTP::Daemon::Threaded::SessionCache | ||
| 92 | to be used to create/manage sessions | ||
| 93 | @param InactivityTimer <i>(optional)</i> number of seconds a WebClient waits before disconnecting | ||
| 94 | an idle connection; default 10 minutes | ||
| 95 | @param ContentParams <i>(optional)</i> name of concrete implementation of HTTP::Daemon::Threaded::ContentParams | ||
| 96 | @param DocRoot <i>(optional)</i> root directory for default file based content handler | ||
| 97 | @param ProductTokens <i>(optional)</i> product token string to return to client; default | ||
| 98 | is 'HTTP::Daemon::Threaded/<version>' | ||
| 99 | @param MediaTypes <i>(optional)</i> hashref mapping 'Content-Type' specifications to | ||
| 100 | file qualifier strings. Values may be either a single string literal, or | ||
| 101 | an arrayref of string literals, e.g.,<br> | ||
| 102 | <code>MediaTypes => { 'text/css' => 'css' }</code>. Used to | ||
| 103 | add media types for LWP::MediaTypes::guess_media_type() | ||
| 104 | |||
| 105 | @return TAC proxy for HTTP::Daemon::Threaded::Listener object | ||
| 106 | @see <a href='http://search.cpan.org/search?mode=module&query=LWP::MediaTypes'>LWP::MediaTypes</a> | ||
| 107 | |||
| 108 | |||
| 109 | =end classdoc | ||
| 110 | |||
| 111 | =cut | ||
| 112 | sub new { | ||
| 113 | # | ||
| 114 | # overrides T::A new, returning the container object | ||
| 115 | # | ||
| 116 | my ($class, %args) = @_; | ||
| 117 | |||
| 118 | $args{AptClass} = 'HTTP::Daemon::Threaded::Listener'; | ||
| 119 | $args{AptTimeout} = $timeout | ||
| 120 | unless exists $args{AptTimeout}; | ||
| 121 | |||
| 122 | $container = Thread::Apartment->new( %args ) | ||
| 123 | || die "Could not install into an apartment: $@."; | ||
| 124 | return $container; | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; | ||
| 128 |