xslt.c 12.2 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
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);

    thread_mutex_unlock(&xsltlock);
}

148
static int evict_cache_entry(void) {
brendan's avatar
brendan committed
149
    int i, age=0, oldest=0;
150 151 152 153 154 155 156 157

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

158
    clear_cache_entry(oldest);
159 160 161 162

    return oldest;
}

163
static xsltStylesheetPtr xslt_get_stylesheet(const char *fn) {
164 165 166 167
    int i;
    int empty = -1;
    struct stat file;

168 169
    ICECAST_LOG_DEBUG("Looking up stylesheet file \"%s\".", fn);

Philipp Schafft's avatar
Philipp Schafft committed
170
    if (stat(fn, &file) != 0) {
171
        ICECAST_LOG_WARN("Error checking for stylesheet file \"%s\": %s", fn,
172
                strerror(errno));
173 174 175
        return NULL;
    }

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

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

Philipp Schafft's avatar
Philipp Schafft committed
199
    if (empty >= 0) {
200
        i = empty;
201
        ICECAST_LOG_DEBUG("Using empty slot %i", i);
Philipp Schafft's avatar
Philipp Schafft committed
202
    } else {
203
        i = evict_cache_entry();
204
        ICECAST_LOG_DEBUG("Using evicted slot %i", i);
Philipp Schafft's avatar
Philipp Schafft committed
205
    }
206 207 208

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

212 213
    return cache[i].stylesheet;
}
214

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

228 229 230
    switch (type) {
        /* In case an include is loaded */
        case XSLT_LOAD_STYLESHEET:
231 232 233 234 235 236
            /* 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;

237
            /* Not look in admindir if the include file exists */
238 239
            if (access(path_URI, F_OK) == 0) {
                free(path_URI);
240
                break;
241 242
            }
            free(path_URI);
243

244 245 246 247
            c = (xsltStylesheet *) ctxt;
            /* Check if we actually have context/path */
            if (ctxt == NULL || c->doc->URL == NULL)
                break;
248

249
            /* Construct the right path */
250 251 252
            rel_URI = xmlBuildRelativeURI(URI, c->doc->URL);
            if (rel_URI != NULL && admin_URI != NULL) {
                fn = xmlBuildURI(rel_URI, admin_URI);
253
                final_URI = fn;
254
                xmlFree(rel_URI);
255
            }
256

257 258
            /* Fail if there was an error constructing the path */
            if (final_URI == NULL) {
259 260
                if (rel_URI)
                    xmlFree(rel_URI);
261
                return NULL;
262 263
            }
        break;
264

265 266
        /* In case a top stylesheet is loaded */
        case XSLT_LOAD_START:
267
            config = config_get_config();
268 269 270 271

            /* Check if we need to load the admin path */
            if (!admin_URI) {
                /* Append path separator to path */
272
                size_t len = strlen(config->adminroot_dir);
273 274
                xmlChar* admin_path = xmlMalloc(len+2);
                xmlStrPrintf(admin_path, len+2, "%s/", XMLSTR(config->adminroot_dir));
275

276 277 278
                /* Convert admin path to URI */
                admin_URI = xmlPathToURI(admin_path);
                xmlFree(admin_path);
279

280 281 282 283 284
                if (!admin_URI) {
                    return NULL;
                } else {
                    ICECAST_LOG_DEBUG("Loaded and cached admin_URI \"%s\"", admin_URI);
                }
285
            }
286 287 288
            config_release_config();
        break;

289
        /* Avoid warnings about other events we don't care for */
290 291 292
        default:
        break;
    }
293

294
    /* Get the actual xmlDoc */
295
    if (final_URI) {
296
        ICECAST_LOG_DEBUG("Calling xslt_loader() for \"%s\" (was: \"%s\").", final_URI, URI);
297 298 299
        ret = xslt_loader(final_URI, dict, options, ctxt, type);
        xmlFree(final_URI);
    } else {
300
        ICECAST_LOG_DEBUG("Calling xslt_loader() for \"%s\".", URI);
301 302
        ret = xslt_loader(URI, dict, options, ctxt, type);
    }
303 304 305
    return ret;
}

306 307 308 309 310 311 312 313 314
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);
}

315
void xslt_transform(xmlDocPtr doc, const char *xslfilename, client_t *client, int status)
316
{
317
    xmlDocPtr res;
318
    xsltStylesheetPtr cur;
319
    xmlChar *string;
320
    int len, problem = 0;
321
    const char *mediatype = NULL;
322
    const char *charset = NULL;
323

324 325
    xmlSetGenericErrorFunc("", log_parse_failure);
    xsltSetGenericErrorFunc("", log_parse_failure);
326
    xsltSetLoaderFunc(custom_loader);
327

328 329 330
    thread_mutex_lock(&xsltlock);
    cur = xslt_get_stylesheet(xslfilename);

331 332
    if (cur == NULL)
    {
333
        thread_mutex_unlock(&xsltlock);
334
        ICECAST_LOG_ERROR("problem reading stylesheet \"%s\"", xslfilename);
335
        _send_error(client, ICECAST_ERROR_XSLT_PARSE, status);
336
        return;
337
    }
338

339
    res = xsltApplyStylesheet(cur, doc, NULL);
340 341 342 343
    if (res != NULL) {
        if (xsltSaveResultToString(&string, &len, res, cur) < 0)
            problem = 1;
    } else {
344
        problem = 1;
345
    }
346

347 348 349 350
    /* lets find out the content type and character encoding to use */
    if (cur->encoding)
       charset = (char *)cur->encoding;

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

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

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

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