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

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

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

32 33 34 35
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

36
#ifdef  HAVE_SYS_TIME_H
37 38 39
#include <sys/time.h>
#endif

40 41 42
#ifdef WIN32
#define snprintf _snprintf
#endif
43

44 45 46 47
#include "common/thread/thread.h"
#include "common/avl/avl.h"
#include "common/httpp/httpp.h"
#include "common/net/sock.h"
48

49
#include "xslt.h"
50 51
#include "refbuf.h"
#include "client.h"
52
#include "errors.h"
53
#include "stats.h"
54
#include "fserve.h"
Philipp Schafft's avatar
Philipp Schafft committed
55
#include "util.h"
56
#include "cfgfile.h"
57

58
#define CATMODULE "xslt"
59

60 61 62
#include "logging.h"

typedef struct {
63 64 65 66
    char *filename;
    time_t last_modified;
    time_t cache_age;
    xsltStylesheetPtr stylesheet;
67 68
} stylesheet_cache_t;

69 70 71 72 73 74 75
#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)
76
        return (0);
77

78
    buf = xmlAllocOutputBuffer(NULL);
79 80

    if (buf == NULL)
81
        return (-1);
82 83
    xsltSaveResultTo(buf, result, style);
    if (buf->conv != NULL) {
84 85
        *doc_txt_len = buf->conv->use;
        *doc_txt_ptr = xmlStrndup(buf->conv->content, *doc_txt_len);
86
    } else {
87 88
        *doc_txt_len = buf->buffer->use;
        *doc_txt_ptr = xmlStrndup(buf->buffer->content, *doc_txt_len);
89
    }
90
    (void) xmlOutputBufferClose(buf);
91 92 93 94
    return 0;
}
#endif

95 96 97
/* Keep it small... */
#define CACHESIZE 3

98 99
static stylesheet_cache_t cache[CACHESIZE];
static mutex_t xsltlock;
100

101 102
/* Reference to the original xslt loader func */
static xsltDocLoaderFunc xslt_loader;
103 104
/* Admin URI cache */
static xmlChar *admin_URI = NULL;
105

106
void xslt_initialize(void)
107
{
108
    memset(cache, 0, sizeof(stylesheet_cache_t) * CACHESIZE);
109
    thread_mutex_create(&xsltlock);
110 111
    xmlInitParser();
    LIBXML_TEST_VERSION
112 113
    xmlSubstituteEntitiesDefault(1);
    xmlLoadExtDtdDefaultValue = 1;
114
    xslt_loader = xsltDocDefaultLoader;
115 116
}

117
void xslt_shutdown(void) {
118

119
    xslt_clear_cache();
120

121
    thread_mutex_destroy (&xsltlock);
122
    xmlCleanupParser();
123
    xsltCleanupGlobals();
124 125
    if (admin_URI)
        xmlFree(admin_URI);
126 127
}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
static void clear_cache_entry(size_t idx) {
    free(cache[idx].filename);
    if (cache[idx].stylesheet)
        xsltFreeStylesheet(cache[idx].stylesheet);
}

void xslt_clear_cache(void)
{
    size_t i;

    ICECAST_LOG_DEBUG("Clearing stylesheet cache.");

    thread_mutex_lock(&xsltlock);

    for (i = 0; i < CACHESIZE; i++)
        clear_cache_entry(i);

145 146 147 148 149
    if (admin_URI) {
        xmlFree(admin_URI);
        admin_URI = NULL;
    }

150 151 152
    thread_mutex_unlock(&xsltlock);
}

153
static int evict_cache_entry(void) {
brendan's avatar
brendan committed
154
    int i, age=0, oldest=0;
155 156 157 158 159 160 161 162

    for(i=0; i < CACHESIZE; i++) {
        if(cache[i].cache_age > age) {
            age = cache[i].cache_age;
            oldest = i;
        }
    }

163
    clear_cache_entry(oldest);
164 165 166 167

    return oldest;
}

168
static xsltStylesheetPtr xslt_get_stylesheet(const char *fn) {
169 170 171 172
    int i;
    int empty = -1;
    struct stat file;

173 174
    ICECAST_LOG_DEBUG("Looking up stylesheet file \"%s\".", fn);

Philipp Schafft's avatar
Philipp Schafft committed
175
    if (stat(fn, &file) != 0) {
176
        ICECAST_LOG_WARN("Error checking for stylesheet file \"%s\": %s", fn,
177
                strerror(errno));
178 179 180
        return NULL;
    }

Philipp Schafft's avatar
Philipp Schafft committed
181
    for (i = 0; i < CACHESIZE; i++) {
Philipp Schafft's avatar
Philipp Schafft committed
182
        if(cache[i].filename) {
183
#ifdef _WIN32
Philipp Schafft's avatar
Philipp Schafft committed
184
            if(!stricmp(fn, cache[i].filename)) {
185
#else
Philipp Schafft's avatar
Philipp Schafft committed
186
            if(!strcmp(fn, cache[i].filename)) {
187
#endif
Philipp Schafft's avatar
Philipp Schafft committed
188
                if(file.st_mtime > cache[i].last_modified) {
189
                    ICECAST_LOG_DEBUG("Source file newer than cached copy. Reloading slot %i", i);
190
                    xsltFreeStylesheet(cache[i].stylesheet);
191

192
                    cache[i].last_modified = file.st_mtime;
193
                    cache[i].stylesheet = xsltParseStylesheetFile(XMLSTR(fn));
194 195
                    cache[i].cache_age = time(NULL);
                }
196
                ICECAST_LOG_DEBUG("Using cached sheet %i", i);
197 198
                return cache[i].stylesheet;
            }
Philipp Schafft's avatar
Philipp Schafft committed
199
        } else {
200
            empty = i;
Philipp Schafft's avatar
Philipp Schafft committed
201
        }
202 203
    }

Philipp Schafft's avatar
Philipp Schafft committed
204
    if (empty >= 0) {
205
        i = empty;
206
        ICECAST_LOG_DEBUG("Using empty slot %i", i);
Philipp Schafft's avatar
Philipp Schafft committed
207
    } else {
208
        i = evict_cache_entry();
209
        ICECAST_LOG_DEBUG("Using evicted slot %i", i);
Philipp Schafft's avatar
Philipp Schafft committed
210
    }
211 212 213

    cache[i].last_modified = file.st_mtime;
    cache[i].filename = strdup(fn);
214
    cache[i].stylesheet = xsltParseStylesheetFile(XMLSTR(fn));
215
    cache[i].cache_age = time(NULL);
Philipp Schafft's avatar
Philipp Schafft committed
216

217 218
    return cache[i].stylesheet;
}
219

220 221 222 223 224 225 226 227
/* Custom xslt loader */
static xmlDocPtr custom_loader(const        xmlChar *URI,
                               xmlDictPtr   dict,
                               int          options,
                               void        *ctxt,
                               xsltLoadType type)
{
    xmlDocPtr ret;
228
    xmlChar *rel_URI, *fn, *final_URI = NULL;
229
    char *path_URI = NULL;
230
    xsltStylesheet *c;
231 232
    ice_config_t *config;

233 234 235
    switch (type) {
        /* In case an include is loaded */
        case XSLT_LOAD_STYLESHEET:
236 237 238 239 240 241
            /* URI is an escaped URI, make an unescaped version */
            path_URI = util_url_unescape((const char*)URI);
            /* Error if we can't unescape */
            if (path_URI == NULL)
                return NULL;

242
            /* Not look in admindir if the include file exists */
243 244
            if (access(path_URI, F_OK) == 0) {
                free(path_URI);
245
                break;
246 247
            }
            free(path_URI);
248

249 250 251 252
            c = (xsltStylesheet *) ctxt;
            /* Check if we actually have context/path */
            if (ctxt == NULL || c->doc->URL == NULL)
                break;
253

254
            /* Construct the right path */
255 256 257
            rel_URI = xmlBuildRelativeURI(URI, c->doc->URL);
            if (rel_URI != NULL && admin_URI != NULL) {
                fn = xmlBuildURI(rel_URI, admin_URI);
258
                final_URI = fn;
259
                xmlFree(rel_URI);
260
            }
261

262 263
            /* Fail if there was an error constructing the path */
            if (final_URI == NULL) {
264 265
                if (rel_URI)
                    xmlFree(rel_URI);
266
                return NULL;
267 268
            }
        break;
269

270 271
        /* In case a top stylesheet is loaded */
        case XSLT_LOAD_START:
272 273 274 275
            /* Check if the admin URI is already cached */
            if (admin_URI != NULL) {
                break;
            }
276

277
            config = config_get_config();
278
                /* Append path separator to path */
279
                size_t len = strlen(config->adminroot_dir);
280 281
                xmlChar* admin_path = xmlMalloc(len+2);
                xmlStrPrintf(admin_path, len+2, "%s/", XMLSTR(config->adminroot_dir));
282

283 284 285
                /* Convert admin path to URI */
                admin_URI = xmlPathToURI(admin_path);
                xmlFree(admin_path);
286

287 288 289 290 291
                if (!admin_URI) {
                    return NULL;
                } else {
                    ICECAST_LOG_DEBUG("Loaded and cached admin_URI \"%s\"", admin_URI);
                }
292 293 294
            config_release_config();
        break;

295
        /* Avoid warnings about other events we don't care for */
296 297 298
        default:
        break;
    }
299

300
    /* Get the actual xmlDoc */
301
    if (final_URI) {
302
        ICECAST_LOG_DEBUG("Calling xslt_loader() for \"%s\" (was: \"%s\").", final_URI, URI);
303 304 305
        ret = xslt_loader(final_URI, dict, options, ctxt, type);
        xmlFree(final_URI);
    } else {
306
        ICECAST_LOG_DEBUG("Calling xslt_loader() for \"%s\".", URI);
307 308
        ret = xslt_loader(URI, dict, options, ctxt, type);
    }
309 310 311
    return ret;
}

312 313 314 315 316 317 318 319 320
static inline void _send_error(client_t *client, icecast_error_id_t id, int old_status) {
    if (old_status >= 400) {
        client_send_error_by_id(client, ICECAST_ERROR_RECURSIVE_ERROR);
        return;
    }

    client_send_error_by_id(client, id);
}

321
void xslt_transform(xmlDocPtr doc, const char *xslfilename, client_t *client, int status)
322
{
323
    xmlDocPtr res;
324
    xsltStylesheetPtr cur;
325
    xmlChar *string;
326
    int len, problem = 0;
327
    const char *mediatype = NULL;
328
    const char *charset = NULL;
329

330 331
    xmlSetGenericErrorFunc("", log_parse_failure);
    xsltSetGenericErrorFunc("", log_parse_failure);
332
    xsltSetLoaderFunc(custom_loader);
333

334 335 336
    thread_mutex_lock(&xsltlock);
    cur = xslt_get_stylesheet(xslfilename);

337 338
    if (cur == NULL)
    {
339
        thread_mutex_unlock(&xsltlock);
340
        ICECAST_LOG_ERROR("problem reading stylesheet \"%s\"", xslfilename);
341
        _send_error(client, ICECAST_ERROR_XSLT_PARSE, status);
342
        return;
343
    }
344

345
    res = xsltApplyStylesheet(cur, doc, NULL);
346 347 348 349
    if (res != NULL) {
        if (xsltSaveResultToString(&string, &len, res, cur) < 0)
            problem = 1;
    } else {
350
        problem = 1;
351
    }
352

353 354 355 356
    /* lets find out the content type and character encoding to use */
    if (cur->encoding)
       charset = (char *)cur->encoding;

357 358 359 360 361
    if (cur->mediaType)
        mediatype = (char *)cur->mediaType;
    else
    {
        /* check method for the default, a missing method assumes xml */
362
        if (cur->method && xmlStrcmp (cur->method, XMLSTR("html")) == 0)
363 364
            mediatype = "text/html";
        else
365
            if (cur->method && xmlStrcmp (cur->method, XMLSTR("text")) == 0)
366 367 368 369
                mediatype = "text/plain";
            else
                mediatype = "text/xml";
    }
370
    if (problem == 0)
371
    {
372 373 374
        ssize_t ret;
        int failed = 0;
        refbuf_t *refbuf;
375
        ssize_t full_len = strlen(mediatype) + (ssize_t)len + (ssize_t)1024;
376 377
        if (full_len < 4096)
            full_len = 4096;
378
        refbuf = refbuf_new (full_len);
379

380
        if (string == NULL)
381
            string = xmlCharStrdup ("");
382
        ret = util_http_build_header(refbuf->data, full_len, 0, 0, status, NULL, mediatype, charset, NULL, NULL, client);
383 384
        if (ret == -1) {
            ICECAST_LOG_ERROR("Dropping client as we can not build response headers.");
385
            _send_error(client, ICECAST_ERROR_GEN_HEADER_GEN_FAILED, status);
386
        } else {
387
            if ( full_len < (ret + (ssize_t)len + (ssize_t)64) ) {
388
                void *new_data;
389
                full_len = ret + (ssize_t)len + (ssize_t)64;
390 391 392 393 394
                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;
395
                    ret = util_http_build_header(refbuf->data, full_len, 0, 0, status, NULL, mediatype, charset, NULL, NULL, client);
396 397
                    if (ret == -1) {
                        ICECAST_LOG_ERROR("Dropping client as we can not build response headers.");
398
                        _send_error(client, ICECAST_ERROR_GEN_HEADER_GEN_FAILED, status);
399 400 401 402
                        failed = 1;
                    }
                } else {
                    ICECAST_LOG_ERROR("Client buffer reallocation failed. Dropping client.");
403
                    _send_error(client, ICECAST_ERROR_GEN_BUFFER_REALLOC, status);
404 405 406 407 408 409 410
                    failed = 1;
                }
            }

            if (!failed) {
                  snprintf(refbuf->data + ret, full_len - ret, "Content-Length: %d\r\n\r\n%s", len, string);

411
                client->respcode = status;
412 413 414 415 416 417
                client_set_queue (client, NULL);
                client->refbuf = refbuf;
                refbuf->len = strlen (refbuf->data);
                fserve_add_client (client, NULL);
            }
        }
418 419
        xmlFree (string);
    }
420 421
    else
    {
422
        ICECAST_LOG_WARN("problem applying stylesheet \"%s\"", xslfilename);
423
        _send_error(client, ICECAST_ERROR_XSLT_problem, status);
424
    }
425
    thread_mutex_unlock (&xsltlock);
426 427 428
    xmlFreeDoc(res);
}