Skip to content

Commit

Permalink
Fixed bug where env{SCRIPT_NAME} not being set properly.
Browse files Browse the repository at this point in the history
- According to http://search.cpan.org/~miyagawa/PSGI-1.03/PSGI.pod,
  the "SERVER_NAME, SERVER_PORT: When combined with SCRIPT_NAME and
  PATH_INFO, these variables can be used to complete the URL.". This
  scenario was not checked in Plack::Test::Suite (which is a bug
  that I reported to the authors) which allowed a bug where the
  SCRIPT_NAME and PATH_INFO were duplicates of each other, and
  where the URL could not be recreated.  (I discovered this when
  tring to run a Jifty application under Mongrel2.)
- Refactored the SCRIPT_NAME and PATH_INFO parsing to be more similar
  to what is done for rack
  (https://github.com/darkhelmet/rack-mongrel2/blob/master/lib/rack/handler/mongrel2.rb)
  and for WSGI
  (https://github.com/rfk/m2wsgi/blob/master/m2wsgi/io/base.py).
  • Loading branch information
Henry Baragar authored and Henry Baragar committed Apr 27, 2011
1 parent 036b7b8 commit 42907bf
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions lib/Plack/Handler/Mongrel2.pm
Expand Up @@ -43,6 +43,25 @@ sub _parse_headers {
return decode_json $headers;
}

sub _parse_pattern {
my $pattern = shift;
if (DEBUG()) {
print STDERR "[Mongrel2.pm] Decoding PATTERN '$pattern'\n";
}
my ($name) = split /\(/, $pattern, 2;
$name =~ s{/+$}{};
$name;
}
sub _parse_path {
my ($path, $script_name) = @_;
if (DEBUG()) {
print STDERR "[Mongrel2.pm] Decoding PATH '$path ($script_name)'\n";
}
$path =~ s/^$script_name//;
URI::Escape::uri_unescape($path);
}


sub mongrel2_req_to_psgi {
my ($self, $data) = @_;
Expand All @@ -64,22 +83,20 @@ sub mongrel2_req_to_psgi {
'psgi.nonblocking' => 0,
);

($env{MONGREL2_SENDER_ID}, $env{MONGREL2_CONN_ID}, $env{PATH_INFO}, $rest) =
($env{MONGREL2_SENDER_ID}, $env{MONGREL2_CONN_ID}, undef, $rest) =
split / /, $data, 4;

$env{PATH_INFO} = URI::Escape::uri_unescape($env{PATH_INFO});

($headers, $rest) = _parse_netstring($rest);

my $hdrs = _parse_headers($headers);

my $script_name = _parse_pattern(delete $hdrs->{PATTERN});

$env{QUERY_STRING} = delete $hdrs->{QUERY} || '';
$env{REQUEST_METHOD} = delete $hdrs->{METHOD};
$env{REQUEST_URI} = delete $hdrs->{URI};
$env{SCRIPT_NAME} = delete $hdrs->{PATH} || '';
if ($env{SCRIPT_NAME} eq '/') {
$env{SCRIPT_NAME} = '';
}
$env{SCRIPT_NAME} = $script_name;
$env{PATH_INFO} = _parse_path(delete $hdrs->{PATH}, $script_name);
$env{SERVER_PROTOCOL} = delete $hdrs->{VERSION};
($env{SERVER_NAME}, $env{SERVER_PORT}) = split /:/, delete $hdrs->{host}, 2;
$env{SERVER_PORT} ||= 80;
Expand Down

0 comments on commit 42907bf

Please sign in to comment.