Skip to content

Commit

Permalink
Merge pull request #311 from bobtfish/iis_redirect_remove_content
Browse files Browse the repository at this point in the history
Iis redirect remove content
  • Loading branch information
miyagawa committed Jul 18, 2012
2 parents ac05d09 + a3e1bde commit 2ef2952
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
54 changes: 54 additions & 0 deletions lib/Plack/Middleware/IIS7KeepAliveFix.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package Plack::Middleware::IIS7KeepAliveFix;

use strict;
use parent 'Plack::Middleware';
use Plack::Util;

sub call {
my($self, $env) = @_;
# Fixes buffer being cut off on redirect when keep-alive is active
my $res = $self->app->($env);

Plack::Util::response_cb($res, sub {
my $res = shift;
if ($res->[0] =~ m!^30[123]$! ) {
Plack::Util::header_remove($res->[1], 'Content-Length');
Plack::Util::header_remove($res->[1], 'Content-Type');
return sub{ my $chunk; return unless defined $chunk; return ''; };
}

return;
});

}

1;
__END__
=head1 NAME
Plack::Middleware::IIS7KeepAliveFix - fixes buffer being cut off on redirect when keep-alive is active on IIS.
=head1 SYNOPSIS
# in your app.psgi
use Plack::Builder;
builder {
enable "IIS7KeepAliveFix";
$app;
};
# Or from the command line
plackup -s FCGI -e 'enable "IIS7KeepAliveFix"' /path/to/app.psgi
=head1 DESCRIPTION
This middleware fixes buffer being cut off on redirect when keep-alive is active on IIS7.
=head1 AUTHORS
KnowZeroX
=cut
41 changes: 41 additions & 0 deletions t/Plack-Middleware/iis7_keep_alive_fix.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use strict;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;
use Plack::Middleware::IIS7KeepAliveFix;


my $app=Plack::Middleware::IIS7KeepAliveFix->wrap(
sub {
my $env = shift;


my $location='/go/?'.join('|', (0..1000));
return [ 302, [
'Content-Type' => 'text/html',
'Content-Length' => 285,
'Location' => $location,
],[qq~<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Moved</title>
</head>
<body>
<p>This item has moved <a href="$location">here</a>.</p>
</body>
</html>~] ];
});


test_psgi(app=>$app,client=> sub {
my $cb = shift;

my $res = $cb->(GET "/");

ok(!$res->content);
ok(!$res->content_length);
ok(!$res->content_type);
});

done_testing;

0 comments on commit 2ef2952

Please sign in to comment.