-/* ====================================================================
- * The Apache Software License, Version 1.1
+/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
+ * applicable.
*
- * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
- * reserved.
+ * Licensed 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
*
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The end-user documentation included with the redistribution,
- * if any, must include the following acknowledgment:
- * "This product includes software developed by the
- * Apache Software Foundation (http://www.apache.org/)."
- * Alternately, this acknowledgment may appear in the software itself,
- * if and wherever such third-party acknowledgments normally appear.
- *
- * 4. The names "Apache" and "Apache Software Foundation" must
- * not be used to endorse or promote products derived from this
- * software without prior written permission. For written
- * permission, please contact apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache",
- * nor may "Apache" appear in their name, without prior written
- * permission of the Apache Software Foundation.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- * Portions of this software are based upon public domain software
- * originally written at the National Center for Supercomputing Applications,
- * University of Illinois, Urbana-Champaign.
+ * 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.
*/
/* _ _ _
* gifted exclusively to the The Apache Software Foundation in July 1997 by
*
* Ralf S. Engelschall
- * rse@engelschall.com
+ * rse engelschall.com
* www.engelschall.com
*/
#include "http_protocol.h"
#include "http_vhost.h"
+#include "mod_ssl.h"
+
#include "mod_rewrite.h"
-#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
+#ifdef AP_NEED_SET_MUTEX_PERMS
#include "unixd.h"
-#define MOD_REWRITE_SET_MUTEX_PERMS /* XXX Apache should define something */
#endif
/*
- * The key in the r->notes apr_table_t wherein we store our accumulated
- * Vary values, and the one used for per-condition checks in a chain.
+ * in order to improve performance on running production systems, you
+ * may strip all rewritelog code entirely from mod_rewrite by using the
+ * -DREWRITELOG_DISABLED compiler option.
+ *
+ * DO NOT USE THIS OPTION FOR PUBLIC BINARY RELEASES. Otherwise YOU are
+ * responsible for answering all the mod_rewrite questions out there.
*/
-#define VARY_KEY "rewrite-Vary"
-#define VARY_KEY_THIS "rewrite-Vary-this"
+#ifndef REWRITELOG_DISABLED
+
+#define rewritelog(x) do_rewritelog x
+#define REWRITELOG_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
+#define REWRITELOG_FLAGS ( APR_WRITE | APR_APPEND | APR_CREATE )
+
+#else /* !REWRITELOG_DISABLED */
+
+#define rewritelog(x)
+
+#endif /* REWRITELOG_DISABLED */
/* remembered mime-type for [T=...] */
#define REWRITE_FORCED_MIMETYPE_NOTEVAR "rewrite-forced-mimetype"
+#define REWRITE_FORCED_HANDLER_NOTEVAR "rewrite-forced-handler"
#define ENVVAR_SCRIPT_URL "SCRIPT_URL"
#define REDIRECT_ENVVAR_SCRIPT_URL "REDIRECT_" ENVVAR_SCRIPT_URL
#define RULEFLAG_NOTMATCH 1<<6
#define RULEFLAG_PROXY 1<<7
#define RULEFLAG_PASSTHROUGH 1<<8
-#define RULEFLAG_FORBIDDEN 1<<9
-#define RULEFLAG_GONE 1<<10
-#define RULEFLAG_QSAPPEND 1<<11
-#define RULEFLAG_NOCASE 1<<12
-#define RULEFLAG_NOESCAPE 1<<13
+#define RULEFLAG_QSAPPEND 1<<9
+#define RULEFLAG_NOCASE 1<<10
+#define RULEFLAG_NOESCAPE 1<<11
+#define RULEFLAG_NOSUB 1<<12
+#define RULEFLAG_STATUS 1<<13
/* return code of the rewrite rule
* the result may be escaped - or not
*/
#define ACTION_NORMAL 1<<0
#define ACTION_NOESCAPE 1<<1
+#define ACTION_STATUS 1<<2
#define MAPTYPE_TXT 1<<0
/* XXX: not used at all. We should do a check somewhere and/or cut the cookie */
#define MAX_COOKIE_LEN 4096
-/* max number of regex captures */
-#define MAX_NMATCH 10
-
-/* default maximum number of internal redirects */
-#define REWRITE_REDIRECT_LIMIT 10
-
-/* for rewrite log file */
-#define REWRITELOG_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
-#define REWRITELOG_FLAGS ( APR_WRITE | APR_APPEND | APR_CREATE )
-
/* max line length (incl.\n) in text rewrite maps */
#ifndef REWRITE_MAX_TXT_MAP_LINE
#define REWRITE_MAX_TXT_MAP_LINE 1024
#endif
-/* max response length (incl.\n) in prg rewrite maps */
-#ifndef REWRITE_MAX_PRG_MAP_LINE
-#define REWRITE_MAX_PRG_MAP_LINE 2048
+/* buffer length for prg rewrite maps */
+#ifndef REWRITE_PRG_MAP_BUF
+#define REWRITE_PRG_MAP_BUF 1024
#endif
/* for better readbility */
const char *datafile; /* filename for map data files */
const char *dbmtype; /* dbm type for dbm map data files */
const char *checkfile; /* filename to check for map existence */
+ const char *cachename; /* for cached maps (txt/rnd/dbm) */
int type; /* the type of the map */
apr_file_t *fpin; /* in file pointer for program maps */
apr_file_t *fpout; /* out file pointer for program maps */
CONDPAT_FILE_SIZE,
CONDPAT_FILE_LINK,
CONDPAT_FILE_DIR,
+ CONDPAT_FILE_XBIT,
CONDPAT_LU_URL,
CONDPAT_LU_FILE,
CONDPAT_STR_GT,
typedef struct {
char *input; /* Input string of RewriteCond */
char *pattern; /* the RegExp pattern string */
- regex_t *regexp; /* the precompiled regexp */
+ ap_regex_t *regexp; /* the precompiled regexp */
int flags; /* Flags which control the match */
pattern_type ptype; /* pattern type */
} rewritecond_entry;
typedef struct {
apr_array_header_t *rewriteconds;/* the corresponding RewriteCond entries */
char *pattern; /* the RegExp pattern string */
- regex_t *regexp; /* the RegExp pattern compilation */
+ ap_regex_t *regexp; /* the RegExp pattern compilation */
char *output; /* the Substitution string */
int flags; /* Flags which control the substitution */
char *forced_mimetype; /* forced MIME type of substitution */
- int forced_responsecode; /* forced HTTP redirect response status */
+ char *forced_handler; /* forced content handler of subst. */
+ int forced_responsecode; /* forced HTTP response status */
data_item *env; /* added environment variables */
data_item *cookie; /* added cookies */
int skip; /* number of next rules to skip */
typedef struct {
int state; /* the RewriteEngine state */
int options; /* the RewriteOption state */
+#ifndef REWRITELOG_DISABLED
const char *rewritelogfile; /* the RewriteLog filename */
apr_file_t *rewritelogfp; /* the RewriteLog open filepointer */
int rewriteloglevel; /* the RewriteLog level of verbosity */
+#endif
apr_hash_t *rewritemaps; /* the RewriteMap entries */
apr_array_header_t *rewriteconds; /* the RewriteCond entries (temp.) */
apr_array_header_t *rewriterules; /* the RewriteRule entries */
server_rec *server; /* the corresponding server indicator */
- int redirect_limit; /* max number of internal redirects */
} rewrite_server_conf;
typedef struct {
apr_array_header_t *rewriterules; /* the RewriteRule entries */
char *directory; /* the directory where it applies */
const char *baseurl; /* the base-URL where it applies */
- int redirect_limit; /* max. number of internal redirects */
} rewrite_perdir_conf;
-typedef struct {
- int redirects; /* current number of redirects */
- int redirect_limit; /* maximum number of redirects */
-} rewrite_request_conf;
-
-
/* the (per-child) cache structures.
*/
typedef struct cache {
typedef struct backrefinfo {
char *source;
int nsub;
- regmatch_t regmatch[10];
+ ap_regmatch_t regmatch[AP_MAX_REG_MATCH];
} backrefinfo;
/* single linked list used for
const char *string;
} result_list;
+/* context structure for variable lookup and expansion
+ */
+typedef struct {
+ request_rec *r;
+ const char *uri;
+ const char *vary_this;
+ const char *vary;
+ char *perdir;
+ backrefinfo briRR;
+ backrefinfo briRC;
+} rewrite_ctx;
/*
* +-------------------------------------------------------+
/* Locks/Mutexes */
static const char *lockname;
static apr_global_mutex_t *rewrite_mapr_lock_acquire = NULL;
+
+#ifndef REWRITELOG_DISABLED
static apr_global_mutex_t *rewrite_log_lock = NULL;
+#endif
+/* Optional functions imported from mod_ssl when loaded: */
+static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *rewrite_ssl_lookup = NULL;
+static APR_OPTIONAL_FN_TYPE(ssl_is_https) *rewrite_is_https = NULL;
/*
* +-------------------------------------------------------+
* +-------------------------------------------------------+
*/
+#ifndef REWRITELOG_DISABLED
static char *current_logtime(request_rec *r)
{
apr_time_exp_t t;
return 1;
}
-static void rewritelog(request_rec *r, int level, const char *fmt, ...)
+static void do_rewritelog(request_rec *r, int level, char *perdir,
+ const char *fmt, ...)
{
rewrite_server_conf *conf;
char *logline, *text;
va_end(ap);
logline = apr_psprintf(r->pool, "%s %s %s %s [%s/sid#%pp][rid#%pp/%s%s%s] "
- "(%d) %s" APR_EOL_STR,
+ "(%d) %s%s%s%s" APR_EOL_STR,
rhost ? rhost : "UNKNOWN-HOST",
rname ? rname : "-",
r->user ? (*r->user ? r->user : "\"\"") : "-",
r->main ? "subreq" : "initial",
redir ? "/redir#" : "",
redir ? apr_itoa(r->pool, redir) : "",
- level, text);
+ level,
+ perdir ? "[perdir " : "",
+ perdir ? perdir : "",
+ perdir ? "] ": "",
+ text);
rv = apr_global_mutex_lock(rewrite_log_lock);
if (rv != APR_SUCCESS) {
return;
}
+#endif /* !REWRITELOG_DISABLED */
/*
}
switch (*uri++) {
+ case 'a':
+ case 'A':
+ if (!strncasecmp(uri, "jp://", 5)) { /* ajp:// */
+ return 6;
+ }
+
+ case 'b':
+ case 'B':
+ if (!strncasecmp(uri, "alancer://", 10)) { /* balancer:// */
+ return 11;
+ }
+ break;
+
case 'f':
case 'F':
if (!strncasecmp(uri, "tp://", 5)) { /* ftp:// */
/* don't touch, unless it's an http or mailto URL.
* See RFC 1738 and RFC 2368.
*/
- if ( is_absolute_uri(r->filename)
+ if (is_absolute_uri(r->filename)
+ && strncasecmp(r->filename, "ajp", 3)
+ && strncasecmp(r->filename, "balancer", 8)
&& strncasecmp(r->filename, "http", 4)
&& strncasecmp(r->filename, "mailto", 6)) {
r->args = NULL; /* forget the query that's still flying around */
r->args[len-1] = '\0';
}
- rewritelog(r, 3, "split uri=%s -> uri=%s, args=%s", olduri,
- r->filename, r->args ? r->args : "<none>");
+ rewritelog((r, 3, NULL, "split uri=%s -> uri=%s, args=%s", olduri,
+ r->filename, r->args ? r->args : "<none>"));
}
return;
char *cp;
apr_size_t l;
- cp = (char *)ap_http_method(r);
+ cp = (char *)ap_http_scheme(r);
l = strlen(cp);
if ( strlen(r->filename) > l+3
&& strncasecmp(r->filename, cp, l) == 0
/* now check whether we could reduce it to a local path... */
if (ap_matches_request_vhost(r, host, port)) {
- rewritelog(r, 3, "reduce %s -> %s", r->filename, url);
+ rewritelog((r, 3, NULL, "reduce %s -> %s", r->filename, url));
r->filename = apr_pstrdup(r->pool, url);
}
}
: apr_psprintf(r->pool, ":%u", port);
r->filename = apr_psprintf(r->pool, "%s://%s%s%s%s",
- ap_http_method(r), thisserver, thisport,
+ ap_http_scheme(r), thisserver, thisport,
(*r->filename == '/') ? "" : "/",
r->filename);
}
apr_size_t slen, outlen;
char *output;
- rewritelog(r, 5, "strip matching prefix: %s -> %s", input, input+len);
+ rewritelog((r, 5, NULL, "strip matching prefix: %s -> %s", input,
+ input+len));
slen = strlen(subst);
if (slen && subst[slen - 1] != '/') {
memcpy(output+slen, input+len, outlen - slen);
output[outlen] = '\0';
- rewritelog(r, 4, "add subst prefix: %s -> %s", input+len, output);
+ rewritelog((r, 4, NULL, "add subst prefix: %s -> %s", input+len,
+ output));
return output;
}
rewrite_server_conf *conf;
apr_hash_index_t *hi;
apr_status_t rc;
+ int lock_warning_issued = 0;
conf = ap_get_module_config(s->module_config, &rewrite_module);
continue;
}
+ if (!lock_warning_issued && (!lockname || !*lockname)) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
+ "mod_rewrite: Running external rewrite maps "
+ "without defining a RewriteLock is DANGEROUS!");
+ ++lock_warning_issued;
+ }
+
rc = rewritemap_program_child(p, map->argv[0], map->argv,
&fpout, &fpin);
if (rc != APR_SUCCESS || fpin == NULL || fpout == NULL) {
static char *lookup_map_program(request_rec *r, apr_file_t *fpin,
apr_file_t *fpout, char *key)
{
- char buf[REWRITE_MAX_PRG_MAP_LINE];
+ char *buf;
char c;
- apr_size_t i;
- apr_size_t nbytes;
+ apr_size_t i, nbytes, combined_len = 0;
apr_status_t rv;
+ const char *eol = APR_EOL_STR;
+ apr_size_t eolc = 0;
+ int found_nl = 0;
+ result_list *buflist = NULL, *curbuf = NULL;
#ifndef NO_WRITEV
struct iovec iova[2];
apr_file_writev(fpin, iova, niov, &nbytes);
#endif
+ buf = apr_palloc(r->pool, REWRITE_PRG_MAP_BUF + 1);
+
/* read in the response value */
- i = 0;
nbytes = 1;
apr_file_read(fpout, &c, &nbytes);
- while (nbytes == 1 && (i < REWRITE_MAX_PRG_MAP_LINE)) {
- if (c == '\n') {
- break;
+ do {
+ i = 0;
+ while (nbytes == 1 && (i < REWRITE_PRG_MAP_BUF)) {
+ if (c == eol[eolc]) {
+ if (!eol[++eolc]) {
+ /* remove eol from the buffer */
+ --eolc;
+ if (i < eolc) {
+ curbuf->len -= eolc-i;
+ i = 0;
+ }
+ else {
+ i -= eolc;
+ }
+ ++found_nl;
+ break;
+ }
+ }
+
+ /* only partial (invalid) eol sequence -> reset the counter */
+ else if (eolc) {
+ eolc = 0;
+ }
+
+ /* catch binary mode, e.g. on Win32 */
+ else if (c == '\n') {
+ ++found_nl;
+ break;
+ }
+
+ buf[i++] = c;
+ apr_file_read(fpout, &c, &nbytes);
+ }
+
+ /* well, if there wasn't a newline yet, we need to read further */
+ if (buflist || (nbytes == 1 && !found_nl)) {
+ if (!buflist) {
+ curbuf = buflist = apr_palloc(r->pool, sizeof(*buflist));
+ }
+ else if (i) {
+ curbuf->next = apr_palloc(r->pool, sizeof(*buflist));
+ curbuf = curbuf->next;
+
+ }
+ curbuf->next = NULL;
+
+ if (i) {
+ curbuf->string = buf;
+ curbuf->len = i;
+ combined_len += i;
+ buf = apr_palloc(r->pool, REWRITE_PRG_MAP_BUF);
+ }
+
+ if (nbytes == 1 && !found_nl) {
+ i = 0;
+ continue;
+ }
}
- buf[i++] = c;
- apr_file_read(fpout, &c, &nbytes);
+ break;
+ } while (1);
+
+ /* concat the stuff */
+ if (buflist) {
+ char *p;
+
+ p = buf = apr_palloc(r->pool, combined_len + 1); /* \0 */
+ while (buflist) {
+ if (buflist->len) {
+ memcpy(p, buflist->string, buflist->len);
+ p += buflist->len;
+ }
+ buflist = buflist->next;
+ }
+ *p = '\0';
+ i = combined_len;
+ }
+ else {
+ buf[i] = '\0';
}
/* give the lock back */
}
}
- if (i == 4 && strncasecmp(buf, "NULL", 4) == 0) {
+ /* catch the "failed" case */
+ if (i == 4 && !strcasecmp(buf, "NULL")) {
return NULL;
}
- return apr_pstrmemdup(r->pool, buf, i);
+ return buf;
}
/*
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"mod_rewrite: can't access text RewriteMap file %s",
s->checkfile);
- rewritelog(r, 1, "can't open RewriteMap file, see error log");
+ rewritelog((r, 1, NULL,
+ "can't open RewriteMap file, see error log"));
return NULL;
}
- value = get_cache_value(name, st.mtime, key, r->pool);
+ value = get_cache_value(s->cachename, st.mtime, key, r->pool);
if (!value) {
- rewritelog(r, 6, "cache lookup FAILED, forcing new map lookup");
+ rewritelog((r, 6, NULL,
+ "cache lookup FAILED, forcing new map lookup"));
value = lookup_map_txtfile(r, s->datafile, key);
if (!value) {
- rewritelog(r, 5, "map lookup FAILED: map=%s[txt] key=%s",
- name, key);
- set_cache_value(name, st.mtime, key, "");
+ rewritelog((r, 5, NULL, "map lookup FAILED: map=%s[txt] key=%s",
+ name, key));
+ set_cache_value(s->cachename, st.mtime, key, "");
return NULL;
}
- rewritelog(r, 5, "map lookup OK: map=%s[txt] key=%s -> val=%s",
- name, key, value);
- set_cache_value(name, st.mtime, key, value);
+ rewritelog((r, 5, NULL,"map lookup OK: map=%s[txt] key=%s -> val=%s",
+ name, key, value));
+ set_cache_value(s->cachename, st.mtime, key, value);
}
else {
- rewritelog(r, 5, "cache lookup OK: map=%s[txt] key=%s -> val=%s",
- name, key, value);
+ rewritelog((r,5,NULL,"cache lookup OK: map=%s[txt] key=%s -> val=%s",
+ name, key, value));
}
if (s->type == MAPTYPE_RND && *value) {
value = select_random_value_part(r, value);
- rewritelog(r, 5, "randomly chosen the subvalue `%s'", value);
+ rewritelog((r, 5, NULL, "randomly chosen the subvalue `%s'",value));
}
return *value ? value : NULL;
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"mod_rewrite: can't access DBM RewriteMap file %s",
s->checkfile);
- rewritelog(r, 1, "can't open DBM RewriteMap file, see error log");
+ rewritelog((r, 1, NULL,
+ "can't open DBM RewriteMap file, see error log"));
return NULL;
}
- value = get_cache_value(name, st.mtime, key, r->pool);
+ value = get_cache_value(s->cachename, st.mtime, key, r->pool);
if (!value) {
- rewritelog(r, 6, "cache lookup FAILED, forcing new map lookup");
+ rewritelog((r, 6, NULL,
+ "cache lookup FAILED, forcing new map lookup"));
value = lookup_map_dbmfile(r, s->datafile, s->dbmtype, key);
if (!value) {
- rewritelog(r, 5, "map lookup FAILED: map=%s[dbm] key=%s",
- name, key);
- set_cache_value(name, st.mtime, key, "");
+ rewritelog((r, 5, NULL, "map lookup FAILED: map=%s[dbm] key=%s",
+ name, key));
+ set_cache_value(s->cachename, st.mtime, key, "");
return NULL;
}
- rewritelog(r, 5, "map lookup OK: map=%s[dbm] key=%s -> val=%s",
- name, key, value);
- set_cache_value(name, st.mtime, key, value);
+ rewritelog((r, 5, NULL, "map lookup OK: map=%s[dbm] key=%s -> "
+ "val=%s", name, key, value));
+
+ set_cache_value(s->cachename, st.mtime, key, value);
return value;
}
- rewritelog(r, 5, "cache lookup OK: map=%s[dbm] key=%s -> val=%s",
- name, key, value);
+ rewritelog((r, 5, NULL, "cache lookup OK: map=%s[dbm] key=%s -> val=%s",
+ name, key, value));
return *value ? value : NULL;
/*
case MAPTYPE_PRG:
value = lookup_map_program(r, s->fpin, s->fpout, key);
if (!value) {
- rewritelog(r, 5, "map lookup FAILED: map=%s key=%s", name, key);
+ rewritelog((r, 5,NULL,"map lookup FAILED: map=%s key=%s", name,
+ key));
return NULL;
}
- rewritelog(r, 5, "map lookup OK: map=%s key=%s -> val=%s",
- name, key, value);
+ rewritelog((r, 5, NULL, "map lookup OK: map=%s key=%s -> val=%s",
+ name, key, value));
return value;
/*
case MAPTYPE_INT:
value = s->func(r, key);
if (!value) {
- rewritelog(r, 5, "map lookup FAILED: map=%s key=%s", name, key);
+ rewritelog((r, 5,NULL,"map lookup FAILED: map=%s key=%s", name,
+ key));
return NULL;
}
- rewritelog(r, 5, "map lookup OK: map=%s key=%s -> val=%s",
- name, key, value);
+ rewritelog((r, 5, NULL, "map lookup OK: map=%s key=%s -> val=%s",
+ name, key, value));
return value;
}
/*
* lookup a HTTP header and set VARY note
*/
-static const char *lookup_header(request_rec *r, const char *name)
+static const char *lookup_header(const char *name, rewrite_ctx *ctx)
{
- const char *val = apr_table_get(r->headers_in, name);
+ const char *val = apr_table_get(ctx->r->headers_in, name);
if (val) {
- apr_table_merge(r->notes, VARY_KEY_THIS, name);
+ ctx->vary_this = ctx->vary_this
+ ? apr_pstrcat(ctx->r->pool, ctx->vary_this, ", ",
+ name, NULL)
+ : apr_pstrdup(ctx->r->pool, name);
}
return val;
}
+/*
+ * lookahead helper function
+ * Determine the correct URI path in perdir context
+ */
+static APR_INLINE const char *la_u(rewrite_ctx *ctx)
+{
+ rewrite_perdir_conf *conf;
+
+ if (*ctx->uri == '/') {
+ return ctx->uri;
+ }
+
+ conf = ap_get_module_config(ctx->r->per_dir_config, &rewrite_module);
+
+ return apr_pstrcat(ctx->r->pool, conf->baseurl
+ ? conf->baseurl : conf->directory,
+ ctx->uri, NULL);
+}
+
/*
* generic variable lookup
*/
-static char *lookup_variable(request_rec *r, char *var)
+static char *lookup_variable(char *var, rewrite_ctx *ctx)
{
const char *result;
+ request_rec *r = ctx->r;
apr_size_t varlen = strlen(var);
/* fast exit */
result = getenv(var);
}
}
+ else if (var[4] && !strncasecmp(var, "SSL", 3) && rewrite_ssl_lookup) {
+ result = rewrite_ssl_lookup(r->pool, r->server, r->connection, r,
+ var + 4);
+ }
}
else if (var[4] == ':') {
if (var[5]) {
request_rec *rr;
+ const char *path;
if (!strncasecmp(var, "HTTP", 4)) {
- result = lookup_header(r, var+5);
+ result = lookup_header(var+5, ctx);
}
else if (!strncasecmp(var, "LA-U", 4)) {
- if (r->filename && subreq_ok(r)) {
- rr = ap_sub_req_lookup_uri(r->filename, r, NULL);
- result = apr_pstrdup(r->pool, lookup_variable(rr, var+5));
+ if (ctx->uri && subreq_ok(r)) {
+ path = ctx->perdir ? la_u(ctx) : ctx->uri;
+ rr = ap_sub_req_lookup_uri(path, r, NULL);
+ ctx->r = rr;
+ result = apr_pstrdup(r->pool, lookup_variable(var+5, ctx));
+ ctx->r = r;
ap_destroy_sub_req(rr);
- rewritelog(r, 5, "lookahead: path=%s var=%s -> val=%s",
- r->filename, var+5, result);
+ rewritelog((r, 5, ctx->perdir, "lookahead: path=%s var=%s "
+ "-> val=%s", path, var+5, result));
return (char *)result;
}
}
else if (!strncasecmp(var, "LA-F", 4)) {
- if (r->filename && subreq_ok(r)) {
- rr = ap_sub_req_lookup_file(r->filename, r, NULL);
- result = apr_pstrdup(r->pool, lookup_variable(rr, var+5));
+ if (ctx->uri && subreq_ok(r)) {
+ path = ctx->uri;
+ if (ctx->perdir && *path == '/') {
+ /* sigh, the user wants a file based subrequest, but
+ * we can't do one, since we don't know what the file
+ * path is! In this case behave like LA-U.
+ */
+ rr = ap_sub_req_lookup_uri(path, r, NULL);
+ }
+ else {
+ if (ctx->perdir) {
+ rewrite_perdir_conf *conf;
+
+ conf = ap_get_module_config(r->per_dir_config,
+ &rewrite_module);
+
+ path = apr_pstrcat(r->pool, conf->directory, path,
+ NULL);
+ }
+
+ rr = ap_sub_req_lookup_file(path, r, NULL);
+ }
+
+ ctx->r = rr;
+ result = apr_pstrdup(r->pool, lookup_variable(var+5, ctx));
+ ctx->r = r;
ap_destroy_sub_req(rr);
- rewritelog(r, 5, "lookahead: path=%s var=%s -> val=%s",
- r->filename, var+5, result);
+ rewritelog((r, 5, ctx->perdir, "lookahead: path=%s var=%s "
+ "-> val=%s", path, var+5, result));
return (char *)result;
}
result = apr_psprintf(r->pool, "%04d%02d%02d%02d%02d%02d",
tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
- rewritelog(r, 1, "RESULT='%s'", result);
+ rewritelog((r, 1, ctx->perdir, "RESULT='%s'", result));
return (char *)result;
}
break;
+
+ case 5:
+ if (!strcmp(var, "HTTPS")) {
+ int flag = rewrite_is_https && rewrite_is_https(r->connection);
+ return apr_pstrdup(r->pool, flag ? "on" : "off");
+ }
+ break;
case 8:
switch (var[6]) {
case 'S':
if (!strcmp(var, "HTTP_HOST")) {
- result = lookup_header(r, "Host");
+ result = lookup_header("Host", ctx);
}
break;
case 'E':
if (*var == 'H' && !strcmp(var, "HTTP_ACCEPT")) {
- result = lookup_header(r, "Accept");
+ result = lookup_header("Accept", ctx);
}
else if (!strcmp(var, "THE_REQUEST")) {
result = r->the_request;
case 'K':
if (!strcmp(var, "HTTP_COOKIE")) {
- result = lookup_header(r, "Cookie");
+ result = lookup_header("Cookie", ctx);
}
break;
case 'O':
- if (*var == 'R' && !strcmp(var, "REMOTE_HOST")) {
+ if (*var == 'S' && !strcmp(var, "SERVER_PORT")) {
+ return apr_psprintf(r->pool, "%u", ap_get_server_port(r));
+ }
+ else if (var[7] == 'H' && !strcmp(var, "REMOTE_HOST")) {
result = ap_get_remote_host(r->connection,r->per_dir_config,
REMOTE_NAME, NULL);
}
- else if (!strcmp(var, "SERVER_PORT")) {
- return apr_psprintf(r->pool, "%u", ap_get_server_port(r));
+ else if (!strcmp(var, "REMOTE_PORT")) {
+ return apr_itoa(r->pool, r->connection->remote_addr->port);
}
break;
case 'P':
if (!strcmp(var, "HTTP_REFERER")) {
- result = lookup_header(r, "Referer");
+ result = lookup_header("Referer", ctx);
}
break;
case 14:
if (*var == 'H' && !strcmp(var, "HTTP_FORWARDED")) {
- result = lookup_header(r, "Forwarded");
+ result = lookup_header("Forwarded", ctx);
}
else if (!strcmp(var, "REQUEST_METHOD")) {
result = r->method;
switch (var[7]) {
case 'E':
if (!strcmp(var, "HTTP_USER_AGENT")) {
- result = lookup_header(r, "User-Agent");
+ result = lookup_header("User-Agent", ctx);
}
break;
case 21:
if (!strcmp(var, "HTTP_PROXY_CONNECTION")) {
- result = lookup_header(r, "Proxy-Connection");
+ result = lookup_header("Proxy-Connection", ctx);
}
break;
}
* are interpreted by a later expansion, producing results that
* were not intended by the administrator.
*/
-static char *do_expand(request_rec *r, char *input,
- backrefinfo *briRR, backrefinfo *briRC)
+static char *do_expand(char *input, rewrite_ctx *ctx)
{
result_list *result, *current;
result_list sresult[SMALL_EXPANSION];
unsigned spc = 0;
apr_size_t span, inputlen, outlen;
char *p, *c;
+ apr_pool_t *pool = ctx->r->pool;
span = strcspn(input, "\\$%");
inputlen = strlen(input);
/* fast exit */
if (inputlen == span) {
- return apr_pstrdup(r->pool, input);
+ return apr_pstrdup(pool, input);
}
/* well, actually something to do */
if (current->len) {
current->next = (spc < SMALL_EXPANSION)
? &(sresult[spc++])
- : apr_palloc(r->pool, sizeof(result_list));
+ : (result_list *)apr_palloc(pool,
+ sizeof(result_list));
current = current->next;
current->next = NULL;
current->len = 0;
/* variable lookup */
else if (*p == '%') {
- p = lookup_variable(r, apr_pstrmemdup(r->pool, p+2, endp-p-2));
+ p = lookup_variable(apr_pstrmemdup(pool, p+2, endp-p-2), ctx);
span = strlen(p);
current->len = span;
else {
char *map, *dflt;
- map = apr_pstrmemdup(r->pool, p+2, endp-p-2);
+ map = apr_pstrmemdup(pool, p+2, endp-p-2);
key = map + (key-p-2);
*key++ = '\0';
dflt = find_char_in_curlies(key, '|');
if (dflt) {
*dflt++ = '\0';
}
- else {
- dflt = "";
- }
/* reuse of key variable as result */
- key = lookup_map(r, map, do_expand(r, key, briRR, briRC));
+ key = lookup_map(ctx->r, map, do_expand(key, ctx));
- if (!key && *dflt) {
- key = do_expand(r, dflt, briRR, briRC);
+ if (!key && dflt && *dflt) {
+ key = do_expand(dflt, ctx);
}
if (key) {
/* backreference */
else if (apr_isdigit(p[1])) {
int n = p[1] - '0';
- backrefinfo *bri = (*p == '$') ? briRR : briRC;
+ backrefinfo *bri = (*p == '$') ? &ctx->briRR : &ctx->briRC;
/* see ap_pregsub() in server/util.c */
- if (bri && n <= bri->nsub
+ if (bri->source && n < AP_MAX_REG_MATCH
&& bri->regmatch[n].rm_eo > bri->regmatch[n].rm_so) {
span = bri->regmatch[n].rm_eo - bri->regmatch[n].rm_so;
if (current->len) {
current->next = (spc < SMALL_EXPANSION)
? &(sresult[spc++])
- : apr_palloc(r->pool, sizeof(result_list));
+ : (result_list *)apr_palloc(pool,
+ sizeof(result_list));
current = current->next;
current->next = NULL;
}
} while (p < input+inputlen);
/* assemble result */
- c = p = apr_palloc(r->pool, outlen + 1); /* don't forget the \0 */
+ c = p = apr_palloc(pool, outlen + 1); /* don't forget the \0 */
do {
if (result->len) {
ap_assert(c+result->len <= p+outlen); /* XXX: can be removed after
/*
* perform all the expansions on the environment variables
*/
-static void do_expand_env(request_rec *r, data_item *env,
- backrefinfo *briRR, backrefinfo *briRC)
+static void do_expand_env(data_item *env, rewrite_ctx *ctx)
{
char *name, *val;
while (env) {
- name = do_expand(r, env->data, briRR, briRC);
+ name = do_expand(env->data, ctx);
if ((val = ap_strchr(name, ':')) != NULL) {
*val++ = '\0';
- apr_table_set(r->subprocess_env, name, val);
- rewritelog(r, 5, "setting env variable '%s' to '%s'", name, val);
+ apr_table_set(ctx->r->subprocess_env, name, val);
+ rewritelog((ctx->r, 5, NULL, "setting env variable '%s' to '%s'",
+ name, val));
}
env = env->next;
notename = apr_pstrcat(rmain->pool, var, "_rewrite", NULL);
apr_pool_userdata_get(&data, notename, rmain->pool);
if (!data) {
+ char *exp_time = NULL;
+
expires = apr_strtok(NULL, ":", &tok_cntx);
path = expires ? apr_strtok(NULL, ":", &tok_cntx) : NULL;
+ if (expires) {
+ apr_time_exp_t tms;
+ apr_time_exp_gmt(&tms, r->request_time
+ + apr_time_from_sec((60 * atol(expires))));
+ exp_time = apr_psprintf(r->pool, "%s, %.2d-%s-%.4d "
+ "%.2d:%.2d:%.2d GMT",
+ apr_day_snames[tms.tm_wday],
+ tms.tm_mday,
+ apr_month_snames[tms.tm_mon],
+ tms.tm_year+1900,
+ tms.tm_hour, tms.tm_min, tms.tm_sec);
+ }
+
cookie = apr_pstrcat(rmain->pool,
var, "=", val,
- "; path=", (path)? path : "/",
+ "; path=", path ? path : "/",
"; domain=", domain,
- (expires)? "; expires=" : NULL,
- (expires)?
- ap_ht_time(r->pool,
- r->request_time +
- apr_time_from_sec((60 *
- atol(expires))),
- "%a, %d-%b-%Y %T GMT", 1)
- : NULL,
+ expires ? "; expires=" : NULL,
+ expires ? exp_time : NULL,
NULL);
- apr_table_add(rmain->err_headers_out, "Set-Cookie", cookie);
+ apr_table_addn(rmain->err_headers_out, "Set-Cookie", cookie);
apr_pool_userdata_set("set", notename, NULL, rmain->pool);
- rewritelog(rmain, 5, "setting cookie '%s'", cookie);
+ rewritelog((rmain, 5, NULL, "setting cookie '%s'", cookie));
}
else {
- rewritelog(rmain, 5, "skipping already set cookie '%s'", var);
+ rewritelog((rmain, 5, NULL, "skipping already set cookie '%s'",
+ var));
}
}
return;
}
-static void do_expand_cookie(request_rec *r, data_item *cookie,
- backrefinfo *briRR, backrefinfo *briRC)
+static void do_expand_cookie(data_item *cookie, rewrite_ctx *ctx)
{
while (cookie) {
- add_cookie(r, do_expand(r, cookie->data, briRR, briRC));
+ add_cookie(ctx->r, do_expand(cookie->data, ctx));
cookie = cookie->next;
}
return rc;
}
-#ifdef MOD_REWRITE_SET_MUTEX_PERMS
+#ifdef AP_NEED_SET_MUTEX_PERMS
rc = unixd_set_global_mutex_perms(rewrite_mapr_lock_acquire);
if (rc != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s,
a->state = ENGINE_DISABLED;
a->options = OPTION_NONE;
+#ifndef REWRITELOG_DISABLED
a->rewritelogfile = NULL;
a->rewritelogfp = NULL;
a->rewriteloglevel = 0;
+#endif
a->rewritemaps = apr_hash_make(p);
a->rewriteconds = apr_array_make(p, 2, sizeof(rewritecond_entry));
a->rewriterules = apr_array_make(p, 2, sizeof(rewriterule_entry));
a->server = s;
- a->redirect_limit = 0; /* unset (use default) */
return (void *)a;
}
a->state = overrides->state;
a->options = overrides->options;
a->server = overrides->server;
- a->redirect_limit = overrides->redirect_limit
- ? overrides->redirect_limit
- : base->redirect_limit;
if (a->options & OPTION_INHERIT) {
/*
* local directives override
* and anything else is inherited
*/
+#ifndef REWRITELOG_DISABLED
a->rewriteloglevel = overrides->rewriteloglevel != 0
? overrides->rewriteloglevel
: base->rewriteloglevel;
a->rewritelogfp = overrides->rewritelogfp != NULL
? overrides->rewritelogfp
: base->rewritelogfp;
+#endif
a->rewritemaps = apr_hash_overlay(p, overrides->rewritemaps,
base->rewritemaps);
a->rewriteconds = apr_array_append(p, overrides->rewriteconds,
* local directives override
* and anything else gets defaults
*/
+#ifndef REWRITELOG_DISABLED
a->rewriteloglevel = overrides->rewriteloglevel;
a->rewritelogfile = overrides->rewritelogfile;
a->rewritelogfp = overrides->rewritelogfp;
+#endif
a->rewritemaps = overrides->rewritemaps;
a->rewriteconds = overrides->rewriteconds;
a->rewriterules = overrides->rewriterules;
a->baseurl = NULL;
a->rewriteconds = apr_array_make(p, 2, sizeof(rewritecond_entry));
a->rewriterules = apr_array_make(p, 2, sizeof(rewriterule_entry));
- a->redirect_limit = 0; /* unset (use server config) */
if (path == NULL) {
a->directory = NULL;
a->options = overrides->options;
a->directory = overrides->directory;
a->baseurl = overrides->baseurl;
- a->redirect_limit = overrides->redirect_limit
- ? overrides->redirect_limit
- : base->redirect_limit;
if (a->options & OPTION_INHERIT) {
a->rewriteconds = apr_array_append(p, overrides->rewriteconds,
static const char *cmd_rewriteoptions(cmd_parms *cmd,
void *in_dconf, const char *option)
{
- int options = 0, limit = 0;
+ int options = 0;
char *w;
while (*option) {
options |= OPTION_INHERIT;
}
else if (!strncasecmp(w, "MaxRedirects=", 13)) {
- limit = atoi(&w[13]);
- if (limit <= 0) {
- return "RewriteOptions: MaxRedirects takes a number greater "
- "than zero.";
- }
- }
- else if (!strcasecmp(w, "MaxRedirects")) { /* be nice */
- return "RewriteOptions: MaxRedirects has the format MaxRedirects"
- "=n.";
+ ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
+ "RewriteOptions: MaxRedirects option has been "
+ "removed in favor of the global "
+ "LimitInternalRecursion directive and will be "
+ "ignored.");
}
else {
return apr_pstrcat(cmd->pool, "RewriteOptions: unknown option '",
&rewrite_module);
conf->options |= options;
- conf->redirect_limit = limit;
}
else { /* is per-directory command */
rewrite_perdir_conf *conf = in_dconf;
conf->options |= options;
- conf->redirect_limit = limit;
}
return NULL;
}
+#ifndef REWRITELOG_DISABLED
static const char *cmd_rewritelog(cmd_parms *cmd, void *dconf, const char *a1)
{
rewrite_server_conf *sconf;
return NULL;
}
+#endif /* rewritelog */
static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
const char *a2)
newmap->type = MAPTYPE_TXT;
newmap->datafile = fname;
newmap->checkfile = fname;
+ newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
+ (void *)cmd->server, a1);
}
else if (strncasecmp(a2, "rnd:", 4) == 0) {
if ((fname = ap_server_root_relative(cmd->pool, a2+4)) == NULL) {
newmap->type = MAPTYPE_RND;
newmap->datafile = fname;
newmap->checkfile = fname;
+ newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
+ (void *)cmd->server, a1);
}
else if (strncasecmp(a2, "dbm", 3) == 0) {
const char *ignored_fname;
newmap->type = MAPTYPE_DBM;
fname = NULL;
+ newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
+ (void *)cmd->server, a1);
if (a2[3] == ':') {
newmap->dbmtype = "default";
newmap->type = MAPTYPE_PRG;
newmap->datafile = NULL;
newmap->checkfile = newmap->argv[0];
+ newmap->cachename = NULL;
}
else if (strncasecmp(a2, "int:", 4) == 0) {
newmap->type = MAPTYPE_INT;
newmap->datafile = NULL;
newmap->checkfile = NULL;
+ newmap->cachename = NULL;
newmap->func = (char *(*)(request_rec *,char *))
apr_hash_get(mapfunc_hash, a2+4, strlen(a2+4));
if ((sconf->state == ENGINE_ENABLED) && (newmap->func == NULL)) {
newmap->type = MAPTYPE_TXT;
newmap->datafile = fname;
newmap->checkfile = fname;
+ newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
+ (void *)cmd->server, a1);
}
newmap->fpin = NULL;
newmap->fpout = NULL;
char *str = apr_pstrdup(cmd->pool, in_str);
rewrite_server_conf *sconf;
rewritecond_entry *newcond;
- regex_t *regexp;
+ ap_regex_t *regexp;
char *a1;
char *a2;
char *a3;
case 's': newcond->ptype = CONDPAT_FILE_SIZE; break;
case 'l': newcond->ptype = CONDPAT_FILE_LINK; break;
case 'd': newcond->ptype = CONDPAT_FILE_DIR; break;
+ case 'x': newcond->ptype = CONDPAT_FILE_XBIT; break;
case 'U': newcond->ptype = CONDPAT_LU_URL; break;
case 'F': newcond->ptype = CONDPAT_LU_FILE; break;
}
if (!newcond->ptype) {
regexp = ap_pregcomp(cmd->pool, a2,
- REG_EXTENDED | ((newcond->flags & CONDFLAG_NOCASE)
- ? REG_ICASE : 0));
+ AP_REG_EXTENDED | ((newcond->flags & CONDFLAG_NOCASE)
+ ? AP_REG_ICASE : 0));
if (!regexp) {
return apr_pstrcat(cmd->pool, "RewriteCond: cannot compile regular "
"expression '", a2, "'", NULL);
char *key, char *val)
{
rewriterule_entry *cfg = _cfg;
- int status = 0;
+ int error = 0;
switch (*key++) {
case 'c':
cp->next = NULL;
cp->data = val;
}
+ else {
+ ++error;
+ }
break;
case 'e':
cp->next = NULL;
cp->data = val;
}
+ else {
+ ++error;
+ }
break;
case 'f':
case 'F':
if (!*key || !strcasecmp(key, "orbidden")) { /* forbidden */
- cfg->flags |= RULEFLAG_FORBIDDEN;
+ cfg->flags |= (RULEFLAG_STATUS | RULEFLAG_NOSUB);
+ cfg->forced_responsecode = HTTP_FORBIDDEN;
+ }
+ else {
+ ++error;
}
break;
case 'g':
case 'G':
if (!*key || !strcasecmp(key, "one")) { /* gone */
- cfg->flags |= RULEFLAG_GONE;
+ cfg->flags |= (RULEFLAG_STATUS | RULEFLAG_NOSUB);
+ cfg->forced_responsecode = HTTP_GONE;
+ }
+ else {
+ ++error;
+ }
+ break;
+
+ case 'h':
+ case 'H':
+ if (!*key || !strcasecmp(key, "andler")) { /* handler */
+ cfg->forced_handler = val;
+ }
+ else {
+ ++error;
}
break;
if (!*key || !strcasecmp(key, "ast")) { /* last */
cfg->flags |= RULEFLAG_LASTRULE;
}
+ else {
+ ++error;
+ }
break;
case 'n':
|| !strcasecmp(key, "ocase")) { /* nocase */
cfg->flags |= RULEFLAG_NOCASE;
}
+ else {
+ ++error;
+ }
break;
case 'p':
|| !strcasecmp(key, "assthrough")) { /* passthrough */
cfg->flags |= RULEFLAG_PASSTHROUGH;
}
+ else {
+ ++error;
+ }
break;
case 'q':
case 'Q':
- if ( !strcasecmp(key, "QSA")
- || !strcasecmp(key, "qsappend")) { /* qsappend */
+ if ( !strcasecmp(key, "SA")
+ || !strcasecmp(key, "sappend")) { /* qsappend */
cfg->flags |= RULEFLAG_QSAPPEND;
}
+ else {
+ ++error;
+ }
break;
case 'r':
case 'R':
if (!*key || !strcasecmp(key, "edirect")) { /* redirect */
+ int status = 0;
+
cfg->flags |= RULEFLAG_FORCEREDIRECT;
if (strlen(val) > 0) {
if (strcasecmp(val, "permanent") == 0) {
}
else if (apr_isdigit(*val)) {
status = atoi(val);
+ if (status != HTTP_INTERNAL_SERVER_ERROR) {
+ int idx =
+ ap_index_of_response(HTTP_INTERNAL_SERVER_ERROR);
+
+ if (ap_index_of_response(status) == idx) {
+ return apr_psprintf(p, "RewriteRule: invalid HTTP "
+ "response code '%s' for "
+ "flag 'R'",
+ val);
+ }
+ }
if (!ap_is_HTTP_REDIRECT(status)) {
- return "RewriteRule: invalid HTTP response code "
- "for flag 'R'";
+ cfg->flags |= (RULEFLAG_STATUS | RULEFLAG_NOSUB);
}
}
cfg->forced_responsecode = status;
}
}
+ else {
+ ++error;
+ }
break;
case 's':
if (!*key || !strcasecmp(key, "kip")) { /* skip */
cfg->skip = atoi(val);
}
+ else {
+ ++error;
+ }
break;
case 't':
case 'T':
if (!*key || !strcasecmp(key, "ype")) { /* type */
cfg->forced_mimetype = val;
- ap_str_tolower(cfg->forced_mimetype);
+ }
+ else {
+ ++error;
}
break;
default:
- return apr_pstrcat(p, "RewriteRule: unknown flag '", key, "'", NULL);
+ ++error;
+ break;
+ }
+
+ if (error) {
+ return apr_pstrcat(p, "RewriteRule: unknown flag '", --key, "'", NULL);
}
return NULL;
char *str = apr_pstrdup(cmd->pool, in_str);
rewrite_server_conf *sconf;
rewriterule_entry *newrule;
- regex_t *regexp;
+ ap_regex_t *regexp;
char *a1;
char *a2;
char *a3;
/* arg3: optional flags field */
newrule->forced_mimetype = NULL;
+ newrule->forced_handler = NULL;
newrule->forced_responsecode = HTTP_MOVED_TEMPORARILY;
newrule->flags = RULEFLAG_NONE;
newrule->env = NULL;
++a1;
}
- regexp = ap_pregcomp(cmd->pool, a1, REG_EXTENDED |
+ regexp = ap_pregcomp(cmd->pool, a1, AP_REG_EXTENDED |
((newrule->flags & RULEFLAG_NOCASE)
- ? REG_ICASE : 0));
+ ? AP_REG_ICASE : 0));
if (!regexp) {
return apr_pstrcat(cmd->pool,
"RewriteRule: cannot compile regular expression '",
/* arg2: the output string */
newrule->output = a2;
+ if (*a2 == '-' && !a2[1]) {
+ newrule->flags |= RULEFLAG_NOSUB;
+ }
/* now, if the server or per-dir config holds an
* array of RewriteCond entries, we take it for us
/*
* Apply a single rewriteCond
*/
-static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
- char *perdir, backrefinfo *briRR,
- backrefinfo *briRC)
+static int apply_rewrite_cond(rewritecond_entry *p, rewrite_ctx *ctx)
{
- char *input = do_expand(r, p->input, briRR, briRC);
+ char *input = do_expand(p->input, ctx);
apr_finfo_t sb;
- request_rec *rsub;
- regmatch_t regmatch[MAX_NMATCH];
+ request_rec *rsub, *r = ctx->r;
+ ap_regmatch_t regmatch[AP_MAX_REG_MATCH];
int rc = 0;
switch (p->ptype) {
case CONDPAT_FILE_LINK:
#if !defined(OS2)
- if ( apr_lstat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS
+ if ( apr_stat(&sb, input, APR_FINFO_MIN | APR_FINFO_LINK,
+ r->pool) == APR_SUCCESS
&& sb.filetype == APR_LNK) {
rc = 1;
}
}
break;
+ case CONDPAT_FILE_XBIT:
+ if ( apr_stat(&sb, input, APR_FINFO_PROT, r->pool) == APR_SUCCESS
+ && (sb.protection & (APR_UEXECUTE | APR_GEXECUTE | APR_WEXECUTE))) {
+ rc = 1;
+ }
+ break;
+
case CONDPAT_LU_URL:
if (*input && subreq_ok(r)) {
rsub = ap_sub_req_lookup_uri(input, r, NULL);
if (rsub->status < 400) {
rc = 1;
}
- rewritelog(r, 5, "RewriteCond URI (-U) check: "
- "path=%s -> status=%d", input, rsub->status);
+ rewritelog((r, 5, NULL, "RewriteCond URI (-U) check: "
+ "path=%s -> status=%d", input, rsub->status));
ap_destroy_sub_req(rsub);
}
break;
r->pool) == APR_SUCCESS) {
rc = 1;
}
- rewritelog(r, 5, "RewriteCond file (-F) check: path=%s "
- "-> file=%s status=%d", input, rsub->filename,
- rsub->status);
+ rewritelog((r, 5, NULL, "RewriteCond file (-F) check: path=%s "
+ "-> file=%s status=%d", input, rsub->filename,
+ rsub->status));
ap_destroy_sub_req(rsub);
}
break;
default:
/* it is really a regexp pattern, so apply it */
- rc = !ap_regexec(p->regexp, input, p->regexp->re_nsub+1, regmatch, 0);
+ rc = !ap_regexec(p->regexp, input, AP_MAX_REG_MATCH, regmatch, 0);
/* update briRC backref info */
if (rc && !(p->flags & CONDFLAG_NOTMATCH)) {
- briRC->source = input;
- briRC->nsub = p->regexp->re_nsub;
- memcpy(briRC->regmatch, regmatch, sizeof(regmatch));
+ ctx->briRC.source = input;
+ ctx->briRC.nsub = p->regexp->re_nsub;
+ memcpy(ctx->briRC.regmatch, regmatch, sizeof(regmatch));
}
break;
}
rc = !rc;
}
- rewritelog(r, 4, "RewriteCond: input='%s' pattern='%s%s%s'%s => %s",
- input, (p->flags & CONDFLAG_NOTMATCH) ? "!" : "",
- (p->ptype == CONDPAT_STR_EQ) ? "=" : "", p->pattern,
- (p->flags & CONDFLAG_NOCASE) ? " [NC]" : "",
- rc ? "matched" : "not-matched");
+ rewritelog((r, 4, ctx->perdir, "RewriteCond: input='%s' pattern='%s%s%s'%s "
+ "=> %s", input, (p->flags & CONDFLAG_NOTMATCH) ? "!" : "",
+ (p->ptype == CONDPAT_STR_EQ) ? "=" : "", p->pattern,
+ (p->flags & CONDFLAG_NOCASE) ? " [NC]" : "",
+ rc ? "matched" : "not-matched"));
return rc;
}
+/* check for forced type and handler */
+static APR_INLINE void force_type_handler(rewriterule_entry *p,
+ rewrite_ctx *ctx)
+{
+ char *expanded;
+
+ if (p->forced_mimetype) {
+ expanded = do_expand(p->forced_mimetype, ctx);
+
+ if (*expanded) {
+ ap_str_tolower(expanded);
+
+ rewritelog((ctx->r, 2, ctx->perdir, "remember %s to have MIME-type "
+ "'%s'", ctx->r->filename, expanded));
+
+ apr_table_setn(ctx->r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
+ expanded);
+ }
+ }
+
+ if (p->forced_handler) {
+ expanded = do_expand(p->forced_handler, ctx);
+
+ if (*expanded) {
+ ap_str_tolower(expanded);
+
+ rewritelog((ctx->r, 2, ctx->perdir, "remember %s to have "
+ "Content-handler '%s'", ctx->r->filename, expanded));
+
+ apr_table_setn(ctx->r->notes, REWRITE_FORCED_HANDLER_NOTEVAR,
+ expanded);
+ }
+ }
+}
+
/*
- * Apply a single RewriteRule
+ * Apply a single RewriteRule
*/
-static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p,
- char *perdir)
+static int apply_rewrite_rule(rewriterule_entry *p, rewrite_ctx *ctx)
{
- char *uri;
- char *output;
- const char *vary;
- char *newuri;
- regex_t *regexp;
- regmatch_t regmatch[MAX_NMATCH];
- backrefinfo *briRR = NULL;
- backrefinfo *briRC = NULL;
- int prefixstrip;
- int failed;
+ ap_regmatch_t regmatch[AP_MAX_REG_MATCH];
apr_array_header_t *rewriteconds;
rewritecond_entry *conds;
- rewritecond_entry *c;
- int i;
- int rc;
+ int i, rc;
+ char *newuri = NULL;
+ request_rec *r = ctx->r;
+ int is_proxyreq = 0;
- /*
- * Initialisation
- */
- uri = r->filename;
- regexp = p->regexp;
- output = p->output;
+ ctx->uri = r->filename;
- /*
- * Add (perhaps splitted away) PATH_INFO postfix to URL to
- * make sure we really match against the complete URL.
- */
- if (perdir != NULL && r->path_info != NULL && r->path_info[0] != '\0') {
- rewritelog(r, 3, "[per-dir %s] add path info postfix: %s -> %s%s",
- perdir, uri, uri, r->path_info);
- uri = apr_pstrcat(r->pool, uri, r->path_info, NULL);
- }
+ if (ctx->perdir) {
+ apr_size_t dirlen = strlen(ctx->perdir);
- /*
- * On per-directory context (.htaccess) strip the location
- * prefix from the URL to make sure patterns apply only to
- * the local part. Additionally indicate this special
- * threatment in the logfile.
- */
- prefixstrip = 0;
- if (perdir != NULL) {
- if ( strlen(uri) >= strlen(perdir)
- && strncmp(uri, perdir, strlen(perdir)) == 0) {
- rewritelog(r, 3, "[per-dir %s] strip per-dir prefix: %s -> %s",
- perdir, uri, uri+strlen(perdir));
- uri = uri+strlen(perdir);
- prefixstrip = 1;
+ /*
+ * Proxy request?
+ */
+ is_proxyreq = ( r->proxyreq && r->filename
+ && !strncmp(r->filename, "proxy:", 6));
+
+ /* Since we want to match against the (so called) full URL, we have
+ * to re-add the PATH_INFO postfix
+ */
+ if (r->path_info && *r->path_info) {
+ rewritelog((r, 3, ctx->perdir, "add path info postfix: %s -> %s%s",
+ ctx->uri, ctx->uri, r->path_info));
+ ctx->uri = apr_pstrcat(r->pool, ctx->uri, r->path_info, NULL);
+ }
+
+ /* Additionally we strip the physical path from the url to match
+ * it independent from the underlaying filesystem.
+ */
+ if (!is_proxyreq && strlen(ctx->uri) >= dirlen &&
+ !strncmp(ctx->uri, ctx->perdir, dirlen)) {
+
+ rewritelog((r, 3, ctx->perdir, "strip per-dir prefix: %s -> %s",
+ ctx->uri, ctx->uri + dirlen));
+ ctx->uri = ctx->uri + dirlen;
}
}
- /*
- * Try to match the URI against the RewriteRule pattern
- * and exit immeddiately if it didn't apply.
+ /* Try to match the URI against the RewriteRule pattern
+ * and exit immediately if it didn't apply.
*/
- if (perdir == NULL) {
- rewritelog(r, 3, "applying pattern '%s' to uri '%s'",
- p->pattern, uri);
- }
- else {
- rewritelog(r, 3, "[per-dir %s] applying pattern '%s' to uri '%s'",
- perdir, p->pattern, uri);
- }
- rc = (ap_regexec(regexp, uri, regexp->re_nsub+1, regmatch, 0) == 0);
+ rewritelog((r, 3, ctx->perdir, "applying pattern '%s' to uri '%s'",
+ p->pattern, ctx->uri));
+
+ rc = !ap_regexec(p->regexp, ctx->uri, AP_MAX_REG_MATCH, regmatch, 0);
if (! (( rc && !(p->flags & RULEFLAG_NOTMATCH)) ||
(!rc && (p->flags & RULEFLAG_NOTMATCH)) ) ) {
return 0;
}
- /*
- * Else create the RewriteRule `regsubinfo' structure which
- * holds the substitution information.
+ /* It matched, wow! Now it's time to prepare the context structure for
+ * further processing
*/
- briRR = (backrefinfo *)apr_palloc(r->pool, sizeof(backrefinfo));
- if (!rc && (p->flags & RULEFLAG_NOTMATCH)) {
- /* empty info on negative patterns */
- briRR->source = "";
- briRR->nsub = 0;
+ ctx->vary_this = NULL;
+ ctx->briRC.source = NULL;
+
+ if (p->flags & RULEFLAG_NOTMATCH) {
+ ctx->briRR.source = NULL;
}
else {
- briRR->source = apr_pstrdup(r->pool, uri);
- briRR->nsub = regexp->re_nsub;
- memcpy((void *)(briRR->regmatch), (void *)(regmatch),
- sizeof(regmatch));
+ ctx->briRR.source = apr_pstrdup(r->pool, ctx->uri);
+ ctx->briRR.nsub = p->regexp->re_nsub;
+ memcpy(ctx->briRR.regmatch, regmatch, sizeof(regmatch));
}
- /*
- * Initiallally create the RewriteCond backrefinfo with
- * empty backrefinfo, i.e. not subst parts
- * (this one is adjusted inside apply_rewrite_cond() later!!)
- */
- briRC = (backrefinfo *)apr_pcalloc(r->pool, sizeof(backrefinfo));
- briRC->source = "";
- briRC->nsub = 0;
-
- /*
- * Ok, we already know the pattern has matched, but we now
- * additionally have to check for all existing preconditions
- * (RewriteCond) which have to be also true. We do this at
- * this very late stage to avoid unnessesary checks which
- * would slow down the rewriting engine!!
+ /* Ok, we already know the pattern has matched, but we now
+ * additionally have to check for all existing preconditions
+ * (RewriteCond) which have to be also true. We do this at
+ * this very late stage to avoid unnessesary checks which
+ * would slow down the rewriting engine.
*/
rewriteconds = p->rewriteconds;
conds = (rewritecond_entry *)rewriteconds->elts;
- failed = 0;
- for (i = 0; i < rewriteconds->nelts; i++) {
- c = &conds[i];
- rc = apply_rewrite_cond(r, c, perdir, briRR, briRC);
+
+ for (i = 0; i < rewriteconds->nelts; ++i) {
+ rewritecond_entry *c = &conds[i];
+
+ rc = apply_rewrite_cond(c, ctx);
if (c->flags & CONDFLAG_ORNEXT) {
- /*
- * The "OR" case
- */
- if (rc == 0) {
- /* One condition is false, but another can be
- * still true, so we have to continue...
- */
- apr_table_unset(r->notes, VARY_KEY_THIS);
+ if (!rc) {
+ /* One condition is false, but another can be still true. */
+ ctx->vary_this = NULL;
continue;
}
else {
- /* One true condition is enough in "or" case, so
- * skip the other conditions which are "ornext"
- * chained
- */
+ /* skip the rest of the chained OR conditions */
while ( i < rewriteconds->nelts
&& c->flags & CONDFLAG_ORNEXT) {
- i++;
- c = &conds[i];
+ c = &conds[++i];
}
continue;
}
}
- else {
- /*
- * The "AND" case, i.e. no "or" flag,
- * so a single failure means total failure.
- */
- if (rc == 0) {
- failed = 1;
- break;
- }
+ else if (!rc) {
+ return 0;
}
- vary = apr_table_get(r->notes, VARY_KEY_THIS);
- if (vary != NULL) {
- apr_table_merge(r->notes, VARY_KEY, vary);
- apr_table_unset(r->notes, VARY_KEY_THIS);
+
+ /* If some HTTP header was involved in the condition, remember it
+ * for later use
+ */
+ if (ctx->vary_this) {
+ ctx->vary = ctx->vary
+ ? apr_pstrcat(r->pool, ctx->vary, ", ", ctx->vary_this,
+ NULL)
+ : ctx->vary_this;
+ ctx->vary_this = NULL;
}
}
- /* if any condition fails the complete rule fails */
- if (failed) {
- apr_table_unset(r->notes, VARY_KEY);
- apr_table_unset(r->notes, VARY_KEY_THIS);
- return 0;
- }
- /*
- * Regardless of what we do next, we've found a match. Check to see
- * if any of the request header fields were involved, and add them
- * to the Vary field of the response.
- */
- if ((vary = apr_table_get(r->notes, VARY_KEY)) != NULL) {
- apr_table_merge(r->headers_out, "Vary", vary);
- apr_table_unset(r->notes, VARY_KEY);
+ /* expand the result */
+ if (!(p->flags & RULEFLAG_NOSUB)) {
+ newuri = do_expand(p->output, ctx);
+ rewritelog((r, 2, ctx->perdir, "rewrite '%s' -> '%s'", ctx->uri,
+ newuri));
}
- /*
- * If this is a pure matching rule (`RewriteRule <pat> -')
- * we stop processing and return immediately. The only thing
- * we have not to forget are the environment variables and
- * cookies:
- * (`RewriteRule <pat> - [E=...,CO=...]')
- */
- if (output[0] == '-' && !output[1]) {
- do_expand_env(r, p->env, briRR, briRC);
- do_expand_cookie(r, p->cookie, briRR, briRC);
- if (p->forced_mimetype != NULL) {
- if (perdir == NULL) {
- /* In the per-server context we can force the MIME-type
- * the correct way by notifying our MIME-type hook handler
- * to do the job when the MIME-type API stage is reached.
- */
- rewritelog(r, 2, "remember %s to have MIME-type '%s'",
- r->filename, p->forced_mimetype);
- apr_table_setn(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
- p->forced_mimetype);
- }
- else {
- /* In per-directory context we operate in the Fixup API hook
- * which is after the MIME-type hook, so our MIME-type handler
- * has no chance to set r->content_type. And because we are
- * in the situation where no substitution takes place no
- * sub-request will happen (which could solve the
- * restriction). As a workaround we do it ourself now
- * immediately although this is not strictly API-conforming.
- * But it's the only chance we have...
- */
- rewritelog(r, 1, "[per-dir %s] force %s to have MIME-type "
- "'%s'", perdir, r->filename, p->forced_mimetype);
- ap_set_content_type(r, p->forced_mimetype);
- }
- }
- return 2;
- }
+ /* expand [E=var:val] and [CO=<cookie>] */
+ do_expand_env(p->env, ctx);
+ do_expand_cookie(p->cookie, ctx);
- /*
- * Ok, now we finally know all patterns have matched and
- * that there is something to replace, so we create the
- * substitution URL string in `newuri'.
- */
- newuri = do_expand(r, output, briRR, briRC);
- if (perdir == NULL) {
- rewritelog(r, 2, "rewrite %s -> %s", uri, newuri);
- }
- else {
- rewritelog(r, 2, "[per-dir %s] rewrite %s -> %s", perdir, uri, newuri);
- }
+ /* non-substitution rules ('RewriteRule <pat> -') end here. */
+ if (p->flags & RULEFLAG_NOSUB) {
+ force_type_handler(p, ctx);
- /*
- * Additionally do expansion for the environment variable
- * strings (`RewriteRule .. .. [E=<string>]').
- */
- do_expand_env(r, p->env, briRR, briRC);
+ if (p->flags & RULEFLAG_STATUS) {
+ rewritelog((r, 2, ctx->perdir, "forcing responsecode %d for %s",
+ p->forced_responsecode, r->filename));
- /*
- * Also set cookies for any cookie strings
- * (`RewriteRule .. .. [CO=<string>]').
- */
- do_expand_cookie(r, p->cookie, briRR, briRC);
+ r->status = p->forced_responsecode;
+ }
- /*
- * Now replace API's knowledge of the current URI:
- * Replace r->filename with the new URI string and split out
- * an on-the-fly generated QUERY_STRING part into r->args
- */
- r->filename = apr_pstrdup(r->pool, newuri);
+ return 2;
+ }
+
+ /* Now adjust API's knowledge about r->filename and r->args */
+ r->filename = newuri;
splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND);
- /*
- * Add the previously stripped per-directory location
- * prefix if the new URI is not a new one for this
- * location, i.e. if it's not an absolute URL (!) path nor
- * a fully qualified URL scheme.
+ /* Add the previously stripped per-directory location prefix, unless
+ * (1) it's an absolute URL path and
+ * (2) it's a full qualified URL
*/
- if (prefixstrip && *r->filename != '/'
- && !is_absolute_uri(r->filename)) {
- rewritelog(r, 3, "[per-dir %s] add per-dir prefix: %s -> %s%s",
- perdir, r->filename, perdir, r->filename);
- r->filename = apr_pstrcat(r->pool, perdir, r->filename, NULL);
+ if ( ctx->perdir && !is_proxyreq && *r->filename != '/'
+ && !is_absolute_uri(r->filename)) {
+ rewritelog((r, 3, ctx->perdir, "add per-dir prefix: %s -> %s%s",
+ r->filename, ctx->perdir, r->filename));
+
+ r->filename = apr_pstrcat(r->pool, ctx->perdir, r->filename, NULL);
}
- /*
- * If this rule is forced for proxy throughput
- * (`RewriteRule ... ... [P]') then emulate mod_proxy's
- * URL-to-filename handler to be sure mod_proxy is triggered
- * for this URL later in the Apache API. But make sure it is
- * a fully-qualified URL. (If not it is qualified with
- * ourself).
+ /* If this rule is forced for proxy throughput
+ * (`RewriteRule ... ... [P]') then emulate mod_proxy's
+ * URL-to-filename handler to be sure mod_proxy is triggered
+ * for this URL later in the Apache API. But make sure it is
+ * a fully-qualified URL. (If not it is qualified with
+ * ourself).
*/
if (p->flags & RULEFLAG_PROXY) {
fully_qualify_uri(r);
- if (perdir == NULL) {
- rewritelog(r, 2, "forcing proxy-throughput with %s", r->filename);
- }
- else {
- rewritelog(r, 2, "[per-dir %s] forcing proxy-throughput with %s",
- perdir, r->filename);
- }
+
+ rewritelog((r, 2, ctx->perdir, "forcing proxy-throughput with %s",
+ r->filename));
+
r->filename = apr_pstrcat(r->pool, "proxy:", r->filename, NULL);
return 1;
}
- /*
- * If this rule is explicitly forced for HTTP redirection
- * (`RewriteRule .. .. [R]') then force an external HTTP
- * redirect. But make sure it is a fully-qualified URL. (If
- * not it is qualified with ourself).
+ /* If this rule is explicitly forced for HTTP redirection
+ * (`RewriteRule .. .. [R]') then force an external HTTP
+ * redirect. But make sure it is a fully-qualified URL. (If
+ * not it is qualified with ourself).
*/
if (p->flags & RULEFLAG_FORCEREDIRECT) {
fully_qualify_uri(r);
- if (perdir == NULL) {
- rewritelog(r, 2,
- "explicitly forcing redirect with %s", r->filename);
- }
- else {
- rewritelog(r, 2,
- "[per-dir %s] explicitly forcing redirect with %s",
- perdir, r->filename);
- }
+
+ rewritelog((r, 2, ctx->perdir, "explicitly forcing redirect with %s",
+ r->filename));
+
r->status = p->forced_responsecode;
return 1;
}
- /*
- * Special Rewriting Feature: Self-Reduction
- * We reduce the URL by stripping a possible
- * http[s]://<ourhost>[:<port>] prefix, i.e. a prefix which
- * corresponds to ourself. This is to simplify rewrite maps
- * and to avoid recursion, etc. When this prefix is not a
- * coincidence then the user has to use [R] explicitly (see
- * above).
+ /* Special Rewriting Feature: Self-Reduction
+ * We reduce the URL by stripping a possible
+ * http[s]://<ourhost>[:<port>] prefix, i.e. a prefix which
+ * corresponds to ourself. This is to simplify rewrite maps
+ * and to avoid recursion, etc. When this prefix is not a
+ * coincidence then the user has to use [R] explicitly (see
+ * above).
*/
reduce_uri(r);
- /*
- * If this rule is still implicitly forced for HTTP
- * redirection (`RewriteRule .. <scheme>://...') then
- * directly force an external HTTP redirect.
+ /* If this rule is still implicitly forced for HTTP
+ * redirection (`RewriteRule .. <scheme>://...') then
+ * directly force an external HTTP redirect.
*/
if (is_absolute_uri(r->filename)) {
- if (perdir == NULL) {
- rewritelog(r, 2,
- "implicitly forcing redirect (rc=%d) with %s",
- p->forced_responsecode, r->filename);
- }
- else {
- rewritelog(r, 2, "[per-dir %s] implicitly forcing redirect "
- "(rc=%d) with %s", perdir, p->forced_responsecode,
- r->filename);
- }
+ rewritelog((r, 2, ctx->perdir, "implicitly forcing redirect (rc=%d) "
+ "with %s", p->forced_responsecode, r->filename));
+
r->status = p->forced_responsecode;
return 1;
}
- /*
- * Finally we had to remember if a MIME-type should be
- * forced for this URL (`RewriteRule .. .. [T=<type>]')
- * Later in the API processing phase this is forced by our
- * MIME API-hook function. This time it's no problem even for
- * the per-directory context (where the MIME-type hook was
- * already processed) because a sub-request happens ;-)
- */
- if (p->forced_mimetype != NULL) {
- apr_table_setn(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
- p->forced_mimetype);
- if (perdir == NULL) {
- rewritelog(r, 2, "remember %s to have MIME-type '%s'",
- r->filename, p->forced_mimetype);
- }
- else {
- rewritelog(r, 2,
- "[per-dir %s] remember %s to have MIME-type '%s'",
- perdir, r->filename, p->forced_mimetype);
- }
- }
+ /* Finally remember the forced mime-type */
+ force_type_handler(p, ctx);
- /*
- * Puuhhhhhhhh... WHAT COMPLICATED STUFF ;_)
- * But now we're done for this particular rule.
+ /* Puuhhhhhhhh... WHAT COMPLICATED STUFF ;_)
+ * But now we're done for this particular rule.
*/
return 1;
}
int changed;
int rc;
int s;
+ rewrite_ctx *ctx;
+
+ ctx = apr_palloc(r->pool, sizeof(*ctx));
+ ctx->perdir = perdir;
+ ctx->r = r;
/*
* Iterate over all existing rules
*/
if (r->main != NULL &&
(p->flags & RULEFLAG_IGNOREONSUBREQ ||
- p->flags & RULEFLAG_PROXY ||
p->flags & RULEFLAG_FORCEREDIRECT )) {
continue;
}
/*
* Apply the current rule.
*/
- rc = apply_rewrite_rule(r, p, perdir);
+ ctx->vary = NULL;
+ rc = apply_rewrite_rule(p, ctx);
+
if (rc) {
+ /* Regardless of what we do next, we've found a match. Check to see
+ * if any of the request header fields were involved, and add them
+ * to the Vary field of the response.
+ */
+ if (ctx->vary) {
+ apr_table_merge(r->headers_out, "Vary", ctx->vary);
+ }
+
+ /*
+ * The rule sets the response code (implies match-only)
+ */
+ if (p->flags & RULEFLAG_STATUS) {
+ return ACTION_STATUS;
+ }
+
/*
- * Indicate a change if this was not a match-only rule.
+ * Indicate a change if this was not a match-only rule.
*/
if (rc != 2) {
changed = ((p->flags & RULEFLAG_NOESCAPE)
* modules like mod_alias, mod_userdir, etc.
*/
if (p->flags & RULEFLAG_PASSTHROUGH) {
- rewritelog(r, 2, "forcing '%s' to get passed through "
- "to next API URI-to-filename handler", r->filename);
+ rewritelog((r, 2, perdir, "forcing '%s' to get passed through "
+ "to next API URI-to-filename handler", r->filename));
r->filename = apr_pstrcat(r->pool, "passthrough:",
r->filename, NULL);
changed = ACTION_NORMAL;
break;
}
- /*
- * Rule has the "forbidden" flag set which means that
- * we stop processing and indicate this to the caller.
- */
- if (p->flags & RULEFLAG_FORBIDDEN) {
- rewritelog(r, 2, "forcing '%s' to be forbidden", r->filename);
- r->filename = apr_pstrcat(r->pool, "forbidden:",
- r->filename, NULL);
- changed = ACTION_NORMAL;
- break;
- }
-
- /*
- * Rule has the "gone" flag set which means that
- * we stop processing and indicate this to the caller.
- */
- if (p->flags & RULEFLAG_GONE) {
- rewritelog(r, 2, "forcing '%s' to be gone", r->filename);
- r->filename = apr_pstrcat(r->pool, "gone:", r->filename, NULL);
- changed = ACTION_NORMAL;
- break;
- }
-
/*
* Stop processing also on proxy pass-through and
* last-rule and new-round flags.
*/
- if (p->flags & RULEFLAG_PROXY) {
- break;
- }
- if (p->flags & RULEFLAG_LASTRULE) {
+ if (p->flags & (RULEFLAG_PROXY | RULEFLAG_LASTRULE)) {
break;
}
/* check if proxy module is available */
proxy_available = (ap_find_linked_module("mod_proxy.c") != NULL);
+#ifndef REWRITELOG_DISABLED
/* create the rewriting lockfiles in the parent */
if ((rv = apr_global_mutex_create(&rewrite_log_lock, NULL,
APR_LOCK_DEFAULT, p)) != APR_SUCCESS) {
return HTTP_INTERNAL_SERVER_ERROR;
}
-#ifdef MOD_REWRITE_SET_MUTEX_PERMS
+#ifdef AP_NEED_SET_MUTEX_PERMS
rv = unixd_set_global_mutex_perms(rewrite_log_lock);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
"rewrite_log_lock; check User and Group directives");
return HTTP_INTERNAL_SERVER_ERROR;
}
-#endif
+#endif /* perms */
+#endif /* rewritelog */
rv = rewritelock_create(s, p);
if (rv != APR_SUCCESS) {
* - open the RewriteMap prg:xxx programs
*/
for (; s; s = s->next) {
+#ifndef REWRITELOG_DISABLED
if (!open_rewritelog(s, p)) {
return HTTP_INTERNAL_SERVER_ERROR;
}
+#endif
if (!first_time) {
if (run_rewritemap_programs(s, p) != APR_SUCCESS) {
}
}
+ rewrite_ssl_lookup = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
+ rewrite_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
+
return OK;
}
static void init_child(apr_pool_t *p, server_rec *s)
{
- apr_status_t rv;
+ apr_status_t rv = 0; /* get a rid of gcc warning (REWRITELOG_DISABLED) */
if (lockname != NULL && *(lockname) != '\0') {
rv = apr_global_mutex_child_init(&rewrite_mapr_lock_acquire,
}
}
+#ifndef REWRITELOG_DISABLED
rv = apr_global_mutex_child_init(&rewrite_log_lock, NULL, p);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
"mod_rewrite: could not init rewrite log lock in child");
}
+#endif
/* create the lookup cache */
if (!init_cache(p)) {
thisurl = apr_table_get(r->subprocess_env, ENVVAR_SCRIPT_URL);
/* set the variable */
- var = apr_pstrcat(r->pool, ap_http_method(r), "://", thisserver, thisport,
+ var = apr_pstrcat(r->pool, ap_http_scheme(r), "://", thisserver, thisport,
thisurl, NULL);
apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URI, var);
*/
if (r->filename == NULL) {
r->filename = apr_pstrdup(r->pool, r->uri);
- rewritelog(r, 2, "init rewrite engine with requested uri %s",
- r->filename);
+ rewritelog((r, 2, NULL, "init rewrite engine with requested uri %s",
+ r->filename));
}
else {
- rewritelog(r, 2, "init rewrite engine with passed filename %s."
- " Original uri = %s", r->filename, r->uri);
+ rewritelog((r, 2, NULL, "init rewrite engine with passed filename "
+ "%s. Original uri = %s", r->filename, r->uri));
}
/*
apr_psprintf(r->pool,"%d",rulestatus));
}
else {
- rewritelog(r, 2,
- "uri already rewritten. Status %s, Uri %s, r->filename %s",
- saved_rulestatus, r->uri, r->filename);
+ rewritelog((r, 2, NULL, "uri already rewritten. Status %s, Uri %s, "
+ "r->filename %s", saved_rulestatus, r->uri, r->filename));
+
rulestatus = atoi(saved_rulestatus);
}
if (rulestatus) {
unsigned skip;
- apr_size_t flen = strlen(r->filename);
+ apr_size_t flen;
+
+ if (ACTION_STATUS == rulestatus) {
+ int n = r->status;
+
+ r->status = HTTP_OK;
+ return n;
+ }
+ flen = r->filename ? strlen(r->filename) : 0;
if (flen > 6 && strncmp(r->filename, "proxy:", 6) == 0) {
/* it should be go on as an internal proxy request */
}
/* now make sure the request gets handled by the proxy handler */
- r->proxyreq = PROXYREQ_REVERSE;
+ if (PROXYREQ_NONE == r->proxyreq) {
+ r->proxyreq = PROXYREQ_REVERSE;
+ }
r->handler = "proxy-server";
- rewritelog(r, 1, "go-ahead with proxy request %s [OK]",
- r->filename);
+ rewritelog((r, 1, NULL, "go-ahead with proxy request %s [OK]",
+ r->filename));
return OK;
}
else if ((skip = is_absolute_uri(r->filename)) > 0) {
/* it was finally rewritten to a remote URL */
if (rulestatus != ACTION_NOESCAPE) {
- rewritelog(r, 1, "escaping %s for redirect", r->filename);
+ rewritelog((r, 1, NULL, "escaping %s for redirect",
+ r->filename));
r->filename = escape_absolute_uri(r->pool, r->filename, skip);
}
/* now do the redirection */
apr_table_setn(r->headers_out, "Location", r->filename);
- rewritelog(r, 1, "redirect to %s [REDIRECT/%d]", r->filename, n);
+ rewritelog((r, 1, NULL, "redirect to %s [REDIRECT/%d]", r->filename,
+ n));
+
return n;
}
- else if (flen > 10 && strncmp(r->filename, "forbidden:", 10) == 0) {
- /* This URLs is forced to be forbidden for the requester */
- return HTTP_FORBIDDEN;
- }
- else if (flen > 5 && strncmp(r->filename, "gone:", 5) == 0) {
- /* This URLs is forced to be gone */
- return HTTP_GONE;
- }
else if (flen > 12 && strncmp(r->filename, "passthrough:", 12) == 0) {
/*
* Hack because of underpowered API: passing the current
#if APR_HAS_USER
r->filename = expand_tildepaths(r, r->filename);
#endif
- rewritelog(r, 2, "local path result: %s", r->filename);
+ rewritelog((r, 2, NULL, "local path result: %s", r->filename));
/* the filename must be either an absolute local path or an
* absolute local URL.
r->uri = tmp;
if (res != OK) {
- rewritelog(r, 1, "prefixing with document_root of %s "
- "FAILED", r->filename);
+ rewritelog((r, 1, NULL, "prefixing with document_root of %s"
+ " FAILED", r->filename));
return res;
}
- rewritelog(r, 2, "prefixed with document_root to %s",
- r->filename);
+ rewritelog((r, 2, NULL, "prefixed with document_root to %s",
+ r->filename));
}
- rewritelog(r, 1, "go-ahead with %s [OK]", r->filename);
+ rewritelog((r, 1, NULL, "go-ahead with %s [OK]", r->filename));
return OK;
}
}
else {
- rewritelog(r, 1, "pass through %s", r->filename);
+ rewritelog((r, 1, NULL, "pass through %s", r->filename));
return DECLINED;
}
}
int rulestatus;
int n;
char *ofilename;
+ int is_proxyreq;
dconf = (rewrite_perdir_conf *)ap_get_module_config(r->per_dir_config,
&rewrite_module);
return DECLINED;
}
- /* we shouldn't do anything in subrequests */
- if (r->main != NULL) {
- return DECLINED;
- }
-
/* if there are no real (i.e. no RewriteRule directives!)
per-dir config of us, we return also immediately */
if (dconf->directory == NULL) {
return DECLINED;
}
+ /*
+ * Proxy request?
+ */
+ is_proxyreq = ( r->proxyreq && r->filename
+ && !strncmp(r->filename, "proxy:", 6));
+
/*
* .htaccess file is called before really entering the directory, i.e.:
* URL: http://localhost/foo and .htaccess is located in foo directory
* Ignore such attempts, since they may lead to undefined behaviour.
*/
- l = strlen(dconf->directory) - 1;
- if (r->filename && strlen(r->filename) == l &&
- (dconf->directory)[l] == '/' &&
- !strncmp(r->filename, dconf->directory, l)) {
- return DECLINED;
+ if (!is_proxyreq) {
+ l = strlen(dconf->directory) - 1;
+ if (r->filename && strlen(r->filename) == l &&
+ (dconf->directory)[l] == '/' &&
+ !strncmp(r->filename, dconf->directory, l)) {
+ return DECLINED;
+ }
}
/*
* remember the current filename before rewriting for later check
* to prevent deadlooping because of internal redirects
* on final URL/filename which can be equal to the inital one.
+ * also, we'll restore original r->filename if we decline this
+ * request
*/
ofilename = r->filename;
+ if (r->filename == NULL) {
+ r->filename = apr_pstrdup(r->pool, r->uri);
+ rewritelog((r, 2, "init rewrite engine with requested uri %s",
+ r->filename));
+ }
+
/*
* now apply the rules ...
*/
rulestatus = apply_rewrite_list(r, dconf->rewriterules, dconf->directory);
if (rulestatus) {
unsigned skip;
- l = strlen(r->filename);
+ if (ACTION_STATUS == rulestatus) {
+ int n = r->status;
+
+ r->status = HTTP_OK;
+ return n;
+ }
+
+ l = strlen(r->filename);
if (l > 6 && strncmp(r->filename, "proxy:", 6) == 0) {
/* it should go on as an internal proxy request */
}
/* now make sure the request gets handled by the proxy handler */
- r->proxyreq = PROXYREQ_REVERSE;
+ if (PROXYREQ_NONE == r->proxyreq) {
+ r->proxyreq = PROXYREQ_REVERSE;
+ }
r->handler = "proxy-server";
- rewritelog(r, 1, "[per-dir %s] go-ahead with proxy request "
- "%s [OK]", dconf->directory, r->filename);
+ rewritelog((r, 1, dconf->directory, "go-ahead with proxy request "
+ "%s [OK]", r->filename));
return OK;
}
else if ((skip = is_absolute_uri(r->filename)) > 0) {
cp = r->filename + skip;
if ((cp = ap_strchr(cp, '/')) != NULL && *(++cp)) {
- rewritelog(r, 2,
- "[per-dir %s] trying to replace "
- "prefix %s with %s",
- dconf->directory, dconf->directory,
- dconf->baseurl);
+ rewritelog((r, 2, dconf->directory,
+ "trying to replace prefix %s with %s",
+ dconf->directory, dconf->baseurl));
/* I think, that hack needs an explanation:
* well, here is it:
/* now prepare the redirect... */
if (rulestatus != ACTION_NOESCAPE) {
- rewritelog(r, 1, "[per-dir %s] escaping %s for redirect",
- dconf->directory, r->filename);
+ rewritelog((r, 1, dconf->directory, "escaping %s for redirect",
+ r->filename));
r->filename = escape_absolute_uri(r->pool, r->filename, skip);
}
/* now do the redirection */
apr_table_setn(r->headers_out, "Location", r->filename);
- rewritelog(r, 1, "[per-dir %s] redirect to %s [REDIRECT/%d]",
- dconf->directory, r->filename, n);
+ rewritelog((r, 1, dconf->directory, "redirect to %s [REDIRECT/%d]",
+ r->filename, n));
return n;
}
- else if (l > 10 && strncmp(r->filename, "forbidden:", 10) == 0) {
- /* This URL is forced to be forbidden for the requester */
- return HTTP_FORBIDDEN;
- }
- else if (l > 5 && strncmp(r->filename, "gone:", 5) == 0) {
- /* This URL is forced to be gone */
- return HTTP_GONE;
- }
else {
/* it was finally rewritten to a local path */
* use the following internal redirection stuff because
* this would lead to a deadloop.
*/
- if (strcmp(r->filename, ofilename) == 0) {
- rewritelog(r, 1, "[per-dir %s] initial URL equal rewritten "
- "URL: %s [IGNORING REWRITE]",
- dconf->directory, r->filename);
+ if (ofilename != NULL && strcmp(r->filename, ofilename) == 0) {
+ rewritelog((r, 1, dconf->directory, "initial URL equal rewritten"
+ " URL: %s [IGNORING REWRITE]", r->filename));
return OK;
}
* plain URL
*/
if (dconf->baseurl != NULL) {
- rewritelog(r, 2,
- "[per-dir %s] trying to replace prefix %s with %s",
- dconf->directory, dconf->directory, dconf->baseurl);
+ rewritelog((r, 2, dconf->directory, "trying to replace prefix "
+ "%s with %s", dconf->directory, dconf->baseurl));
+
r->filename = subst_prefix_path(r, r->filename,
dconf->directory,
dconf->baseurl);
}
if (!strncmp(r->filename, ccp, l) &&
r->filename[l] == '/') {
- rewritelog(r, 2,
- "[per-dir %s] strip document_root "
- "prefix: %s -> %s",
- dconf->directory, r->filename,
- r->filename+l);
+ rewritelog((r, 2,dconf->directory, "strip document_root"
+ " prefix: %s -> %s", r->filename,
+ r->filename+l));
+
r->filename = apr_pstrdup(r->pool, r->filename+l);
}
}
}
/* now initiate the internal redirect */
- rewritelog(r, 1, "[per-dir %s] internal redirect with %s "
- "[INTERNAL REDIRECT]", dconf->directory, r->filename);
+ rewritelog((r, 1, dconf->directory, "internal redirect with %s "
+ "[INTERNAL REDIRECT]", r->filename));
r->filename = apr_pstrcat(r->pool, "redirect:", r->filename, NULL);
r->handler = "redirect-handler";
return OK;
}
}
else {
- rewritelog(r, 1, "[per-dir %s] pass through %s",
- dconf->directory, r->filename);
+ rewritelog((r, 1, dconf->directory, "pass through %s", r->filename));
+ r->filename = ofilename;
return DECLINED;
}
}
/*
* MIME-type hook
- * [T=...] in server-context
+ * [T=...,H=...] execution
*/
static int hook_mimetype(request_rec *r)
{
const char *t;
- /* now check if we have to force a MIME-type */
+ /* type */
t = apr_table_get(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR);
- if (t == NULL) {
- return DECLINED;
- }
- else {
- rewritelog(r, 1, "force filename %s to have MIME-type '%s'",
- r->filename, t);
- ap_set_content_type(r, t);
- return OK;
- }
-}
-
-/* check whether redirect limit is reached */
-static int is_redirect_limit_exceeded(request_rec *r)
-{
- request_rec *top = r;
- rewrite_request_conf *reqc;
- rewrite_perdir_conf *dconf;
-
- /* we store it in the top request */
- while (top->main) {
- top = top->main;
- }
- while (top->prev) {
- top = top->prev;
- }
-
- /* fetch our config */
- reqc = (rewrite_request_conf *) ap_get_module_config(top->request_config,
- &rewrite_module);
+ if (t && *t) {
+ rewritelog((r, 1, NULL, "force filename %s to have MIME-type '%s'",
+ r->filename, t));
- /* no config there? create one. */
- if (!reqc) {
- rewrite_server_conf *sconf;
-
- reqc = apr_palloc(top->pool, sizeof(rewrite_request_conf));
- sconf = ap_get_module_config(r->server->module_config, &rewrite_module);
-
- reqc->redirects = 0;
- reqc->redirect_limit = sconf->redirect_limit
- ? sconf->redirect_limit
- : REWRITE_REDIRECT_LIMIT;
-
- /* associate it with this request */
- ap_set_module_config(top->request_config, &rewrite_module, reqc);
+ ap_set_content_type(r, t);
}
- /* allow to change the limit during redirects. */
- dconf = (rewrite_perdir_conf *)ap_get_module_config(r->per_dir_config,
- &rewrite_module);
+ /* handler */
+ t = apr_table_get(r->notes, REWRITE_FORCED_HANDLER_NOTEVAR);
+ if (t && *t) {
+ rewritelog((r, 1, NULL, "force filename %s to have the "
+ "Content-handler '%s'", r->filename, t));
- /* 0 == unset; take server conf ... */
- if (dconf->redirect_limit) {
- reqc->redirect_limit = dconf->redirect_limit;
+ r->handler = t;
}
- ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
- "mod_rewrite's internal redirect status: %d/%d.",
- reqc->redirects, reqc->redirect_limit);
-
- /* and now give the caller a hint */
- return (reqc->redirects++ >= reqc->redirect_limit);
+ return OK;
}
+
/*
* "content" handler for internal redirects
*/
return DECLINED;
}
- if (is_redirect_limit_exceeded(r)) {
- ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
- "mod_rewrite: maximum number of internal redirects "
- "reached. Assuming configuration error. Use "
- "'RewriteOptions MaxRedirects' to increase the limit "
- "if neccessary.");
- return HTTP_INTERNAL_SERVER_ERROR;
- }
-
/* now do the internal redirect */
ap_internal_redirect(apr_pstrcat(r->pool, r->filename+9,
r->args ? "?" : NULL, r->args, NULL), r);
* +-------------------------------------------------------+
*/
+#ifdef REWRITELOG_DISABLED
+static const char *fake_rewritelog(cmd_parms *cmd, void *dummy, const char *a1)
+{
+ return "RewriteLog and RewriteLogLevel are not supported by this build "
+ "of mod_rewrite because it was compiled using the "
+ "-DREWRITELOG_DISABLED compiler option. You have to recompile "
+ "mod_rewrite WITHOUT this option in order to use the rewrite log.";
+}
+#endif
+
static const command_rec command_table[] = {
AP_INIT_FLAG( "RewriteEngine", cmd_rewriteengine, NULL, OR_FILEINFO,
"On or Off to enable or disable (default) the whole "
AP_INIT_TAKE1( "RewriteLock", cmd_rewritelock, NULL, RSRC_CONF,
"the filename of a lockfile used for inter-process "
"synchronization"),
+#ifndef REWRITELOG_DISABLED
AP_INIT_TAKE1( "RewriteLog", cmd_rewritelog, NULL, RSRC_CONF,
"the filename of the rewriting logfile"),
AP_INIT_TAKE1( "RewriteLogLevel", cmd_rewriteloglevel, NULL, RSRC_CONF,
"the level of the rewriting logfile verbosity "
"(0=none, 1=std, .., 9=max)"),
+#else
+ AP_INIT_TAKE1( "RewriteLog", fake_rewritelog, NULL, RSRC_CONF,
+ "[DISABLED] the filename of the rewriting logfile"),
+ AP_INIT_TAKE1( "RewriteLogLevel", fake_rewritelog, NULL, RSRC_CONF,
+ "[DISABLED] the level of the rewriting logfile verbosity"),
+#endif
{ NULL }
};
*/
static const char * const aszPre[]={ "mod_proxy.c", NULL };
- /* check type before mod_mime, so that [T=foo/bar] will not be
- * overridden by AddType definitions.
- */
- static const char * const ct_aszSucc[]={ "mod_mime.c", NULL };
-
APR_REGISTER_OPTIONAL_FN(ap_register_rewrite_mapfunc);
ap_hook_handler(handler_redirect, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_child_init(init_child, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_fixups(hook_fixup, aszPre, NULL, APR_HOOK_FIRST);
+ ap_hook_fixups(hook_mimetype, NULL, NULL, APR_HOOK_LAST);
ap_hook_translate_name(hook_uri2file, NULL, NULL, APR_HOOK_FIRST);
- ap_hook_type_checker(hook_mimetype, NULL, ct_aszSucc, APR_HOOK_MIDDLE);
}
/* the main config structure */