mod_perl.pl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. use lib qw(/var/www/html);
  18. package Bugzilla::ModPerl;
  19. use strict;
  20. # If you have an Apache2::Status handler in your Apache configuration,
  21. # you need to load Apache2::Status *here*, so that Apache::DBI can
  22. # report information to Apache2::Status.
  23. #use Apache2::Status ();
  24. # We don't want to import anything into the global scope during
  25. # startup, so we always specify () after using any module in this
  26. # file.
  27. use Apache2::ServerUtil;
  28. use Apache2::SizeLimit;
  29. use ModPerl::RegistryLoader ();
  30. use CGI ();
  31. CGI->compile(qw(:cgi -no_xhtml -oldstyle_urls :private_tempfiles
  32. :unique_headers SERVER_PUSH :push));
  33. use Template::Config ();
  34. Template::Config->preload();
  35. use Bugzilla ();
  36. use Bugzilla::Constants ();
  37. use Bugzilla::CGI ();
  38. use Bugzilla::Mailer ();
  39. use Bugzilla::Template ();
  40. use Bugzilla::Util ();
  41. # For PerlChildInitHandler
  42. eval { require Math::Random::Secure };
  43. #if WEBKIT_CHANGES
  44. # This means that every httpd child will die after processing
  45. # a CGI if it is taking up more than 700MB of RAM all by itself.
  46. # our children are normally about 400MB
  47. $Apache2::SizeLimit::MAX_UNSHARED_SIZE = 700000;
  48. #endif // WEBKIT_CHANGES
  49. my $cgi_path = Bugzilla::Constants::bz_locations()->{'cgi_path'};
  50. # Set up the configuration for the web server
  51. my $server = Apache2::ServerUtil->server;
  52. my $conf = <<EOT;
  53. # Make sure each httpd child receives a different random seed (bug 476622).
  54. # Math::Random::Secure has one srand that needs to be called for
  55. # every process, and Perl has another. (Various Perl modules still use
  56. # the built-in rand(), even though we only use Math::Random::Secure in
  57. # Bugzilla itself, so we need to srand() both of them.) However,
  58. # Math::Random::Secure may not be installed, so we call its srand in an
  59. # eval.
  60. PerlChildInitHandler "sub { eval { Math::Random::Secure::srand() }; srand(); }"
  61. <Directory "$cgi_path">
  62. AddHandler perl-script .cgi
  63. # No need to PerlModule these because they're already defined in mod_perl.pl
  64. PerlResponseHandler Bugzilla::ModPerl::ResponseHandler
  65. PerlCleanupHandler Bugzilla::ModPerl::CleanupHandler
  66. PerlCleanupHandler Apache2::SizeLimit
  67. PerlOptions +ParseHeaders
  68. Options +ExecCGI
  69. AllowOverride Limit
  70. DirectoryIndex index.cgi index.html
  71. </Directory>
  72. EOT
  73. $server->add_config([split("\n", $conf)]);
  74. # Have ModPerl::RegistryLoader pre-compile all CGI scripts.
  75. my $rl = new ModPerl::RegistryLoader();
  76. # If we try to do this in "new" it fails because it looks for a
  77. # Bugzilla/ModPerl/ResponseHandler.pm
  78. $rl->{package} = 'Bugzilla::ModPerl::ResponseHandler';
  79. # Note that $cgi_path will be wrong if somebody puts the libraries
  80. # in a different place than the CGIs.
  81. foreach my $file (glob "$cgi_path/*.cgi") {
  82. Bugzilla::Util::trick_taint($file);
  83. $rl->handler($file, $file);
  84. }
  85. package Bugzilla::ModPerl::ResponseHandler;
  86. use strict;
  87. use base qw(ModPerl::Registry);
  88. use Bugzilla;
  89. sub handler : method {
  90. my $class = shift;
  91. # $0 is broken under mod_perl before 2.0.2, so we have to set it
  92. # here explicitly or init_page's shutdownhtml code won't work right.
  93. $0 = $ENV{'SCRIPT_FILENAME'};
  94. Bugzilla::init_page();
  95. return $class->SUPER::handler(@_);
  96. }
  97. package Bugzilla::ModPerl::CleanupHandler;
  98. use strict;
  99. use Apache2::Const -compile => qw(OK);
  100. sub handler {
  101. my $r = shift;
  102. Bugzilla::_cleanup();
  103. # Sometimes mod_perl doesn't properly call DESTROY on all
  104. # the objects in pnotes()
  105. foreach my $key (keys %{$r->pnotes}) {
  106. delete $r->pnotes->{$key};
  107. }
  108. return Apache2::Const::OK;
  109. }
  110. 1;