]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/RESTHandler.pm
RESTHandler: define empty DESTROY to avoid useless warings
[pve-common.git] / data / PVE / RESTHandler.pm
index e1fb823ac9279cafb4dcef767b04b677a4ea6b7a..41531925fe1ab7cc0be9f190494ec28e00739598 100644 (file)
@@ -218,6 +218,11 @@ sub register_method {
     die "$errprefix duplicate method definition\n" 
        if defined($path_lookup->{$method});
 
+    if ($method eq 'SUBCLASS') {
+       foreach my $m (qw(GET PUT POST DELETE)) {
+           die "$errprefix duplicate method definition SUBCLASS and $m\n" if $path_lookup->{$m};
+       }
+    }
     $path_lookup->{$method} = $info;
 
     $info->{match_re} = $match_re;
@@ -235,7 +240,7 @@ sub register_method {
     push @{$method_registry->{$self}}, $info;
 }
 
-sub register_page_formater {
+sub register_page_formatter {
     my ($self, %config) = @_;
 
     my $format = $config{format} ||
@@ -248,18 +253,20 @@ sub register_page_formater {
        die "missing method";
        
     my $code = $config{code} ||
-       die "missing formater code";
+       die "missing formatter code";
     
     my $uri_param = {};
     my ($handler, $info) = $self->find_handler($method, $path, $uri_param);
     die "unabe to find handler for '$method: $path'" if !($handler && $info);
 
-    die "duplicate formater for '$method: $path'" 
-       if $info->{formater} && $info->{formater}->{$format};
+    die "duplicate formatter for '$method: $path'" 
+       if $info->{formatter} && $info->{formatter}->{$format};
 
-    $info->{formater}->{$format} = $code;
+    $info->{formatter}->{$format} = $code;
 }
 
+sub DESTROY {}; # avoid problems with autoload
+
 sub AUTOLOAD {
     my ($this) = @_;
 
@@ -295,10 +302,17 @@ sub map_method_by_name {
 }
 
 sub map_path_to_methods {
-    my ($class, $stack, $uri_param) = @_;
+    my ($class, $stack, $uri_param, $pathmatchref) = @_;
 
     my $path_lookup = $method_path_lookup->{$class};
 
+    # Note: $pathmatchref can be used to obtain path including
+    # uri patterns like '/cluster/firewall/groups/{group}'.
+    # Used by pvesh to display help
+    if (defined($pathmatchref)) {
+       $$pathmatchref = '' if !$$pathmatchref;
+    }
+
     while (defined(my $comp = shift @$stack)) {
        return undef if !$path_lookup; # not registerd?
        if ($path_lookup->{regex}) {
@@ -308,8 +322,10 @@ sub map_path_to_methods {
            return undef if $comp !~ m/^($regex)$/;
            $uri_param->{$name} = $1;
            $path_lookup = $path_lookup->{regex};
+           $$pathmatchref .= '/{' . $name . '}' if defined($pathmatchref);
        } elsif ($path_lookup->{folders}) {
            $path_lookup = $path_lookup->{folders}->{$comp};
+           $$pathmatchref .= '/' . $comp if defined($pathmatchref);
        } else {
            die "internal error";
        }
@@ -338,13 +354,13 @@ sub map_path_to_methods {
 }
 
 sub find_handler {
-    my ($class, $method, $path, $uri_param) = @_;
+    my ($class, $method, $path, $uri_param, $pathmatchref) = @_;
 
     my $stack = [ grep { length($_) > 0 }  split('\/+' , $path)]; # skip empty fragments
 
     my ($handler_class, $path_info);
     eval {
-       ($handler_class, $path_info) = $class->map_path_to_methods($stack, $uri_param);
+       ($handler_class, $path_info) = $class->map_path_to_methods($stack, $uri_param, $pathmatchref);
     };
     my $err = $@;
     syslog('err', $err) if $err;