ASP4x-Router

 view release on metacpan or  search on metacpan

lib/ASP4x/Router.pm  view on Meta::CPAN

  $r->pnotes( route => $router->route_for( $r->uri, $r->method ) );
  
  my $fullpath = $r->document_root . $r->uri;
  if( $fullpath =~ m{/$} && -f $fullpath . 'index.asp' )
  {
    $r->uri( $r->uri . 'index.asp' );
    return -1;
  }
  elsif( -f $fullpath )
  {
    return -1;
  }
  elsif( $r->uri =~ m{^/handlers/} )
  {
    (my $handler_path = $r->uri) =~ s{\.}{/}g;
    $handler_path = $Config->web->application_root . "$handler_path.pm";
    if( -f $handler_path )
    {
      return -1;
    }# end if()
  }# end if()
  
  return $res unless $router;
  
  my @matches = $router->match( $r->uri . ( $r->args ? '?' . $r->args : '' ), $r->method )
    or return -1;
  
  # TODO: Check matches to see if maybe they point to another route not on disk:
  my ($new_uri) = grep {
    my ($path) = split /\?/, $_;
    if( m{^/handlers/} )
    {
      $path =~ s/\./\//g;
      $path .= ".pm";
      if( -f $Config->web->application_root . $path )
      {
        1;
      }# end if()
    }
    else
    {
      -f ($r->document_root . $path);
    }# end if()
  } @matches
    or return -1;
  
  # Require a trailing '/' on the end of the URI:
  unless( $r->uri =~ m{\.[^/]+$} || $r->uri =~ m{/$} )
  {
    my $loc = $r->uri . '/';
    if( $r->args )
    {
      $loc .= "?" . $r->args;
    }# end if()
    $r->status( 301 );
    $r->err_headers_out->add( Location => $loc );
    return 301;
  }# end unless()

  my ($uri, $args) = split /\?/, $new_uri;
  my @args = split /&/, $args if defined($args) && length($args);
  $r->args( join '&', @args );
  $ENV{QUERY_STRING} = $r->args;
  $r->uri( $uri );
  
  return -1;
}# end handler()


sub run
{
  my ($s, $context) = @_;
  
  if( my $route = $context->r->pnotes('route') )
  {
    $Stash->{route} = $route;
  }# end if()
  return $Response->Declined if $context->r->pnotes('__routed');
  
  if( my $app = eval { $Config->app } )
  {
    map {
      $Config->load_class( $_ ); 
      $_->import
    } @$app;
  }# end if()
  
  my $router = $s->get_router()
    or return $Response->Declined;
  
  my ($uri) = split /\?/, $ENV{REQUEST_URI};
  my $route = $router->route_for( $uri, $ENV{REQUEST_METHOD} );
  $Stash->{route} = $route;
  
  my $r = $context->r;
  my $path = $r->document_root . $r->uri;
  if( $path =~ m{/$} && -f $path . 'index.asp' )
  {
    return $Response->Declined;
  }
  elsif( -f $path )
  {
    return $Response->Declined;
  }
  elsif( $r->uri =~ m{^/handlers/} )
  {
    # Check to see if there is a handler on-disk that matches the uri:
    (my $handler_path = $r->uri) =~ s{\.}{/}g;
    $handler_path = $Config->web->application_root . "$handler_path.pm";
    if( -f $handler_path )
    {
      return $Response->Declined;
    }# end if()
  }# end if()

  # Try routing:
  if( my @matches = $router->match( $ENV{REQUEST_URI}, $ENV{REQUEST_METHOD} ) )
  {
    # TODO: Check matches to see if maybe they point to another route not on disk:
    my ($new_uri) = grep {
      my ($path) = split /\?/, $_;



( run in 0.748 second using v1.01-cache-2.11-cpan-140bd7fdf52 )