Support infrastructure for simpler xml handler.

This commit is contained in:
Ron Pedde 2005-11-20 09:50:50 +00:00
parent 3ac71ba469
commit 3fe312ead6
1 changed files with 146 additions and 29 deletions

View File

@ -5,6 +5,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
@ -16,23 +17,142 @@
#include "webserver.h" #include "webserver.h"
/* typedefs */ /* typedefs */
typedef struct tag_xmlstack {
char *tag;
struct tag_xmlstack *next;
} XMLSTACK;
typedef struct tag_xmlstruct { typedef struct tag_xmlstruct {
WS_CONNINFO *pwsc;
int stack_level;
XMLSTACK stack;
} XMLSTRUCT; } XMLSTRUCT;
/* Forwards */ /* Forwards */
void xml_get_stats(WS_CONNINFO *pwsc); void xml_get_stats(WS_CONNINFO *pwsc);
char *xml_entity_encode(char *original); 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 * create an xml response structure, a helper struct for
* building xml responses. * building xml responses.
* *
* @param pwsc the pwsc we are emitting to
* @param emit_header whether or not to throw out html headers and xml header
* @returns XMLSTRUCT on success, or NULL if failure * @returns XMLSTRUCT on success, or NULL if failure
*/ */
XMLSTRUCT *xml_init(void) { XMLSTRUCT *xml_init(WS_CONNINFO *pwsc, int emit_header) {
XMLSTRUCT *pxml;
pxml=(XMLSTRUCT*)malloc(sizeof(XMLSTRUCT));
if(!pxml) {
DPRINTF(E_FATAL,L_XML,"Malloc error\n");
}
memset(pxml,0,sizeof(XMLSTRUCT));
pxml->pwsc = pwsc;
if(emit_header) {
ws_addresponseheader(pwsc,"Content-Type","text/xml; charset=utf-8");
ws_writefd(pwsc,"HTTP/1.0 200 OK\r\n");
ws_emitheaders(pwsc);
ws_writefd(pwsc,"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
}
return pxml;
} }
/**
* push a new term on the stack
*
* @param pxml xml struct obtained from xml_init
* @param term next xlm section to start
*/
void xml_push(XMLSTRUCT *pxml, char *term) {
XMLSTACK *pstack;
pstack = (XMLSTACK *)malloc(sizeof(XMLSTACK));
pstack->next=pxml->stack.next;
pstack->tag=strdup(term);
pxml->stack.next=pstack;
pxml->stack_level++;
ws_writefd(pxml->pwsc,"<%s>",term);
}
/**
* end an xml section
*
* @param pxml xml struct we are working with
*/
void xml_pop(XMLSTRUCT *pxml) {
XMLSTACK *pstack;
pstack=pxml->stack.next;
if(!pstack) {
DPRINTF(E_LOG,L_XML,"xml_pop: tried to pop an empty stack\n");
return;
}
pxml->stack.next = pstack->next;
ws_writefd(pxml->pwsc,"</%s>",pstack->tag);
free(pstack->tag);
free(pstack);
pxml->stack_level--;
}
/**
* output a string
*/
void xml_output(XMLSTRUCT *pxml, char *section, char *fmt, ...) {
va_list ap;
char buf[256];
char *output;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
output = xml_entity_encode(buf);
if(section) {
xml_push(pxml,section);
}
ws_writefd(pxml->pwsc,"%s",output);
free(output);
if(section) {
xml_pop(pxml);
}
}
/**
* clean up an xml struct
*
* @param pxml xml struct to clean up
*/
void xml_deinit(XMLSTRUCT *pxml) {
XMLSTACK *pstack;
if(pxml->stack.next) {
DPRINTF(E_LOG,L_XML,"xml_deinit: entries still on stack (%s)\n",pxml->stack.next->tag);
}
while((pstack=pxml->stack.next)) {
pxml->stack.next=pstack->next;
free(pstack->tag);
free(pstack);
}
free(pxml);
}
/** /**
* main entrypoint for the xmlrpc functions. * main entrypoint for the xmlrpc functions.
@ -66,38 +186,32 @@ void xml_get_stats(WS_CONNINFO *pwsc) {
SCAN_STATUS *pss; SCAN_STATUS *pss;
WSTHREADENUM wste; WSTHREADENUM wste;
XMLSTRUCT *pxml;
ws_addresponseheader(pwsc,"Content-Type","text/xml; charset=utf-8"); pxml=xml_init(pwsc,1);
ws_writefd(pwsc,"HTTP/1.0 200 OK\r\n"); xml_push(pxml,"status");
ws_emitheaders(pwsc);
ws_writefd(pwsc,"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"); xml_push(pxml,"service_status");
ws_writefd(pwsc,"<status>"); xml_pop(pxml); /* service_status */
ws_writefd(pwsc,"<service_status>"); xml_push(pxml,"thread_status");
/* enumerate services? */
ws_writefd(pwsc,"</service_status>");
ws_writefd(pwsc,"<thread_status>");
/* enumerate thread status */
pci = ws_thread_enum_first(config.server,&wste); pci = ws_thread_enum_first(config.server,&wste);
while(pci) { while(pci) {
pss = ws_get_local_storage(pci); pss = ws_get_local_storage(pci);
if(pss) { if(pss) {
ws_writefd(pwsc,"<thread><id>%d</id><sourceip>%s</sourceip><action>%s</action></thread>", xml_push(pxml,"thread");
pss->thread,pss->host,pss->what); xml_output(pxml,"id","%d",pss->thread);
xml_output(pxml,"sourceip","%s",pss->host);
xml_output(pxml,"action","%s",pss->what);
xml_pop(pxml); /* thread */
} }
pci=ws_thread_enum_next(config.server,&wste); pci=ws_thread_enum_next(config.server,&wste);
} }
xml_pop(pxml); /* thread_status */
ws_writefd(pwsc,"</thread_status>"); xml_push(pxml,"statistics");
ws_writefd(pwsc,"<statistics>");
/* dump stats */
ws_writefd(pwsc,"<stat name=\"uptime\">");
r_secs=time(NULL)-config.stats.start_time; r_secs=time(NULL)-config.stats.start_time;
@ -126,11 +240,14 @@ void xml_get_stats(WS_CONNINFO *pwsc) {
sprintf((char*)&buf[strlen(buf)],"%d second%s ", r_secs, sprintf((char*)&buf[strlen(buf)],"%d second%s ", r_secs,
r_secs == 1 ? "" : "s"); r_secs == 1 ? "" : "s");
ws_writefd(pwsc,"<name>Uptime</name>"); xml_push(pxml,"stat");
ws_writefd(pwsc,"<value>%s</value>",buf); xml_output(pxml,"name","Uptime");
ws_writefd(pwsc,"</stat>"); xml_output(pxml,"value","%s",buf);
ws_writefd(pwsc,"</statistics>"); xml_pop(pxml); /* stat */
ws_writefd(pwsc,"</status>"); xml_pop(pxml); /* statistics */
xml_pop(pxml); /* status */
xml_deinit(pxml);
return; return;
} }