summaryrefslogtreecommitdiff
path: root/rt/bin/mason_handler.svc
blob: e6d83784ca2a4870a5869acb62a5664acf6859b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/perl
# BEGIN LICENSE BLOCK
# 
# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
# 
# (Except where explictly superceded by other copyright notices)
# 
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
# 
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# Unless otherwise specified, all modifications, corrections or
# extensions to this work which alter its source code become the
# property of Best Practical Solutions, LLC when submitted for
# inclusion in the work.
# 
# 
# END LICENSE BLOCK

=head1 NAME

mason_handler.svc - Win32 IIS Service handler for RT

=head1 SYNOPSIS

    perl mason_handler.svc --install	# install as service
    perl mason_handler.svc --deinstall	# deinstall this service
    perl mason_handler.svc --help	# show this help
    perl mason_handler.svc 		# launch handler from command line

=head1 DESCRIPTION

This script manages a stand-alone FastCGI server, and populates the necessary
registry settings to run it with Microsoft IIS Server 4.0 or above.

Before running it, you need to install the B<FCGI> module from CPAN, as well as
B<Win32::Daemon> from L<http://www.roth.net/perl/Daemon/> if you want to install
itself as a service.

This script will automatically create a virtual directory under the IIS root;
its name is taken from C<$WebPath> in the F<RT_Config.pm> file.  Additionally,
please install the ISAPI binary from L<http://www.caraveo.com/fastcgi/> and set
up an ISAPI Script Map that maps F<.html> files to F<isapi_fcgi.dll>.

Once the service is launched (either via C<net start RTFastCGI> or by running
C<perl mason_handler.svc>), a FCGI server will start and bind to port C<8284>
(mnemonics: the ASCII value of C<R> and C<T>); the ISAPI handler's C<BindPath>
registry setting will also be automatically populated.

=cut

use strict;
use File::Basename;
require (dirname(__FILE__) . '/webmux.pl');

use Cwd;
use File::Spec;

use Win32;
use Win32::Process;
use Win32::Service;
use Win32::TieRegistry;

my $ProcessObj;

BEGIN {
    my $runsvc = sub {
	Win32::Process::Create(
	    $ProcessObj, $^X, "$^X $0 --run", 0, NORMAL_PRIORITY_CLASS, "."
	) or do {
	    die Win32::FormatMessage( Win32::GetLastError() );
	};

	chdir File::Basename::dirname($0);
	my $path = Cwd::cwd();
	$path =~ s|/|\\|g;
	$path =~ s|bin$|share\\html|;

	$Win32::TieRegistry::Registry->{
	    'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\\'.
	    'W3SVC\Parameters\Virtual Roots\\'
	}->{$RT::WebPath || '/'} = "$path,,205";
	    
	$Win32::TieRegistry::Registry->{
	    'HKEY_LOCAL_MACHINE\Software\FASTCGI\.html\\'
	}->{'BindPath'} = $ENV{'FCGI_SOCKET_PATH'};

	Win32::Service::StartService(Win32::NodeName, 'W3SVC');
    };
    
    if ($ARGV[0] eq '--deinstall') {
	chdir File::Basename::dirname($0);
	my $path = Cwd::cwd();
	$path =~ s|/|\\|g;

	require Win32::Daemon;
	Win32::Daemon::DeleteService('RTFastCGI');
	warn "Service 'RTFastCGI' successfully deleted.\n";
	exit;
    }
    elsif ($ARGV[0] eq '--install') {
	chdir File::Basename::dirname($0);
	my $path = Cwd::cwd();
	$path =~ s|/|\\|g;

	require Win32::Daemon;
	Win32::Daemon::DeleteService('RTFastCGI');
	
	my $rv = Win32::Daemon::CreateService( {
	    machine =>  '',
	    name    =>  'RTFastCGI',
	    display =>  'RT FastCGI Handler',
	    path    =>  $^X,
	    user    =>  '',
	    pwd     =>  $path,
	    description => 'Enables port 8284 as the RT FastCGI handler.',
	    parameters  => File::Spec->catfile(
		    $path, File::Basename::basename($0)
	    ) . ' --service',
	} );
    
	if ($rv) {
	    warn "Service 'RTFastCGI' successfully created.\n";
	}
	else {
	    warn "Failed to add service: " . Win32::FormatMessage(
		Win32::Daemon::GetLastError()
	    ) . "\n";
	}
	exit;
    }
    elsif ($ARGV[0] eq '--service') {
	require Win32::Daemon;

	my $PrevState = Win32::Daemon::SERVICE_START_PENDING();
	Win32::Daemon::StartService() or die $^E;

	while ( 1 ) {
	    my $State = Win32::Daemon::State();
	    last if $State == Win32::Daemon::SERVICE_STOPPED();
	    
	    if ( $State == Win32::Daemon::SERVICE_START_PENDING() ) {
		$runsvc->();
		Win32::Daemon::State( Win32::Daemon::SERVICE_RUNNING() );
		$PrevState = Win32::Daemon::SERVICE_RUNNING();
	    }
	    elsif ( $State == Win32::Daemon::SERVICE_CONTINUE_PENDING() ) {
		$ProcessObj->Resume;
		Win32::Daemon::State( Win32::Daemon::SERVICE_RUNNING() );
		$PrevState = Win32::Daemon::SERVICE_RUNNING();
	    }
	    elsif ( $State == Win32::Daemon::SERVICE_STOP_PENDING() ) {
	    $ProcessObj->Kill(0);
		Win32::Daemon::State( Win32::Daemon::SERVICE_STOPPED() );
		$PrevState = Win32::Daemon::SERVICE_STOPPED();
	    }
	    elsif ( $State == Win32::Daemon::SERVICE_RUNNING() ) {
		my $Message = Win32::Daemon::QueryLastMessage(1);
		if ( $Message == Win32::Daemon::SERVICE_CONTROL_INTERROGATE() ) {
		    Win32::Daemon::State( $PrevState );
		}
		elsif ( $Message == Win32::Daemon::SERVICE_CONTROL_SHUTDOWN() ) {
		    Win32::Daemon::State( Win32::Daemon::SERVICE_STOP_PENDING(), 15000 );
		}
		elsif ( $Message != Win32::Daemon::SERVICE_CONTROL_NONE() ) {
		    Win32::Daemon::State( $PrevState );
		}
	    }
	    
	    Win32::Sleep( 1000 );
	}
		
	Win32::Daemon::StopService();
	exit;
    }
    elsif ($ARGV[0] eq '--help') {
	system("perldoc $0");
	exit;
    }
    elsif ($ARGV[0] ne '--run') {
	$SIG{__DIE__} = sub { $ProcessObj->Kill(0) if $ProcessObj };
	$runsvc->();
	warn "RT FastCGI Handler launched. Press [Enter] to terminate...\n";
	<STDIN>;
	exit;
    }
}

###############################################################################

warn "Begin listening on $ENV{'FCGI_SOCKET_PATH'}\n";

require CGI::Fast;
my $h = &RT::Interface::Web::NewCGIHandler();

RT::Init();

# Response loop
while( my $cgi = CGI::Fast->new ) {
    my $comp = $ENV{'PATH_INFO'};

    $comp = $1 if ($comp =~ /^(.*)$/);
    $comp =~ s|^$RT::WebPath\b||i;
    $comp .= "index.html" if ($comp =~ /\/$/);
    $comp =~ s/.pl$/.html/g;
    
    warn "Serving $comp\n";

    $h->handle_cgi($comp);
    # _should_ always be tied
}

1;

=head1 AUTHORS

Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>

=head1 COPYRIGHT

Copyright 2002 by Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>.

This program is free software; you can redistribute it and/or 
modify it under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>

=cut