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_obj_header
[] = "\
61 static const char xml_obj_entry
[] = "\
64 <bucket>%s</bucket>\n\
69 static const char xml_obj_footer
[] = "\
75 tmpl_format_t xml_format
= {
76 .root_header
= xml_root_header
,
77 .root_entry
= xml_root_entry
,
78 .root_footer
= xml_root_footer
,
79 .prov_header
= xml_prov_header
,
80 .prov_entry
= xml_prov_entry
,
81 .prov_footer
= xml_prov_footer
,
82 .obj_header
= xml_obj_header
,
83 .obj_entry
= xml_obj_entry
,
84 .obj_footer
= xml_obj_footer
,
88 static const char json_root_header
[] = "\
90 \"service\": \"%s\",\n\
91 \"version\": \"%s\",\n\
95 static const char json_root_entry
[] = "\
99 \"link\": \"http://%s/%s\"\n\
103 static const char json_root_footer
[] = "\
109 static const char json_prov_header
[] = "\
113 static const char json_prov_entry
[] = "\
120 \"username\": \"%s\",\n\
121 \"password\": \"%s\"\n\
125 static const char json_prov_footer
[] = "\
130 static const char json_obj_header
[] = "\
134 static const char json_obj_entry
[] = "\
137 \"bucket\": \"%s\",\n\
142 static const char json_obj_footer
[] = "\
147 tmpl_format_t json_format
= {
148 .root_header
= json_root_header
,
149 .root_entry
= json_root_entry
,
150 .root_footer
= json_root_footer
,
151 .prov_header
= json_prov_header
,
152 .prov_entry
= json_prov_entry
,
153 .prov_footer
= json_prov_footer
,
154 .obj_header
= json_obj_header
,
155 .obj_entry
= json_obj_entry
,
156 .obj_footer
= json_obj_footer
,
161 tmpl_get_ctx (const char *type
)
165 tmp
= malloc(sizeof(*tmp
));
167 if (type
&& strstr(type
,"/json")) {
168 tmp
->format
= &json_format
;
171 tmp
->format
= &xml_format
;
179 tmpl_root_header (tmpl_ctx_t
*ctx
, const char *name
, const char *version
)
182 tmpl_format_t
*fmt
= ctx
->format
;
184 size
= snprintf(ctx
->raw_buf
,TMPL_BUF_SIZE
,fmt
->root_header
,
186 if (size
>= TMPL_BUF_SIZE
) {
189 ctx
->buf
= ctx
->raw_buf
;
195 tmpl_root_entry (tmpl_ctx_t
*ctx
, const char *rel
, const char *link
)
198 tmpl_format_t
*fmt
= ctx
->format
;
200 size
= snprintf(ctx
->raw_buf
,TMPL_BUF_SIZE
,fmt
->root_entry
,
201 rel
, ctx
->base
, link
);
202 if (size
>= TMPL_BUF_SIZE
) {
205 ctx
->buf
= ctx
->raw_buf
;
207 if (size
&& (ctx
->index
== 0)) {
208 ctx
->buf
+= fmt
->z_offset
;
209 size
-= fmt
->z_offset
;
217 tmpl_root_footer (tmpl_ctx_t
*ctx
)
219 ctx
->buf
= ctx
->format
->root_footer
;
220 return strlen(ctx
->buf
);
224 tmpl_prov_header (tmpl_ctx_t
*ctx
)
226 ctx
->buf
= ctx
->format
->prov_header
;
227 return strlen(ctx
->buf
);
231 tmpl_prov_entry (tmpl_ctx_t
*ctx
,
232 const char *name
, const char *type
,
233 const char *host
, int port
,
234 const char *user
, const char *pass
)
237 tmpl_format_t
*fmt
= ctx
->format
;
239 size
= snprintf(ctx
->raw_buf
,TMPL_BUF_SIZE
,fmt
->prov_entry
,
240 name
, type
, host
, port
, user
, pass
);
241 if (size
>= TMPL_BUF_SIZE
) {
244 ctx
->buf
= ctx
->raw_buf
;
246 if (size
&& (ctx
->index
== 0)) {
247 ctx
->buf
+= fmt
->z_offset
;
248 size
-= fmt
->z_offset
;
256 tmpl_prov_footer (tmpl_ctx_t
*ctx
)
258 ctx
->buf
= ctx
->format
->prov_footer
;
259 return strlen(ctx
->buf
);
263 tmpl_obj_header (tmpl_ctx_t
*ctx
)
265 ctx
->buf
= ctx
->format
->obj_header
;
266 return strlen(ctx
->buf
);
270 tmpl_obj_entry (tmpl_ctx_t
*ctx
, const char *bucket
, const char *key
)
273 tmpl_format_t
*fmt
= ctx
->format
;
275 size
= snprintf(ctx
->raw_buf
,TMPL_BUF_SIZE
,fmt
->obj_entry
,bucket
,key
);
276 if (size
>= TMPL_BUF_SIZE
) {
279 ctx
->buf
= ctx
->raw_buf
;
281 if (size
&& (ctx
->index
== 0)) {
282 ctx
->buf
+= fmt
->z_offset
;
283 size
-= fmt
->z_offset
;
291 tmpl_obj_footer (tmpl_ctx_t
*ctx
)
293 ctx
->buf
= ctx
->format
->obj_footer
;
294 return strlen(ctx
->buf
);