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/DOCBparser.h>
|
|
|
|
#include <libxml/xinclude.h>
|
|
|
|
#include <libxml/catalog.h>
|
|
|
|
#include <libxslt/xslt.h>
|
|
|
|
#include <libxslt/xsltInternals.h>
|
|
|
|
#include <libxslt/transform.h>
|
|
|
|
#include <libxslt/xsltutils.h>
|
|
|
|
|
2002-08-13 08:46:45 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2002-08-09 04:16:51 -04:00
|
|
|
|
|
|
|
#include <thread/thread.h>
|
|
|
|
#include <avl/avl.h>
|
|
|
|
#include <httpp/httpp.h>
|
|
|
|
#include <net/sock.h>
|
|
|
|
|
|
|
|
#include "connection.h"
|
|
|
|
|
|
|
|
#include "global.h"
|
|
|
|
#include "refbuf.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "stats.h"
|
|
|
|
|
2002-08-13 08:46:45 -04:00
|
|
|
#define CATMODULE "xslt"
|
|
|
|
#include "log.h"
|
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *filename;
|
|
|
|
time_t last_modified;
|
|
|
|
time_t cache_age;
|
|
|
|
xsltStylesheetPtr stylesheet;
|
|
|
|
} stylesheet_cache_t;
|
|
|
|
|
|
|
|
/* Keep it small... */
|
|
|
|
#define CACHESIZE 3
|
|
|
|
|
|
|
|
stylesheet_cache_t cache[CACHESIZE];
|
|
|
|
mutex_t xsltlock;
|
|
|
|
|
|
|
|
void xslt_initialize()
|
|
|
|
{
|
|
|
|
memset(cache, 0, sizeof(stylesheet_cache_t)*CACHESIZE);
|
|
|
|
thread_mutex_create(&xsltlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void xslt_shutdown() {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0; i < CACHESIZE; i++) {
|
|
|
|
if(cache[i].filename)
|
|
|
|
free(cache[i].filename);
|
|
|
|
if(cache[i].stylesheet)
|
|
|
|
xsltFreeStylesheet(cache[i].stylesheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
xsltCleanupGlobals();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int evict_cache_entry() {
|
|
|
|
int i, age=0, oldest;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static xsltStylesheetPtr xslt_get_stylesheet(char *fn) {
|
|
|
|
int i;
|
|
|
|
int empty = -1;
|
|
|
|
struct stat file;
|
|
|
|
|
|
|
|
if(stat(fn, &file)) {
|
|
|
|
DEBUG1("Error checking for stylesheet file: %s", strerror(errno));
|
|
|
|
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);
|
|
|
|
|
|
|
|
cache[i].last_modified = file.st_mtime;
|
|
|
|
cache[i].stylesheet = xsltParseStylesheetFile(fn);
|
|
|
|
cache[i].cache_age = time(NULL);
|
|
|
|
}
|
2002-12-29 04:21:32 -05:00
|
|
|
DEBUG1("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);
|
|
|
|
cache[i].stylesheet = xsltParseStylesheetFile(fn);
|
|
|
|
cache[i].cache_age = time(NULL);
|
|
|
|
return cache[i].stylesheet;
|
|
|
|
}
|
2002-08-09 04:16:51 -04:00
|
|
|
|
2002-08-09 10:38:42 -04:00
|
|
|
void xslt_transform(xmlDocPtr doc, char *xslfilename, client_t *client)
|
2002-08-09 04:16:51 -04:00
|
|
|
{
|
|
|
|
xmlOutputBufferPtr outputBuffer;
|
2003-03-14 21:10:19 -05:00
|
|
|
xmlDocPtr res;
|
|
|
|
xsltStylesheetPtr cur;
|
|
|
|
const char *params[16 + 1];
|
2002-08-09 10:15:08 -04:00
|
|
|
size_t count,bytes;
|
2002-08-09 04:16:51 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
params[0] = NULL;
|
2002-08-09 04:16:51 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
xmlSubstituteEntitiesDefault(1);
|
|
|
|
xmlLoadExtDtdDefaultValue = 1;
|
2002-08-09 04:16:51 -04:00
|
|
|
|
2002-08-13 08:46:45 -04:00
|
|
|
thread_mutex_lock(&xsltlock);
|
|
|
|
cur = xslt_get_stylesheet(xslfilename);
|
|
|
|
thread_mutex_unlock(&xsltlock);
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (cur == NULL) {
|
|
|
|
bytes = sock_write_string(client->con->sock,
|
2002-08-09 10:15:08 -04:00
|
|
|
(char *)"Could not parse XSLT file");
|
|
|
|
if(bytes > 0) client->con->sent_bytes += bytes;
|
|
|
|
|
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
|
|
|
|
|
|
|
res = xsltApplyStylesheet(cur, doc, params);
|
|
|
|
|
|
|
|
outputBuffer = xmlAllocOutputBuffer(NULL);
|
|
|
|
|
|
|
|
count = xsltSaveResultTo(outputBuffer, res, cur);
|
|
|
|
|
|
|
|
/* Add null byte to end. */
|
2002-08-09 10:15:08 -04:00
|
|
|
bytes = xmlOutputBufferWrite(outputBuffer, 1, "");
|
2002-08-09 04:16:51 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if(sock_write_string(client->con->sock,
|
2002-08-09 10:15:08 -04:00
|
|
|
(char *)outputBuffer->buffer->content))
|
|
|
|
client->con->sent_bytes += bytes;
|
|
|
|
|
2002-12-29 09:06:20 -05:00
|
|
|
xmlOutputBufferClose(outputBuffer);
|
2002-08-09 04:16:51 -04:00
|
|
|
xmlFreeDoc(res);
|
|
|
|
}
|
|
|
|
|