Home > Articles > Web Development > Perl

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Setting Headers for Subrequests

You need to alter request headers for a subrequest.

Technique

Call $r->headers_in() before initiating the subrequest.

$r->headers_in->set(Accept-Language => 'es');

my $sub = $r->lookup_uri('/armada.html');
my $filename = $sub->filename;

Comments

Ordinarily, the request headers for a subrequest are an exact copy of the headers present in the main client request. Part of the reason you may be initiating a subrequest, however, is to determine server behavior based on some additional parameter you don't currently have, such as a client cookie or, as in the preceding example, a different language tag. In these cases, tricking Apache by setting the incoming headers to simulate a different set of client parameters is often useful.

One of the downsides of this approach is that, depending on where you are in the request cycle, setting your own request headers can change the way the current request is processed. One possible workaround is to set the headers of the subrequest itself, as in

my $sub = $r->lookup_uri('/armada.html');

$sub->headers_in->set(Accept-Language => 'es'); 
return $sub->run(1);

but this is only really useful if you plan on calling run(), because setting headers for the subrequest occurs too late to affect anything other than the content generation phase.

  • + Share This
  • 🔖 Save To Your Account