From c71543759796ca7eb5a91fb124b56d9fb05ca286 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 14 Jan 2017 10:25:10 +0100 Subject: [PATCH] rework formatter registration Do the whole thing inside PVE/APIServer/Formatter.pm --- PVE/APIServer/AnyEvent.pm | 8 +---- PVE/APIServer/Formatter.pm | 61 ++++++++++++++++++++++----------- PVE/APIServer/Formatter/HTML.pm | 10 +++--- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/PVE/APIServer/AnyEvent.pm b/PVE/APIServer/AnyEvent.pm index d818e61..31380ab 100755 --- a/PVE/APIServer/AnyEvent.pm +++ b/PVE/APIServer/AnyEvent.pm @@ -657,7 +657,7 @@ sub handle_api2_request { my ($rel_uri, $format) = &$split_abs_uri($path, $self->{base_uri}); - my $formatter = PVE::APIServer::Formatter::get_formatter($format); + my $formatter = PVE::APIServer::Formatter::get_formatter($format, $method, $rel_uri); if (!defined($formatter)) { $self->error($reqstate, HTTP_NOT_IMPLEMENTED, "no such uri $rel_uri, $format"); @@ -738,12 +738,6 @@ sub handle_api2_request { $delay = 0 if $delay < 0; } - if ($res->{info} && $res->{info}->{formatter}) { - if (defined(my $func = $res->{info}->{formatter}->{$format})) { - $formatter = $func; - } - } - my ($raw, $ct, $nocomp) = &$formatter($res, $res->{data}, $params, $path, $auth); my $resp; diff --git a/PVE/APIServer/Formatter.pm b/PVE/APIServer/Formatter.pm index 29f1898..fa26756 100644 --- a/PVE/APIServer/Formatter.pm +++ b/PVE/APIServer/Formatter.pm @@ -3,53 +3,74 @@ package PVE::APIServer::Formatter; use strict; use warnings; +use URI::Escape; + # generic formatter support # PVE::APIServer::Formatter::* classes should register themselves here my $formatter_hash = {}; +my $page_formatter_hash = {}; sub register_formatter { - my ($format, $func) = @_; + my ($format, $code) = @_; + + die "formatter '$format' already defined" + if defined($formatter_hash->{$format}); + + $formatter_hash->{$format} = $code; +} + +sub register_page_formatter { + my (%config) = @_; + + my $format = $config{format} || + die "missing format"; - die "formatter '$format' already defined" if $formatter_hash->{$format}; + my $path = $config{path} || + die "missing path"; - $formatter_hash->{$format} = { - func => $func, - }; + my $method = $config{method} || + die "missing method"; + + my $code = $config{code} || + die "missing formatter code"; + + die "duplicate page formatter for '$method: $path'" + if defined($page_formatter_hash->{$format}->{$method}->{$path}); + + $page_formatter_hash->{$format}->{$method}->{$path} = $code; } sub get_formatter { - my ($format) = @_; + my ($format, $method, $path) = @_; - return undef if !$format; + return undef if !defined($format); - my $info = $formatter_hash->{$format}; - return undef if !$info; + if (defined($method) && defined($path)) { + my $code = $page_formatter_hash->{$format}->{$method}->{$path}; + return $code if defined($code); + } - return $info->{func}; + return $formatter_hash->{$format}; } my $login_formatter_hash = {}; sub register_login_formatter { - my ($format, $func) = @_; + my ($format, $code) = @_; - die "login formatter '$format' already defined" if $login_formatter_hash->{$format}; + die "login formatter '$format' already defined" + if defined($login_formatter_hash->{$format}); - $login_formatter_hash->{$format} = { - func => $func, - }; + $login_formatter_hash->{$format} = $code; } sub get_login_formatter { my ($format) = @_; - return undef if !$format; - - my $info = $login_formatter_hash->{$format}; - return undef if !$info; + return undef if !defined($format); - return $info->{func}; + return $login_formatter_hash->{$format}; } # some helper functions diff --git a/PVE/APIServer/Formatter/HTML.pm b/PVE/APIServer/Formatter/HTML.pm index a27c88a..7979526 100644 --- a/PVE/APIServer/Formatter/HTML.pm +++ b/PVE/APIServer/Formatter/HTML.pm @@ -170,11 +170,11 @@ PVE::APIServer::Formatter::register_formatter($portal_format, sub { my ($res, $data, $param, $path, $auth) = @_; # fixme: clumsy! - PVE::API2::Formatter::Standard::prepare_response_data($portal_format, $res); + PVE::APIServer::Formatter::Standard::prepare_response_data($portal_format, $res); $data = $res->{data}; my $html = ''; - my $doc = PVE::API2::Formatter::Bootstrap->new($res, $path); + my $doc = PVE::APIServer::Formatter::Bootstrap->new($res, $path); if (!HTTP::Status::is_success($res->{status})) { $html .= $doc->alert(text => "Error $res->{status}: $res->{message}"); @@ -243,14 +243,14 @@ PVE::APIServer::Formatter::register_formatter($portal_format, sub { return ($raw, $portal_ct); }); -PVE::RESTHandler->register_page_formatter( +PVE::APIServer::Formatter::register_page_formatter( 'format' => $portal_format, method => 'GET', path => "/access/ticket", code => sub { my ($res, $data, $param, $path, $auth) = @_; - my $doc = PVE::API2::Formatter::Bootstrap->new($res, $path); + my $doc = PVE::APIServer::Formatter::Bootstrap->new($res, $path); my $html = &$login_form($doc); @@ -258,7 +258,7 @@ PVE::RESTHandler->register_page_formatter( return ($raw, $portal_ct); }); -PVE::RESTHandler->register_page_formatter( +PVE::APIServer::Formatter::register_page_formatter( 'format' => $portal_format, method => 'POST', path => "/access/ticket", -- 2.39.2