owntone-server/src/xml-rpc.c

173 lines
3.7 KiB
C
Raw Normal View History

2005-02-16 23:24:16 -05:00
/*
* $Id$
*
* This really isn't xmlrpc. It's xmlrpc-ish. Emphasis on -ish.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
2005-02-16 23:24:16 -05:00
#include "configfile.h"
#include "daapd.h"
2005-02-16 23:24:16 -05:00
#include "err.h"
2005-02-18 19:50:29 -05:00
#include "mp3-scanner.h"
2005-02-16 23:24:16 -05:00
#include "webserver.h"
/* Forwards */
void xml_get_stats(WS_CONNINFO *pwsc);
2005-02-16 23:24:16 -05:00
char *xml_entity_encode(char *original);
/**
* main entrypoint for the xmlrpc functions.
*
* @arg pwsc Pointer to the web request structure
*/
void xml_handle(WS_CONNINFO *pwsc) {
char *method;
if((method=ws_getvar(pwsc,"method")) == NULL) {
ws_returnerror(pwsc,500,"no method specified");
return;
2005-02-16 23:24:16 -05:00
}
if(strcasecmp(method,"stats") == 0) {
xml_get_stats(pwsc);
return;
2005-02-16 23:24:16 -05:00
}
ws_returnerror(pwsc,500,"Invalid method");
return;
}
/**
* return xml file of all playlists
*/
void xml_get_stats(WS_CONNINFO *pwsc) {
int r_secs, r_days, r_hours, r_mins;
char buf[80];
WS_CONNINFO *pci;
SCAN_STATUS *pss;
WSTHREADENUM wste;
2005-02-16 23:24:16 -05:00
2005-02-18 19:50:29 -05:00
ws_addresponseheader(pwsc,"Content-Type","text/xml; charset=utf-8");
2005-02-16 23:24:16 -05:00
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\"?>");
ws_writefd(pwsc,"<status>");
ws_writefd(pwsc,"<service_status>");
/* enumerate services? */
ws_writefd(pwsc,"</service_status>");
ws_writefd(pwsc,"<thread_status>");
/* enumerate thread status */
pci = ws_thread_enum_first(config.server,&wste);
while(pci) {
pss = ws_get_local_storage(pci);
if(pss) {
ws_writefd(pwsc,"<id>%d</id><sourceip>%s</sourceip><action>%s</action>",
pss->thread,pss->host,pss->what);
}
pci=ws_thread_enum_next(config.server,&wste);
2005-02-16 23:24:16 -05:00
}
ws_writefd(pwsc,"</thread_status>");
2005-02-16 23:24:16 -05:00
ws_writefd(pwsc,"<statistics>");
/* dump stats */
ws_writefd(pwsc,"<stat name=\"uptime\">");
2005-02-16 23:24:16 -05:00
r_secs=time(NULL)-config.stats.start_time;
2005-02-16 23:24:16 -05:00
r_days=r_secs/(3600 * 24);
r_secs -= ((3600 * 24) * r_days);
2005-02-16 23:24:16 -05:00
r_hours=r_secs/3600;
r_secs -= (3600 * r_hours);
2005-02-18 19:50:29 -05:00
r_mins=r_secs/60;
r_secs -= 60 * r_mins;
2005-02-18 19:50:29 -05:00
memset(buf,0x0,sizeof(buf));
if(r_days)
sprintf((char*)&buf[strlen(buf)],"%d day%s, ", r_days,
r_days == 1 ? "" : "s");
2005-02-18 19:50:29 -05:00
if(r_days || r_hours)
sprintf((char*)&buf[strlen(buf)],"%d hour%s, ", r_hours,
r_hours == 1 ? "" : "s");
if(r_days || r_hours || r_mins)
sprintf((char*)&buf[strlen(buf)],"%d minute%s, ", r_mins,
r_mins == 1 ? "" : "s");
sprintf((char*)&buf[strlen(buf)],"%d second%s ", r_secs,
r_secs == 1 ? "" : "s");
2005-02-18 19:50:29 -05:00
ws_writefd(pwsc,"<name>Uptime</name>");
ws_writefd(pwsc,"<value>%s</value>",buf);
ws_writefd(pwsc,"</stat>");
ws_writefd(pwsc,"</statistics>");
ws_writefd(pwsc,"</status>");
2005-02-18 19:50:29 -05:00
return;
2005-02-16 23:24:16 -05:00
}
/**
* xml entity encoding, stupid style
*/
char *xml_entity_encode(char *original) {
char *new;
char *s, *d;
int destsize;
destsize = 6*strlen(original)+1;
new=(char *)malloc(destsize);
if(!new) return NULL;
memset(new,0x00,destsize);
s=original;
d=new;
while(*s) {
switch(*s) {
case '>':
strcat(d,"&gt;");
d += 4;
s++;
break;
case '<':
strcat(d,"&lt;");
d += 4;
s++;
break;
case '"':
strcat(d,"&quot;");
d += 6;
s++;
break;
case '\'':
strcat(d,"&apos;");
d += 6;
s++;
break;
case '&':
strcat(d,"&amp;");
d += 5;
s++;
break;
default:
*d++ = *s++;
}
2005-02-16 23:24:16 -05:00
}
return new;
}