1 /* Copyright (C) 2010 Red Hat, Inc.
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 static const char xml_root_header
[] = "\
24 <api service=\"%s\" version=\"%s\">\
27 static const char xml_root_entry
[] = "\
29 <link rel=\"%s\" href=\"http://%s/\%s\"/>\
32 static const char xml_root_footer
[] = "\
37 static const char xml_prov_header
[] = "\
41 static const char xml_prov_entry
[] = "\
43 <provider name=\"%s\">\n\
47 <username>%s</username>\n\
48 <password>%s</password>\n\
52 static const char xml_prov_footer
[] = "\
57 static const char xml_list_header
[] = "\
61 static const char xml_list_entry
[] = "\
64 <bucket>%s</bucket>\n\
69 static const char xml_list_footer
[] = "\
74 static const char xml_obj_header
[] = "\
76 <object_body path=\"http://%s/%s/%s/body\"/>\n\
77 <object_attr_list path=\"http://%s/%s/%s/attrs\"/>\
80 static const char xml_obj_entry
[] = "\
82 <object_attr name=\"%s\" path=\"http:/%s/%s/%s/attr_%s\"\
85 static const char xml_obj_footer
[] = "\
90 static const tmpl_format_t xml_format
= {
91 .root_header
= xml_root_header
,
92 .root_entry
= xml_root_entry
,
93 .root_footer
= xml_root_footer
,
94 .prov_header
= xml_prov_header
,
95 .prov_entry
= xml_prov_entry
,
96 .prov_footer
= xml_prov_footer
,
97 .list_header
= xml_list_header
,
98 .list_entry
= xml_list_entry
,
99 .list_footer
= xml_list_footer
,
100 .obj_header
= xml_obj_header
,
101 .obj_entry
= xml_obj_entry
,
102 .obj_footer
= xml_obj_footer
,
106 static const char json_root_header
[] = "\
108 \"service\": \"%s\",\n\
109 \"version\": \"%s\",\n\
113 static const char json_root_entry
[] = "\
117 \"link\": \"http://%s/%s\"\n\
121 static const char json_root_footer
[] = "\
127 static const char json_prov_header
[] = "\
131 static const char json_prov_entry
[] = "\
138 \"username\": \"%s\",\n\
139 \"password\": \"%s\"\n\
143 static const char json_prov_footer
[] = "\
148 static const char json_list_header
[] = "\
152 static const char json_list_entry
[] = "\
155 \"bucket\": \"%s\",\n\
160 static const char json_list_footer
[] = "\
165 static char json_obj_header
[] = "\
167 \"object_body\": \"http://%s/%s/%s/body\"\n\
168 \"object_attr_list\": \"http://%s/%s/%s/attrs\"\
171 static char json_obj_entry
[] = "\
173 \"attr_%s\": \"http://%s/%s/%s/attr_%s\"\
176 static char json_obj_footer
[] = "\
181 static const tmpl_format_t json_format
= {
182 .root_header
= json_root_header
,
183 .root_entry
= json_root_entry
,
184 .root_footer
= json_root_footer
,
185 .prov_header
= json_prov_header
,
186 .prov_entry
= json_prov_entry
,
187 .prov_footer
= json_prov_footer
,
188 .list_header
= json_list_header
,
189 .list_entry
= json_list_entry
,
190 .list_footer
= json_list_footer
,
191 .obj_header
= json_obj_header
,
192 .obj_entry
= json_obj_entry
,
193 .obj_footer
= json_obj_footer
,
198 tmpl_get_ctx (const char *type
)
202 tmp
= malloc(sizeof(*tmp
));
204 if (type
&& strstr(type
,"/json")) {
205 tmp
->format
= &json_format
;
208 tmp
->format
= &xml_format
;
216 tmpl_root_header (tmpl_ctx_t
*ctx
, const char *name
, const char *version
)
219 const tmpl_format_t
*fmt
= ctx
->format
;
221 size
= snprintf(ctx
->raw_buf
,TMPL_BUF_SIZE
,fmt
->root_header
,
223 if (size
>= TMPL_BUF_SIZE
) {
226 ctx
->buf
= ctx
->raw_buf
;
232 tmpl_root_entry (tmpl_ctx_t
*ctx
, const char *rel
, const char *link
)
235 const tmpl_format_t
*fmt
= ctx
->format
;
237 size
= snprintf(ctx
->raw_buf
,TMPL_BUF_SIZE
,fmt
->root_entry
,
238 rel
, ctx
->base
, link
);
239 if (size
>= TMPL_BUF_SIZE
) {
242 ctx
->buf
= ctx
->raw_buf
;
244 if (size
&& (ctx
->index
== 0)) {
245 ctx
->buf
+= fmt
->z_offset
;
246 size
-= fmt
->z_offset
;
254 tmpl_root_footer (tmpl_ctx_t
*ctx
)
256 ctx
->buf
= ctx
->format
->root_footer
;
257 return strlen(ctx
->buf
);
261 tmpl_prov_header (tmpl_ctx_t
*ctx
)
263 ctx
->buf
= ctx
->format
->prov_header
;
264 return strlen(ctx
->buf
);
268 tmpl_prov_entry (tmpl_ctx_t
*ctx
,
269 const char *name
, const char *type
,
270 const char *host
, int port
,
271 const char *user
, const char *pass
)
274 const tmpl_format_t
*fmt
= ctx
->format
;
276 if (!name
) name
= "";
277 if (!type
) type
= "";
278 if (!host
) host
= "";
279 if (!user
) user
= "";
280 if (!pass
) pass
= "";
282 size
= snprintf(ctx
->raw_buf
,TMPL_BUF_SIZE
,fmt
->prov_entry
,
283 name
, type
, host
, port
, user
, pass
);
284 if (size
>= TMPL_BUF_SIZE
) {
287 ctx
->buf
= ctx
->raw_buf
;
289 if (size
&& (ctx
->index
== 0)) {
290 ctx
->buf
+= fmt
->z_offset
;
291 size
-= fmt
->z_offset
;
299 tmpl_prov_footer (tmpl_ctx_t
*ctx
)
301 ctx
->buf
= ctx
->format
->prov_footer
;
302 return strlen(ctx
->buf
);
306 tmpl_list_header (tmpl_ctx_t
*ctx
)
308 ctx
->buf
= ctx
->format
->list_header
;
309 return strlen(ctx
->buf
);
313 tmpl_list_entry (tmpl_ctx_t
*ctx
, const char *bucket
, const char *key
)
316 const tmpl_format_t
*fmt
= ctx
->format
;
318 size
= snprintf(ctx
->raw_buf
,TMPL_BUF_SIZE
,fmt
->list_entry
,bucket
,key
);
319 if (size
>= TMPL_BUF_SIZE
) {
322 ctx
->buf
= ctx
->raw_buf
;
324 if (size
&& (ctx
->index
== 0)) {
325 ctx
->buf
+= fmt
->z_offset
;
326 size
-= fmt
->z_offset
;
334 tmpl_list_footer (tmpl_ctx_t
*ctx
)
336 ctx
->buf
= ctx
->format
->list_footer
;
337 return strlen(ctx
->buf
);
341 tmpl_obj_header (tmpl_ctx_t
*ctx
, const char *bucket
, const char *key
)
344 const tmpl_format_t
*fmt
= ctx
->format
;
346 size
= snprintf(ctx
->raw_buf
,TMPL_BUF_SIZE
,fmt
->obj_header
,
347 ctx
->base
, bucket
, key
, /* once for the body... */
348 ctx
->base
, bucket
, key
); /* ...and once for the attrs */
349 if (size
>= TMPL_BUF_SIZE
) {
352 ctx
->buf
= ctx
->raw_buf
;
359 tmpl_obj_entry (tmpl_ctx_t
*ctx
, const char *bucket
, const char *key
,
363 const tmpl_format_t
*fmt
= ctx
->format
;
365 size
= snprintf(ctx
->raw_buf
,TMPL_BUF_SIZE
,fmt
->obj_entry
,
366 attr
, ctx
->base
, bucket
, key
, attr
);
367 if (size
>= TMPL_BUF_SIZE
) {
370 ctx
->buf
= ctx
->raw_buf
;
372 if (size
&& (ctx
->index
== 0)) {
373 ctx
->buf
+= fmt
->z_offset
;
374 size
-= fmt
->z_offset
;
382 tmpl_obj_footer (tmpl_ctx_t
*ctx
)
384 ctx
->buf
= ctx
->format
->obj_footer
;
385 return strlen(ctx
->buf
);