7cc647c9b829ed0d6162730db0258ff63bd81f2f
[vuplus_webkit] / Websites / bugs.webkit.org / mod_perl.pl
1 #!/usr/bin/env perl -wT
2 # -*- Mode: perl; indent-tabs-mode: nil -*-
3 #
4 # The contents of this file are subject to the Mozilla Public
5 # License Version 1.1 (the "License"); you may not use this file
6 # except in compliance with the License. You may obtain a copy of
7 # the License at http://www.mozilla.org/MPL/
8 #
9 # Software distributed under the License is distributed on an "AS
10 # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11 # implied. See the License for the specific language governing
12 # rights and limitations under the License.
13 #
14 # The Original Code is the Bugzilla Bug Tracking System.
15 #
16 # Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
17
18 package Bugzilla::ModPerl;
19
20 use strict;
21
22 # If you have an Apache2::Status handler in your Apache configuration,
23 # you need to load Apache2::Status *here*, so that Apache::DBI can
24 # report information to Apache2::Status.
25 #use Apache2::Status ();
26
27 # We don't want to import anything into the global scope during
28 # startup, so we always specify () after using any module in this
29 # file.
30
31 use Apache2::ServerUtil;
32 use Apache2::SizeLimit;
33 use ModPerl::RegistryLoader ();
34 use CGI ();
35 CGI->compile(qw(:cgi -no_xhtml -oldstyle_urls :private_tempfiles
36                 :unique_headers SERVER_PUSH :push));
37 use Template::Config ();
38 Template::Config->preload();
39
40 use Bugzilla ();
41 use Bugzilla::Constants ();
42 use Bugzilla::CGI ();
43 use Bugzilla::Mailer ();
44 use Bugzilla::Template ();
45 use Bugzilla::Util ();
46
47 # For PerlChildInitHandler
48 eval { require Math::Random::Secure };
49
50 # This means that every httpd child will die after processing
51 # a CGI if it is taking up more than 70MB of RAM all by itself.
52 $Apache2::SizeLimit::MAX_UNSHARED_SIZE = 70000;
53
54 my $cgi_path = Bugzilla::Constants::bz_locations()->{'cgi_path'};
55
56 # Set up the configuration for the web server
57 my $server = Apache2::ServerUtil->server;
58 my $conf = <<EOT;
59 # Make sure each httpd child receives a different random seed (bug 476622).
60 # Math::Random::Secure has one srand that needs to be called for
61 # every process, and Perl has another. (Various Perl modules still use
62 # the built-in rand(), even though we only use Math::Random::Secure in
63 # Bugzilla itself, so we need to srand() both of them.) However, 
64 # Math::Random::Secure may not be installed, so we call its srand in an
65 # eval.
66 PerlChildInitHandler "sub { eval { Math::Random::Secure::srand() }; srand(); }"
67 <Directory "$cgi_path">
68     AddHandler perl-script .cgi
69     # No need to PerlModule these because they're already defined in mod_perl.pl
70     PerlResponseHandler Bugzilla::ModPerl::ResponseHandler
71     PerlCleanupHandler  Bugzilla::ModPerl::CleanupHandler
72     PerlCleanupHandler  Apache2::SizeLimit
73     PerlOptions +ParseHeaders
74     Options +ExecCGI
75     AllowOverride Limit
76     DirectoryIndex index.cgi index.html
77 </Directory>
78 EOT
79
80 $server->add_config([split("\n", $conf)]);
81
82 # Have ModPerl::RegistryLoader pre-compile all CGI scripts.
83 my $rl = new ModPerl::RegistryLoader();
84 # If we try to do this in "new" it fails because it looks for a 
85 # Bugzilla/ModPerl/ResponseHandler.pm
86 $rl->{package} = 'Bugzilla::ModPerl::ResponseHandler';
87 # Note that $cgi_path will be wrong if somebody puts the libraries
88 # in a different place than the CGIs.
89 foreach my $file (glob "$cgi_path/*.cgi") {
90     Bugzilla::Util::trick_taint($file);
91     $rl->handler($file, $file);
92 }
93
94
95 package Bugzilla::ModPerl::ResponseHandler;
96 use strict;
97 use base qw(ModPerl::Registry);
98 use Bugzilla;
99
100 sub handler : method {
101     my $class = shift;
102
103     # $0 is broken under mod_perl before 2.0.2, so we have to set it
104     # here explicitly or init_page's shutdownhtml code won't work right.
105     $0 = $ENV{'SCRIPT_FILENAME'};
106
107     Bugzilla::init_page();
108     return $class->SUPER::handler(@_);
109 }
110
111
112 package Bugzilla::ModPerl::CleanupHandler;
113 use strict;
114 use Apache2::Const -compile => qw(OK);
115
116 sub handler {
117     my $r = shift;
118
119     Bugzilla::_cleanup();
120     # Sometimes mod_perl doesn't properly call DESTROY on all
121     # the objects in pnotes()
122     foreach my $key (keys %{$r->pnotes}) {
123         delete $r->pnotes->{$key};
124     }
125
126     return Apache2::Const::OK;
127 }
128
129 1;