Permalink
Browse files

Add middleware to fix broken env values on IIS6

This is extracted from Catalyst.
  • Loading branch information...
1 parent f428b85 commit bc572180219217730954db1146d43ce70a00ef6b @rafl rafl committed Mar 2, 2011
Showing with 127 additions and 0 deletions.
  1. +2 −0 MANIFEST
  2. +57 −0 lib/Plack/Middleware/IIS6ScriptNameFix.pm
  3. +68 −0 t/Plack-Middleware/iis6_script_name_fix.t
View
@@ -85,6 +85,7 @@ lib/Plack/Middleware/ContentMD5.pm
lib/Plack/Middleware/ErrorDocument.pm
lib/Plack/Middleware/Head.pm
lib/Plack/Middleware/HTTPExceptions.pm
+lib/Plack/Middleware/IIS6ScriptNameFix.pm
lib/Plack/Middleware/JSONP.pm
lib/Plack/Middleware/LighttpdScriptNameFix.pm
lib/Plack/Middleware/Lint.pm
@@ -187,6 +188,7 @@ t/Plack-Middleware/head.t
t/Plack-Middleware/htpasswd
t/Plack-Middleware/httpexceptions.t
t/Plack-Middleware/httpexceptions_streaming.t
+t/Plack-Middleware/iis6_script_name_fix.t
t/Plack-Middleware/jsonp.t
t/Plack-Middleware/lint.t
t/Plack-Middleware/lint_utf8_false_alarm.t
@@ -0,0 +1,57 @@
+package Plack::Middleware::IIS6ScriptNameFix;
+
+use strict;
+use parent 'Plack::Middleware';
+
+sub call {
+ my($self, $env) = @_;
+
+ if ($env->{SERVER_SOFTWARE} && $env->{SERVER_SOFTWARE} =~ /IIS\/[6-9]\.[0-9]/) {
+ my @script_name = split(m!/!, $env->{PATH_INFO});
+ my @path_translated = split(m!/|\\\\?!, $env->{PATH_TRANSLATED});
+ my @path_info;
+
+ while ($script_name[$#script_name] eq $path_translated[$#path_translated]) {
+ pop(@path_translated);
+ unshift(@path_info, pop(@script_name));
+ }
+
+ unshift(@path_info, '', '');
+
+ $env->{PATH_INFO} = join('/', @path_info);
+ $env->{SCRIPT_NAME} = join('/', @script_name);
+ }
+
+ return $self->app->($env);
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Plack::Middleware::IIS6ScriptNameFix - fixes wrong SCRIPT_NAME and PATH_INFO that IIS6 sets
+
+=head1 SYNOPSIS
+
+ # in your app.psgi
+ use Plack::Builder;
+
+ builder {
+ enable "IIS6ScriptNameFix";
+ $app;
+ };
+
+ # Or from the command line
+ plackup -s FCGI -e 'enable "IIS6ScriptNameFix"' /path/to/app.psgi
+
+=head1 DESCRIPTION
+
+This middleware fixes wrong C<SCRIPT_NAME> and C<PATH_INFO> set by IIS6.
+
+=head1 AUTHORS
+
+Florian Ragwitz
+
+=cut
@@ -0,0 +1,68 @@
+use strict;
+use warnings;
+use Test::More;
+
+use Plack::Middleware::IIS6ScriptNameFix;
+
+my %env = (
+ 'SCRIPT_NAME' => '/koo/blurb',
+ 'PATH_INFO' => '/koo/blurb',
+ 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
+ 'REQUEST_METHOD' => 'GET',
+ 'SCRIPT_FILENAME' => 'C:\\Foo\\script\\blurb',
+ 'INSTANCE_META_PATH' => '/LM/W3SVC/793536',
+ 'SERVER_SOFTWARE' => 'Microsoft-IIS/6.0',
+ 'AUTH_PASSWORD' => '',
+ 'AUTH_TYPE' => '',
+ 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows; U; Windows NT 5.2; de; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4 (.NET CLR 3.5.30729)',
+ 'REMOTE_PORT' => '1281',
+ 'QUERY_STRING' => '',
+ 'URL' => '/koo/blurb',
+ 'HTTP_ACCEPT_LANGUAGE' => 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3',
+ 'FCGI_ROLE' => 'RESPONDER',
+ 'HTTP_KEEP_ALIVE' => '300',
+ 'CONTENT_TYPE' => '',
+ 'LOCAL_ADDR' => '127.0.0.1',
+ 'GATEWAY_INTERFACE' => 'CGI/1.1',
+ 'HTTPS' => 'off',
+ 'DOCUMENT_ROOT' => 'C:\\Foo\\script',
+ 'REMOTE_HOST' => '127.0.0.1',
+ 'PATH_TRANSLATED' => 'C:\\Foo\\script\\blurb',
+ 'APPL_PHYSICAL_PATH' => 'C:\\Foo\\script\\',
+ 'SERVER_NAME' => '127.0.0.1',
+ 'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
+ 'HTTP_CONNECTION' => 'keep-alive',
+ 'INSTANCE_ID' => '793536',
+ 'CONTENT_LENGTH' => '0',
+ 'AUTH_USER' => '',
+ 'APPL_MD_PATH' => '/LM/W3SVC/793536/Root/koo',
+ 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
+ 'REMOTE_USER' => '',
+ 'SERVER_PORT_SECURE' => '0',
+ 'SERVER_PORT' => 83,
+ 'REMOTE_ADDR' => '127.0.0.1',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ 'REQUEST_URI' => '/koo/blurb',
+ 'APP_POOL_ID' => 'DefaultAppPool',
+ 'HTTP_HOST' => '127.0.0.1:83'
+);
+
+sub test_fix {
+ my ($input_env) = @_;
+
+ my $mangled_env;
+ Plack::Middleware::IIS6ScriptNameFix->wrap(sub {
+ my ($env) = @_;
+ $mangled_env = $env;
+ return [ 200, ['Content-Type' => 'text/plain'], [''] ];
+ })->($input_env);
+
+ return $mangled_env;
+}
+
+my $fixed_env = test_fix({ %env });
+
+is($fixed_env->{PATH_INFO}, '//blurb', 'check PATH_INFO');
+is($fixed_env->{SCRIPT_NAME}, '/koo', 'check SCRIPT_NAME');
+
+done_testing;

0 comments on commit bc57218

Please sign in to comment.