]> git.proxmox.com Git - pve-http-server.git/blame - src/PVE/APIServer/Formatter/Standard.pm
proxy request: assert that API url starts with a slash
[pve-http-server.git] / src / PVE / APIServer / Formatter / Standard.pm
CommitLineData
63307beb
DM
1package PVE::APIServer::Formatter::Standard;
2
3use strict;
4use warnings;
5
6use PVE::APIServer::Formatter;
7use HTTP::Status;
8use JSON;
9use HTML::Entities;
10use PVE::JSONSchema;
11
12# register result formatters
13
14sub prepare_response_data {
15 my ($format, $res) = @_;
16
17 my $success = 1;
18 my $new = {
19 data => $res->{data},
20 };
21 if (scalar(keys %{$res->{errors}})) {
22 $success = 0;
23 $new->{errors} = $res->{errors};
24 }
25
26 if ($format eq 'extjs' || $format eq 'htmljs') {
27 # HACK: extjs wants 'success' property instead of useful HTTP status codes
28 if (HTTP::Status::is_error($res->{status})) {
29 $success = 0;
30 $new->{message} = $res->{message} || status_message($res->{status});
31 $new->{status} = $res->{status} || 200;
32 $res->{message} = undef;
33 $res->{status} = 200;
34 }
35 $new->{success} = $success;
36 }
37
38 if ($success && $res->{total}) {
39 $new->{total} = $res->{total};
40 }
41
42 if ($success && $res->{changes}) {
43 $new->{changes} = $res->{changes};
44 }
45
46 $res->{data} = $new;
47}
48
49PVE::APIServer::Formatter::register_formatter('json', sub {
ca304f91 50 my ($res, $data, $param, $path, $auth, $config) = @_;
63307beb
DM
51
52 my $nocomp = 0;
53
54 my $ct = 'application/json;charset=UTF-8';
55
56 prepare_response_data('json', $res);
57
58 my $raw = to_json($res->{data}, {utf8 => 1, allow_nonref => 1});
59
60 return ($raw, $ct, $nocomp);
61});
62
63
64PVE::APIServer::Formatter::register_formatter('extjs', sub {
ca304f91 65 my ($res, $data, $param, $path, $auth, $config) = @_;
63307beb
DM
66
67 my $nocomp = 0;
68
69 my $ct = 'application/json;charset=UTF-8';
70
71 prepare_response_data('extjs', $res);
72
73 my $raw = to_json($res->{data}, {utf8 => 1, allow_nonref => 1});
74
75 return ($raw, $ct, $nocomp);
76});
77
78PVE::APIServer::Formatter::register_formatter('htmljs', sub {
ca304f91 79 my ($res, $data, $param, $path, $auth, $config) = @_;
63307beb
DM
80
81 my $nocomp = 0;
82
83 # we use this for extjs file upload forms
84
85 my $ct = 'text/html;charset=UTF-8';
86
87 prepare_response_data('htmljs', $res);
88
89 my $raw = encode_entities(to_json($res->{data}, {allow_nonref => 1}));
90
91 return ($raw, $ct, $nocomp);
92});
93
94
95PVE::APIServer::Formatter::register_formatter('spiceconfig', sub {
ca304f91 96 my ($res, $data, $param, $path, $auth, $config) = @_;
63307beb
DM
97
98 my $nocomp = 0;
99
100 my $ct = 'application/x-virt-viewer;charset=UTF-8';
101
102 prepare_response_data('spiceconfig', $res);
103
104 $data = $res->{data};
105
106 my $raw;
107
108 if ($data && ref($data) && ref($data->{data})) {
109 $raw = "[virt-viewer]\n";
110 while (my ($key, $value) = each %{$data->{data}}) {
111 $raw .= "$key=$value\n" if defined($value);
112 }
113 }
114
115 return ($raw, $ct, $nocomp);
116});
117
118PVE::APIServer::Formatter::register_formatter('png', sub {
ca304f91 119 my ($res, $data, $param, $path, $auth, $config) = @_;
63307beb
DM
120
121 my $nocomp = 1;
122
123 my $ct = 'image/png';
124
125 prepare_response_data('png', $res);
126
127 $data = $res->{data};
128
129 # fixme: better to revove that whole png thing ?
130
131 my $filename;
132 my $raw = '';
133
134 if ($data && ref($data) && ref($data->{data}) &&
135 $data->{data}->{filename} && defined($data->{data}->{image})) {
136 $filename = $data->{data}->{filename};
137 $raw = $data->{data}->{image};
138 }
139
140 return ($raw, $ct, $nocomp);
141});