1 #!/usr/bin/env perl -wT
2 # -*- Mode: perl; indent-tabs-mode: nil -*-
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/
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.
14 # The Original Code is the Bugzilla Bug Tracking System.
16 # Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
18 package Bugzilla::ModPerl;
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 ();
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
31 use Apache2::ServerUtil;
32 use Apache2::SizeLimit;
33 use ModPerl::RegistryLoader ();
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();
41 use Bugzilla::Constants ();
43 use Bugzilla::Mailer ();
44 use Bugzilla::Template ();
45 use Bugzilla::Util ();
47 # For PerlChildInitHandler
48 eval { require Math::Random::Secure };
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;
54 my $cgi_path = Bugzilla::Constants::bz_locations()->{'cgi_path'};
56 # Set up the configuration for the web server
57 my $server = Apache2::ServerUtil->server;
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
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
76 DirectoryIndex index.cgi index.html
80 $server->add_config([split("\n", $conf)]);
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);
95 package Bugzilla::ModPerl::ResponseHandler;
97 use base qw(ModPerl::Registry);
100 sub handler : method {
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'};
107 Bugzilla::init_page();
108 return $class->SUPER::handler(@_);
112 package Bugzilla::ModPerl::CleanupHandler;
114 use Apache2::Const -compile => qw(OK);
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};
126 return Apache2::Const::OK;