summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/apache404.pm
blob: 3ac6b3af507a17c0612fe9619b1a5ae4c0c00124 (plain)
  1. #!/usr/bin/perl
  2. # Copyright © 2009 Simon McVittie <http://smcv.pseudorandom.co.uk/>
  3. # Licensed under the GNU GPL, version 2, or any later version published by the
  4. # Free Software Foundation
  5. package IkiWiki::Plugin::apache404;
  6. use warnings;
  7. use strict;
  8. use IkiWiki 3.00;
  9. sub import {
  10. hook(type => "cgi", id => 'apache404', call => \&cgi);
  11. }
  12. sub getsetup () {
  13. return
  14. plugin => {
  15. # not really a matter of safety, but enabling/disabling
  16. # through a web interface is useless - it needs web
  17. # server admin action too
  18. safe => 0,
  19. rebuild => 0,
  20. }
  21. }
  22. sub cgi_page_from_404 ($$$) {
  23. my $path = shift;
  24. my $baseurl = shift;
  25. my $usedirs = shift;
  26. # fail if missing from environment or whatever
  27. return undef unless defined $path;
  28. return undef unless defined $baseurl;
  29. # with usedirs on, path is like /~fred/foo/bar/ or /~fred/foo/bar or
  30. # /~fred/foo/bar/index.html
  31. # with usedirs off, path is like /~fred/foo/bar.html
  32. # baseurl is like 'http://people.example.com/~fred'
  33. # convert baseurl to ~fred
  34. unless ($baseurl =~ s{^https?://[^/]+/?}{}) {
  35. return undef;
  36. }
  37. # convert path to /~fred/foo/bar
  38. if ($usedirs) {
  39. $path =~ s/\/*(?:index\.$config{htmlext})?$//;
  40. }
  41. else {
  42. $path =~ s/\.$config{htmlext}$//;
  43. }
  44. # remove /~fred/
  45. unless ($path =~ s{^/*\Q$baseurl\E/*}{}) {
  46. return undef;
  47. }
  48. # special case for the index
  49. unless ($path) {
  50. return 'index';
  51. }
  52. return $path;
  53. }
  54. sub cgi ($) {
  55. my $cgi=shift;
  56. if ($ENV{REDIRECT_STATUS} eq '404') {
  57. my $page = cgi_page_from_404($ENV{REDIRECT_URL},
  58. $config{url}, $config{usedirs});
  59. IkiWiki::cgi_goto($cgi, $page);
  60. }
  61. }
  62. 1;