Commit 27d5f5ea authored by Dietmar Maurer's avatar Dietmar Maurer

move formater registration to HTTPServer

parent 23e0c0ba
...@@ -4,7 +4,7 @@ use strict; ...@@ -4,7 +4,7 @@ use strict;
use warnings; use warnings;
use PVE::pvecfg; use PVE::pvecfg;
use PVE::REST; use PVE::HTTPServer;
use PVE::RESTHandler; use PVE::RESTHandler;
use HTTP::Status; use HTTP::Status;
use JSON; use JSON;
...@@ -151,7 +151,7 @@ my $prepare_response_data = sub { ...@@ -151,7 +151,7 @@ my $prepare_response_data = sub {
$res->{data} = $new; $res->{data} = $new;
}; };
PVE::REST::register_formater('json', sub { PVE::HTTPServer::register_formater('json', sub {
my ($res, $data, $param, $path, $auth) = @_; my ($res, $data, $param, $path, $auth) = @_;
my $nocomp = 0; my $nocomp = 0;
...@@ -166,7 +166,7 @@ PVE::REST::register_formater('json', sub { ...@@ -166,7 +166,7 @@ PVE::REST::register_formater('json', sub {
}); });
PVE::REST::register_formater('extjs', sub { PVE::HTTPServer::register_formater('extjs', sub {
my ($res, $data, $param, $path, $auth) = @_; my ($res, $data, $param, $path, $auth) = @_;
my $nocomp = 0; my $nocomp = 0;
...@@ -180,7 +180,7 @@ PVE::REST::register_formater('extjs', sub { ...@@ -180,7 +180,7 @@ PVE::REST::register_formater('extjs', sub {
return ($raw, $ct, $nocomp); return ($raw, $ct, $nocomp);
}); });
PVE::REST::register_formater('htmljs', sub { PVE::HTTPServer::register_formater('htmljs', sub {
my ($res, $data, $param, $path, $auth) = @_; my ($res, $data, $param, $path, $auth) = @_;
my $nocomp = 0; my $nocomp = 0;
...@@ -197,7 +197,7 @@ PVE::REST::register_formater('htmljs', sub { ...@@ -197,7 +197,7 @@ PVE::REST::register_formater('htmljs', sub {
}); });
PVE::REST::register_formater('spiceconfig', sub { PVE::HTTPServer::register_formater('spiceconfig', sub {
my ($res, $data, $param, $path, $auth) = @_; my ($res, $data, $param, $path, $auth) = @_;
my $nocomp = 0; my $nocomp = 0;
...@@ -220,7 +220,7 @@ PVE::REST::register_formater('spiceconfig', sub { ...@@ -220,7 +220,7 @@ PVE::REST::register_formater('spiceconfig', sub {
return ($raw, $ct, $nocomp); return ($raw, $ct, $nocomp);
}); });
PVE::REST::register_formater('png', sub { PVE::HTTPServer::register_formater('png', sub {
my ($res, $data, $param, $path, $auth) = @_; my ($res, $data, $param, $path, $auth) = @_;
my $nocomp = 1; my $nocomp = 1;
...@@ -245,7 +245,7 @@ PVE::REST::register_formater('png', sub { ...@@ -245,7 +245,7 @@ PVE::REST::register_formater('png', sub {
return ($raw, $ct, $nocomp); return ($raw, $ct, $nocomp);
}); });
PVE::REST::register_formater('html', sub { PVE::HTTPServer::register_formater('html', sub {
my ($res, $data, $param, $path, $auth) = @_; my ($res, $data, $param, $path, $auth) = @_;
my $nocomp = 0; my $nocomp = 0;
......
...@@ -55,6 +55,56 @@ sub split_abs_uri { ...@@ -55,6 +55,56 @@ sub split_abs_uri {
return wantarray ? ($rel_uri, $format) : $rel_uri; return wantarray ? ($rel_uri, $format) : $rel_uri;
} }
# generic formater support
my $formater_hash = {};
sub register_formater {
my ($format, $func) = @_;
die "formater '$format' already defined" if $formater_hash->{$format};
$formater_hash->{$format} = {
func => $func,
};
}
sub get_formater {
my ($format) = @_;
return undef if !$format;
my $info = $formater_hash->{$format};
return undef if !$info;
return $info->{func};
}
my $login_formater_hash = {};
sub register_login_formater {
my ($format, $func) = @_;
die "login formater '$format' already defined" if $login_formater_hash->{$format};
$login_formater_hash->{$format} = {
func => $func,
};
}
sub get_login_formater {
my ($format) = @_;
return undef if !$format;
my $info = $login_formater_hash->{$format};
return undef if !$info;
return $info->{func};
}
# server implementation
sub log_request { sub log_request {
my ($self, $reqstate) = @_; my ($self, $reqstate) = @_;
...@@ -446,7 +496,7 @@ sub handle_api2_request { ...@@ -446,7 +496,7 @@ sub handle_api2_request {
my ($rel_uri, $format) = split_abs_uri($path); my ($rel_uri, $format) = split_abs_uri($path);
my $formater = PVE::REST::get_formater($format); my $formater = get_formater($format);
if (!defined($formater)) { if (!defined($formater)) {
$self->error($reqstate, HTTP_NOT_IMPLEMENTED, "no such uri $rel_uri, $format"); $self->error($reqstate, HTTP_NOT_IMPLEMENTED, "no such uri $rel_uri, $format");
...@@ -964,7 +1014,7 @@ sub unshift_read_header { ...@@ -964,7 +1014,7 @@ sub unshift_read_header {
if (my $err = $@) { if (my $err = $@) {
# always delay unauthorized calls by 3 seconds # always delay unauthorized calls by 3 seconds
my $delay = 3; my $delay = 3;
if (my $formater = PVE::REST::get_login_formater($format)) { if (my $formater = get_login_formater($format)) {
my ($raw, $ct, $nocomp) = &$formater($path, $auth); my ($raw, $ct, $nocomp) = &$formater($path, $auth);
my $resp; my $resp;
if (ref($raw) && (ref($raw) eq 'HTTP::Response')) { if (ref($raw) && (ref($raw) eq 'HTTP::Response')) {
......
...@@ -199,83 +199,4 @@ sub rest_handler { ...@@ -199,83 +199,4 @@ sub rest_handler {
return $resp; return $resp;
} }
# generic formater support
my $formater_hash = {};
sub register_formater {
my ($format, $func) = @_;
die "formater '$format' already defined" if $formater_hash->{$format};
$formater_hash->{$format} = {
func => $func,
};
}
sub get_formater {
my ($format) = @_;
return undef if !$format;
my $info = $formater_hash->{$format};
return undef if !$info;
return $info->{func};
}
my $login_formater_hash = {};
sub register_login_formater {
my ($format, $func) = @_;
die "login formater '$format' already defined" if $login_formater_hash->{$format};
$login_formater_hash->{$format} = {
func => $func,
};
}
sub get_login_formater {
my ($format) = @_;
return undef if !$format;
my $info = $login_formater_hash->{$format};
return undef if !$info;
return $info->{func};
}
sub register_page_formater {
my (%config) = @_;
my $base_handler_class = $config{base_handler_class} ||
die "missing base_handler_class";
my $format = $config{format} ||
die "missing format";
die "format '$format' is not registered"
if !$formater_hash->{$format};
my $path = $config{path} ||
die "missing path";
my $method = $config{method} ||
die "missing method";
my $code = $config{code} ||
die "missing formater code";
my $uri_param = {};
my ($handler, $info) = $base_handler_class->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};
$info->{formater}->{$format} = $code;
}
1; 1;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment