Permalink
Browse files

Fix broken host/port

OpenBSD's httpd reverse proxy doesn't send the proper Host: header
so we need to add support for X-Forwarded-Host and X-Forwarded-Port.

Well, X-Forwarded-Port is optional but may as well.
  • Loading branch information...
afresh1 committed Dec 2, 2012
1 parent b04e11c commit 4d223bf77707f574166989daa732343e1a824c4e
Showing with 21 additions and 0 deletions.
  1. +21 −0 OpenBSDtracker
View
@@ -109,6 +109,27 @@ app->hook( before_dispatch => sub {
my $tx = shift;
my $default_file = 'index.html';
+ # Fix broken host/port due to b0rk3d httpd mod_proxy Host: header
+ # -- http://marc.info/?t=133254469700005&r=1&w=2
+ if ($ENV{MOJO_REVERSE_PROXY}) {
+ my $headers = $tx->req->headers;
+
+ my $host = $headers->header('X-Forwarded-Host');
+ my $port = $headers->header('X-Forwarded-Port');
+
+ $host =~ s/:(\d+)$//;
+ $port ||= $1;
+
+ my $base = $tx->req->url->base;
+ if ($host) {
+ $base->host($host);
+ $base->port($port);
+ }
+ elsif ($port) {
+ $base->port($port);
+ }
+ }
+
# Canonical path
my $stash = $tx->stash;
my $rel = $stash->{path} || $tx->req->url->path->clone->canonicalize;

0 comments on commit 4d223bf

Please sign in to comment.