should fix intermittant slowness, RT#18719
[freeside.git] / htetc / handler.pl
1 #!/usr/bin/perl
2
3 package HTML::Mason;
4
5 use strict;
6 use warnings;
7 use FS::Mason qw( mason_interps );
8 use FS::Trace;
9
10 if ( %%%RT_ENABLED%%% ) {
11   require RT;
12   $> = scalar(getpwnam('freeside'));
13   RT::LoadConfig();
14   RT::Init();
15   $> = $<;
16 }
17
18 #use vars qw($r);
19
20 # Bring in ApacheHandler, necessary for mod_perl integration.
21 # Uncomment the second line (and comment the first) to use
22 # Apache::Request instead of CGI.pm to parse arguments.
23 use HTML::Mason::ApacheHandler;
24 # use HTML::Mason::ApacheHandler (args_method=>'mod_perl');
25
26 ###use Module::Refresh;###
27
28 # Create Mason objects
29
30 my( $fs_interp, $rt_interp ) = mason_interps('apache');
31
32 my $ah = new HTML::Mason::ApacheHandler (
33   interp        => $fs_interp,
34   request_class => 'FS::Mason::Request',
35   args_method   => 'CGI', #(and FS too)
36 );
37
38 # Activate the following if running httpd as root (the normal case).
39 # Resets ownership of all files created by Mason at startup.
40 #
41 #chown (Apache->server->uid, Apache->server->gid, $interp->files_written);
42
43 my $protect_fds;
44
45 sub handler
46 {
47     #($r) = @_;
48     my $r = shift;
49
50     FS::Trace->log('protecting fds');
51
52     #from rt/bin/webmux.pl(.in)
53     if ( !$protect_fds && $ENV{'MOD_PERL'} && exists $ENV{'MOD_PERL_API_VERSION'}
54         && $ENV{'MOD_PERL_API_VERSION'} >= 2
55     ) {
56         # under mod_perl2, STDIN and STDOUT get closed and re-opened,
57         # however they are not on FD 0 and 1.  In this case, the next
58         # socket that gets opened will occupy one of these FDs, and make
59         # all system() and open "|-" calls dangerous; for example, the
60         # DBI handle can get this FD, which later system() calls will
61         # close by putting garbage into the socket.
62         $protect_fds = [];
63         push @{$protect_fds}, IO::Handle->new_from_fd(0, "r")
64             if fileno(STDIN) != 0;
65         push @{$protect_fds}, IO::Handle->new_from_fd(1, "w")
66             if fileno(STDOUT) != 1;
67     }
68
69     # If you plan to intermix images in the same directory as
70     # components, activate the following to prevent Mason from
71     # evaluating image files as components.
72     #
73     #return -1 if $r->content_type && $r->content_type !~ m|^text/|i;
74
75     ###Module::Refresh->refresh;###
76
77     FS::Trace->log('setting content_type / headers');
78
79     $r->content_type('text/html; charset=utf-8');
80     #$r->content_type('text/html; charset=iso-8859-1');
81     #eorar
82
83     my $headers = $r->headers_out;
84     $headers->{'Cache-control'} = 'no-cache';
85     #$r->no_cache(1);
86     $headers->{'Expires'} = '0';
87
88 #    $r->send_http_header;
89
90     if ( $r->filename =~ /\/rt\// ) { #RT
91
92       FS::Trace->log('handling RT file');
93
94       # We don't need to handle non-text, non-xml items
95       return -1 if defined( $r->content_type )
96                 && $r->content_type !~ m!(^text/|\bxml\b)!io;
97
98
99       local $SIG{__WARN__};
100       local $SIG{__DIE__};
101
102       FS::Trace->log('initializing RT');
103       my_rt_init();
104
105       FS::Trace->log('setting RT interpreter');
106       $ah->interp($rt_interp);
107
108     } else {
109
110       FS::Trace->log('handling Freeside file');
111
112       local $SIG{__WARN__};
113       local $SIG{__DIE__};
114
115       FS::Trace->log('initializing RT');
116       my_rt_init();
117
118       #we don't want the RT error handlers under FS
119       {
120         no warnings 'uninitialized';
121         undef($SIG{__WARN__}) if defined($SIG{__WARN__});
122         undef($SIG{__DIE__})  if defined($SIG{__DIE__} );
123       }
124
125       FS::Trace->log('setting Freeside interpreter');
126       $ah->interp($fs_interp);
127
128     }
129
130     FS::Trace->log('handling request');
131     my %session;
132     my $status;
133     eval { $status = $ah->handle_request($r); };
134 #!!
135 #    if ( $@ ) {
136 #       $RT::Logger->crit($@);
137 #    }
138     warn $@ if $@;
139
140     undef %session;
141
142 #!!
143 #    if ($RT::Handle->TransactionDepth) {
144 #       $RT::Handle->ForceRollback;
145 #       $RT::Logger->crit(
146 #"Transaction not committed. Usually indicates a software fault. Data loss may have occurred"
147 #       );
148 #    }
149
150     FS::Trace->log('done');
151
152     FS::Trace->dumpfile( "%%%FREESIDE_EXPORT%%%/profile/$$.".time,
153                          FS::Trace->total. ' '. $r->filename
154                        )
155       if FS::Trace->total > 5; #10?
156
157     FS::Trace->reset;
158
159     $status;
160 }
161
162 sub my_rt_init {
163   return unless $RT::VERSION;
164   RT::ConnectToDatabase();
165   RT::InitSignalHandlers();
166 }
167
168 1;