summaryrefslogtreecommitdiff
path: root/rt/bin/mason_handler.svc
blob: 4276b6ea186803c3f9b137ff818d170905d259c3 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/perl
# BEGIN BPS TAGGED BLOCK {{{
# 
# COPYRIGHT:
# 
# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
#                                          <jesse@bestpractical.com>
# 
# (Except where explicitly superseded by other copyright notices)
# 
# 
# LICENSE:
# 
# 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.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.gnu.org/copyleft/gpl.html.
# 
# 
# CONTRIBUTION SUBMISSION POLICY:
# 
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
# 
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
# 
# END BPS TAGGED 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

package RT::Mason;

use strict;
use File::Basename;
use vars '$Handler';
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->Config->Get('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;

RT::Init();
$Handler ||= RT::Interface::Web::Handler->new(
    RT->Config->Get('MasonParameters')
);


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

    $comp = $1 if ($comp =~ /^(.*)$/);
    my $web_path = RT->Config->Get('WebPath');
    $comp =~ s|^\Q$web_path\E\b||i;
    $comp .= "index.html" if ($comp =~ /\/$/);
    $comp =~ s/.pl$/.html/g;
    
    warn "Serving $comp\n";

    $Handler->handle_cgi($comp);
    RT::Interface::Web::Handler->CleanupRequest();
    # _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