I'm writing a mod_perl2 script which intercepts some requests (based on dynamic criteria) and serves an HTML page instead of the requested document. It successfully sets the content type and length to new values using $req->content_type('text/html')
and $req->headers_out->add('Content-length'=>…)
.
My main Apache 2.4 config includes
<FilesMatch "\.pdf$">
ForceType application/pdf
Header set Content-Disposition attachment
</FilesMatch>
My problem is that when my script intercepts a request for a PDF, I cannot remove the "Content-Disposition: attachment" header. Because of this, my HTML page ends up in Downloads rather than being displayed. I've tried both $r->headers_out->unset('Content-Disposition'); # and $r->err_headers_out->unset('Content-Disposition');
I can add a different content-disposition, but then I get two or more contradictory headers.
The relevant bits of the current code look like this:
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Const -compile => qw(OK DECLINED SERVER_ERROR);
use APR::Table;
use Template;
…
my $data = {
KEY => $key,
NAME => $name,
TARGET => $target,
};
# Our document is not the same type or size as that requested, so
$r->content_type('text/html'); # works
$r->headers_out->unset('Content-Disposition');
$r->err_headers_out->unset('Content-Disposition');
$r->headers_out->set('Content-Disposition' => 'inline; filename="intercept.html" );
$r->headers_out->add('Content-Disposition' => 'inline');
$r->err_headers_out->set('Content-Disposition' => 'inline; filename="intercept.html" );
$r->err_headers_out->add('Content-Disposition' => 'inline');
$r->headers_out->add('Content-Length' =>12345 ); # works
$templates->process( 'intercepted.tt', $data ) or do {
$r->log_reason($templates->error());
return Apache2::Const::SERVER_ERROR;
};
return Apache2::Const::OK;
The resulting headers are
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Date: Mon, 10 Jul 2023 14:18:26 GMT
Server: Apache/2.4.56 (Debian)
Content-Disposition: inline; filename='intercept.html'
Content-Disposition: inline
Cache-Control: no-cache
Content-Disposition: attachment
Content-Length: 10580
Vary: Accept-Encoding
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
I've searched the Apache2::RequestRec and APR::Table documents for "Content-Disposition" in case there are any special methods for it (as there is for Content-Type,for example) but I've not found anything. An internet search for terms mod_perl and Content-Disposition got me nowhere.
I'd like to either remove the Content-Disposition header that Apache is adding or munge the request in some way such that Apache doesn't think that it needs to set that header. I'd be grateful for any pointers.