« blib/lib/Lemonldap/NG/Common/Conf/RESTServer.pm
↑ Summary
blib/lib/Lemonldap/NG/Common/OpenIDConnect/Metadata.pm »
| 1 | package Lemonldap::NG::Common::PSGI::Router; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use Lemonldap::NG::Common::PSGI; | ||
| 6 | use Lemonldap::NG::Common::PSGI::Constants; | ||
| 7 | |||
| 8 | our $VERSION = '2.23.0'; | ||
| 9 | |||
| 10 | extends 'Lemonldap::NG::Common::PSGI'; | ||
| 11 | |||
| 12 | # Properties | ||
| 13 | has routes => ( | ||
| 14 | is => 'rw', | ||
| 15 | isa => 'HashRef', | ||
| 16 | default => | ||
| 17 | sub { { GET => {}, POST => {}, PUT => {}, PATCH => {}, DELETE => {} } } | ||
| 18 | ); | ||
| 19 | has defaultRoute => ( is => 'rw', default => 'index.html' ); | ||
| 20 | |||
| 21 | # Routes initialization | ||
| 22 | |||
| 23 | sub addRoute { | ||
| 24 | my ( $self, $word, $dest, $methods, $transform ) = (@_); | ||
| 25 | $methods ||= [qw(GET POST PUT PATCH DELETE)]; | ||
| 26 | foreach my $method (@$methods) { | ||
| 27 | $self->logger->debug("Add $method route:"); | ||
| 28 | $self->genRoute( $self->routes->{$method}, $word, $dest, $transform ); | ||
| 29 | } | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub genRoute { | ||
| 34 | my ( $self, $routes, $word, $dest, $transform ) = @_; | ||
| 35 | unless ( ref $routes eq 'HASH' ) { | ||
| 36 | $self->logger->error( | ||
| 37 | 'Conflict detected between 2 extensions, aborting 1 route'); | ||
| 38 | return; | ||
| 39 | } | ||
| 40 | if ( ref $word eq 'ARRAY' ) { | ||
| 41 | foreach my $w (@$word) { | ||
| 42 | $self->genRoute( $routes, $w, $dest, $transform ); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | $dest //= $word; | ||
| 47 | if ( $word =~ /^:(.*)$/ ) { | ||
| 48 | $routes->{'#'} = $1; | ||
| 49 | die "Target required for $word" unless ($dest); | ||
| 50 | $word = ':'; | ||
| 51 | } | ||
| 52 | elsif ( my ( $first, $rest ) = $word =~ m#^(.*?)/(.*)# ) { | ||
| 53 | return $self->genRoute( $routes->{$first}, | ||
| 54 | $rest, $dest, $transform ); | ||
| 55 | } | ||
| 56 | else { | ||
| 57 | $dest ||= $word; | ||
| 58 | } | ||
| 59 | if ( $word =~ /^(.+)\.html$/ and $word eq $dest ) { | ||
| 60 | my $tpl = $1 or die; | ||
| 61 | $self->logger->debug("route $dest will use $tpl"); | ||
| 62 | $routes->{$word} = sub { $self->sendHtml( $_[1], $tpl ) }; | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | if ( $transform and ( not ref($dest) or ref($dest) eq 'CODE' ) ) { | ||
| 66 | $dest = $transform->($dest); | ||
| 67 | } | ||
| 68 | if ( my $t = ref $dest ) { | ||
| 69 | if ( $t eq 'HASH' ) { | ||
| 70 | $routes->{$word} ||= {}; | ||
| 71 | foreach my $w ( keys %$dest ) { | ||
| 72 | $self->genRoute( $routes->{$word}, $w, $dest->{$w}, | ||
| 73 | $transform ); | ||
| 74 | } | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | elsif ( $t eq 'ARRAY' ) { | ||
| 78 | $routes->{$word} ||= {}; | ||
| 79 | foreach my $w ( @{$dest} ) { | ||
| 80 | $self->genRoute( $routes->{$word}, $w, $w, $transform ); | ||
| 81 | } | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | if ( $routes->{$word} ) { | ||
| 86 | eval { $self->logger->warn(qq'Route "$word" redefined'); }; | ||
| 87 | } | ||
| 88 | if ( my $t = ref $dest ) { | ||
| 89 | if ( $t eq 'CODE' ) { | ||
| 90 | $routes->{$word} = $dest; | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | die "Type $t unauthorized in routes"; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | elsif ( $self->can($dest) ) { | ||
| 97 | $routes->{$word} = sub { shift; $self->$dest(@_) }; | ||
| 98 | } | ||
| 99 | else { | ||
| 100 | die "$dest() isn't a method"; | ||
| 101 | } | ||
| 102 | $self->logger->debug("route $word added"); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | sub handlerAbort { | ||
| 107 | my ( $self, $path, $msg ) = @_; | ||
| 108 | delete $self->routes->{$path}; | ||
| 109 | $self->addRoute( | ||
| 110 | $path => sub { | ||
| 111 | my ( $self, $req ) = @_; | ||
| 112 | return $self->sendError( $req, $msg, 500 ); | ||
| 113 | } | ||
| 114 | ); | ||
| 115 | } | ||
| 116 | |||
| 117 | # Methods that dispatch requests | ||
| 118 | |||
| 119 | sub handler { | ||
| 120 | my ( $self, $req ) = @_; | ||
| 121 | |||
| 122 | # Reinitialize configuration message | ||
| 123 | $Lemonldap::NG::Common::Conf::msg = ''; | ||
| 124 | |||
| 125 | # Launch reqInit() if exists | ||
| 126 | if ( $self->can('reqInit') ) { | ||
| 127 | $self->reqInit($req); | ||
| 128 | } | ||
| 129 | |||
| 130 | # Only words are taken in path | ||
| 131 | my $last = 0; | ||
| 132 | my @path = grep { | ||
| 133 | $last = 1 if ( $_ =~ /[^\.\w]/ ); | ||
| 134 | ( $last or /^$/ ? 0 : 1 ); | ||
| 135 | } split /\//, $req->path(); | ||
| 136 | $self->logger->debug( "Start routing " . ( $path[0] // 'default route' ) ); | ||
| 137 | |||
| 138 | if ( !@path and $self->defaultRoute ) { | ||
| 139 | @path = ( $self->defaultRoute ); | ||
| 140 | } | ||
| 141 | my $head = 0; | ||
| 142 | my $method = $req->method; | ||
| 143 | if ( $method eq 'HEAD' ) { | ||
| 144 | $head = 1; | ||
| 145 | $method = 'GET'; | ||
| 146 | } | ||
| 147 | my $res = $self->followPath( $req, $self->routes->{$method}, \@path ); | ||
| 148 | if ( $res and $head ) { $res->[2] = [] } | ||
| 149 | return $res ? $res : $self->sendError( $req, 'Bad request', 400 ); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub followPath { | ||
| 153 | my ( $self, $req, $routes, $path ) = @_; | ||
| 154 | if ( $path->[0] and defined $routes->{ $path->[0] } ) { | ||
| 155 | my $w = shift @$path; | ||
| 156 | if ( ref( $routes->{$w} ) eq 'CODE' ) { | ||
| 157 | return $routes->{$w}->( $self, $req, @$path ); | ||
| 158 | } | ||
| 159 | my $res = $self->followPath( $req, $routes->{$w}, $path ); | ||
| 160 | return $res if ($res); | ||
| 161 | unshift @$path, $w; | ||
| 162 | } | ||
| 163 | if ( $routes->{':'} ) { | ||
| 164 | my $v = shift @$path; | ||
| 165 | $req->set_param( $routes->{'#'}, $v ); | ||
| 166 | if ( ref( $routes->{':'} ) eq 'CODE' ) { | ||
| 167 | return $routes->{':'}->( $self, $req, @$path ); | ||
| 168 | } | ||
| 169 | return $self->followPath( $req, $routes->{':'}, $path ); | ||
| 170 | } | ||
| 171 | elsif ( my $sub = $routes->{'*'} ) { | ||
| 172 | return $self->$sub( $req, @$path ); | ||
| 173 | } | ||
| 174 | else { | ||
| 175 | return undef; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | 1; | ||
| 180 | __END__ |