Skip to content

Instantly share code, notes, and snippets.

@ap
Created January 23, 2015 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ap/a63575c3127a6d4e963c to your computer and use it in GitHub Desktop.
Save ap/a63575c3127a6d4e963c to your computer and use it in GitHub Desktop.
Plack::App::DynamicURLMap
use strict;
use warnings;
package Plack::App::DynamicURLMap;
use parent 'Plack::App::URLMap';
use Carp ();
use Scalar::Util ();
sub unmount { shift->unmap( @_ ) }
sub unmap {
my $self = shift;
my ( $location ) = @_;
my $host;
if ( $location =~ m!^https?://(.*?)(/.*)! ) {
$host = $1;
$location = $2;
}
Carp::croak 'Paths need to start with /' if $location !~ m!^/!;
$location =~ s!/$!!;
my $mapping = $self->{'_mapping'};
for my $i ( 0 .. $#$mapping ) {
next if $location ne $mapping->[$i][1];
next if defined $host ? $host ne $mapping->[$i][0] : defined $mapping->[$i][0];
splice @$mapping, $i, 1;
return 1;
}
return;
}
sub unmount_app { shift->unmap_app( @_ ) }
sub unmap_app {
my $self = shift;
my ( $app ) = @_;
$app = Scalar::Util::refaddr $app;
my $mapping = $self->{'_mapping'};
@$mapping = grep { $app != Scalar::Util::refaddr $_->[2] } @$mapping;
return;
}
sub have_host {
my $self = shift;
my ( $host ) = @_;
for ( @{ $self->{'_sorted_mapping'} } ) {
return 1 if $host eq lc $_->[0];
}
return;
}
sub commit { shift->prepare_app }
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment