diff --git a/src/xml-rpc.c b/src/xml-rpc.c index 31474cb8..5d37285f 100644 --- a/src/xml-rpc.c +++ b/src/xml-rpc.c @@ -21,7 +21,6 @@ #include "mp3-scanner.h" #include "rend.h" #include "webserver.h" -#include "xml-rpc.h" /* typedefs */ typedef struct tag_xmlstack { @@ -32,8 +31,6 @@ typedef struct tag_xmlstack { typedef struct tag_xmlstruct { WS_CONNINFO *pwsc; int stack_level; - int suppress_lead; - int flags; XMLSTACK stack; } XMLSTRUCT; @@ -41,6 +38,12 @@ typedef struct tag_xmlstruct { void xml_get_stats(WS_CONNINFO *pwsc); char *xml_entity_encode(char *original); +XMLSTRUCT *xml_init(WS_CONNINFO *pwsc, int emit_header); +void xml_push(XMLSTRUCT *pxml, char *term); +void xml_pop(XMLSTRUCT *pxml); +void xml_output(XMLSTRUCT *pxml, char *section, char *fmt, ...); +void xml_deinit(XMLSTRUCT *pxml); + /** * create an xml response structure, a helper struct for * building xml responses. @@ -49,7 +52,7 @@ char *xml_entity_encode(char *original); * @param emit_header whether or not to throw out html headers and xml header * @returns XMLSTRUCT on success, or NULL if failure */ -XMLSTRUCT *xml_init(WS_CONNINFO *pwsc, int emit_header, int flags) { +XMLSTRUCT *xml_init(WS_CONNINFO *pwsc, int emit_header) { XMLSTRUCT *pxml; pxml=(XMLSTRUCT*)malloc(sizeof(XMLSTRUCT)); @@ -60,20 +63,13 @@ XMLSTRUCT *xml_init(WS_CONNINFO *pwsc, int emit_header, int flags) { memset(pxml,0,sizeof(XMLSTRUCT)); pxml->pwsc = pwsc; - pxml->flags = flags; if(emit_header) { - if(flags & XML_FLAG_JSON) { - ws_addresponseheader(pwsc,"Content-Type","text/json"); - } else { - ws_addresponseheader(pwsc,"Content-Type","text/xml; charset=utf-8"); - } + ws_addresponseheader(pwsc,"Content-Type","text/xml; charset=utf-8"); ws_writefd(pwsc,"HTTP/1.0 200 OK\r\n"); ws_emitheaders(pwsc); - if(!(flags & XML_FLAG_JSON)) { - ws_writefd(pwsc,""); - } + ws_writefd(pwsc,""); } return pxml; @@ -85,55 +81,17 @@ XMLSTRUCT *xml_init(WS_CONNINFO *pwsc, int emit_header, int flags) { * @param pxml xml struct obtained from xml_init * @param term next xlm section to start */ -void xml_push(XMLSTRUCT *pxml, char *term, int container) { +void xml_push(XMLSTRUCT *pxml, char *term) { XMLSTACK *pstack; - int is_array=0; - int parent_is_array=0; - int suppress_nl=0; - char *parent_term; pstack = (XMLSTACK *)malloc(sizeof(XMLSTACK)); pstack->next=pxml->stack.next; pstack->tag=strdup(term); pxml->stack.next=pstack; - if((strlen(term) > 5)&&(strcasecmp(&term[strlen(term)-5],"array") == 0)) { - is_array=1; - } - - if(pstack->next) { - parent_term = pstack->next->tag; - if((strlen(parent_term) > 5) && - (strcasecmp(&parent_term[strlen(parent_term)-5],"array") == 0)) { - parent_is_array=1; - } - } - - if((pxml->flags & XML_FLAG_READABLE) && (!pxml->suppress_lead)) - ws_writefd(pxml->pwsc,"%*s",pxml->stack_level,""); - - if(pxml->flags & XML_FLAG_JSON) { - if(pxml->suppress_lead) { - ws_writefd(pxml->pwsc,", "); - } - if(!parent_is_array) { - ws_writefd(pxml->pwsc,"\"%s\": ",term); - } else { - suppress_nl=1; - } - if(is_array) { - ws_writefd(pxml->pwsc,"["); - } else if(container) { - ws_writefd(pxml->pwsc,"{"); - } - } else { - ws_writefd(pxml->pwsc,"<%s>",term); - } - - if((container) && (pxml->flags & XML_FLAG_READABLE)&&(!suppress_nl)) - ws_writefd(pxml->pwsc,"\n"); - pxml->stack_level++; + + ws_writefd(pxml->pwsc,"<%s>",term); } /** @@ -141,14 +99,9 @@ void xml_push(XMLSTRUCT *pxml, char *term, int container) { * * @param pxml xml struct we are working with */ -void xml_pop(XMLSTRUCT *pxml, int container) { +void xml_pop(XMLSTRUCT *pxml) { XMLSTACK *pstack; - int is_array=0; - int parent_is_array=0; - char *term; - char *parent_term; - int suppress_nl = 0; pstack=pxml->stack.next; if(!pstack) { DPRINTF(E_LOG,L_XML,"xml_pop: tried to pop an empty stack\n"); @@ -157,43 +110,11 @@ void xml_pop(XMLSTRUCT *pxml, int container) { pxml->stack.next = pstack->next; - pxml->stack_level--; - - term = pstack->tag; - if((strlen(term) > 5)&&(strcasecmp(&term[strlen(term)-5],"array") == 0)) { - is_array=1; - } - - if(pxml->stack.next) { - parent_term = pxml->stack.next->tag; - if((strlen(parent_term) > 5) && - (strcasecmp(&parent_term[strlen(parent_term)-5],"array") == 0)) { - parent_is_array=1; - } - } - - if((container) && (pxml->flags & XML_FLAG_READABLE)) - ws_writefd(pxml->pwsc,"%*s",pxml->stack_level,""); - - if(pxml->flags & XML_FLAG_JSON) { - pxml->suppress_lead=0; - if(is_array) { - ws_writefd(pxml->pwsc,"]"); - } else if(container) { - ws_writefd(pxml->pwsc,"}"); - } else { - suppress_nl = 1; - pxml->suppress_lead=1; - } - } else { - ws_writefd(pxml->pwsc,"%s>",pstack->tag); - } - - if((pxml->flags & XML_FLAG_READABLE)&&(!suppress_nl)) - ws_writefd(pxml->pwsc,"\n"); - + ws_writefd(pxml->pwsc,"%s>",pstack->tag); free(pstack->tag); free(pstack); + + pxml->stack_level--; } /** @@ -208,22 +129,14 @@ void xml_output(XMLSTRUCT *pxml, char *section, char *fmt, ...) { vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); - if(section) { - xml_push(pxml,section,FALSE); - } - output = xml_entity_encode(buf); - if(pxml->flags & XML_FLAG_JSON) { - ws_writefd(pxml->pwsc,"\"%s\"",output); - - } else { - ws_writefd(pxml->pwsc,"%s",output); - } - - free(output); - if(section) { - xml_pop(pxml,FALSE); + xml_push(pxml,section); + } + ws_writefd(pxml->pwsc,"%s",output); + free(output); + if(section) { + xml_pop(pxml); } } @@ -274,7 +187,6 @@ void xml_handle(WS_CONNINFO *pwsc) { */ void xml_get_stats(WS_CONNINFO *pwsc) { int r_secs, r_days, r_hours, r_mins; - int flag=0; char buf[80]; WS_CONNINFO *pci; SCAN_STATUS *pss; @@ -282,20 +194,13 @@ void xml_get_stats(WS_CONNINFO *pwsc) { int count; XMLSTRUCT *pxml; - /* check output... */ - if(ws_getvar(pwsc,"output") == NULL) { - flag = XML_FLAG_NONE; - } else if(strcasecmp(ws_getvar(pwsc,"output"),"json") == 0) { - flag = XML_FLAG_JSON | XML_FLAG_READABLE; - } else if(strcasecmp(ws_getvar(pwsc,"output"),"readable") == 0) { - flag = XML_FLAG_READABLE; - } + pxml=xml_init(pwsc,1); + xml_push(pxml,"status"); - pxml=xml_init(pwsc,1,flag); + xml_push(pxml,"service_status"); + + xml_push(pxml,"service"); - xml_push(pxml,"status",TRUE); - xml_push(pxml,"service_array",TRUE); - xml_push(pxml,"service",TRUE); xml_output(pxml,"name","Rendezvous"); #ifndef WITHOUT_MDNS @@ -308,38 +213,38 @@ void xml_get_stats(WS_CONNINFO *pwsc) { ws_writefd(pwsc,"