[Catalyst] Catalyst::Engine::CGI::prepare_path port

Jim Spath jspath at pangeamedia.com
Thu Feb 15 21:03:34 GMT 2007


Jim Spath wrote:
> Jim Spath wrote:
>> I was wondering why Catalyst::Engine::CGI::prepare_path() ignores the 
>> port in the Host header and instead uses $ENV{SERVER_PORT}.
>>
>>     my $scheme = $c->request->secure ? 'https' : 'http';
>>     my $host      = $ENV{HTTP_HOST}   || $ENV{SERVER_NAME};
>>     my $port      = $ENV{SERVER_PORT} || 80;
>>     ...
>>     my $uri = $uri_proto->clone;
>>     $uri->scheme($scheme);
>>     $uri->host($host);
>>     $uri->port($port);
>>
>> The reason I ask is that we are running a Pound load balancer that 
>> listens on port 80, but passes requests off to the Catalyst machines 
>> on port 81.  Pound does not pass an X_FORWARDED_HOST header, instead 
>> it appends the port to the original Host header, and passes that through.
>>
>> If Catalyst::Engine::CGI allowed URI to retrieve the port from the 
>> Host header, which is allowed in the HTTP 1.1 spec, my problem would 
>> be solved:
>>
>>     $uri->host($host);
>>     $uri->port($port) unless $uri->port;
>>
>> Does this solution make sense?  I could provide a patch if so... it's 
>> obviously a tiny change.  I guess the only thing I am uncertain of is 
>> if there are situations where we do not want to use a port from the 
>> Host header.
>>
>> - Jim
> 
> I just realized that URI sets the port to 80 by default, so my change as 
>  proposed above would not work correctly, but something like the 
> following could also work:
> 
> if ($host =~ /^(.+):(\d+)$/) {
>   $host = $1;
>   $port = $2;
> }

After much struggle (installing the latest pound, using 
Catalyst::Plugin::Setenv, none of which worked), we figured out a 
solution to our problem.  Since Pound refused to pass the 
X-Forwarded-Host header, and Catalyst requires it, we used mod_setenv in 
lighttpd to set the X-Forwarded-Host header:

setenv.add-request-header = ("X-Forwarded-Host" => "some.host.com")

We then had to add the using_frontend_proxy config flag in MyApp.pm and 
everything worked!

It is still somewhat worrying to me that Pound and Catalyst don't play 
nicely together.  Catalyst requires the X-Forwarded-Host header for 
proxies, but Pound doesn't add it because the necessary information is 
already contained in the Host header.

- Jim



More information about the Catalyst mailing list