Created
September 9, 2012 15:42
-
-
Save dex4er/3685138 to your computer and use it in GitHub Desktop.
HTTP to HTTPS proxy server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# generic HTTP to HTTP proxy server | |
use strict; | |
use Plack::Builder; | |
use Plack::App::Proxy; | |
my $error_500_res = [500, ['Content-Type' => 'text/plain'], [ "INTERNAL SERVER ERROR\n" ] ]; | |
builder { | |
# Catch exception, convert to warning and return default 500 error | |
enable sub { | |
my ($app) = @_; | |
sub { | |
my ($env) = @_; | |
my $res = eval { $app->($env) }; | |
if ($@) { | |
warn $@; | |
$res = $error_500_res; | |
}; | |
return $res; | |
}; | |
}; | |
enable 'Proxy::AddVia'; | |
Plack::App::Proxy->new( remote => $ENV{PLACK_PROXY_REMOTE} )->to_app; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# HTTP to HTTPS router proxy server | |
use strict; | |
use Plack::Util; | |
delete $ENV{http_proxy}; | |
my $proxy_app = Plack::Util::load_psgi('http2http.psgi'); | |
print STDERR "Loaded http2http.psgi\n"; | |
use URI; | |
my $mw = sub { | |
my ($app) = @_; | |
sub { | |
my ($env) = @_; | |
my $uri = URI->new($env->{REQUEST_URI}); | |
my $host_port = $uri->host_port; | |
my $is_default_port = $uri->default_port == $uri->port; | |
$uri->scheme('https'); | |
$uri->host_port($host_port); | |
$uri->port($uri->default_port) if $is_default_port; | |
$env->{'plack.proxy.url'} = $uri->as_string; | |
$app->($env); | |
}; | |
}; | |
use Plack::Builder; | |
builder { | |
enable $mw; | |
enable 'RequestHeaders', | |
unset => [ qw( Via X-Forwarded-For X-Forwarded-Host X-Forwarded-Server X-Powered-By ) ]; | |
$proxy_app; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment