404handler.pl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 2 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, write to the
  16. # Free Software Foundation, Inc.
  17. # 59 Temple Place, Suite 330
  18. # Boston, MA 02111-1307 USA
  19. package OddMuse;
  20. my $dir = '/var/www/wiki'; # absolute path to the file cache
  21. my $origname = '/wiki'; # relative url to the file cache, with trailing slash
  22. my $script = '/usr/lib/cgi-bin/wiki.pl'; # absolute path to the wiki script
  23. my $name = '/cgi-bin/wiki.pl'; # relative url to the wiki script
  24. my @path = split(/\//, $ENV{REDIRECT_URL});
  25. my $file = $path[$#path];
  26. # for dynamic pages
  27. our ($NotFoundHandlerExceptionsPage);
  28. $NotFoundHandlerExceptionsPage = 'NoCachePages';
  29. $RunCGI = 0;
  30. do $script;
  31. Init();
  32. # call the wiki for the page missing in the cache. first set up CGI
  33. # environment -- see http://localhost/cgi-bin/printenv. then call the
  34. # script and read output from the pipe.
  35. local $/;
  36. $ENV{REQUEST_METHOD}="GET";
  37. $ENV{QUERY_STRING}=$file;
  38. $ENV{SCRIPT_FILENAME}=$script;
  39. $ENV{SCRIPT_NAME}=$name;
  40. $ENV{REQUEST_URI}=$origname;
  41. # print "Content-Type: text/plain\r\n\r\n";
  42. # print "$script $file\n";
  43. open(F, "$script |") || print STDERR "can't run $script: $!\n";
  44. my $data = <F>;
  45. close(F);
  46. # print data to stdout and write a copy without headers into the cache
  47. # if the script didn't print a Status (since the default is "200 Ok").
  48. print $data;
  49. $data =~ /^Status: ([1-9][0-9][0-9])/;
  50. my $status = $1;
  51. $data =~ /((.+:.*\n)*)/;
  52. my $header = $1;
  53. # print "<pre>$header</pre>";
  54. if (not $status) { # ie. 200
  55. my %skip = ();
  56. foreach (split(/\n/, GetPageContent($NotFoundHandlerExceptionsPage))) {
  57. if (/^ ([^ ]+)[ \t]*$/) { # only read lines with one word after one space
  58. $skip{$1} = 1;
  59. }
  60. }
  61. if (not $skip{$file}) {
  62. $data =~ s/^(.*\r\n)+//; # strip header
  63. open(G, "> $dir/$file") || print STDERR "can't write $dir/$file: $!\n";
  64. print G $data;
  65. close(G);
  66. }
  67. }
  68. 1;
  69. # cache cleanup has to hook into the wiki!