mod_substitute.c revision 88708383f7de95ca5f619036150d938e42776bb5
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* mod_substitute.c: Perform content rewriting on the fly
*/
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "apr_general.h"
#include "apr_strings.h"
#include "apr_strmatch.h"
#include "apr_lib.h"
#include "util_filter.h"
#include "apr_buckets.h"
#include "http_request.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
static const char substitute_filter_name[] = "SUBSTITUTE";
typedef struct subst_pattern_t {
const apr_strmatch_pattern *pattern;
const ap_regex_t *regexp;
const char *replacement;
int flatten;
typedef struct {
typedef struct {
static void *create_substitute_dcfg(apr_pool_t *p, char *d)
{
return dcfg;
}
{
subst_dir_conf *a =
return a;
}
if (!s1) { \
} \
else { \
} \
} while (0)
apr_bucket_split(b, offset); \
tmp_b = APR_BUCKET_NEXT(b); \
b = APR_BUCKET_NEXT(tmp_b); \
} while (0)
{
int i;
const char *buff;
const char *repl;
char *scratch;
char *p;
char *s1;
char *s2;
apr_bucket *b;
fbytes = 0;
for (b = APR_BRIGADE_FIRST(mybb);
b != APR_BRIGADE_SENTINEL(mybb);
b = APR_BUCKET_NEXT(b)) {
if (APR_BUCKET_IS_METADATA(b)) {
/*
* we should NEVER see this, because we should never
* be passed any, but "handle" it just in case.
*/
continue;
}
== APR_SUCCESS) {
{
/* get offset into buff for pattern */
/*
* We are flattening the buckets here, meaning
* that we don't do the fast bucket splits.
* Instead we copy over what the buckets would
* contain and use them. This is slow, since we
* are constanting allocing space and copying
* strings.
*/
}
else {
/*
* We now split off the stuff before the regex
* as its own bucket, then isolate the pattern
* and delete it.
*/
/*
* Finally, we create a bucket that contains the
* replacement...
*/
f->r->connection->bucket_alloc);
/* ... and insert it */
}
/* now we need to adjust buff for all these changes */
}
/*
* we've finished looking at the bucket, so remove the
* old one and add in our new one
*/
tmp_b = APR_BUCKET_NEXT(b);
b = tmp_b;
}
}
/*
* we need a null terminated string here :(. To hopefully
* save time and memory, we don't alloc for each run
* through, but only if we need to have a larger chunk
* to save the string to. So we keep track of how much
* we've allocated and only re-alloc when we need it.
* NOTE: this screams for a macro.
*/
}
/* reset pointer to the scratch space */
p = scratch;
p[bytes] = '\0';
AP_MAX_REG_MATCH, regm, 0)) {
/* first, grab the replacement string */
}
else {
f->r->pool,
f->r->connection->bucket_alloc);
}
/*
* reset to past what we just did. buff now maps to b
* again
*/
}
tmp_b = APR_BUCKET_NEXT(b);
b = tmp_b;
}
}
else {
/* huh? */
continue;
}
}
}
script++;
}
return mybb;
}
{
const char *buff;
char *bflat;
apr_bucket *b;
/*
* First time around? Create the saved bb that we used for each pass
* through. Note that we can also get here when we explicitly clear ctx,
* for error handling
*/
if (!ctx) {
}
/*
* Shortcircuit processing
*/
if (APR_BRIGADE_EMPTY(bb))
return APR_SUCCESS;
/*
* Everything to be passed to the next filter goes in
* here, our pass brigade.
*/
/*
* Here's the concept:
* Read in the data and look for newlines. Once we
* find a full "line", add it to our working brigade.
* If we've finished reading the brigade and we have
* any left over data (not a "full" line), store that
* for the next pass.
*
* Note: anything stored in ctxbb for sure does not have
* a newline char, so we don't concat that bb with the
* new bb, since we would spending time searching for the newline
* in data we know it doesn't exist. So instead, we simply scan
* our current bb and, if we see a newline, prepend ctxbb
* to the front of it. This makes the code much less straight-
* forward (otherwise we could APR_BRIGADE_CONCAT(ctx->ctxbb, bb)
* and just scan for newlines and not bother with needing to know
* when ctx->ctxbb needs to be reset) but also faster. We'll take
* the speed.
*
* Note: apr_brigade_split_line would be nice here, but we
* really can't use it since we need more control and we want
* to re-use already read bucket data.
*
* See mod_include if still confused :)
*/
if (rv != APR_SUCCESS)
return rv;
}
if (APR_BUCKET_IS_EOS(b)) {
/*
* if we see the EOS, then we need to pass along everything we
* have. But if the ctxbb isn't empty, then we need to add that
* to the end of what we'll be passing.
*/
f->r->connection->bucket_alloc);
}
break;
}
else if (APR_BUCKET_IS_FLUSH(b)) {
if (rv != APR_SUCCESS)
return rv;
}
else if (APR_BUCKET_IS_METADATA(b)) {
}
else {
/*
* We have actual "data" so read in as much as we can and start
* scanning and splitting from our read buffer
*/
}
else {
while (bytes > 0) {
if (nl) {
/* split *after* the newline */
apr_bucket_split(b, len);
/*
* We've likely read more data, so bypass rereading
* bucket data and continue scanning through this
* buffer
*/
/*
* we need b to be updated for future potential
* splitting
*/
tmp_b = APR_BUCKET_NEXT(b);
/*
* Hey, we found a newline! Don't forget the old
* stuff that needs to be added to the front. So we
* add the split bucket to the end, flatten the whole
* bb, morph the whole shebang into a bucket which is
* then added to the tail of the newline bb.
*/
f->r->pool,
f->r->connection->bucket_alloc);
}
pattbb = do_pattmatch(f, b);
b = tmp_b;
}
else {
/*
* no newline in whatever is left of this buffer so
* tuck data away and get next bucket
*/
bytes = 0;
}
}
}
}
}
/* Pass it down */
}
return rv;
}
{
char *ourline;
char delim;
int is_pattern = 0;
int ignore_case = 0;
int flatten = 0;
ap_regex_t *r = NULL;
return "Bad Rewrite format, must be an s/// pattern";
}
if (delim)
if (from) {
if (*ourline) {
*ourline = '\0';
}
}
if (to) {
if (*ourline) {
*ourline = '\0';
}
}
return "Bad Rewrite format, must be a complete s/// pattern";
}
while (*flags) {
if (delim == 'i')
ignore_case = 1;
else if (delim == 'n')
is_pattern = 1;
else if (delim == 'f')
flatten = 1;
else
return "Bad Rewrite flag, only s///[inf] are supported";
flags++;
}
/* first see if we can compile the regex */
if (!is_pattern) {
(ignore_case ? AP_REG_ICASE : 0));
if (!r)
return "Rewrite could not compile regex";
}
/* init the new entries */
if (is_pattern) {
!ignore_case);
}
else {
}
return NULL;
}
{
}
static const command_rec substitute_cmds[] = {
{NULL}
};
create_substitute_dcfg, /* dir config creater */
merge_substitute_dcfg, /* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server config */
substitute_cmds, /* command table */
register_hooks /* register hooks */
};