# $Id: AddContentDisposition.pm,v 1.2 2004/01/22 19:27:31 jmates Exp $ # # Apache 1.3 mod_perl PerlTypeHandler to add the Content-Disposition # header to outgoing files from a ".filename" file in the same directory # containing the real file. This allows files with names such as # "341ee566.gif" to have a human-friendly name associated with them. # # See RFC 2183 for more information on the Content-Disposition header. # # Enable this module with a PerlTypeHandler directive for the # or similar areas in question after installing this file # under an Apache directory in @INC. # # PerlTypeHandler Apache::AddContentDisposition # # The author disclaims all copyrights and releases this module into the # public domain. package Apache::AddContentDisposition; use Apache::Constants qw(OK DECLINED); # to determine the Content-Type of the file use File::MMagic (); sub handler { my $r = shift; # /foo/bar file from Apache -> /foo/.bar metafile (my $metafile = $r->filename) =~ s,/([^/]+)$,/.$1,; # assumes first line only contains "real" filename # TODO save MIME type from MIMEDefang in event File::MMagic # has problems with the file? open FILE, "< $metafile" or return DECLINED; my $filename = ; close FILE; return DECLINED unless $filename; # sanitize out characters not listed and .. runs to mitigate potential # security problems on clients $filename =~ s/[^\w.-]//g; $filename =~ s/\.\.+//g; return DECLINED unless $filename; # need to set Content-Type as is set to text/plain once our custom # Content-Disposition header is set, which trips up Mozilla my $mime_type = File::MMagic->new->checktype_filename($r->filename); return DECLINED unless $mime_type; $r->content_type($mime_type); $r->headers_out->set("Content-Disposition" => "inline; filename=$filename"); return OK; } 1;