xslt.c 4.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org, 
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
 */

13 14 15 16
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

17 18 19 20 21 22 23 24 25 26 27 28
#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>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>

29 30 31 32 33 34 35 36
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

#ifndef _WIN32
#include <sys/time.h>
#endif

37

Karl Heyes's avatar
Karl Heyes committed
38 39 40 41
#include "thread/thread.h"
#include "avl/avl.h"
#include "httpp/httpp.h"
#include "net/sock.h"
42 43 44 45 46 47 48

#include "connection.h"

#include "global.h"
#include "refbuf.h"
#include "client.h"
#include "stats.h"
49
#include "fserve.h"
50

51
#define CATMODULE "xslt"
Karl Heyes's avatar
Karl Heyes committed
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#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);
72 73
    xmlSubstituteEntitiesDefault(1);
    xmlLoadExtDtdDefaultValue = 1;
74 75 76 77 78 79 80 81 82 83 84 85
}

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);
    }

86
    thread_mutex_destroy (&xsltlock);
87 88 89 90
    xsltCleanupGlobals();
}

static int evict_cache_entry() {
brendan's avatar
brendan committed
91
    int i, age=0, oldest=0;
92 93 94 95 96 97 98 99 100 101 102 103 104 105

    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;
}

106
static xsltStylesheetPtr xslt_get_stylesheet(const char *fn) {
107 108 109 110 111
    int i;
    int empty = -1;
    struct stat file;

    if(stat(fn, &file)) {
112 113
        WARN2("Error checking for stylesheet file \"%s\": %s", fn, 
                strerror(errno));
114 115 116 117 118 119
        return NULL;
    }

    for(i=0; i < CACHESIZE; i++) {
        if(cache[i].filename)
        {
120 121 122
#ifdef _WIN32
            if(!stricmp(fn, cache[i].filename))
#else
123
            if(!strcmp(fn, cache[i].filename))
124
#endif
125 126 127 128 129 130 131 132 133
            {
                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);
                }
134
                DEBUG1("Using cached sheet %i", i);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
                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;
}
153

154
void xslt_transform(xmlDocPtr doc, const char *xslfilename, client_t *client)
155
{
156 157
    xmlDocPtr    res;
    xsltStylesheetPtr cur;
158
    xmlChar *string;
159
    int len, problem = 0;
160

161 162 163
    thread_mutex_lock(&xsltlock);
    cur = xslt_get_stylesheet(xslfilename);

164 165
    if (cur == NULL)
    {
166
        thread_mutex_unlock(&xsltlock);
167 168
        ERROR1 ("problem reading stylesheet \"%s\"", xslfilename);
        client_send_404 (client, "Could not parse XSLT file");
169
        return;
170
    }
171

172
    res = xsltApplyStylesheet(cur, doc, NULL);
173

174 175
    if (xsltSaveResultToString (&string, &len, res, cur) < 0)
        problem = 1;
176
    thread_mutex_unlock(&xsltlock);
177
    if (problem == 0)
178 179
    {
        const char *http = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nContent-Length: ";
180
        int buf_len = strlen (http) + 20 + len;
181

182 183
        if (string == NULL)
            string = xmlStrdup ("");
184 185
        client->respcode = 200;
        client->refbuf = refbuf_new (buf_len);
186 187
        len = snprintf (client->refbuf->data, buf_len, "%s%d\r\n\r\n%s", http, len, string);
        client->refbuf->len = len;
188 189 190
        fserve_add_client (client, NULL);
        xmlFree (string);
    }
191 192 193 194 195
    else
    {
        WARN1 ("problem applying stylesheet \"%s\"", xslfilename);
        client_send_404 (client, "XSLT problem");
    }
196 197 198
    xmlFreeDoc(res);
}