store render times in access log, RT#39822
[freeside.git] / FS / FS / Mason / Request.pm
1 package FS::Mason::Request;
2
3 use strict;
4 use warnings;
5 use vars qw( $FSURL $QUERY_STRING );
6 use base 'HTML::Mason::Request';
7 use IO::Handle;
8 use FS::Trace;
9
10 $FSURL = 'http://Set/FS_Mason_Request_FSURL/in_standalone_mode/';
11 $QUERY_STRING = '';
12
13 sub new {
14     my $class = shift;
15
16     FS::Trace->log('creating new FS::Mason::Request object');
17
18     my $superclass = $HTML::Mason::ApacheHandler::VERSION ?
19                      'HTML::Mason::Request::ApacheHandler' :
20                      $HTML::Mason::CGIHandler::VERSION ?
21                      'HTML::Mason::Request::CGI' :
22                      'HTML::Mason::Request';
23
24     FS::Trace->log('  altering superclass');
25     $class->alter_superclass( $superclass );
26
27     FS::Trace->log('  setting valid params');
28     #huh... shouldn't alter_superclass take care of this for us?
29     __PACKAGE__->valid_params( %{ $superclass->valid_params() } );
30
31     FS::Trace->log('  freeside_setup');
32     my %opt = @_;
33     my $mode = $superclass =~ /Apache/i ? 'apache' : 'standalone';
34     $class->freeside_setup($opt{'comp'}, $mode);
35
36     FS::Trace->log('  SUPER::new');
37     $class->SUPER::new(@_);
38
39 }
40
41 #override alter_superclass ala RT::Interface::Web::Request ??
42 # for Mason 1.39 vs. Perl 5.10.0
43
44 my $protect_fds;
45
46 sub freeside_setup {
47     my( $class, $filename, $mode ) = @_;
48
49     #from rt/bin/webmux.pl(.in)
50     if ( !$protect_fds && $ENV{'MOD_PERL'} && exists $ENV{'MOD_PERL_API_VERSION'}
51         && $ENV{'MOD_PERL_API_VERSION'} >= 2
52     ) {
53         FS::Trace->log('    protecting fds');
54         # under mod_perl2, STDIN and STDOUT get closed and re-opened,
55         # however they are not on FD 0 and 1.  In this case, the next
56         # socket that gets opened will occupy one of these FDs, and make
57         # all system() and open "|-" calls dangerous; for example, the
58         # DBI handle can get this FD, which later system() calls will
59         # close by putting garbage into the socket.
60         $protect_fds = [];
61         push @{$protect_fds}, IO::Handle->new_from_fd(0, "r")
62             if fileno(STDIN) != 0;
63         push @{$protect_fds}, IO::Handle->new_from_fd(1, "w")
64             if fileno(STDOUT) != 1;
65     }
66
67     if ( $HTML::Mason::Commands::r ) {
68       FS::Trace->log('    adding headers');
69       #frame-ancestors not supported by all the major browsers yet
70       $HTML::Mason::Commands::r->header_out( 'X-Frame-Options', 'SAMEORIGIN' );
71     }
72
73     if ( $filename =~ qr(/REST/\d+\.\d+/NoAuth/) ) {
74
75       FS::Trace->log('    handling RT REST/NoAuth file');
76
77       package HTML::Mason::Commands; #?
78       use FS::UID qw( adminsuidsetup setcgi );
79
80       #need to log somebody in for the mail gw
81
82       ##old installs w/fs_selfs or selfserv??
83       #&adminsuidsetup('fs_selfservice');
84
85       FS::Trace->log('    adminsuidsetup fs_queue');
86       &adminsuidsetup('fs_queue');
87
88     } else {
89
90       FS::Trace->log('    handling regular file');
91
92       package HTML::Mason::Commands;
93       use vars qw( $cgi $p $fsurl ); # $lh ); #not using /mt
94       use Encode;
95       #use FS::UID qw( cgisuidsetup );
96       use FS::CGI qw( popurl rooturl );
97
98       if ( $mode eq 'apache' ) {
99         $cgi = new CGI;
100         setcgi($cgi);
101
102         #cgisuidsetup is gone, equivalent is now done in AuthCookieHandler
103
104         $fsurl = rooturl();
105         $p = popurl(2);
106       } elsif ( $mode eq 'standalone' ) {
107         $cgi = new CGI $FS::Mason::Request::QUERY_STRING; #better keep setting
108                                                           #if you set it once
109         $FS::UID::cgi = $cgi;
110         $fsurl = $FS::Mason::Request::FSURL; #kludgy, but what the hell
111         $p = popurl(2, "$fsurl$filename");
112       } else {
113         die "unknown mode $mode";
114       }
115
116       FS::Trace->log('    UTF-8-decoding form data');
117       #
118       foreach my $param ( $cgi->param ) {
119         
120         #we can't switch to multi_param until we're done supporting deb 7
121         local($CGI::LIST_CONTEXT_WARN) = 0;
122
123         my @values = $cgi->param($param);
124         next if $cgi->uploadInfo($values[0]);
125         #warn $param;
126         @values = map decode(utf8=>$_), @values;
127         $cgi->param($param, @values);
128       }
129
130     }
131
132     FS::Trace->log('    done');
133
134 }
135
136 sub callback {
137   RT::Interface::Web::Request::callback(@_);
138 }
139
140 sub request_path {
141   RT::Interface::Web::Request::request_path(@_);
142 }
143
144 1;