Skip to content

Instantly share code, notes, and snippets.

@cho45
Created January 27, 2011 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cho45/798418 to your computer and use it in GitHub Desktop.
Save cho45/798418 to your computer and use it in GitHub Desktop.
#!/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 (-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