mod_proxy_html.c revision 1861c6caddf9043aff6d7b1a9e2ffb515be652ff
af84459fbf938e508fd10b01cb8d699c79083813takashi/* Copyright (c) 2003-11, WebThing Ltd
af84459fbf938e508fd10b01cb8d699c79083813takashi * Copyright (c) 2011-, The Apache Software Foundation
af84459fbf938e508fd10b01cb8d699c79083813takashi *
af84459fbf938e508fd10b01cb8d699c79083813takashi * Licensed to the Apache Software Foundation (ASF) under one or more
af84459fbf938e508fd10b01cb8d699c79083813takashi * contributor license agreements. See the NOTICE file distributed with
af84459fbf938e508fd10b01cb8d699c79083813takashi * this work for additional information regarding copyright ownership.
af84459fbf938e508fd10b01cb8d699c79083813takashi * The ASF licenses this file to You under the Apache License, Version 2.0
af84459fbf938e508fd10b01cb8d699c79083813takashi * (the "License"); you may not use this file except in compliance with
af84459fbf938e508fd10b01cb8d699c79083813takashi * the License. You may obtain a copy of the License at
af84459fbf938e508fd10b01cb8d699c79083813takashi *
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * http://www.apache.org/licenses/LICENSE-2.0
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen *
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * Unless required by applicable law or agreed to in writing, software
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * distributed under the License is distributed on an "AS IS" BASIS,
af84459fbf938e508fd10b01cb8d699c79083813takashi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
af84459fbf938e508fd10b01cb8d699c79083813takashi * See the License for the specific language governing permissions and
af84459fbf938e508fd10b01cb8d699c79083813takashi * limitations under the License.
3f08db06526d6901aa08c110b5bc7dde6bc39905nd */
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi/* GO_FASTER
af84459fbf938e508fd10b01cb8d699c79083813takashi You can #define GO_FASTER to disable trace logging.
3f08db06526d6901aa08c110b5bc7dde6bc39905nd*/
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi#ifdef GO_FASTER
af84459fbf938e508fd10b01cb8d699c79083813takashi#define VERBOSE(x)
af84459fbf938e508fd10b01cb8d699c79083813takashi#define VERBOSEB(x)
af84459fbf938e508fd10b01cb8d699c79083813takashi#else
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung#define VERBOSE(x) if (verbose) x
af84459fbf938e508fd10b01cb8d699c79083813takashi#define VERBOSEB(x) if (verbose) {x}
af84459fbf938e508fd10b01cb8d699c79083813takashi#endif
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi#include <ctype.h>
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi/* libxml2 */
af84459fbf938e508fd10b01cb8d699c79083813takashi#include <libxml/HTMLparser.h>
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "http_protocol.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "http_config.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "http_log.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "apr_strings.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "apr_hash.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "apr_strmatch.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "apr_optional.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "mod_xml2enc.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "http_request.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi#include "ap_expr.h"
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi/* globals set once at startup */
af84459fbf938e508fd10b01cb8d699c79083813takashistatic ap_rxplus_t* old_expr;
af84459fbf938e508fd10b01cb8d699c79083813takashistatic ap_regex_t* seek_meta;
af84459fbf938e508fd10b01cb8d699c79083813takashistatic const apr_strmatch_pattern* seek_content;
af84459fbf938e508fd10b01cb8d699c79083813takashistatic apr_status_t (*xml2enc_charset)(request_rec*, xmlCharEncoding*, const char**) = NULL;
af84459fbf938e508fd10b01cb8d699c79083813takashistatic apr_status_t (*xml2enc_filter)(request_rec*, const char*, unsigned int) = NULL;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashimodule AP_MODULE_DECLARE_DATA proxy_html_module;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi#define M_HTML 0x01
af84459fbf938e508fd10b01cb8d699c79083813takashi#define M_EVENTS 0x02
af84459fbf938e508fd10b01cb8d699c79083813takashi#define M_CDATA 0x04
af84459fbf938e508fd10b01cb8d699c79083813takashi#define M_REGEX 0x08
af84459fbf938e508fd10b01cb8d699c79083813takashi#define M_ATSTART 0x10
af84459fbf938e508fd10b01cb8d699c79083813takashi#define M_ATEND 0x20
af84459fbf938e508fd10b01cb8d699c79083813takashi#define M_LAST 0x40
3c13a815670b54d1c17bf02954f7d2b066cde95cnd#define M_NOTLAST 0x80
3c13a815670b54d1c17bf02954f7d2b066cde95cnd#define M_INTERPOLATE_TO 0x100
af84459fbf938e508fd10b01cb8d699c79083813takashi#define M_INTERPOLATE_FROM 0x200
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashitypedef struct {
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* val;
af84459fbf938e508fd10b01cb8d699c79083813takashi} tattr;
af84459fbf938e508fd10b01cb8d699c79083813takashitypedef struct {
af84459fbf938e508fd10b01cb8d699c79083813takashi unsigned int start;
af84459fbf938e508fd10b01cb8d699c79083813takashi unsigned int end;
af84459fbf938e508fd10b01cb8d699c79083813takashi} meta;
af84459fbf938e508fd10b01cb8d699c79083813takashitypedef struct urlmap {
af84459fbf938e508fd10b01cb8d699c79083813takashi struct urlmap* next;
af84459fbf938e508fd10b01cb8d699c79083813takashi unsigned int flags;
af84459fbf938e508fd10b01cb8d699c79083813takashi unsigned int regflags;
af84459fbf938e508fd10b01cb8d699c79083813takashi union {
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* c;
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_regex_t* r;
af84459fbf938e508fd10b01cb8d699c79083813takashi } from;
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* to;
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_expr_info_t *cond;
af84459fbf938e508fd10b01cb8d699c79083813takashi} urlmap;
af84459fbf938e508fd10b01cb8d699c79083813takashitypedef struct {
af84459fbf938e508fd10b01cb8d699c79083813takashi urlmap* map;
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* doctype;
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* etag;
3c13a815670b54d1c17bf02954f7d2b066cde95cnd unsigned int flags;
3c13a815670b54d1c17bf02954f7d2b066cde95cnd size_t bufsz;
af84459fbf938e508fd10b01cb8d699c79083813takashi apr_hash_t* links;
3c13a815670b54d1c17bf02954f7d2b066cde95cnd apr_array_header_t* events;
3c13a815670b54d1c17bf02954f7d2b066cde95cnd const char* charset_out;
af84459fbf938e508fd10b01cb8d699c79083813takashi int extfix;
3c13a815670b54d1c17bf02954f7d2b066cde95cnd int metafix;
af84459fbf938e508fd10b01cb8d699c79083813takashi int strip_comments;
af84459fbf938e508fd10b01cb8d699c79083813takashi int interp;
af84459fbf938e508fd10b01cb8d699c79083813takashi int enabled;
af84459fbf938e508fd10b01cb8d699c79083813takashi} proxy_html_conf;
af84459fbf938e508fd10b01cb8d699c79083813takashitypedef struct {
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_filter_t* f;
af84459fbf938e508fd10b01cb8d699c79083813takashi proxy_html_conf* cfg;
3c13a815670b54d1c17bf02954f7d2b066cde95cnd htmlParserCtxtPtr parser;
af84459fbf938e508fd10b01cb8d699c79083813takashi apr_bucket_brigade* bb;
af84459fbf938e508fd10b01cb8d699c79083813takashi char* buf;
af84459fbf938e508fd10b01cb8d699c79083813takashi size_t offset;
af84459fbf938e508fd10b01cb8d699c79083813takashi size_t avail;
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* encoding;
af84459fbf938e508fd10b01cb8d699c79083813takashi urlmap* map;
af84459fbf938e508fd10b01cb8d699c79083813takashi} saxctxt;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi#define NORM_LC 0x1
af84459fbf938e508fd10b01cb8d699c79083813takashi#define NORM_MSSLASH 0x2
af84459fbf938e508fd10b01cb8d699c79083813takashi#define NORM_RESET 0x4
af84459fbf938e508fd10b01cb8d699c79083813takashistatic htmlSAXHandler sax;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashitypedef enum { ATTR_IGNORE, ATTR_URI, ATTR_EVENT } rewrite_t;
af84459fbf938e508fd10b01cb8d699c79083813takashi
3c13a815670b54d1c17bf02954f7d2b066cde95cndstatic const char* const fpi_html =
af84459fbf938e508fd10b01cb8d699c79083813takashi "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n";
af84459fbf938e508fd10b01cb8d699c79083813takashistatic const char* const fpi_html_legacy =
af84459fbf938e508fd10b01cb8d699c79083813takashi "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
af84459fbf938e508fd10b01cb8d699c79083813takashistatic const char* const fpi_xhtml =
3c13a815670b54d1c17bf02954f7d2b066cde95cnd "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
af84459fbf938e508fd10b01cb8d699c79083813takashistatic const char* const fpi_xhtml_legacy =
af84459fbf938e508fd10b01cb8d699c79083813takashi "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
af84459fbf938e508fd10b01cb8d699c79083813takashistatic const char* const html_etag = ">";
af84459fbf938e508fd10b01cb8d699c79083813takashistatic const char* const xhtml_etag = " />";
af84459fbf938e508fd10b01cb8d699c79083813takashi/*#define DEFAULT_DOCTYPE fpi_html */
af84459fbf938e508fd10b01cb8d699c79083813takashistatic const char* const DEFAULT_DOCTYPE = "";
af84459fbf938e508fd10b01cb8d699c79083813takashi#define DEFAULT_ETAG html_etag
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashistatic void normalise(unsigned int flags, char* str)
af84459fbf938e508fd10b01cb8d699c79083813takashi{
af84459fbf938e508fd10b01cb8d699c79083813takashi char* p;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (flags & NORM_LC)
af84459fbf938e508fd10b01cb8d699c79083813takashi for (p = str; *p; ++p)
af84459fbf938e508fd10b01cb8d699c79083813takashi if (isupper(*p))
af84459fbf938e508fd10b01cb8d699c79083813takashi *p = tolower(*p);
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi if (flags & NORM_MSSLASH)
af84459fbf938e508fd10b01cb8d699c79083813takashi for (p = ap_strchr(str, '\\'); p; p = ap_strchr(p+1, '\\'))
af84459fbf938e508fd10b01cb8d699c79083813takashi *p = '/';
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi}
af84459fbf938e508fd10b01cb8d699c79083813takashi#define consume_buffer(ctx,inbuf,bytes,flag) \
af84459fbf938e508fd10b01cb8d699c79083813takashi htmlParseChunk(ctx->parser, inbuf, bytes, flag)
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi#define AP_fwrite(ctx,inbuf,bytes,flush) \
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_fwrite(ctx->f->next, ctx->bb, inbuf, bytes);
3c13a815670b54d1c17bf02954f7d2b066cde95cnd
af84459fbf938e508fd10b01cb8d699c79083813takashi/* This is always utf-8 on entry. We can convert charset within FLUSH */
af84459fbf938e508fd10b01cb8d699c79083813takashi#define FLUSH AP_fwrite(ctx, (chars+begin), (i-begin), 0); begin = i+1
af84459fbf938e508fd10b01cb8d699c79083813takashistatic void pcharacters(void* ctxt, const xmlChar *uchars, int length)
3c13a815670b54d1c17bf02954f7d2b066cde95cnd{
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* chars = (const char*) uchars;
af84459fbf938e508fd10b01cb8d699c79083813takashi saxctxt* ctx = (saxctxt*) ctxt;
af84459fbf938e508fd10b01cb8d699c79083813takashi int i;
af84459fbf938e508fd10b01cb8d699c79083813takashi int begin;
af84459fbf938e508fd10b01cb8d699c79083813takashi for (begin=i=0; i<length; i++) {
af84459fbf938e508fd10b01cb8d699c79083813takashi switch (chars[i]) {
af84459fbf938e508fd10b01cb8d699c79083813takashi case '&' : FLUSH; ap_fputs(ctx->f->next, ctx->bb, "&amp;"); break;
af84459fbf938e508fd10b01cb8d699c79083813takashi case '<' : FLUSH; ap_fputs(ctx->f->next, ctx->bb, "&lt;"); break;
af84459fbf938e508fd10b01cb8d699c79083813takashi case '>' : FLUSH; ap_fputs(ctx->f->next, ctx->bb, "&gt;"); break;
af84459fbf938e508fd10b01cb8d699c79083813takashi case '"' : FLUSH; ap_fputs(ctx->f->next, ctx->bb, "&quot;"); break;
af84459fbf938e508fd10b01cb8d699c79083813takashi default : break;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi FLUSH;
af84459fbf938e508fd10b01cb8d699c79083813takashi}
af84459fbf938e508fd10b01cb8d699c79083813takashistatic void preserve(saxctxt* ctx, const size_t len)
af84459fbf938e508fd10b01cb8d699c79083813takashi{
af84459fbf938e508fd10b01cb8d699c79083813takashi char* newbuf;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (len <= (ctx->avail - ctx->offset))
af84459fbf938e508fd10b01cb8d699c79083813takashi return;
af84459fbf938e508fd10b01cb8d699c79083813takashi else while (len > (ctx->avail - ctx->offset))
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->avail += ctx->cfg->bufsz;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi newbuf = realloc(ctx->buf, ctx->avail);
af84459fbf938e508fd10b01cb8d699c79083813takashi if (newbuf != ctx->buf) {
af84459fbf938e508fd10b01cb8d699c79083813takashi if (ctx->buf)
af84459fbf938e508fd10b01cb8d699c79083813takashi apr_pool_cleanup_kill(ctx->f->r->pool, ctx->buf,
af84459fbf938e508fd10b01cb8d699c79083813takashi (int(*)(void*))free);
af84459fbf938e508fd10b01cb8d699c79083813takashi apr_pool_cleanup_register(ctx->f->r->pool, newbuf,
af84459fbf938e508fd10b01cb8d699c79083813takashi (int(*)(void*))free, apr_pool_cleanup_null);
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->buf = newbuf;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi}
af84459fbf938e508fd10b01cb8d699c79083813takashistatic void pappend(saxctxt* ctx, const char* buf, const size_t len)
af84459fbf938e508fd10b01cb8d699c79083813takashi{
af84459fbf938e508fd10b01cb8d699c79083813takashi preserve(ctx, len);
af84459fbf938e508fd10b01cb8d699c79083813takashi memcpy(ctx->buf+ctx->offset, buf, len);
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->offset += len;
af84459fbf938e508fd10b01cb8d699c79083813takashi}
af84459fbf938e508fd10b01cb8d699c79083813takashistatic void dump_content(saxctxt* ctx)
af84459fbf938e508fd10b01cb8d699c79083813takashi{
af84459fbf938e508fd10b01cb8d699c79083813takashi urlmap* m;
af84459fbf938e508fd10b01cb8d699c79083813takashi char* found;
af84459fbf938e508fd10b01cb8d699c79083813takashi size_t s_from, s_to;
af84459fbf938e508fd10b01cb8d699c79083813takashi size_t match;
af84459fbf938e508fd10b01cb8d699c79083813takashi char c = 0;
af84459fbf938e508fd10b01cb8d699c79083813takashi int nmatch;
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_regmatch_t pmatch[10];
af84459fbf938e508fd10b01cb8d699c79083813takashi char* subs;
ec5fd46fd2a4a263bb5cdf419e8d4106007a2ac8gryzor size_t len, offs;
af84459fbf938e508fd10b01cb8d699c79083813takashi urlmap* themap = ctx->map;
af84459fbf938e508fd10b01cb8d699c79083813takashi#ifndef GO_FASTER
af84459fbf938e508fd10b01cb8d699c79083813takashi int verbose = APLOGrtrace1(ctx->f->r);
af84459fbf938e508fd10b01cb8d699c79083813takashi#endif
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi pappend(ctx, &c, 1); /* append null byte */
af84459fbf938e508fd10b01cb8d699c79083813takashi /* parse the text for URLs */
af84459fbf938e508fd10b01cb8d699c79083813takashi for (m = themap; m; m = m->next) {
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!(m->flags & M_CDATA))
af84459fbf938e508fd10b01cb8d699c79083813takashi continue;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (m->flags & M_REGEX) {
af84459fbf938e508fd10b01cb8d699c79083813takashi nmatch = 10;
af84459fbf938e508fd10b01cb8d699c79083813takashi offs = 0;
af84459fbf938e508fd10b01cb8d699c79083813takashi while (!ap_regexec(m->from.r, ctx->buf+offs, nmatch, pmatch, 0)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi match = pmatch[0].rm_so;
af84459fbf938e508fd10b01cb8d699c79083813takashi s_from = pmatch[0].rm_eo - match;
af84459fbf938e508fd10b01cb8d699c79083813takashi subs = ap_pregsub(ctx->f->r->pool, m->to, ctx->buf+offs,
af84459fbf938e508fd10b01cb8d699c79083813takashi nmatch, pmatch);
af84459fbf938e508fd10b01cb8d699c79083813takashi s_to = strlen(subs);
af84459fbf938e508fd10b01cb8d699c79083813takashi len = strlen(ctx->buf);
af84459fbf938e508fd10b01cb8d699c79083813takashi offs += match;
af84459fbf938e508fd10b01cb8d699c79083813takashi VERBOSEB(
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* f = apr_pstrndup(ctx->f->r->pool,
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->buf + offs, s_from);
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, ctx->f->r,
af84459fbf938e508fd10b01cb8d699c79083813takashi "C/RX: match at %s, substituting %s", f, subs);
af84459fbf938e508fd10b01cb8d699c79083813takashi )
af84459fbf938e508fd10b01cb8d699c79083813takashi if (s_to > s_from) {
af84459fbf938e508fd10b01cb8d699c79083813takashi preserve(ctx, s_to - s_from);
af84459fbf938e508fd10b01cb8d699c79083813takashi memmove(ctx->buf+offs+s_to, ctx->buf+offs+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi len + 1 - s_from - offs);
af84459fbf938e508fd10b01cb8d699c79083813takashi memcpy(ctx->buf+offs, subs, s_to);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else {
af84459fbf938e508fd10b01cb8d699c79083813takashi memcpy(ctx->buf + offs, subs, s_to);
af84459fbf938e508fd10b01cb8d699c79083813takashi memmove(ctx->buf+offs+s_to, ctx->buf+offs+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi len + 1 - s_from - offs);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi offs += s_to;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else {
af84459fbf938e508fd10b01cb8d699c79083813takashi s_from = strlen(m->from.c);
af84459fbf938e508fd10b01cb8d699c79083813takashi s_to = strlen(m->to);
af84459fbf938e508fd10b01cb8d699c79083813takashi for (found = strstr(ctx->buf, m->from.c); found;
af84459fbf938e508fd10b01cb8d699c79083813takashi found = strstr(ctx->buf+match+s_to, m->from.c)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi match = found - ctx->buf;
af84459fbf938e508fd10b01cb8d699c79083813takashi if ((m->flags & M_ATSTART) && (match != 0))
af84459fbf938e508fd10b01cb8d699c79083813takashi break;
af84459fbf938e508fd10b01cb8d699c79083813takashi len = strlen(ctx->buf);
af84459fbf938e508fd10b01cb8d699c79083813takashi if ((m->flags & M_ATEND) && (match < (len - s_from)))
af84459fbf938e508fd10b01cb8d699c79083813takashi continue;
af84459fbf938e508fd10b01cb8d699c79083813takashi VERBOSE(ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, ctx->f->r,
af84459fbf938e508fd10b01cb8d699c79083813takashi "C: matched %s, substituting %s",
af84459fbf938e508fd10b01cb8d699c79083813takashi m->from.c, m->to));
af84459fbf938e508fd10b01cb8d699c79083813takashi if (s_to > s_from) {
af84459fbf938e508fd10b01cb8d699c79083813takashi preserve(ctx, s_to - s_from);
af84459fbf938e508fd10b01cb8d699c79083813takashi memmove(ctx->buf+match+s_to, ctx->buf+match+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi len + 1 - s_from - match);
af84459fbf938e508fd10b01cb8d699c79083813takashi memcpy(ctx->buf+match, m->to, s_to);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else {
af84459fbf938e508fd10b01cb8d699c79083813takashi memcpy(ctx->buf+match, m->to, s_to);
af84459fbf938e508fd10b01cb8d699c79083813takashi memmove(ctx->buf+match+s_to, ctx->buf+match+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi len + 1 - s_from - match);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
3c13a815670b54d1c17bf02954f7d2b066cde95cnd }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi AP_fwrite(ctx, ctx->buf, strlen(ctx->buf), 1);
af84459fbf938e508fd10b01cb8d699c79083813takashi}
af84459fbf938e508fd10b01cb8d699c79083813takashistatic void pcdata(void* ctxt, const xmlChar *uchars, int length)
af84459fbf938e508fd10b01cb8d699c79083813takashi{
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* chars = (const char*) uchars;
af84459fbf938e508fd10b01cb8d699c79083813takashi saxctxt* ctx = (saxctxt*) ctxt;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (ctx->cfg->extfix) {
af84459fbf938e508fd10b01cb8d699c79083813takashi pappend(ctx, chars, length);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else {
af84459fbf938e508fd10b01cb8d699c79083813takashi /* not sure if this should force-flush
af84459fbf938e508fd10b01cb8d699c79083813takashi * (i.e. can one cdata section come in multiple calls?)
af84459fbf938e508fd10b01cb8d699c79083813takashi */
af84459fbf938e508fd10b01cb8d699c79083813takashi AP_fwrite(ctx, chars, length, 0);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi}
af84459fbf938e508fd10b01cb8d699c79083813takashistatic void pcomment(void* ctxt, const xmlChar *uchars)
af84459fbf938e508fd10b01cb8d699c79083813takashi{
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* chars = (const char*) uchars;
af84459fbf938e508fd10b01cb8d699c79083813takashi saxctxt* ctx = (saxctxt*) ctxt;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (ctx->cfg->strip_comments)
af84459fbf938e508fd10b01cb8d699c79083813takashi return;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi if (ctx->cfg->extfix) {
af84459fbf938e508fd10b01cb8d699c79083813takashi pappend(ctx, "<!--", 4);
af84459fbf938e508fd10b01cb8d699c79083813takashi pappend(ctx, chars, strlen(chars));
af84459fbf938e508fd10b01cb8d699c79083813takashi pappend(ctx, "-->", 3);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else {
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_fputs(ctx->f->next, ctx->bb, "<!--");
af84459fbf938e508fd10b01cb8d699c79083813takashi AP_fwrite(ctx, chars, strlen(chars), 1);
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_fputs(ctx->f->next, ctx->bb, "-->");
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi}
af84459fbf938e508fd10b01cb8d699c79083813takashistatic void pendElement(void* ctxt, const xmlChar* uname)
af84459fbf938e508fd10b01cb8d699c79083813takashi{
af84459fbf938e508fd10b01cb8d699c79083813takashi saxctxt* ctx = (saxctxt*) ctxt;
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* name = (const char*) uname;
af84459fbf938e508fd10b01cb8d699c79083813takashi const htmlElemDesc* desc = htmlTagLookup(uname);
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi if ((ctx->cfg->doctype == fpi_html) || (ctx->cfg->doctype == fpi_xhtml)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi /* enforce html */
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!desc || desc->depr)
af84459fbf938e508fd10b01cb8d699c79083813takashi return;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else if ((ctx->cfg->doctype == fpi_html)
af84459fbf938e508fd10b01cb8d699c79083813takashi || (ctx->cfg->doctype == fpi_xhtml)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi /* enforce html legacy */
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!desc)
af84459fbf938e508fd10b01cb8d699c79083813takashi return;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi /* TODO - implement HTML "allowed here" using the stack */
af84459fbf938e508fd10b01cb8d699c79083813takashi /* nah. Keeping the stack is too much overhead */
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi if (ctx->offset > 0) {
af84459fbf938e508fd10b01cb8d699c79083813takashi dump_content(ctx);
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->offset = 0; /* having dumped it, we can re-use the memory */
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!desc || !desc->empty) {
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_fprintf(ctx->f->next, ctx->bb, "</%s>", name);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi}
af84459fbf938e508fd10b01cb8d699c79083813takashistatic void pstartElement(void* ctxt, const xmlChar* uname,
af84459fbf938e508fd10b01cb8d699c79083813takashi const xmlChar** uattrs)
af84459fbf938e508fd10b01cb8d699c79083813takashi{
af84459fbf938e508fd10b01cb8d699c79083813takashi int required_attrs;
af84459fbf938e508fd10b01cb8d699c79083813takashi int num_match;
af84459fbf938e508fd10b01cb8d699c79083813takashi size_t offs, len;
af84459fbf938e508fd10b01cb8d699c79083813takashi char* subs;
af84459fbf938e508fd10b01cb8d699c79083813takashi rewrite_t is_uri;
af84459fbf938e508fd10b01cb8d699c79083813takashi const char** a;
af84459fbf938e508fd10b01cb8d699c79083813takashi urlmap* m;
af84459fbf938e508fd10b01cb8d699c79083813takashi size_t s_to, s_from, match;
af84459fbf938e508fd10b01cb8d699c79083813takashi char* found;
af84459fbf938e508fd10b01cb8d699c79083813takashi saxctxt* ctx = (saxctxt*) ctxt;
af84459fbf938e508fd10b01cb8d699c79083813takashi size_t nmatch;
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_regmatch_t pmatch[10];
af84459fbf938e508fd10b01cb8d699c79083813takashi#ifndef GO_FASTER
af84459fbf938e508fd10b01cb8d699c79083813takashi int verbose = APLOGrtrace1(ctx->f->r);
af84459fbf938e508fd10b01cb8d699c79083813takashi#endif
af84459fbf938e508fd10b01cb8d699c79083813takashi apr_array_header_t *linkattrs;
af84459fbf938e508fd10b01cb8d699c79083813takashi int i;
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* name = (const char*) uname;
af84459fbf938e508fd10b01cb8d699c79083813takashi const char** attrs = (const char**) uattrs;
af84459fbf938e508fd10b01cb8d699c79083813takashi const htmlElemDesc* desc = htmlTagLookup(uname);
af84459fbf938e508fd10b01cb8d699c79083813takashi urlmap* themap = ctx->map;
af84459fbf938e508fd10b01cb8d699c79083813takashi#ifdef HAVE_STACK
af84459fbf938e508fd10b01cb8d699c79083813takashi const void** descp;
af84459fbf938e508fd10b01cb8d699c79083813takashi#endif
af84459fbf938e508fd10b01cb8d699c79083813takashi int enforce = 0;
af84459fbf938e508fd10b01cb8d699c79083813takashi if ((ctx->cfg->doctype == fpi_html) || (ctx->cfg->doctype == fpi_xhtml)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi /* enforce html */
af84459fbf938e508fd10b01cb8d699c79083813takashi enforce = 2;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!desc || desc->depr)
af84459fbf938e508fd10b01cb8d699c79083813takashi return;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else if ((ctx->cfg->doctype == fpi_html)
af84459fbf938e508fd10b01cb8d699c79083813takashi || (ctx->cfg->doctype == fpi_xhtml)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi enforce = 1;
af84459fbf938e508fd10b01cb8d699c79083813takashi /* enforce html legacy */
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!desc) {
af84459fbf938e508fd10b01cb8d699c79083813takashi return;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!desc && enforce) {
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, ctx->f->r,
af84459fbf938e508fd10b01cb8d699c79083813takashi "Bogus HTML element %s dropped", name);
af84459fbf938e508fd10b01cb8d699c79083813takashi return;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi if (desc && desc->depr && (enforce == 2)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, ctx->f->r,
af84459fbf938e508fd10b01cb8d699c79083813takashi "Deprecated HTML element %s dropped", name);
af84459fbf938e508fd10b01cb8d699c79083813takashi return;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi#ifdef HAVE_STACK
af84459fbf938e508fd10b01cb8d699c79083813takashi descp = apr_array_push(ctx->stack);
af84459fbf938e508fd10b01cb8d699c79083813takashi *descp = desc;
af84459fbf938e508fd10b01cb8d699c79083813takashi /* TODO - implement HTML "allowed here" */
af84459fbf938e508fd10b01cb8d699c79083813takashi#endif
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_fputc(ctx->f->next, ctx->bb, '<');
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_fputs(ctx->f->next, ctx->bb, name);
af84459fbf938e508fd10b01cb8d699c79083813takashi
3c13a815670b54d1c17bf02954f7d2b066cde95cnd required_attrs = 0;
a610901168de82df5fc5d99b8759fd80e0f70aeasf if ((enforce > 0) && (desc != NULL) && (desc->attrs_req != NULL))
af84459fbf938e508fd10b01cb8d699c79083813takashi for (a = desc->attrs_req; *a; a++)
af84459fbf938e508fd10b01cb8d699c79083813takashi ++required_attrs;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi if (attrs) {
af84459fbf938e508fd10b01cb8d699c79083813takashi linkattrs = apr_hash_get(ctx->cfg->links, name, APR_HASH_KEY_STRING);
af84459fbf938e508fd10b01cb8d699c79083813takashi for (a = attrs; *a; a += 2) {
af84459fbf938e508fd10b01cb8d699c79083813takashi if (desc && enforce > 0) {
af84459fbf938e508fd10b01cb8d699c79083813takashi switch (htmlAttrAllowed(desc, (xmlChar*)*a, 2-enforce)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi case HTML_INVALID:
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, ctx->f->r,
af84459fbf938e508fd10b01cb8d699c79083813takashi "Bogus HTML attribute %s of %s dropped",
af84459fbf938e508fd10b01cb8d699c79083813takashi *a, name);
af84459fbf938e508fd10b01cb8d699c79083813takashi continue;
af84459fbf938e508fd10b01cb8d699c79083813takashi case HTML_DEPRECATED:
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, ctx->f->r,
af84459fbf938e508fd10b01cb8d699c79083813takashi "Deprecated HTML attribute %s of %s dropped",
af84459fbf938e508fd10b01cb8d699c79083813takashi *a, name);
af84459fbf938e508fd10b01cb8d699c79083813takashi continue;
af84459fbf938e508fd10b01cb8d699c79083813takashi case HTML_REQUIRED:
af84459fbf938e508fd10b01cb8d699c79083813takashi required_attrs--; /* cross off the number still needed */
af84459fbf938e508fd10b01cb8d699c79083813takashi /* fallthrough - required implies valid */
af84459fbf938e508fd10b01cb8d699c79083813takashi default:
af84459fbf938e508fd10b01cb8d699c79083813takashi break;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->offset = 0;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (a[1]) {
af84459fbf938e508fd10b01cb8d699c79083813takashi pappend(ctx, a[1], strlen(a[1])+1);
3c13a815670b54d1c17bf02954f7d2b066cde95cnd is_uri = ATTR_IGNORE;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (linkattrs) {
af84459fbf938e508fd10b01cb8d699c79083813takashi tattr* attrs = (tattr*) linkattrs->elts;
af84459fbf938e508fd10b01cb8d699c79083813takashi for (i=0; i < linkattrs->nelts; ++i) {
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!strcmp(*a, attrs[i].val)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi is_uri = ATTR_URI;
af84459fbf938e508fd10b01cb8d699c79083813takashi break;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi if ((is_uri == ATTR_IGNORE) && ctx->cfg->extfix
af84459fbf938e508fd10b01cb8d699c79083813takashi && (ctx->cfg->events != NULL)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi for (i=0; i < ctx->cfg->events->nelts; ++i) {
af84459fbf938e508fd10b01cb8d699c79083813takashi tattr* attrs = (tattr*) ctx->cfg->events->elts;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!strcmp(*a, attrs[i].val)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi is_uri = ATTR_EVENT;
af84459fbf938e508fd10b01cb8d699c79083813takashi break;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi switch (is_uri) {
af84459fbf938e508fd10b01cb8d699c79083813takashi case ATTR_URI:
af84459fbf938e508fd10b01cb8d699c79083813takashi num_match = 0;
af84459fbf938e508fd10b01cb8d699c79083813takashi for (m = themap; m; m = m->next) {
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!(m->flags & M_HTML))
af84459fbf938e508fd10b01cb8d699c79083813takashi continue;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (m->flags & M_REGEX) {
af84459fbf938e508fd10b01cb8d699c79083813takashi nmatch = 10;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!ap_regexec(m->from.r, ctx->buf, nmatch,
af84459fbf938e508fd10b01cb8d699c79083813takashi pmatch, 0)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi ++num_match;
af84459fbf938e508fd10b01cb8d699c79083813takashi offs = match = pmatch[0].rm_so;
af84459fbf938e508fd10b01cb8d699c79083813takashi s_from = pmatch[0].rm_eo - match;
af84459fbf938e508fd10b01cb8d699c79083813takashi subs = ap_pregsub(ctx->f->r->pool, m->to,
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->buf, nmatch, pmatch);
af84459fbf938e508fd10b01cb8d699c79083813takashi VERBOSE({
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* f;
af84459fbf938e508fd10b01cb8d699c79083813takashi f = apr_pstrndup(ctx->f->r->pool,
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->buf + offs, s_from);
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0,
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->f->r,
af84459fbf938e508fd10b01cb8d699c79083813takashi "H/RX: match at %s, substituting %s",
af84459fbf938e508fd10b01cb8d699c79083813takashi f, subs);
af84459fbf938e508fd10b01cb8d699c79083813takashi })
af84459fbf938e508fd10b01cb8d699c79083813takashi s_to = strlen(subs);
af84459fbf938e508fd10b01cb8d699c79083813takashi len = strlen(ctx->buf);
3c13a815670b54d1c17bf02954f7d2b066cde95cnd if (s_to > s_from) {
af84459fbf938e508fd10b01cb8d699c79083813takashi preserve(ctx, s_to - s_from);
af84459fbf938e508fd10b01cb8d699c79083813takashi memmove(ctx->buf+offs+s_to,
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->buf+offs+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi len + 1 - s_from - offs);
af84459fbf938e508fd10b01cb8d699c79083813takashi memcpy(ctx->buf+offs, subs, s_to);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else {
af84459fbf938e508fd10b01cb8d699c79083813takashi memcpy(ctx->buf + offs, subs, s_to);
af84459fbf938e508fd10b01cb8d699c79083813takashi memmove(ctx->buf+offs+s_to,
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->buf+offs+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi len + 1 - s_from - offs);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi } else {
af84459fbf938e508fd10b01cb8d699c79083813takashi s_from = strlen(m->from.c);
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!strncasecmp(ctx->buf, m->from.c, s_from)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi ++num_match;
af84459fbf938e508fd10b01cb8d699c79083813takashi s_to = strlen(m->to);
af84459fbf938e508fd10b01cb8d699c79083813takashi len = strlen(ctx->buf);
af84459fbf938e508fd10b01cb8d699c79083813takashi VERBOSE(ap_log_rerror(APLOG_MARK, APLOG_TRACE3,
af84459fbf938e508fd10b01cb8d699c79083813takashi 0, ctx->f->r,
af84459fbf938e508fd10b01cb8d699c79083813takashi "H: matched %s, substituting %s",
af84459fbf938e508fd10b01cb8d699c79083813takashi m->from.c, m->to));
af84459fbf938e508fd10b01cb8d699c79083813takashi if (s_to > s_from) {
af84459fbf938e508fd10b01cb8d699c79083813takashi preserve(ctx, s_to - s_from);
af84459fbf938e508fd10b01cb8d699c79083813takashi memmove(ctx->buf+s_to, ctx->buf+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi len + 1 - s_from);
3c13a815670b54d1c17bf02954f7d2b066cde95cnd memcpy(ctx->buf, m->to, s_to);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else { /* it fits in the existing space */
af84459fbf938e508fd10b01cb8d699c79083813takashi memcpy(ctx->buf, m->to, s_to);
af84459fbf938e508fd10b01cb8d699c79083813takashi memmove(ctx->buf+s_to, ctx->buf+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi len + 1 - s_from);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi break;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi /* URIs only want one match unless overridden in the config */
af84459fbf938e508fd10b01cb8d699c79083813takashi if ((num_match > 0) && !(m->flags & M_NOTLAST))
af84459fbf938e508fd10b01cb8d699c79083813takashi break;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi break;
af84459fbf938e508fd10b01cb8d699c79083813takashi case ATTR_EVENT:
af84459fbf938e508fd10b01cb8d699c79083813takashi for (m = themap; m; m = m->next) {
af84459fbf938e508fd10b01cb8d699c79083813takashi num_match = 0; /* reset here since we're working per-rule */
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!(m->flags & M_EVENTS))
3c13a815670b54d1c17bf02954f7d2b066cde95cnd continue;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (m->flags & M_REGEX) {
af84459fbf938e508fd10b01cb8d699c79083813takashi nmatch = 10;
af84459fbf938e508fd10b01cb8d699c79083813takashi offs = 0;
af84459fbf938e508fd10b01cb8d699c79083813takashi while (!ap_regexec(m->from.r, ctx->buf+offs,
af84459fbf938e508fd10b01cb8d699c79083813takashi nmatch, pmatch, 0)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi match = pmatch[0].rm_so;
af84459fbf938e508fd10b01cb8d699c79083813takashi s_from = pmatch[0].rm_eo - match;
af84459fbf938e508fd10b01cb8d699c79083813takashi subs = ap_pregsub(ctx->f->r->pool, m->to, ctx->buf+offs,
3c13a815670b54d1c17bf02954f7d2b066cde95cnd nmatch, pmatch);
af84459fbf938e508fd10b01cb8d699c79083813takashi VERBOSE({
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* f;
af84459fbf938e508fd10b01cb8d699c79083813takashi f = apr_pstrndup(ctx->f->r->pool,
3c13a815670b54d1c17bf02954f7d2b066cde95cnd ctx->buf + offs, s_from);
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0,
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->f->r,
af84459fbf938e508fd10b01cb8d699c79083813takashi "E/RX: match at %s, substituting %s",
af84459fbf938e508fd10b01cb8d699c79083813takashi f, subs);
af84459fbf938e508fd10b01cb8d699c79083813takashi })
af84459fbf938e508fd10b01cb8d699c79083813takashi s_to = strlen(subs);
af84459fbf938e508fd10b01cb8d699c79083813takashi offs += match;
af84459fbf938e508fd10b01cb8d699c79083813takashi len = strlen(ctx->buf);
af84459fbf938e508fd10b01cb8d699c79083813takashi if (s_to > s_from) {
af84459fbf938e508fd10b01cb8d699c79083813takashi preserve(ctx, s_to - s_from);
af84459fbf938e508fd10b01cb8d699c79083813takashi memmove(ctx->buf+offs+s_to,
3c13a815670b54d1c17bf02954f7d2b066cde95cnd ctx->buf+offs+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi len + 1 - s_from - offs);
3c13a815670b54d1c17bf02954f7d2b066cde95cnd memcpy(ctx->buf+offs, subs, s_to);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else {
af84459fbf938e508fd10b01cb8d699c79083813takashi memcpy(ctx->buf + offs, subs, s_to);
3c13a815670b54d1c17bf02954f7d2b066cde95cnd memmove(ctx->buf+offs+s_to,
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->buf+offs+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi len + 1 - s_from - offs);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi offs += s_to;
af84459fbf938e508fd10b01cb8d699c79083813takashi ++num_match;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else {
af84459fbf938e508fd10b01cb8d699c79083813takashi found = strstr(ctx->buf, m->from.c);
af84459fbf938e508fd10b01cb8d699c79083813takashi if ((m->flags & M_ATSTART) && (found != ctx->buf))
af84459fbf938e508fd10b01cb8d699c79083813takashi continue;
af84459fbf938e508fd10b01cb8d699c79083813takashi while (found) {
af84459fbf938e508fd10b01cb8d699c79083813takashi s_from = strlen(m->from.c);
af84459fbf938e508fd10b01cb8d699c79083813takashi s_to = strlen(m->to);
af84459fbf938e508fd10b01cb8d699c79083813takashi match = found - ctx->buf;
af84459fbf938e508fd10b01cb8d699c79083813takashi if ((s_from < strlen(found))
af84459fbf938e508fd10b01cb8d699c79083813takashi && (m->flags & M_ATEND)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi found = strstr(ctx->buf+match+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi m->from.c);
af84459fbf938e508fd10b01cb8d699c79083813takashi continue;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else {
af84459fbf938e508fd10b01cb8d699c79083813takashi found = strstr(ctx->buf+match+s_to,
af84459fbf938e508fd10b01cb8d699c79083813takashi m->from.c);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi VERBOSE(ap_log_rerror(APLOG_MARK, APLOG_TRACE3,
af84459fbf938e508fd10b01cb8d699c79083813takashi 0, ctx->f->r,
af84459fbf938e508fd10b01cb8d699c79083813takashi "E: matched %s, substituting %s",
af84459fbf938e508fd10b01cb8d699c79083813takashi m->from.c, m->to));
af84459fbf938e508fd10b01cb8d699c79083813takashi len = strlen(ctx->buf);
af84459fbf938e508fd10b01cb8d699c79083813takashi if (s_to > s_from) {
af84459fbf938e508fd10b01cb8d699c79083813takashi preserve(ctx, s_to - s_from);
af84459fbf938e508fd10b01cb8d699c79083813takashi memmove(ctx->buf+match+s_to,
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->buf+match+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi len + 1 - s_from - match);
af84459fbf938e508fd10b01cb8d699c79083813takashi memcpy(ctx->buf+match, m->to, s_to);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi else {
af84459fbf938e508fd10b01cb8d699c79083813takashi memcpy(ctx->buf+match, m->to, s_to);
af84459fbf938e508fd10b01cb8d699c79083813takashi memmove(ctx->buf+match+s_to,
af84459fbf938e508fd10b01cb8d699c79083813takashi ctx->buf+match+s_from,
af84459fbf938e508fd10b01cb8d699c79083813takashi len + 1 - s_from - match);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi ++num_match;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi if (num_match && (m->flags & M_LAST))
3c13a815670b54d1c17bf02954f7d2b066cde95cnd break;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi break;
af84459fbf938e508fd10b01cb8d699c79083813takashi case ATTR_IGNORE:
af84459fbf938e508fd10b01cb8d699c79083813takashi break;
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi if (!a[1])
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_fputstrs(ctx->f->next, ctx->bb, " ", a[0], NULL);
af84459fbf938e508fd10b01cb8d699c79083813takashi else {
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi if (ctx->cfg->flags != 0)
af84459fbf938e508fd10b01cb8d699c79083813takashi normalise(ctx->cfg->flags, ctx->buf);
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi /* write the attribute, using pcharacters to html-escape
af84459fbf938e508fd10b01cb8d699c79083813takashi anything that needs it in the value.
af84459fbf938e508fd10b01cb8d699c79083813takashi */
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_fputstrs(ctx->f->next, ctx->bb, " ", a[0], "=\"", NULL);
af84459fbf938e508fd10b01cb8d699c79083813takashi pcharacters(ctx, (const xmlChar*)ctx->buf, strlen(ctx->buf));
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_fputc(ctx->f->next, ctx->bb, '"');
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi }
3c13a815670b54d1c17bf02954f7d2b066cde95cnd ctx->offset = 0;
af84459fbf938e508fd10b01cb8d699c79083813takashi if (desc && desc->empty)
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_fputs(ctx->f->next, ctx->bb, ctx->cfg->etag);
af84459fbf938e508fd10b01cb8d699c79083813takashi else
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_fputc(ctx->f->next, ctx->bb, '>');
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi if ((enforce > 0) && (required_attrs > 0)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi /* if there are more required attributes than we found then complain */
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, ctx->f->r,
af84459fbf938e508fd10b01cb8d699c79083813takashi "HTML element %s is missing %d required attributes",
af84459fbf938e508fd10b01cb8d699c79083813takashi name, required_attrs);
af84459fbf938e508fd10b01cb8d699c79083813takashi }
af84459fbf938e508fd10b01cb8d699c79083813takashi}
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashistatic meta* metafix(request_rec* r, const char* buf)
af84459fbf938e508fd10b01cb8d699c79083813takashi{
af84459fbf938e508fd10b01cb8d699c79083813takashi meta* ret = NULL;
af84459fbf938e508fd10b01cb8d699c79083813takashi size_t offs = 0;
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* p;
af84459fbf938e508fd10b01cb8d699c79083813takashi const char* q;
af84459fbf938e508fd10b01cb8d699c79083813takashi char* header;
af84459fbf938e508fd10b01cb8d699c79083813takashi char* content;
af84459fbf938e508fd10b01cb8d699c79083813takashi ap_regmatch_t pmatch[2];
af84459fbf938e508fd10b01cb8d699c79083813takashi char delim;
af84459fbf938e508fd10b01cb8d699c79083813takashi
af84459fbf938e508fd10b01cb8d699c79083813takashi while (!ap_regexec(seek_meta, buf+offs, 2, pmatch, 0)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi header = NULL;
af84459fbf938e508fd10b01cb8d699c79083813takashi content = NULL;
af84459fbf938e508fd10b01cb8d699c79083813takashi p = buf+offs+pmatch[1].rm_eo;
af84459fbf938e508fd10b01cb8d699c79083813takashi while (!isalpha(*++p));
af84459fbf938e508fd10b01cb8d699c79083813takashi for (q = p; isalnum(*q) || (*q == '-'); ++q);
af84459fbf938e508fd10b01cb8d699c79083813takashi header = apr_pstrndup(r->pool, p, q-p);
af84459fbf938e508fd10b01cb8d699c79083813takashi if (strncasecmp(header, "Content-", 8)) {
af84459fbf938e508fd10b01cb8d699c79083813takashi /* find content=... string */
af84459fbf938e508fd10b01cb8d699c79083813takashi p = apr_strmatch(seek_content, buf+offs+pmatch[0].rm_so,
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung pmatch[0].rm_eo - pmatch[0].rm_so);
af84459fbf938e508fd10b01cb8d699c79083813takashi /* if it doesn't contain "content", ignore, don't crash! */
5effc8b39fae5cd169d17f342bfc265705840014rbowen if (p != NULL) {
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen while (*p) {
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen p += 7;
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen while (*p && isspace(*p))
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen ++p;
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen if (*p != '=')
af84459fbf938e508fd10b01cb8d699c79083813takashi continue;
while (*p && isspace(*++p));
if ((*p == '\'') || (*p == '"')) {
delim = *p++;
for (q = p; *q != delim; ++q);
} else {
for (q = p; *q && !isspace(*q) && (*q != '>'); ++q);
}
content = apr_pstrndup(r->pool, p, q-p);
break;
}
}
}
else if (!strncasecmp(header, "Content-Type", 12)) {
ret = apr_palloc(r->pool, sizeof(meta));
ret->start = pmatch[0].rm_so;
ret->end = pmatch[0].rm_eo;
}
if (header && content) {
#ifndef GO_FASTER
ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r,
"Adding header [%s: %s] from HTML META",
header, content);
#endif
apr_table_setn(r->headers_out, header, content);
}
offs += pmatch[0].rm_eo;
}
return ret;
}
static const char* interpolate_vars(request_rec* r, const char* str)
{
const char* start;
const char* end;
const char* delim;
const char* before;
const char* after;
const char* replacement;
const char* var;
for (;;) {
start = str;
if (start = ap_strstr_c(start, "${"), start == NULL)
break;
if (end = ap_strchr_c(start+2, '}'), end == NULL)
break;
delim = ap_strchr_c(start, '|');
before = apr_pstrndup(r->pool, str, start-str);
after = end+1;
if (delim) {
var = apr_pstrndup(r->pool, start+2, delim-start-2);
}
else {
var = apr_pstrndup(r->pool, start+2, end-start-2);
}
replacement = apr_table_get(r->subprocess_env, var);
if (!replacement) {
if (delim)
replacement = apr_pstrndup(r->pool, delim+1, end-delim-1);
else
replacement = "";
}
str = apr_pstrcat(r->pool, before, replacement, after, NULL);
ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
"Interpolating %s => %s", var, replacement);
}
return str;
}
static void fixup_rules(saxctxt* ctx)
{
urlmap* newp;
urlmap* p;
urlmap* prev = NULL;
request_rec* r = ctx->f->r;
for (p = ctx->cfg->map; p; p = p->next) {
if (p->cond != NULL) {
const char *err;
int ok = ap_expr_exec(r, p->cond, &err);
if (err) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"Error evaluating expr: %s", err);
}
if (ok == 0) {
continue; /* condition is unsatisfied */
}
}
newp = apr_pmemdup(r->pool, p, sizeof(urlmap));
if (newp->flags & M_INTERPOLATE_FROM) {
newp->from.c = interpolate_vars(r, newp->from.c);
if (!newp->from.c || !*newp->from.c)
continue; /* don't use empty from-pattern */
if (newp->flags & M_REGEX) {
newp->from.r = ap_pregcomp(r->pool, newp->from.c,
newp->regflags);
}
}
if (newp->flags & M_INTERPOLATE_TO) {
newp->to = interpolate_vars(r, newp->to);
}
/* evaluate p->cond; continue if unsatisfied */
/* create new urlmap with memcpy and append to map */
/* interpolate from if flagged to do so */
/* interpolate to if flagged to do so */
if (prev != NULL)
prev->next = newp;
else
ctx->map = newp;
prev = newp;
}
if (prev)
prev->next = NULL;
}
static saxctxt* check_filter_init (ap_filter_t* f)
{
saxctxt* fctx;
if (!f->ctx) {
proxy_html_conf* cfg;
const char* force;
const char* errmsg = NULL;
cfg = ap_get_module_config(f->r->per_dir_config, &proxy_html_module);
force = apr_table_get(f->r->subprocess_env, "PROXY_HTML_FORCE");
if (!force) {
if (!f->r->proxyreq) {
errmsg = "Non-proxy request; not inserting proxy-html filter";
}
else if (!f->r->content_type) {
errmsg = "No content-type; bailing out of proxy-html filter";
}
else if (strncasecmp(f->r->content_type, "text/html", 9) &&
strncasecmp(f->r->content_type,
"application/xhtml+xml", 21)) {
errmsg = "Non-HTML content; not inserting proxy-html filter";
}
}
if (!cfg->links) {
errmsg = "No links configured: nothing for proxy-html filter to do";
}
if (errmsg) {
#ifndef GO_FASTER
ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, f->r, "%s", errmsg);
#endif
ap_remove_output_filter(f);
return NULL;
}
fctx = f->ctx = apr_pcalloc(f->r->pool, sizeof(saxctxt));
fctx->f = f;
fctx->bb = apr_brigade_create(f->r->pool,
f->r->connection->bucket_alloc);
fctx->cfg = cfg;
apr_table_unset(f->r->headers_out, "Content-Length");
if (cfg->interp)
fixup_rules(fctx);
else
fctx->map = cfg->map;
/* defer dealing with charset_out until after sniffing charset_in
* so we can support setting one to t'other.
*/
}
return f->ctx;
}
static int proxy_html_filter(ap_filter_t* f, apr_bucket_brigade* bb)
{
apr_bucket* b;
meta* m = NULL;
xmlCharEncoding enc;
const char* buf = 0;
apr_size_t bytes = 0;
#ifndef USE_OLD_LIBXML2
int xmlopts = XML_PARSE_RECOVER | XML_PARSE_NONET |
XML_PARSE_NOBLANKS | XML_PARSE_NOERROR | XML_PARSE_NOWARNING;
#endif
saxctxt* ctxt = check_filter_init(f);
if (!ctxt)
return ap_pass_brigade(f->next, bb);
for (b = APR_BRIGADE_FIRST(bb);
b != APR_BRIGADE_SENTINEL(bb);
b = APR_BUCKET_NEXT(b)) {
if (APR_BUCKET_IS_METADATA(b)) {
if (APR_BUCKET_IS_EOS(b)) {
if (ctxt->parser != NULL) {
consume_buffer(ctxt, buf, 0, 1);
}
APR_BRIGADE_INSERT_TAIL(ctxt->bb,
apr_bucket_eos_create(ctxt->bb->bucket_alloc));
ap_pass_brigade(ctxt->f->next, ctxt->bb);
}
else if (APR_BUCKET_IS_FLUSH(b)) {
/* pass on flush, except at start where it would cause
* headers to be sent before doc sniffing
*/
if (ctxt->parser != NULL) {
ap_fflush(ctxt->f->next, ctxt->bb);
}
}
}
else if (apr_bucket_read(b, &buf, &bytes, APR_BLOCK_READ)
== APR_SUCCESS) {
if (ctxt->parser == NULL) {
const char* cenc;
if (!xml2enc_charset ||
(xml2enc_charset(f->r, &enc, &cenc) != APR_SUCCESS)) {
if (!xml2enc_charset)
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, f->r,
"No i18n support found. Install mod_xml2enc if required");
enc = XML_CHAR_ENCODING_NONE;
ap_set_content_type(f->r, "text/html;charset=utf-8");
}
else {
/* if we wanted a non-default charset_out, insert the
* xml2enc filter now that we've sniffed it
*/
if (ctxt->cfg->charset_out && xml2enc_filter) {
if (*ctxt->cfg->charset_out != '*')
cenc = ctxt->cfg->charset_out;
xml2enc_filter(f->r, cenc, ENCIO_OUTPUT);
ap_set_content_type(f->r,
apr_pstrcat(f->r->pool,
"text/html;charset=",
cenc, NULL));
}
else /* Normal case, everything worked, utf-8 output */
ap_set_content_type(f->r, "text/html;charset=utf-8");
}
ap_fputs(f->next, ctxt->bb, ctxt->cfg->doctype);
ctxt->parser = htmlCreatePushParserCtxt(&sax, ctxt, buf,
4, 0, enc);
buf += 4;
bytes -= 4;
if (ctxt->parser == NULL) {
apr_status_t rv = ap_pass_brigade(f->next, bb);
ap_remove_output_filter(f);
return rv;
}
apr_pool_cleanup_register(f->r->pool, ctxt->parser,
(int(*)(void*))htmlFreeParserCtxt,
apr_pool_cleanup_null);
#ifndef USE_OLD_LIBXML2
if (xmlopts = xmlCtxtUseOptions(ctxt->parser, xmlopts), xmlopts)
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, f->r,
"Unsupported parser opts %x", xmlopts);
#endif
if (ctxt->cfg->metafix)
m = metafix(f->r, buf);
if (m) {
consume_buffer(ctxt, buf, m->start, 0);
consume_buffer(ctxt, buf+m->end, bytes-m->end, 0);
}
else {
consume_buffer(ctxt, buf, bytes, 0);
}
}
else {
consume_buffer(ctxt, buf, bytes, 0);
}
}
else {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r,
"Error in bucket read");
}
}
/*ap_fflush(ctxt->f->next, ctxt->bb); // uncomment for debug */
apr_brigade_cleanup(bb);
return APR_SUCCESS;
}
static void* proxy_html_config(apr_pool_t* pool, char* x)
{
proxy_html_conf* ret = apr_pcalloc(pool, sizeof(proxy_html_conf));
ret->doctype = DEFAULT_DOCTYPE;
ret->etag = DEFAULT_ETAG;
ret->bufsz = 8192;
/* ret->interp = 1; */
/* don't initialise links and events until they get set/used */
return ret;
}
static void* proxy_html_merge(apr_pool_t* pool, void* BASE, void* ADD)
{
proxy_html_conf* base = (proxy_html_conf*) BASE;
proxy_html_conf* add = (proxy_html_conf*) ADD;
proxy_html_conf* conf = apr_palloc(pool, sizeof(proxy_html_conf));
/* don't merge declarations - just use the most specific */
conf->links = (add->links == NULL) ? base->links : add->links;
conf->events = (add->events == NULL) ? base->events : add->events;
conf->charset_out = (add->charset_out == NULL)
? base->charset_out : add->charset_out;
if (add->map && base->map) {
urlmap* a;
conf->map = NULL;
for (a = base->map; a; a = a->next) {
urlmap* save = conf->map;
conf->map = apr_pmemdup(pool, a, sizeof(urlmap));
conf->map->next = save;
}
for (a = add->map; a; a = a->next) {
urlmap* save = conf->map;
conf->map = apr_pmemdup(pool, a, sizeof(urlmap));
conf->map->next = save;
}
}
else
conf->map = add->map ? add->map : base->map;
conf->doctype = (add->doctype == DEFAULT_DOCTYPE)
? base->doctype : add->doctype;
conf->etag = (add->etag == DEFAULT_ETAG) ? base->etag : add->etag;
conf->bufsz = add->bufsz;
if (add->flags & NORM_RESET) {
conf->flags = add->flags ^ NORM_RESET;
conf->metafix = add->metafix;
conf->extfix = add->extfix;
conf->interp = add->interp;
conf->strip_comments = add->strip_comments;
conf->enabled = add->enabled;
}
else {
conf->flags = base->flags | add->flags;
conf->metafix = base->metafix | add->metafix;
conf->extfix = base->extfix | add->extfix;
conf->interp = base->interp | add->interp;
conf->strip_comments = base->strip_comments | add->strip_comments;
conf->enabled = add->enabled | base->enabled;
}
return conf;
}
#define REGFLAG(n,s,c) ((s&&(ap_strchr_c((s),(c))!=NULL)) ? (n) : 0)
#define XREGFLAG(n,s,c) ((!s||(ap_strchr_c((s),(c))==NULL)) ? (n) : 0)
static const char* comp_urlmap(cmd_parms *cmd, urlmap* newmap,
const char* from, const char* to,
const char* flags, const char* cond)
{
const char *err = NULL;
newmap->flags
= XREGFLAG(M_HTML,flags,'h')
| XREGFLAG(M_EVENTS,flags,'e')
| XREGFLAG(M_CDATA,flags,'c')
| REGFLAG(M_ATSTART,flags,'^')
| REGFLAG(M_ATEND,flags,'$')
| REGFLAG(M_REGEX,flags,'R')
| REGFLAG(M_LAST,flags,'L')
| REGFLAG(M_NOTLAST,flags,'l')
| REGFLAG(M_INTERPOLATE_TO,flags,'V')
| REGFLAG(M_INTERPOLATE_FROM,flags,'v');
if ((newmap->flags & M_INTERPOLATE_FROM) || !(newmap->flags & M_REGEX)) {
newmap->from.c = from;
newmap->to = to;
}
else {
newmap->regflags
= REGFLAG(AP_REG_EXTENDED,flags,'x')
| REGFLAG(AP_REG_ICASE,flags,'i')
| REGFLAG(AP_REG_NOSUB,flags,'n')
| REGFLAG(AP_REG_NEWLINE,flags,'s');
newmap->from.r = ap_pregcomp(cmd->pool, from, newmap->regflags);
newmap->to = to;
}
if (cond != NULL) {
/* back-compatibility: support old-style ENV expressions
* by converting to ap_expr syntax.
*
* 1. var --> env(var)
* 2. var=val --> env(var)=val
* 3. !var --> !env(var)
* 4. !var=val --> env(var)!=val
*/
char *newcond = NULL;
if (ap_rxplus_exec(cmd->temp_pool, old_expr, cond, &newcond)) {
/* we got a substitution. Check for the case (3) above
* that the regexp gets wrong: a negation without a comparison.
*/
if ((cond[0] == '!') && !strchr(cond, '=')) {
memmove(newcond+1, newcond, strlen(newcond)-1);
newcond[0] = '!';
}
cond = newcond;
}
#if 0
newmap->cond = apr_palloc(pool, sizeof(ap_expr_info_t));
err = ap_expr_parse(pool, tpool, newmap->cond, cond, NULL);
#else
newmap->cond = ap_expr_parse_cmd(cmd, cond, 0, &err, NULL);
#endif
}
else {
newmap->cond = NULL;
}
return err;
}
static const char* set_urlmap(cmd_parms* cmd, void* CFG, const char* args)
{
proxy_html_conf* cfg = (proxy_html_conf*)CFG;
urlmap* map;
apr_pool_t* pool = cmd->pool;
urlmap* newmap;
const char* usage =
"Usage: ProxyHTMLURLMap from-pattern to-pattern [flags] [cond]";
const char* from;
const char* to;
const char* flags;
const char* cond = NULL;
if (from = ap_getword_conf(cmd->pool, &args), !from)
return usage;
if (to = ap_getword_conf(cmd->pool, &args), !to)
return usage;
flags = ap_getword_conf(cmd->pool, &args);
if (flags && *flags)
cond = ap_getword_conf(cmd->pool, &args);
if (cond && !*cond)
cond = NULL;
/* the args look OK, so let's use them */
newmap = apr_palloc(pool, sizeof(urlmap));
newmap->next = NULL;
if (cfg->map) {
for (map = cfg->map; map->next; map = map->next);
map->next = newmap;
}
else
cfg->map = newmap;
return comp_urlmap(cmd, newmap, from, to, flags, cond);
}
static const char* set_doctype(cmd_parms* cmd, void* CFG,
const char* t, const char* l)
{
proxy_html_conf* cfg = (proxy_html_conf*)CFG;
if (!strcasecmp(t, "xhtml")) {
cfg->etag = xhtml_etag;
if (l && !strcasecmp(l, "legacy"))
cfg->doctype = fpi_xhtml_legacy;
else
cfg->doctype = fpi_xhtml;
}
else if (!strcasecmp(t, "html")) {
cfg->etag = html_etag;
if (l && !strcasecmp(l, "legacy"))
cfg->doctype = fpi_html_legacy;
else
cfg->doctype = fpi_html;
}
else {
cfg->doctype = apr_pstrdup(cmd->pool, t);
if (l && ((l[0] == 'x') || (l[0] == 'X')))
cfg->etag = xhtml_etag;
else
cfg->etag = html_etag;
}
return NULL;
}
static const char* set_flags(cmd_parms* cmd, void* CFG, const char* arg)
{
proxy_html_conf* cfg = CFG;
if (arg && *arg) {
if (!strcmp(arg, "lowercase"))
cfg->flags |= NORM_LC;
else if (!strcmp(arg, "dospath"))
cfg->flags |= NORM_MSSLASH;
else if (!strcmp(arg, "reset"))
cfg->flags |= NORM_RESET;
}
return NULL;
}
static const char* set_events(cmd_parms* cmd, void* CFG, const char* arg)
{
tattr* attr;
proxy_html_conf* cfg = CFG;
if (cfg->events == NULL)
cfg->events = apr_array_make(cmd->pool, 20, sizeof(tattr));
attr = apr_array_push(cfg->events);
attr->val = arg;
return NULL;
}
static const char* set_links(cmd_parms* cmd, void* CFG,
const char* elt, const char* att)
{
apr_array_header_t* attrs;
tattr* attr;
proxy_html_conf* cfg = CFG;
if (cfg->links == NULL)
cfg->links = apr_hash_make(cmd->pool);
attrs = apr_hash_get(cfg->links, elt, APR_HASH_KEY_STRING);
if (!attrs) {
attrs = apr_array_make(cmd->pool, 2, sizeof(tattr*));
apr_hash_set(cfg->links, elt, APR_HASH_KEY_STRING, attrs);
}
attr = apr_array_push(attrs);
attr->val = att;
return NULL;
}
static const command_rec proxy_html_cmds[] = {
AP_INIT_ITERATE("ProxyHTMLEvents", set_events, NULL,
RSRC_CONF|ACCESS_CONF,
"Strings to be treated as scripting events"),
AP_INIT_ITERATE2("ProxyHTMLLinks", set_links, NULL,
RSRC_CONF|ACCESS_CONF, "Declare HTML Attributes"),
AP_INIT_RAW_ARGS("ProxyHTMLURLMap", set_urlmap, NULL,
RSRC_CONF|ACCESS_CONF, "Map URL From To"),
AP_INIT_TAKE12("ProxyHTMLDoctype", set_doctype, NULL,
RSRC_CONF|ACCESS_CONF, "(HTML|XHTML) [Legacy]"),
AP_INIT_ITERATE("ProxyHTMLFixups", set_flags, NULL,
RSRC_CONF|ACCESS_CONF, "Options are lowercase, dospath"),
AP_INIT_FLAG("ProxyHTMLMeta", ap_set_flag_slot,
(void*)APR_OFFSETOF(proxy_html_conf, metafix),
RSRC_CONF|ACCESS_CONF, "Fix META http-equiv elements"),
AP_INIT_FLAG("ProxyHTMLInterp", ap_set_flag_slot,
(void*)APR_OFFSETOF(proxy_html_conf, interp),
RSRC_CONF|ACCESS_CONF,
"Support interpolation and conditions in URLMaps"),
AP_INIT_FLAG("ProxyHTMLExtended", ap_set_flag_slot,
(void*)APR_OFFSETOF(proxy_html_conf, extfix),
RSRC_CONF|ACCESS_CONF, "Map URLs in Javascript and CSS"),
AP_INIT_FLAG("ProxyHTMLStripComments", ap_set_flag_slot,
(void*)APR_OFFSETOF(proxy_html_conf, strip_comments),
RSRC_CONF|ACCESS_CONF, "Strip out comments"),
AP_INIT_TAKE1("ProxyHTMLBufSize", ap_set_int_slot,
(void*)APR_OFFSETOF(proxy_html_conf, bufsz),
RSRC_CONF|ACCESS_CONF, "Buffer size"),
AP_INIT_TAKE1("ProxyHTMLCharsetOut", ap_set_string_slot,
(void*)APR_OFFSETOF(proxy_html_conf, charset_out),
RSRC_CONF|ACCESS_CONF, "Usage: ProxyHTMLCharsetOut charset"),
AP_INIT_FLAG("ProxyHTMLEnable", ap_set_flag_slot,
(void*)APR_OFFSETOF(proxy_html_conf, enabled),
RSRC_CONF|ACCESS_CONF,
"Enable proxy-html and xml2enc filters"),
{ NULL }
};
static int mod_proxy_html(apr_pool_t* p, apr_pool_t* p1, apr_pool_t* p2)
{
seek_meta = ap_pregcomp(p, "<meta[^>]*(http-equiv)[^>]*>",
AP_REG_EXTENDED|AP_REG_ICASE);
seek_content = apr_strmatch_precompile(p, "content", 0);
memset(&sax, 0, sizeof(htmlSAXHandler));
sax.startElement = pstartElement;
sax.endElement = pendElement;
sax.characters = pcharacters;
sax.comment = pcomment;
sax.cdataBlock = pcdata;
xml2enc_charset = APR_RETRIEVE_OPTIONAL_FN(xml2enc_charset);
xml2enc_filter = APR_RETRIEVE_OPTIONAL_FN(xml2enc_filter);
if (!xml2enc_charset) {
ap_log_perror(APLOG_MARK, APLOG_NOTICE, 0, p2,
"I18n support in mod_proxy_html requires mod_xml2enc. "
"Without it, non-ASCII characters in proxied pages are "
"likely to display incorrectly.");
}
/* old_expr only needs to last the life of the config phase */
old_expr = ap_rxplus_compile(p1, "s/^(!)?(\\w+)((=)(.+))?$/reqenv('$2')$1$4'$5'/");
return OK;
}
static void proxy_html_insert(request_rec* r)
{
proxy_html_conf* cfg;
cfg = ap_get_module_config(r->per_dir_config, &proxy_html_module);
if (cfg->enabled) {
if (xml2enc_filter)
xml2enc_filter(r, NULL, ENCIO_INPUT_CHECKS);
ap_add_output_filter("proxy-html", NULL, r, r->connection);
}
}
static void proxy_html_hooks(apr_pool_t* p)
{
static const char* aszSucc[] = { "mod_filter.c", NULL };
ap_register_output_filter_protocol("proxy-html", proxy_html_filter,
NULL, AP_FTYPE_RESOURCE,
AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH);
/* move this to pre_config so old_expr is available to interpret
* old-style conditions on URL maps.
*/
ap_hook_pre_config(mod_proxy_html, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_insert_filter(proxy_html_insert, NULL, aszSucc, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA proxy_html_module = {
STANDARD20_MODULE_STUFF,
proxy_html_config,
proxy_html_merge,
NULL,
NULL,
proxy_html_cmds,
proxy_html_hooks
};