2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC
7 # <jesse@bestpractical.com>
9 # (Except where explicitly superseded by other copyright notices)
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # CONTRIBUTION SUBMISSION POLICY:
31 # (The following paragraph is not intended to limit the rights granted
32 # to you to modify and distribute this software under the terms of
33 # the GNU General Public License and is only of importance to you if
34 # you choose to contribute your changes and enhancements to the
35 # community by submitting them to Best Practical Solutions, LLC.)
37 # By intentionally submitting any modifications, corrections or
38 # derivatives to this work, or any other work intended for use with
39 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
40 # you are the copyright holder for those contributions and you grant
41 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
42 # royalty-free, perpetual, license to use, copy, create derivative
43 # works based on those contributions, and sublicense and distribute
44 # those contributions and any derivatives thereof.
46 # END BPS TAGGED BLOCK }}}
50 mason_handler.svc - Win32 IIS Service handler for RT
54 perl mason_handler.svc --install # install as service
55 perl mason_handler.svc --deinstall # deinstall this service
56 perl mason_handler.svc --help # show this help
57 perl mason_handler.svc # launch handler from command line
61 This script manages a stand-alone FastCGI server, and populates the necessary
62 registry settings to run it with Microsoft IIS Server 4.0 or above.
64 Before running it, you need to install the B<FCGI> module from CPAN, as well as
65 B<Win32::Daemon> from L<http://www.roth.net/perl/Daemon/> if you want to install
68 This script will automatically create a virtual directory under the IIS root;
69 its name is taken from C<$WebPath> in the F<RT_Config.pm> file. Additionally,
70 please install the ISAPI binary from L<http://www.caraveo.com/fastcgi/> and set
71 up an ISAPI Script Map that maps F<.html> files to F<isapi_fcgi.dll>.
73 Once the service is launched (either via C<net start RTFastCGI> or by running
74 C<perl mason_handler.svc>), a FCGI server will start and bind to port C<8284>
75 (mnemonics: the ASCII value of C<R> and C<T>); the ISAPI handler's C<BindPath>
76 registry setting will also be automatically populated.
85 require (dirname(__FILE__) . '/webmux.pl');
93 use Win32::TieRegistry;
99 Win32::Process::Create(
100 $ProcessObj, $^X, "$^X $0 --run", 0, NORMAL_PRIORITY_CLASS, "."
102 die Win32::FormatMessage( Win32::GetLastError() );
105 chdir File::Basename::dirname($0);
106 my $path = Cwd::cwd();
108 $path =~ s|bin$|share\\html|;
110 $Win32::TieRegistry::Registry->{
111 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\\'.
112 'W3SVC\Parameters\Virtual Roots\\'
113 }->{$RT::WebPath || '/'} = "$path,,205";
115 $Win32::TieRegistry::Registry->{
116 'HKEY_LOCAL_MACHINE\Software\FASTCGI\.html\\'
117 }->{'BindPath'} = $ENV{'FCGI_SOCKET_PATH'};
119 Win32::Service::StartService(Win32::NodeName, 'W3SVC');
122 if ($ARGV[0] eq '--deinstall') {
123 chdir File::Basename::dirname($0);
124 my $path = Cwd::cwd();
127 require Win32::Daemon;
128 Win32::Daemon::DeleteService('RTFastCGI');
129 warn "Service 'RTFastCGI' successfully deleted.\n";
132 elsif ($ARGV[0] eq '--install') {
133 chdir File::Basename::dirname($0);
134 my $path = Cwd::cwd();
137 require Win32::Daemon;
138 Win32::Daemon::DeleteService('RTFastCGI');
140 my $rv = Win32::Daemon::CreateService( {
143 display => 'RT FastCGI Handler',
147 description => 'Enables port 8284 as the RT FastCGI handler.',
148 parameters => File::Spec->catfile(
149 $path, File::Basename::basename($0)
154 warn "Service 'RTFastCGI' successfully created.\n";
157 warn "Failed to add service: " . Win32::FormatMessage(
158 Win32::Daemon::GetLastError()
163 elsif ($ARGV[0] eq '--service') {
164 require Win32::Daemon;
166 my $PrevState = Win32::Daemon::SERVICE_START_PENDING();
167 Win32::Daemon::StartService() or die $^E;
170 my $State = Win32::Daemon::State();
171 last if $State == Win32::Daemon::SERVICE_STOPPED();
173 if ( $State == Win32::Daemon::SERVICE_START_PENDING() ) {
175 Win32::Daemon::State( Win32::Daemon::SERVICE_RUNNING() );
176 $PrevState = Win32::Daemon::SERVICE_RUNNING();
178 elsif ( $State == Win32::Daemon::SERVICE_CONTINUE_PENDING() ) {
180 Win32::Daemon::State( Win32::Daemon::SERVICE_RUNNING() );
181 $PrevState = Win32::Daemon::SERVICE_RUNNING();
183 elsif ( $State == Win32::Daemon::SERVICE_STOP_PENDING() ) {
184 $ProcessObj->Kill(0);
185 Win32::Daemon::State( Win32::Daemon::SERVICE_STOPPED() );
186 $PrevState = Win32::Daemon::SERVICE_STOPPED();
188 elsif ( $State == Win32::Daemon::SERVICE_RUNNING() ) {
189 my $Message = Win32::Daemon::QueryLastMessage(1);
190 if ( $Message == Win32::Daemon::SERVICE_CONTROL_INTERROGATE() ) {
191 Win32::Daemon::State( $PrevState );
193 elsif ( $Message == Win32::Daemon::SERVICE_CONTROL_SHUTDOWN() ) {
194 Win32::Daemon::State( Win32::Daemon::SERVICE_STOP_PENDING(), 15000 );
196 elsif ( $Message != Win32::Daemon::SERVICE_CONTROL_NONE() ) {
197 Win32::Daemon::State( $PrevState );
201 Win32::Sleep( 1000 );
204 Win32::Daemon::StopService();
207 elsif ($ARGV[0] eq '--help') {
208 system("perldoc $0");
211 elsif ($ARGV[0] ne '--run') {
212 $SIG{__DIE__} = sub { $ProcessObj->Kill(0) if $ProcessObj };
214 warn "RT FastCGI Handler launched. Press [Enter] to terminate...\n";
220 ###############################################################################
222 warn "Begin listening on $ENV{'FCGI_SOCKET_PATH'}\n";
229 while( my $cgi = CGI::Fast->new ) {
230 my $comp = $ENV{'PATH_INFO'};
232 $comp = $1 if ($comp =~ /^(.*)$/);
233 $comp =~ s|^$RT::WebPath\b||i;
234 $comp .= "index.html" if ($comp =~ /\/$/);
235 $comp =~ s/.pl$/.html/g;
237 warn "Serving $comp\n";
239 $Handler->handle_cgi($comp);
240 RT::Interface::Web::Handler->CleanupRequest();
241 # _should_ always be tied
248 Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>
252 Copyright 2002 by Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>.
254 This program is free software; you can redistribute it and/or
255 modify it under the same terms as Perl itself.
257 See L<http://www.perl.com/perl/misc/Artistic.html>