404handler.pl 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #! /usr/bin/perl
  2. # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. package OddMuse;
  17. my $dir = '/var/www/wiki'; # absolute path to the file cache
  18. my $origname = '/wiki'; # relative url to the file cache, with trailing slash
  19. my $script = '/usr/lib/cgi-bin/wiki.pl'; # absolute path to the wiki script
  20. my $name = '/cgi-bin/wiki.pl'; # relative url to the wiki script
  21. my @path = split(/\//, $ENV{REDIRECT_URL});
  22. my $file = $path[$#path];
  23. # for dynamic pages
  24. our ($NotFoundHandlerExceptionsPage);
  25. $NotFoundHandlerExceptionsPage = 'NoCachePages';
  26. $RunCGI = 0;
  27. do $script;
  28. Init();
  29. # call the wiki for the page missing in the cache. first set up CGI
  30. # environment -- see http://localhost/cgi-bin/printenv. then call the
  31. # script and read output from the pipe.
  32. local $/;
  33. $ENV{REQUEST_METHOD}="GET";
  34. $ENV{QUERY_STRING}=$file;
  35. $ENV{SCRIPT_FILENAME}=$script;
  36. $ENV{SCRIPT_NAME}=$name;
  37. $ENV{REQUEST_URI}=$origname;
  38. # print "Content-Type: text/plain\r\n\r\n";
  39. # print "$script $file\n";
  40. open(F, "$script |") || print STDERR "can't run $script: $!\n";
  41. my $data = <F>;
  42. close(F);
  43. # print data to stdout and write a copy without headers into the cache
  44. # if the script didn't print a Status (since the default is "200 Ok").
  45. print $data;
  46. $data =~ /^Status: ([1-9][0-9][0-9])/;
  47. my $status = $1;
  48. $data =~ /((.+:.*\n)*)/;
  49. my $header = $1;
  50. # print "<pre>$header</pre>";
  51. if (not $status) { # ie. 200
  52. my %skip = ();
  53. foreach (split(/\n/, GetPageContent($NotFoundHandlerExceptionsPage))) {
  54. if (/^ ([^ ]+)[ \t]*$/) { # only read lines with one word after one space
  55. $skip{$1} = 1;
  56. }
  57. }
  58. if (not $skip{$file}) {
  59. $data =~ s/^(.*\r\n)+//; # strip header
  60. open(G, "> $dir/$file") || print STDERR "can't write $dir/$file: $!\n";
  61. print G $data;
  62. close(G);
  63. }
  64. }
  65. 1;
  66. # cache cleanup has to hook into the wiki!