Re: Moving to PSGI: Issues with $ENV

Mark Stosberg <[email protected]> Tue, 16 Oct 2012 11:14:33 -0400
Newsgroups gmane.comp.lang.perl.modules.cgi-appplication
Message-ID <[email protected]>
On 10/15/2012 08:42 AM, Mike Tonks wrote:
> Another issue that has come up along the way is using $ENV
> 
> While this is fine under apache it doesn't seem to work with PSGI.

You are correct that PSGI uses $env, not %ENV.

> A common occurence is using $ENV{REMOTE_ADDR} for a user IP Address,
> but this is easily solved by using $self->query->remote_addr which
> does the right thing in both environments.

Right, a good choice.

> However today I stumbled across an issue with
> CGI::Application::Plugin::DetectAjax
> 
>   my $header = 'HTTP_X_REQUESTED_WITH';
> 
>   if (exists $ENV{$header} && lc $ENV{$header} eq 'xmlhttprequest') {
>     return 1;
>   }
>   else {
>     return 0;
>   }
> 
> This does not work under PSGI.
> 
> Looking at CGI::PSGI the correct approach seems to be:
> 
> $self->env->{$header}
> 
> so a possible patch would look something like:
> 
>   my $header = 'HTTP_X_REQUESTED_WITH';
> 
>   my $header_value = $self->{__IS_PSGI} ? $self->env->{$header} : $ENV{$header};
> 
>   if ($header_value && lc $header_value eq 'xmlhttprequest') {
>     return 1;
>   }
>   else {
>     return 0;
>   }
> 
> Can anyone help / confirm / improve on this?

If you want to use the plugin un-modified, I think you can do this:

{
   local *ENV = $env;
   $self->is_ajax;
}


  Mark

#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################