- Introduction
- The Apache Request Object
- The HTTP Request Message
- The Client Request
- Accessing Client Request Headers
- Accessing HTML Form Fields
- Reading POSTed Data Manually
- Manipulating Cookies
- Handling File Uploads
- Setting Server Response Headers
- Controlling Caching Behavior
- Sending Server Response Headers
- Setting the Response Status
- Setting Error Headers
- Manipulating Headers with Multiple Like Fields
- Using Subrequests
- Setting Headers for Subrequests
- Short-Circuiting Subrequests
- Getting or Setting the Request Method
- Accessing the Request Object from XS
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.