-
-
Save kentaro/886525 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 plackup --port 5432 -E production | |
# vim:set ft=perl: | |
package Plack::App::CocProxy; | |
use strict; | |
use warnings; | |
use parent qw(Plack::App::File); | |
use Plack::App::Proxy; | |
my $proxy = Plack::App::Proxy->new->to_app; | |
sub call { | |
my $self = shift; | |
my $env = shift; | |
my $res = $self->SUPER::call($env); | |
if ($res->[0] != 404) { | |
$res; | |
} else { | |
$env->{'plack.proxy.url'} = $env->{REQUEST_URI}; | |
$proxy->($env); | |
} | |
} | |
sub locate_file { | |
my ($self, $env) = @_; | |
# XXX : invalid PATH_INFO? | |
my $req = URI->new($env->{REQUEST_URI}); | |
$env->{PATH_INFO} = $req->path; | |
my $path = $req->path; | |
my $host = $req->host; | |
my $base = $path; | |
$path =~ s{^/}{}; | |
$base =~ s{^.*/}{}; | |
$path ||= 'index.html'; | |
$base ||= 'index.html'; | |
my @paths = ( | |
$base, | |
"$host/$path", | |
"$host/$base", | |
$path, | |
); | |
my $docroot = $self->root || "."; | |
for my $path (@paths) { | |
my $try = "$docroot/$path"; | |
if (-l $try) { | |
$try = readlink($try); | |
} | |
if (-r $try) { | |
$env->{'psgi.errors'}->print(sprintf("Arrogated %s => %s\n", $req, $try)); | |
return $try, undef; | |
} | |
} | |
$self->return_404; | |
} | |
__PACKAGE__->new( root => 'files' )->to_app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment