Skip to content

Commit

Permalink
improved host condition to work in more environments
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 9, 2011
1 parent 9654e52 commit 90eaef3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

1.98 2011-09-09 00:00:00
- Improved host condition to work in more environments.
- Improved CSS of all built in templates.
- Improved documentation. (rhaen, sri)
- Improved tests.
Expand Down
21 changes: 14 additions & 7 deletions lib/Mojolicious/Plugin/HeaderCondition.pm
Expand Up @@ -16,21 +16,27 @@ sub register {

# "host" condition
$app->routes->add_condition(
host => sub { _headers(@_[0 .. 2], {'Host' => $_[3]}) });
host => sub { _check($_[1]->req->url->to_abs->host, $_[3]) });
}

# "Wow, there's a million aliens! I've never seen something so mind-blowing!
# Ooh, a reception table with muffins!"
sub _check {
my ($value, $pattern) = @_;
return 1
if $value && $pattern && ref $pattern eq 'Regexp' && $value =~ $pattern;
return 1 if $value && defined $pattern && $pattern eq $value;
return;
}

sub _headers {
my ($r, $c, $captures, $patterns) = @_;
return unless $patterns && ref $patterns eq 'HASH' && keys %$patterns;

# All headers need to match
while (my ($k, $v) = each %$patterns) {
my $header = $c->req->headers->header($k);
if ($header && $v && ref $v eq 'Regexp' && $header =~ $v) {next}
elsif ($header && defined $v && $v eq $header) {next}
else {return}
my $headers = $c->req->headers;
while (my ($name, $pattern) = each %$patterns) {
return unless _check(scalar $headers->header($name), $pattern);
}
return 1;
}
Expand Down Expand Up @@ -62,7 +68,8 @@ Mojolicious::Plugin::HeaderCondition - Header Condition Plugin
# The "agent" condition is a shortcut for the "User-Agent" header
get '/' => (agent => qr/Firefox/) => sub {...};
# The "host" condition is a shortcut for the "Host" header
# The "host" condition is a shortcut for the detected host
# (usually the "Host" or "X-Forwarded-Host" header)
get '/' => (host => qr/mojolicio\.us/) => sub {...};
=head1 DESCRIPTION
Expand Down
21 changes: 20 additions & 1 deletion t/mojolicious/embedded_lite_app.t
Expand Up @@ -10,7 +10,7 @@ BEGIN {
$ENV{MOJO_MODE} = 'testing';
}

use Test::More tests => 113;
use Test::More tests => 122;

use FindBin;
use lib "$FindBin::Bin/lib";
Expand Down Expand Up @@ -227,10 +227,29 @@ $t->get_ok('/host')->status_is(200)->content_is('main application!');
$t->get_ok('/' => {Host => 'mojolicious.org'})->status_is(200)
->content_is("works!\n\ntoo!works!!!\n");

# GET / (full external application with domain and reverse proxy)
my $backup = $ENV{MOJO_REVERSE_PROXY};
$ENV{MOJO_REVERSE_PROXY} = 1;
$t->get_ok('/' => {'X-Forwarded-Host' => 'mojolicious.org'})->status_is(200)
->content_is("works!\n\ntoo!works!!!\n");
$ENV{MOJO_REVERSE_PROXY} = $backup;

# GET /host (full external application with domain)
$t->get_ok('/host' => {Host => 'mojolicious.org'})->status_is(200)
->content_is('mojolicious.org');

# GET /host (full external application with domain and reverse proxy)
$backup = $ENV{MOJO_REVERSE_PROXY};
$ENV{MOJO_REVERSE_PROXY} = 1;
$t->get_ok('/host' => {'X-Forwarded-Host' => 'mojolicious.org'})
->status_is(200)->content_is('mojolicious.org');
$ENV{MOJO_REVERSE_PROXY} = $backup;

# GET /host (full external application with domain and no reverse proxy)
$t->get_ok(
'/host' => {Host => 'mojolicious.org', 'X-Forwarded-Host' => 'kraih.com'})
->status_is(200)->content_is('mojolicious.org');

# GET / (full external application with domain)
$t->get_ok('/' => {Host => 'mojolicio.us'})->status_is(200)
->content_is("works!\n\ntoo!works!!!\n");
Expand Down

0 comments on commit 90eaef3

Please sign in to comment.