2004-01-28 20:02:12 -05:00
|
|
|
/* Icecast
|
|
|
|
*
|
|
|
|
* This program is distributed under the GNU General Public License, version 2.
|
|
|
|
* A copy of this license is included with this source.
|
|
|
|
*
|
2015-01-10 13:53:44 -05:00
|
|
|
* Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
|
2004-01-28 20:02:12 -05:00
|
|
|
* Michael Smith <msmith@xiph.org>,
|
|
|
|
* oddsock <oddsock@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>
|
|
|
|
* and others (see AUTHORS for details).
|
|
|
|
*/
|
|
|
|
|
2003-07-20 21:58:54 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2002-08-09 04:16:51 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <libxml/xmlmemory.h>
|
|
|
|
#include <libxml/debugXML.h>
|
|
|
|
#include <libxml/HTMLtree.h>
|
|
|
|
#include <libxml/xmlIO.h>
|
|
|
|
#include <libxml/xinclude.h>
|
|
|
|
#include <libxml/catalog.h>
|
2015-02-25 09:49:01 -05:00
|
|
|
#include <libxml/uri.h>
|
2002-08-09 04:16:51 -04:00
|
|
|
#include <libxslt/xslt.h>
|
|
|
|
#include <libxslt/xsltInternals.h>
|
|
|
|
#include <libxslt/transform.h>
|
|
|
|
#include <libxslt/xsltutils.h>
|
2015-02-25 09:49:01 -05:00
|
|
|
#include <libxslt/documents.h>
|
2002-08-09 04:16:51 -04:00
|
|
|
|
2002-08-13 08:46:45 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2005-08-11 23:27:09 -04:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2002-08-13 08:46:45 -04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2005-08-11 23:27:09 -04:00
|
|
|
#ifdef WIN32
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
2002-08-09 04:16:51 -04:00
|
|
|
|
2014-12-02 16:50:57 -05:00
|
|
|
#include "common/thread/thread.h"
|
|
|
|
#include "common/avl/avl.h"
|
|
|
|
#include "common/httpp/httpp.h"
|
|
|
|
#include "common/net/sock.h"
|
2002-08-09 04:16:51 -04:00
|
|
|
|
|
|
|
#include "connection.h"
|
|
|
|
|
|
|
|
#include "global.h"
|
|
|
|
#include "refbuf.h"
|
|
|
|
#include "client.h"
|
2015-02-25 09:49:01 -05:00
|
|
|
#include "config.h"
|
2002-08-09 04:16:51 -04:00
|
|
|
#include "stats.h"
|
2005-06-18 06:54:53 -04:00
|
|
|
#include "fserve.h"
|
2012-10-10 19:34:54 -04:00
|
|
|
#include "util.h"
|
2002-08-09 04:16:51 -04:00
|
|
|
|
2002-08-13 08:46:45 -04:00
|
|
|
#define CATMODULE "xslt"
|
2003-07-16 15:41:59 -04:00
|
|
|
|
2002-08-13 08:46:45 -04:00
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
typedef struct {
|
2014-11-30 15:32:30 -05:00
|
|
|
char *filename;
|
|
|
|
time_t last_modified;
|
|
|
|
time_t cache_age;
|
|
|
|
xsltStylesheetPtr stylesheet;
|
2002-08-13 08:46:45 -04:00
|
|
|
} stylesheet_cache_t;
|
|
|
|
|
2005-08-11 23:27:09 -04:00
|
|
|
#ifndef HAVE_XSLTSAVERESULTTOSTRING
|
|
|
|
int xsltSaveResultToString(xmlChar **doc_txt_ptr, int * doc_txt_len, xmlDocPtr result, xsltStylesheetPtr style) {
|
|
|
|
xmlOutputBufferPtr buf;
|
|
|
|
|
|
|
|
*doc_txt_ptr = NULL;
|
|
|
|
*doc_txt_len = 0;
|
|
|
|
if (result->children == NULL)
|
2014-11-30 15:32:30 -05:00
|
|
|
return (0);
|
2005-08-11 23:27:09 -04:00
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
buf = xmlAllocOutputBuffer(NULL);
|
2005-08-11 23:27:09 -04:00
|
|
|
|
|
|
|
if (buf == NULL)
|
2014-11-30 15:32:30 -05:00
|
|
|
return (-1);
|
2005-08-11 23:27:09 -04:00
|
|
|
xsltSaveResultTo(buf, result, style);
|
|
|
|
if (buf->conv != NULL) {
|
2014-11-30 15:32:30 -05:00
|
|
|
*doc_txt_len = buf->conv->use;
|
|
|
|
*doc_txt_ptr = xmlStrndup(buf->conv->content, *doc_txt_len);
|
2005-08-11 23:27:09 -04:00
|
|
|
} else {
|
2014-11-30 15:32:30 -05:00
|
|
|
*doc_txt_len = buf->buffer->use;
|
|
|
|
*doc_txt_ptr = xmlStrndup(buf->buffer->content, *doc_txt_len);
|
2005-08-11 23:27:09 -04:00
|
|
|
}
|
2014-11-30 15:32:30 -05:00
|
|
|
(void) xmlOutputBufferClose(buf);
|
2005-08-11 23:27:09 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-08-13 08:46:45 -04:00
|
|
|
/* Keep it small... */
|
|
|
|
#define CACHESIZE 3
|
|
|
|
|
2005-08-11 23:27:09 -04:00
|
|
|
static stylesheet_cache_t cache[CACHESIZE];
|
|
|
|
static mutex_t xsltlock;
|
2002-08-13 08:46:45 -04:00
|
|
|
|
2015-02-25 09:49:01 -05:00
|
|
|
/* Reference to the original xslt loader func */
|
|
|
|
static xsltDocLoaderFunc xslt_loader;
|
|
|
|
/* Admin path cache */
|
|
|
|
static xmlChar *admin_path = NULL;
|
|
|
|
|
2005-12-17 07:23:09 -05:00
|
|
|
void xslt_initialize(void)
|
2002-08-13 08:46:45 -04:00
|
|
|
{
|
2014-11-30 15:32:30 -05:00
|
|
|
memset(cache, 0, sizeof(stylesheet_cache_t) * CACHESIZE);
|
2002-08-13 08:46:45 -04:00
|
|
|
thread_mutex_create(&xsltlock);
|
2008-02-10 20:00:41 -05:00
|
|
|
xmlInitParser();
|
|
|
|
LIBXML_TEST_VERSION
|
2005-04-18 10:32:26 -04:00
|
|
|
xmlSubstituteEntitiesDefault(1);
|
|
|
|
xmlLoadExtDtdDefaultValue = 1;
|
2015-02-25 09:49:01 -05:00
|
|
|
xslt_loader = xsltDocDefaultLoader;
|
2002-08-13 08:46:45 -04:00
|
|
|
}
|
|
|
|
|
2005-12-17 07:23:09 -05:00
|
|
|
void xslt_shutdown(void) {
|
2002-08-13 08:46:45 -04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0; i < CACHESIZE; i++) {
|
|
|
|
if(cache[i].filename)
|
|
|
|
free(cache[i].filename);
|
|
|
|
if(cache[i].stylesheet)
|
|
|
|
xsltFreeStylesheet(cache[i].stylesheet);
|
|
|
|
}
|
|
|
|
|
2005-04-18 10:32:26 -04:00
|
|
|
thread_mutex_destroy (&xsltlock);
|
2008-02-10 20:00:41 -05:00
|
|
|
xmlCleanupParser();
|
2002-08-13 08:46:45 -04:00
|
|
|
xsltCleanupGlobals();
|
2015-02-25 09:49:01 -05:00
|
|
|
if (admin_path)
|
|
|
|
xmlFree(admin_path);
|
2002-08-13 08:46:45 -04:00
|
|
|
}
|
|
|
|
|
2005-12-17 07:23:09 -05:00
|
|
|
static int evict_cache_entry(void) {
|
2003-07-24 01:24:00 -04:00
|
|
|
int i, age=0, oldest=0;
|
2002-08-13 08:46:45 -04:00
|
|
|
|
|
|
|
for(i=0; i < CACHESIZE; i++) {
|
|
|
|
if(cache[i].cache_age > age) {
|
|
|
|
age = cache[i].cache_age;
|
|
|
|
oldest = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xsltFreeStylesheet(cache[oldest].stylesheet);
|
|
|
|
free(cache[oldest].filename);
|
|
|
|
|
|
|
|
return oldest;
|
|
|
|
}
|
|
|
|
|
2004-10-26 10:21:36 -04:00
|
|
|
static xsltStylesheetPtr xslt_get_stylesheet(const char *fn) {
|
2002-08-13 08:46:45 -04:00
|
|
|
int i;
|
|
|
|
int empty = -1;
|
|
|
|
struct stat file;
|
|
|
|
|
|
|
|
if(stat(fn, &file)) {
|
2015-01-10 13:53:44 -05:00
|
|
|
ICECAST_LOG_WARN("Error checking for stylesheet file \"%s\": %s", fn,
|
2005-02-15 19:54:55 -05:00
|
|
|
strerror(errno));
|
2002-08-13 08:46:45 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i < CACHESIZE; i++) {
|
|
|
|
if(cache[i].filename)
|
|
|
|
{
|
2002-08-13 09:53:07 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
if(!stricmp(fn, cache[i].filename))
|
|
|
|
#else
|
2002-08-13 08:46:45 -04:00
|
|
|
if(!strcmp(fn, cache[i].filename))
|
2002-08-13 09:53:07 -04:00
|
|
|
#endif
|
2002-08-13 08:46:45 -04:00
|
|
|
{
|
|
|
|
if(file.st_mtime > cache[i].last_modified)
|
|
|
|
{
|
|
|
|
xsltFreeStylesheet(cache[i].stylesheet);
|
2015-01-10 13:53:44 -05:00
|
|
|
|
2002-08-13 08:46:45 -04:00
|
|
|
cache[i].last_modified = file.st_mtime;
|
2014-11-30 15:32:30 -05:00
|
|
|
cache[i].stylesheet = xsltParseStylesheetFile(XMLSTR(fn));
|
2002-08-13 08:46:45 -04:00
|
|
|
cache[i].cache_age = time(NULL);
|
|
|
|
}
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("Using cached sheet %i", i);
|
2002-08-13 08:46:45 -04:00
|
|
|
return cache[i].stylesheet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
empty = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(empty>=0)
|
|
|
|
i = empty;
|
|
|
|
else
|
|
|
|
i = evict_cache_entry();
|
|
|
|
|
|
|
|
cache[i].last_modified = file.st_mtime;
|
|
|
|
cache[i].filename = strdup(fn);
|
2014-11-30 15:32:30 -05:00
|
|
|
cache[i].stylesheet = xsltParseStylesheetFile(XMLSTR(fn));
|
2002-08-13 08:46:45 -04:00
|
|
|
cache[i].cache_age = time(NULL);
|
|
|
|
return cache[i].stylesheet;
|
|
|
|
}
|
2002-08-09 04:16:51 -04:00
|
|
|
|
2015-02-25 09:49:01 -05:00
|
|
|
/* Custom xslt loader */
|
|
|
|
static xmlDocPtr custom_loader(const xmlChar *URI,
|
|
|
|
xmlDictPtr dict,
|
|
|
|
int options,
|
|
|
|
void *ctxt,
|
|
|
|
xsltLoadType type)
|
|
|
|
{
|
|
|
|
xmlDocPtr ret;
|
|
|
|
xmlChar *rel_path, *fn, *final_URI;
|
|
|
|
xsltStylesheet *c;
|
|
|
|
ice_config_t *config;
|
|
|
|
final_URI = xmlStrdup(URI);
|
2015-02-25 10:11:42 -05:00
|
|
|
struct stat file;
|
2015-02-25 09:49:01 -05:00
|
|
|
switch (type) {
|
|
|
|
/* In case an include is loaded */
|
|
|
|
case XSLT_LOAD_STYLESHEET:
|
2015-02-25 10:11:42 -05:00
|
|
|
/* Not look in admindir if the include file exists */
|
|
|
|
if (stat((char *)URI, &file) == 0)
|
|
|
|
break;
|
2015-02-25 09:49:01 -05:00
|
|
|
c = (xsltStylesheet *) ctxt;
|
|
|
|
/* Check if we actually have context/path */
|
|
|
|
if (ctxt == NULL || c->doc->URL == NULL)
|
|
|
|
break;
|
|
|
|
rel_path = xmlBuildRelativeURI(URI, c->doc->URL);
|
|
|
|
if (rel_path != NULL && admin_path != NULL) {
|
|
|
|
fn = xmlBuildURI(rel_path, admin_path);
|
|
|
|
if (fn != NULL && stat((char *)fn, &file) == 0) {
|
|
|
|
final_URI = fn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* In case a top stylesheet is loaded */
|
|
|
|
case XSLT_LOAD_START:
|
|
|
|
config = config_get_config();
|
|
|
|
admin_path = xmlCharStrdup(config->adminroot_dir);
|
|
|
|
config_release_config();
|
|
|
|
admin_path = xmlStrcat(admin_path, (xmlChar *)"/");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Get the actual xmlDoc */
|
|
|
|
ret = xslt_loader(final_URI, dict, options, ctxt, type);
|
|
|
|
xmlFree(final_URI);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-10-26 10:21:36 -04:00
|
|
|
void xslt_transform(xmlDocPtr doc, const char *xslfilename, client_t *client)
|
2002-08-09 04:16:51 -04:00
|
|
|
{
|
2014-11-30 15:32:30 -05:00
|
|
|
xmlDocPtr res;
|
2003-03-14 21:10:19 -05:00
|
|
|
xsltStylesheetPtr cur;
|
2005-06-18 06:54:53 -04:00
|
|
|
xmlChar *string;
|
2005-07-04 16:11:15 -04:00
|
|
|
int len, problem = 0;
|
2007-08-10 20:24:51 -04:00
|
|
|
const char *mediatype = NULL;
|
2014-02-23 16:29:35 -05:00
|
|
|
const char *charset = NULL;
|
2002-08-09 04:16:51 -04:00
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
xmlSetGenericErrorFunc("", log_parse_failure);
|
|
|
|
xsltSetGenericErrorFunc("", log_parse_failure);
|
2015-02-25 09:49:01 -05:00
|
|
|
xsltSetLoaderFunc(custom_loader);
|
2005-08-16 12:56:24 -04:00
|
|
|
|
2002-08-13 08:46:45 -04:00
|
|
|
thread_mutex_lock(&xsltlock);
|
|
|
|
cur = xslt_get_stylesheet(xslfilename);
|
|
|
|
|
2005-06-18 06:54:53 -04:00
|
|
|
if (cur == NULL)
|
|
|
|
{
|
2005-01-11 11:36:34 -05:00
|
|
|
thread_mutex_unlock(&xsltlock);
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_ERROR("problem reading stylesheet \"%s\"", xslfilename);
|
2014-11-21 13:05:17 -05:00
|
|
|
client_send_error(client, 404, 0, "Could not parse XSLT file");
|
2002-08-09 04:16:51 -04:00
|
|
|
return;
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
2002-08-09 04:16:51 -04:00
|
|
|
|
2005-06-18 06:54:53 -04:00
|
|
|
res = xsltApplyStylesheet(cur, doc, NULL);
|
2002-08-09 04:16:51 -04:00
|
|
|
|
2005-07-04 16:11:15 -04:00
|
|
|
if (xsltSaveResultToString (&string, &len, res, cur) < 0)
|
|
|
|
problem = 1;
|
2007-08-10 20:24:51 -04:00
|
|
|
|
2014-02-23 16:29:35 -05:00
|
|
|
/* lets find out the content type and character encoding to use */
|
|
|
|
if (cur->encoding)
|
|
|
|
charset = (char *)cur->encoding;
|
|
|
|
|
2007-08-10 20:24:51 -04:00
|
|
|
if (cur->mediaType)
|
|
|
|
mediatype = (char *)cur->mediaType;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* check method for the default, a missing method assumes xml */
|
2007-10-04 12:48:38 -04:00
|
|
|
if (cur->method && xmlStrcmp (cur->method, XMLSTR("html")) == 0)
|
2007-08-10 20:24:51 -04:00
|
|
|
mediatype = "text/html";
|
|
|
|
else
|
2007-10-04 12:48:38 -04:00
|
|
|
if (cur->method && xmlStrcmp (cur->method, XMLSTR("text")) == 0)
|
2007-08-10 20:24:51 -04:00
|
|
|
mediatype = "text/plain";
|
|
|
|
else
|
|
|
|
mediatype = "text/xml";
|
|
|
|
}
|
2005-07-04 16:11:15 -04:00
|
|
|
if (problem == 0)
|
2005-06-18 06:54:53 -04:00
|
|
|
{
|
2014-11-18 12:43:34 -05:00
|
|
|
ssize_t ret;
|
|
|
|
int failed = 0;
|
|
|
|
refbuf_t *refbuf;
|
2014-12-14 14:18:22 -05:00
|
|
|
ssize_t full_len = strlen(mediatype) + (ssize_t)len + (ssize_t)1024;
|
2014-11-10 05:46:55 -05:00
|
|
|
if (full_len < 4096)
|
|
|
|
full_len = 4096;
|
2014-11-18 12:43:34 -05:00
|
|
|
refbuf = refbuf_new (full_len);
|
2005-06-18 06:54:53 -04:00
|
|
|
|
2005-07-04 16:11:15 -04:00
|
|
|
if (string == NULL)
|
2007-08-10 20:24:51 -04:00
|
|
|
string = xmlCharStrdup ("");
|
2015-02-01 06:56:03 -05:00
|
|
|
ret = util_http_build_header(refbuf->data, full_len, 0, 0, 200, NULL, mediatype, charset, NULL, NULL, client);
|
2014-11-10 05:46:55 -05:00
|
|
|
if (ret == -1) {
|
|
|
|
ICECAST_LOG_ERROR("Dropping client as we can not build response headers.");
|
2014-11-21 13:05:17 -05:00
|
|
|
client_send_error(client, 500, 0, "Header generation failed.");
|
2014-11-10 05:46:55 -05:00
|
|
|
} else {
|
2014-12-14 14:18:22 -05:00
|
|
|
if ( full_len < (ret + (ssize_t)len + (ssize_t)64) ) {
|
2014-11-10 05:46:55 -05:00
|
|
|
void *new_data;
|
2014-12-14 14:18:22 -05:00
|
|
|
full_len = ret + (ssize_t)len + (ssize_t)64;
|
2014-11-10 05:46:55 -05:00
|
|
|
new_data = realloc(refbuf->data, full_len);
|
|
|
|
if (new_data) {
|
|
|
|
ICECAST_LOG_DEBUG("Client buffer reallocation succeeded.");
|
|
|
|
refbuf->data = new_data;
|
|
|
|
refbuf->len = full_len;
|
2015-02-01 06:56:03 -05:00
|
|
|
ret = util_http_build_header(refbuf->data, full_len, 0, 0, 200, NULL, mediatype, charset, NULL, NULL, client);
|
2014-11-10 05:46:55 -05:00
|
|
|
if (ret == -1) {
|
|
|
|
ICECAST_LOG_ERROR("Dropping client as we can not build response headers.");
|
2014-11-21 13:05:17 -05:00
|
|
|
client_send_error(client, 500, 0, "Header generation failed.");
|
2014-11-10 05:46:55 -05:00
|
|
|
failed = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ICECAST_LOG_ERROR("Client buffer reallocation failed. Dropping client.");
|
2014-11-21 13:05:17 -05:00
|
|
|
client_send_error(client, 500, 0, "Buffer reallocation failed.");
|
2014-11-10 05:46:55 -05:00
|
|
|
failed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!failed) {
|
|
|
|
snprintf(refbuf->data + ret, full_len - ret, "Content-Length: %d\r\n\r\n%s", len, string);
|
|
|
|
|
|
|
|
client->respcode = 200;
|
|
|
|
client_set_queue (client, NULL);
|
|
|
|
client->refbuf = refbuf;
|
|
|
|
refbuf->len = strlen (refbuf->data);
|
|
|
|
fserve_add_client (client, NULL);
|
|
|
|
}
|
|
|
|
}
|
2005-06-18 06:54:53 -04:00
|
|
|
xmlFree (string);
|
|
|
|
}
|
2005-07-04 16:11:15 -04:00
|
|
|
else
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_WARN("problem applying stylesheet \"%s\"", xslfilename);
|
2014-11-21 13:05:17 -05:00
|
|
|
client_send_error(client, 404, 0, "XSLT problem");
|
2005-07-04 16:11:15 -04:00
|
|
|
}
|
2007-08-10 20:24:51 -04:00
|
|
|
thread_mutex_unlock (&xsltlock);
|
2002-08-09 04:16:51 -04:00
|
|
|
xmlFreeDoc(res);
|
|
|
|
}
|
|
|
|
|