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

Marvin Scholz's avatar
Marvin Scholz committed
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"
Karl Heyes's avatar
Karl Heyes committed
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
static void clear_cache_entry(size_t idx) {
    free(cache[idx].filename);
    if (cache[idx].stylesheet)
        xsltFreeStylesheet(cache[idx].stylesheet);
132
    cache[idx].filename = NULL;
133 134 135 136 137 138 139 140 141 142 143 144 145
}

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

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

151 152 153
    thread_mutex_unlock(&xsltlock);
}

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

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

164
    clear_cache_entry(oldest);
165 166 167 168

    return oldest;
}

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

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

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

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

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

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

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

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

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

234 235 236
    switch (type) {
        /* In case an include is loaded */
        case XSLT_LOAD_STYLESHEET:
237 238 239 240 241 242
            /* 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;

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

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

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

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

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

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

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

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

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

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

313 314 315 316 317 318 319 320 321
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);
}

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

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

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

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

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

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

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

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

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

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