9a7cbf
diff --git a/docs/manual/mod/mod_rewrite.html.en b/docs/manual/mod/mod_rewrite.html.en
9a7cbf
index 815ec72..2b8ed35 100644
9a7cbf
--- a/docs/manual/mod/mod_rewrite.html.en
9a7cbf
+++ b/docs/manual/mod/mod_rewrite.html.en
9a7cbf
@@ -1265,7 +1265,17 @@ cannot use $N in the substitution string!
9a7cbf
         B
9a7cbf
         Escape non-alphanumeric characters in backreferences before
9a7cbf
         applying the transformation. details ...
9a7cbf
-    
9a7cbf
+     
9a7cbf
+
9a7cbf
+        BCTLS
9a7cbf
+        Like [B], but only escape control characters and spaces.
9a7cbf
+        details ...
9a7cbf
+
9a7cbf
+    
9a7cbf
+        BNE
9a7cbf
+        Characters of [B] or [BCTLS] which should not be escaped.
9a7cbf
+        details ...
9a7cbf
+     
9a7cbf
 
9a7cbf
         backrefnoplus|BNP
9a7cbf
         If backreferences are being escaped, spaces should be escaped to
6a034d
diff --git a/docs/manual/rewrite/flags.html.en b/docs/manual/rewrite/flags.html.en
9a7cbf
index 80d0759..21edfe7 100644
6a034d
--- a/docs/manual/rewrite/flags.html.en
6a034d
+++ b/docs/manual/rewrite/flags.html.en
6a034d
@@ -85,10 +85,6 @@ of how you might use them.

6a034d
 

B (escape backreferences)

6a034d
 

The [B] flag instructs RewriteRule to escape non-alphanumeric

6a034d
 characters before applying the transformation.

6a034d
-

In 2.4.26 and later, you can limit the escaping to specific characters

6a034d
-in backreferences by listing them: [B=#?;]. Note: The space
6a034d
-character can be used in the list of characters to escape, but it cannot be
6a034d
-the last character in the list.

6a034d
 
6a034d
 

mod_rewrite has to unescape URLs before mapping them,

6a034d
 so backreferences are unescaped at the time they are applied.
9a7cbf
@@ -120,6 +116,20 @@ when the backend may break if presented with an unescaped URL.

6a034d
 
6a034d
 

An alternative to this flag is using a RewriteCond to capture against %{THE_REQUEST} which will capture

6a034d
 strings in the encoded form.

6a034d
+
6a034d
+

In 2.4.26 and later, you can limit the escaping to specific characters

6a034d
+in backreferences by listing them: [B=#?;]. Note: The space
6a034d
+character can be used in the list of characters to escape, but you must quote
6a034d
+the entire third argument of RewriteRule
6a034d
+and the space must not be the last character in the list.

6a034d
+
9a7cbf
+
# Escape spaces and question marks.  The quotes around the final argument
9a7cbf
+# are required when a space is included.
6a034d
+RewriteRule "^search/(.*)$" "/search.php?term=$1" "[B= ?]"
6a034d
+
9a7cbf
+

To limit the characters escaped this way, see #flag_bne

9a7cbf
+and #flag_bctls

9a7cbf
+
6a034d
 
top
6a034d
 
6a034d
 

BNP|backrefnoplus (don't escape space to +)

9a7cbf
@@ -127,8 +137,40 @@ strings in the encoded form.

9a7cbf
 in a backreference to %20 rather than '+'. Useful when the backreference
9a7cbf
 will be used in the path component rather than the query string.

9a7cbf
 
9a7cbf
+
9a7cbf
+# Escape spaces to %20 in the path instead of + as used in form submission via
9a7cbf
+# the query string
9a7cbf
+RewriteRule "^search/(.*)$" "/search.php/$1" "[B,BNP]"
9a7cbf
+
9a7cbf
+
9a7cbf
 

This flag is available in version 2.4.26 and later.

9a7cbf
 
9a7cbf
+
top
9a7cbf
+
9a7cbf
+

BCTLS

9a7cbf
+

The [BCTLS] flag is similar to the [B] flag, but only escapes

9a7cbf
+control characters and the space character. This is the same set of
9a7cbf
+characters rejected when they are copied into the query string unencoded.
9a7cbf
+

9a7cbf
+
9a7cbf
+
9a7cbf
+# Escape control characters and spaces
9a7cbf
+RewriteRule "^search/(.*)$" "/search.php/$1" "[BCTLS]"
9a7cbf
+
9a7cbf
+
9a7cbf
+
top
9a7cbf
+
9a7cbf
+

BNE

9a7cbf
+

The list of characters in [BNE=...] are treated as exclusions to the

9a7cbf
+characters of the [B] or [BCTLS] flags. The listed characters will not be
9a7cbf
+escaped.
9a7cbf
+

9a7cbf
+
9a7cbf
+
9a7cbf
+# Escape the default characters, but leave /
9a7cbf
+RewriteRule "^search/(.*)$" "/search.php?term=$1" "[B,BNE=/]"
9a7cbf
+
9a7cbf
+
9a7cbf
 
top
9a7cbf
 
9a7cbf
 

C|chain

9a7cbf
@@ -204,7 +246,7 @@ browsers that support this feature.
9a7cbf
 

Consider this example:

9a7cbf
 
9a7cbf
 
RewriteEngine On
9a7cbf
-RewriteRule "^/index\.html" "-" [CO=frontdoor:yes:.example.com:1440:/]
9a7cbf
+RewriteRule  "^/index\.html"  "-" [CO=frontdoor:yes:.example.com:1440:/]
9a7cbf
 
9a7cbf
 
9a7cbf
 

In the example give, the rule doesn't rewrite the request.

9a7cbf
@@ -410,8 +452,8 @@ argument to index.php, however, the 
9a7cbf
 is already for index.php, the RewriteRule will be skipped.

9a7cbf
 
9a7cbf
 
RewriteBase "/"
9a7cbf
-RewriteCond "%{REQUEST_URI}" "!=/index.php"
9a7cbf
-RewriteRule "^(.*)" "/index.php?req=$1" [L,PT]
9a7cbf
+RewriteCond "%{REQUEST_URI}" !=/index.php
9a7cbf
+RewriteRule "^(.*)"          "/index.php?req=$1" [L,PT]
9a7cbf
 
9a7cbf
 
top
9a7cbf
 
9a7cbf
@@ -434,11 +476,11 @@ pattern still matches (i.e., while the URI still contains an
9a7cbf
 A), perform this substitution (i.e., replace the
9a7cbf
 A with a B).

9a7cbf
 
9a7cbf
-

In 2.4.8 and later, this module returns an error after 32,000 iterations to

9a7cbf
+

In 2.4.8 and later, this module returns an error after 10,000 iterations to

9a7cbf
 protect against unintended looping.  An alternative maximum number of 
9a7cbf
 iterations can be specified by adding to the N flag.  

9a7cbf
 
# Be willing to replace 1 character in each pass of the loop
9a7cbf
-RewriteRule "(.+)[><;]$" "$1" [N=64000]
9a7cbf
+RewriteRule "(.+)[><;]$" "$1" [N=32000]
9a7cbf
 # ... or, give up if after 10 loops
9a7cbf
 RewriteRule "(.+)[><;]$" "$1" [N=10]
9a7cbf
 
9a7cbf
@@ -681,19 +723,21 @@ URI in request' warnings.
9a7cbf
 

The [S] flag is used to skip rules that you don't want to run. The

9a7cbf
 syntax of the skip flag is [S=N], where N signifies
9a7cbf
 the number of rules to skip (provided the 
9a7cbf
-RewriteRule matches). This can be thought of as a goto
9a7cbf
-statement in your rewrite ruleset. In the following example, we only want
9a7cbf
-to run the RewriteRule if the
9a7cbf
-requested URI doesn't correspond with an actual file.

9a7cbf
+RewriteRule and any preceding  
9a7cbf
+RewriteCond directives match). This can be thought of as a
9a7cbf
+goto statement in your rewrite ruleset. In the following
9a7cbf
+example, we only want to run the  
9a7cbf
+RewriteRule if the requested URI doesn't correspond with an
9a7cbf
+actual file.

9a7cbf
 
9a7cbf
 
# Is the request for a non-existent file?
9a7cbf
-RewriteCond "%{REQUEST_FILENAME}" "!-f"
9a7cbf
-RewriteCond "%{REQUEST_FILENAME}" "!-d"
9a7cbf
+RewriteCond "%{REQUEST_FILENAME}" !-f
9a7cbf
+RewriteCond "%{REQUEST_FILENAME}" !-d
9a7cbf
 # If so, skip these two RewriteRules
9a7cbf
-RewriteRule ".?" "-" [S=2]
9a7cbf
+RewriteRule ".?"                  "-" [S=2]
9a7cbf
 
9a7cbf
-RewriteRule "(.*\.gif)" "images.php?$1"
9a7cbf
-RewriteRule "(.*\.html)" "docs.php?$1"
9a7cbf
+RewriteRule "(.*\.gif)"           "images.php?$1"
9a7cbf
+RewriteRule "(.*\.html)"          "docs.php?$1"
9a7cbf
 
9a7cbf
 
9a7cbf
 

This technique is useful because a RewriteCond only applies to the

9a7cbf
@@ -705,18 +749,18 @@ use this to make pseudo if-then-else constructs: The last rule of
9a7cbf
 the then-clause becomes skip=N, where N is the
9a7cbf
 number of rules in the else-clause:

9a7cbf
 
# Does the file exist?
9a7cbf
-RewriteCond "%{REQUEST_FILENAME}" "!-f"
9a7cbf
-RewriteCond "%{REQUEST_FILENAME}" "!-d"
9a7cbf
+RewriteCond "%{REQUEST_FILENAME}" !-f
9a7cbf
+RewriteCond "%{REQUEST_FILENAME}" !-d
9a7cbf
 # Create an if-then-else construct by skipping 3 lines if we meant to go to the "else" stanza.
9a7cbf
-RewriteRule ".?" "-" [S=3]
9a7cbf
+RewriteRule ".?"                  "-" [S=3]
9a7cbf
 
9a7cbf
 # IF the file exists, then:
9a7cbf
-    RewriteRule "(.*\.gif)" "images.php?$1"
9a7cbf
+    RewriteRule "(.*\.gif)"  "images.php?$1"
9a7cbf
     RewriteRule "(.*\.html)" "docs.php?$1"
9a7cbf
     # Skip past the "else" stanza.
9a7cbf
-    RewriteRule ".?" "-" [S=1]
9a7cbf
+    RewriteRule ".?"         "-" [S=1]
9a7cbf
 # ELSE...
9a7cbf
-    RewriteRule "(.*)" "404.php?file=$1"
9a7cbf
+    RewriteRule "(.*)"       "404.php?file=$1"
9a7cbf
 # END
9a7cbf
 
9a7cbf
 
9a7cbf
@@ -733,7 +777,7 @@ sent. This has the same effect as the 
9a7cbf
 source code as plain text, if requested in a particular way:

9a7cbf
 
9a7cbf
 
# Serve .pl files as plain text
9a7cbf
-RewriteRule "\.pl$" "-" [T=text/plain]
9a7cbf
+RewriteRule "\.pl$"  "-" [T=text/plain]
9a7cbf
 
9a7cbf
 
9a7cbf
 

Or, perhaps, if you have a camera that produces jpeg images without

9a7cbf
@@ -741,7 +785,7 @@ file extensions, you could force those images to be served with the
9a7cbf
 correct MIME type by virtue of their file names:

9a7cbf
 
9a7cbf
 
# Files with 'IMG' in the name are jpg images.
9a7cbf
-RewriteRule "IMG" "-" [T=image/jpg]
9a7cbf
+RewriteRule "IMG"  "-" [T=image/jpg]
9a7cbf
 
9a7cbf
 
9a7cbf
 

Please note that this is a trivial example, and could be better done

6a034d
diff --git a/modules/mappers/mod_rewrite.c b/modules/mappers/mod_rewrite.c
9a7cbf
index 38dbb24..b71c67c 100644
6a034d
--- a/modules/mappers/mod_rewrite.c
6a034d
+++ b/modules/mappers/mod_rewrite.c
9a7cbf
@@ -101,6 +101,8 @@
9a7cbf
 #include "mod_rewrite.h"
9a7cbf
 #include "ap_expr.h"
9a7cbf
 
9a7cbf
+#include "test_char.h"
9a7cbf
+
9a7cbf
 static ap_dbd_t *(*dbd_acquire)(request_rec*) = NULL;
9a7cbf
 static void (*dbd_prepare)(server_rec*, const char*, const char*) = NULL;
9a7cbf
 static const char* really_last_key = "rewrite_really_last";
9a7cbf
@@ -168,6 +170,8 @@ static const char* really_last_key = "rewrite_really_last";
6a034d
 #define RULEFLAG_END                (1<<17)
6a034d
 #define RULEFLAG_ESCAPENOPLUS       (1<<18)
6a034d
 #define RULEFLAG_QSLAST             (1<<19)
6a034d
+#define RULEFLAG_QSNONE             (1<<20) /* programattic only */
9a7cbf
+#define RULEFLAG_ESCAPECTLS         (1<<21)
6a034d
 
6a034d
 /* return code of the rewrite rule
6a034d
  * the result may be escaped - or not
9a7cbf
@@ -321,7 +325,8 @@ typedef struct {
9a7cbf
     data_item *cookie;               /* added cookies                         */
9a7cbf
     int        skip;                 /* number of next rules to skip          */
9a7cbf
     int        maxrounds;            /* limit on number of loops with N flag  */
9a7cbf
-    char       *escapes;             /* specific backref escapes              */
9a7cbf
+    const char *escapes;             /* specific backref escapes              */
9a7cbf
+    const char *noescapes;           /* specific backref chars not to escape  */
9a7cbf
 } rewriterule_entry;
9a7cbf
 
9a7cbf
 typedef struct {
9a7cbf
@@ -422,7 +427,9 @@ static const char *rewritemap_mutex_type = "rewrite-map";
9a7cbf
 /* Optional functions imported from mod_ssl when loaded: */
9a7cbf
 static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *rewrite_ssl_lookup = NULL;
9a7cbf
 static APR_OPTIONAL_FN_TYPE(ssl_is_https) *rewrite_is_https = NULL;
9a7cbf
-static char *escape_backref(apr_pool_t *p, const char *path, const char *escapeme, int noplus);
9a7cbf
+static char *escape_backref(apr_pool_t *p, const char *path,
9a7cbf
+                            const char *escapeme, const char *noescapeme,
9a7cbf
+                            int flags);
9a7cbf
 
9a7cbf
 /*
9a7cbf
  * +-------------------------------------------------------+
9a7cbf
@@ -645,18 +652,26 @@ static APR_INLINE unsigned char *c2x(unsigned what, unsigned char prefix,
9a7cbf
     return where;
9a7cbf
 }
9a7cbf
 
9a7cbf
+
9a7cbf
 /*
9a7cbf
  * Escapes a backreference in a similar way as php's urlencode does.
9a7cbf
  * Based on ap_os_escape_path in server/util.c
9a7cbf
  */
9a7cbf
-static char *escape_backref(apr_pool_t *p, const char *path, const char *escapeme, int noplus) {
9a7cbf
-    char *copy = apr_palloc(p, 3 * strlen(path) + 3);
9a7cbf
+static char *escape_backref(apr_pool_t *p, const char *path,
9a7cbf
+                            const char *escapeme, const char *noescapeme,
9a7cbf
+                            int flags)
9a7cbf
+{
9a7cbf
+    char *copy = apr_palloc(p, 3 * strlen(path) + 1);
9a7cbf
     const unsigned char *s = (const unsigned char *)path;
9a7cbf
     unsigned char *d = (unsigned char *)copy;
9a7cbf
-    unsigned c;
9a7cbf
+    int noplus = (flags & RULEFLAG_ESCAPENOPLUS) != 0;
9a7cbf
+    int ctls = (flags & RULEFLAG_ESCAPECTLS) != 0;
9a7cbf
+    unsigned char c;
9a7cbf
 
9a7cbf
     while ((c = *s)) {
9a7cbf
-        if (!escapeme) { 
9a7cbf
+        if (((ctls ? !TEST_CHAR(c, T_VCHAR_OBSTEXT) : !escapeme)
9a7cbf
+             || (escapeme && ap_strchr_c(escapeme, c)))
9a7cbf
+            && (!noescapeme || !ap_strchr_c(noescapeme, c))) {
9a7cbf
             if (apr_isalnum(c) || c == '_') {
9a7cbf
                 *d++ = c;
9a7cbf
             }
9a7cbf
@@ -667,23 +682,8 @@ static char *escape_backref(apr_pool_t *p, const char *path, const char *escapem
9a7cbf
                 d = c2x(c, '%', d);
9a7cbf
             }
9a7cbf
         }
9a7cbf
-        else { 
9a7cbf
-            const char *esc = escapeme;
9a7cbf
-            while (*esc) { 
9a7cbf
-                if (c == *esc) { 
9a7cbf
-                    if (c == ' ' && !noplus) { 
9a7cbf
-                        *d++ = '+';
9a7cbf
-                    }
9a7cbf
-                    else { 
9a7cbf
-                        d = c2x(c, '%', d);
9a7cbf
-                    }
9a7cbf
-                    break;
9a7cbf
-                }
9a7cbf
-                ++esc;
9a7cbf
-            }
9a7cbf
-            if (!*esc) { 
9a7cbf
-                *d++ = c;
9a7cbf
-            }
9a7cbf
+        else {
9a7cbf
+            *d++ = c;
9a7cbf
         }
9a7cbf
         ++s;
9a7cbf
     }
9a7cbf
@@ -761,15 +761,24 @@ static char *escape_absolute_uri(apr_pool_t *p, char *uri, unsigned scheme)
6a034d
                        ap_escape_uri(p, cp), NULL);
6a034d
 }
6a034d
 
6a034d
+
6a034d
 /*
6a034d
  * split out a QUERY_STRING part from
6a034d
  * the current URI string
6a034d
  */
6a034d
-static void splitout_queryargs(request_rec *r, int qsappend, int qsdiscard, 
6a034d
-                               int qslast)
6a034d
+static void splitout_queryargs(request_rec *r, int flags)
6a034d
 {
6a034d
     char *q;
6a034d
     int split;
6a034d
+    int qsappend = flags & RULEFLAG_QSAPPEND;
6a034d
+    int qsdiscard = flags & RULEFLAG_QSDISCARD;
6a034d
+    int qslast = flags & RULEFLAG_QSLAST;
6a034d
+
6a034d
+    if (flags & RULEFLAG_QSNONE) {
6a034d
+        rewritelog((r, 2, NULL, "discarding query string, no parse from substitution"));
6a034d
+        r->args = NULL;
6a034d
+        return;
6a034d
+    }
6a034d
 
6a034d
     /* don't touch, unless it's a scheme for which a query string makes sense.
6a034d
      * See RFC 1738 and RFC 2368.
9a7cbf
@@ -794,7 +803,7 @@ static void splitout_queryargs(request_rec *r, int qsappend, int qsdiscard,
6a034d
         olduri = apr_pstrdup(r->pool, r->filename);
6a034d
         *q++ = '\0';
6a034d
         if (qsappend) {
6a034d
-            if (*q) { 
6a034d
+            if (*q) {
6a034d
                 r->args = apr_pstrcat(r->pool, q, "&" , r->args, NULL);
6a034d
             }
6a034d
         }
9a7cbf
@@ -802,9 +811,9 @@ static void splitout_queryargs(request_rec *r, int qsappend, int qsdiscard,
6a034d
             r->args = apr_pstrdup(r->pool, q);
6a034d
         }
6a034d
 
6a034d
-        if (r->args) { 
6a034d
+        if (r->args) {
6a034d
            len = strlen(r->args);
6a034d
-      
6a034d
+
6a034d
            if (!len) {
6a034d
                r->args = NULL;
6a034d
            }
9a7cbf
@@ -2436,7 +2445,8 @@ static char *do_expand(char *input, rewrite_ctx *ctx, rewriterule_entry *entry)
9a7cbf
                     /* escape the backreference */
9a7cbf
                     char *tmp2, *tmp;
9a7cbf
                     tmp = apr_pstrmemdup(pool, bri->source + bri->regmatch[n].rm_so, span);
9a7cbf
-                    tmp2 = escape_backref(pool, tmp, entry->escapes, entry->flags & RULEFLAG_ESCAPENOPLUS);
9a7cbf
+                    tmp2 = escape_backref(pool, tmp, entry->escapes, entry->noescapes,
9a7cbf
+                                          entry->flags);
9a7cbf
                     rewritelog((ctx->r, 5, ctx->perdir, "escaping backreference '%s' to '%s'",
9a7cbf
                             tmp, tmp2));
9a7cbf
 
6a034d
@@ -2733,7 +2743,7 @@ static apr_status_t rewritelock_remove(void *data)
6a034d
  * XXX: what an inclined parser. Seems we have to leave it so
6a034d
  *      for backwards compat. *sigh*
6a034d
  */
6a034d
-static int parseargline(char *str, char **a1, char **a2, char **a3)
6a034d
+static int parseargline(char *str, char **a1, char **a2, char **a2_end, char **a3)
6a034d
 {
6a034d
     char quote;
6a034d
 
6a034d
@@ -2784,8 +2794,10 @@ static int parseargline(char *str, char **a1, char **a2, char **a3)
6a034d
 
6a034d
     if (!*str) {
6a034d
         *a3 = NULL; /* 3rd argument is optional */
6a034d
+        *a2_end = str;
6a034d
         return 0;
6a034d
     }
6a034d
+    *a2_end = str;
6a034d
     *str++ = '\0';
6a034d
 
6a034d
     while (apr_isspace(*str)) {
6a034d
@@ -3323,7 +3335,7 @@ static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
6a034d
     rewrite_server_conf *sconf;
6a034d
     rewritecond_entry *newcond;
6a034d
     ap_regex_t *regexp;
6a034d
-    char *a1 = NULL, *a2 = NULL, *a3 = NULL;
6a034d
+    char *a1 = NULL, *a2 = NULL, *a2_end, *a3 = NULL;
6a034d
     const char *err;
6a034d
 
6a034d
     sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
6a034d
@@ -3341,7 +3353,7 @@ static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
6a034d
      * of the argument line. So we can use a1 .. a3 without
6a034d
      * copying them again.
6a034d
      */
6a034d
-    if (parseargline(str, &a1, &a2, &a3)) {
6a034d
+    if (parseargline(str, &a1, &a2, &a2_end, &a3)) {
6a034d
         return apr_pstrcat(cmd->pool, "RewriteCond: bad argument line '", str,
6a034d
                            "'", NULL);
6a034d
     }
9a7cbf
@@ -3500,13 +3512,24 @@ static const char *cmd_rewriterule_setflag(apr_pool_t *p, void *_cfg,
9a7cbf
     case 'B':
9a7cbf
         if (!*key || !strcasecmp(key, "ackrefescaping")) {
9a7cbf
             cfg->flags |= RULEFLAG_ESCAPEBACKREF;
9a7cbf
-            if (val && *val) { 
9a7cbf
+            if (val && *val) {
9a7cbf
                 cfg->escapes = val;
9a7cbf
             }
9a7cbf
         }
9a7cbf
+        else if (!strcasecmp(key, "NE")) {
9a7cbf
+            if (val && *val) {
9a7cbf
+                cfg->noescapes = val;
9a7cbf
+            }
9a7cbf
+            else {
9a7cbf
+                return "flag 'BNE' wants a list of characters (i.e. [BNE=...])";
9a7cbf
+            }
9a7cbf
+        }
9a7cbf
         else if (!strcasecmp(key, "NP") || !strcasecmp(key, "ackrefernoplus")) { 
9a7cbf
             cfg->flags |= RULEFLAG_ESCAPENOPLUS;
9a7cbf
         }
9a7cbf
+        else if (!strcasecmp(key, "CTLS")) {
9a7cbf
+            cfg->flags |= RULEFLAG_ESCAPECTLS|RULEFLAG_ESCAPEBACKREF;
9a7cbf
+        }
9a7cbf
         else {
9a7cbf
             ++error;
9a7cbf
         }
9a7cbf
@@ -3749,7 +3772,7 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
6a034d
     rewrite_server_conf *sconf;
6a034d
     rewriterule_entry *newrule;
6a034d
     ap_regex_t *regexp;
6a034d
-    char *a1 = NULL, *a2 = NULL, *a3 = NULL;
6a034d
+    char *a1 = NULL, *a2 = NULL, *a2_end, *a3 = NULL;
6a034d
     const char *err;
6a034d
 
6a034d
     sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
9a7cbf
@@ -3763,12 +3786,11 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
6a034d
     }
6a034d
 
6a034d
     /*  parse the argument line ourself */
6a034d
-    if (parseargline(str, &a1, &a2, &a3)) {
6a034d
+    if (parseargline(str, &a1, &a2, &a2_end, &a3)) {
6a034d
         return apr_pstrcat(cmd->pool, "RewriteRule: bad argument line '", str,
6a034d
                            "'", NULL);
6a034d
     }
9a7cbf
 
9a7cbf
-    /* arg3: optional flags field */
9a7cbf
     newrule->forced_mimetype     = NULL;
9a7cbf
     newrule->forced_handler      = NULL;
9a7cbf
     newrule->forced_responsecode = HTTP_MOVED_TEMPORARILY;
9a7cbf
@@ -3777,6 +3799,9 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
9a7cbf
     newrule->cookie = NULL;
9a7cbf
     newrule->skip   = 0;
9a7cbf
     newrule->maxrounds = REWRITE_MAX_ROUNDS;
9a7cbf
+    newrule->escapes = newrule->noescapes = NULL;
9a7cbf
+
9a7cbf
+    /* arg3: optional flags field */
9a7cbf
     if (a3 != NULL) {
9a7cbf
         if ((err = cmd_parseflagfield(cmd->pool, newrule, a3,
9a7cbf
                                       cmd_rewriterule_setflag)) != NULL) {
9a7cbf
@@ -3810,6 +3835,17 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
6a034d
         newrule->flags |= RULEFLAG_NOSUB;
6a034d
     }
6a034d
 
6a034d
+    if (*(a2_end-1) == '?') {
6a034d
+        /* a literal ? at the end of the unsubstituted rewrite rule */
6a034d
+        newrule->flags |= RULEFLAG_QSNONE;
9a7cbf
+        *(a2_end-1) = '\0'; /* trailing ? has done its job */
6a034d
+    }
6a034d
+    else if (newrule->flags & RULEFLAG_QSDISCARD) {
6a034d
+        if (NULL == ap_strchr(newrule->output, '?')) {
6a034d
+            newrule->flags |= RULEFLAG_QSNONE;
6a034d
+        }
6a034d
+    }
6a034d
+
6a034d
     /* now, if the server or per-dir config holds an
6a034d
      * array of RewriteCond entries, we take it for us
6a034d
      * and clear the array
9a7cbf
@@ -4215,9 +4251,7 @@ static int apply_rewrite_rule(rewriterule_entry *p, rewrite_ctx *ctx)
6a034d
         r->path_info = NULL;
6a034d
     }
6a034d
 
6a034d
-    splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND, 
6a034d
-                          p->flags & RULEFLAG_QSDISCARD, 
6a034d
-                          p->flags & RULEFLAG_QSLAST);
6a034d
+    splitout_queryargs(r, p->flags);
6a034d
 
6a034d
     /* Add the previously stripped per-directory location prefix, unless
6a034d
      * (1) it's an absolute URL path and
9a7cbf
@@ -4696,8 +4730,25 @@ static int hook_uri2file(request_rec *r)
9a7cbf
     }
6a034d
 
9a7cbf
     if (rulestatus) {
9a7cbf
-        unsigned skip;
9a7cbf
-        apr_size_t flen;
9a7cbf
+        unsigned skip_absolute = is_absolute_uri(r->filename, NULL);
9a7cbf
+        apr_size_t flen =  r->filename ? strlen(r->filename) : 0;
9a7cbf
+        int to_proxyreq = (flen > 6 && strncmp(r->filename, "proxy:", 6) == 0);
9a7cbf
+        int will_escape = skip_absolute && (rulestatus != ACTION_NOESCAPE);
9a7cbf
+
9a7cbf
+        if (r->args
9a7cbf
+                && !will_escape
9a7cbf
+                && *(ap_scan_vchar_obstext(r->args))) {
6a034d
+            /*
6a034d
+             * We have a raw control character or a ' ' in r->args.
6a034d
+             * Correct encoding was missed.
9a7cbf
+             * Correct encoding was missed and we're not going to escape
9a7cbf
+             * it before returning.
6a034d
+             */
6a034d
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10410)
6a034d
+                          "Rewritten query string contains control "
6a034d
+                          "characters or spaces");
6a034d
+            return HTTP_FORBIDDEN;
6a034d
+        }
9a7cbf
 
6a034d
         if (ACTION_STATUS == rulestatus) {
6a034d
             int n = r->status;
9a7cbf
@@ -4706,8 +4757,7 @@ static int hook_uri2file(request_rec *r)
9a7cbf
             return n;
9a7cbf
         }
6a034d
 
9a7cbf
-        flen = r->filename ? strlen(r->filename) : 0;
9a7cbf
-        if (flen > 6 && strncmp(r->filename, "proxy:", 6) == 0) {
9a7cbf
+        if (to_proxyreq) {
9a7cbf
             /* it should be go on as an internal proxy request */
6a034d
 
9a7cbf
             /* check if the proxy module is enabled, so
9a7cbf
@@ -4749,7 +4799,7 @@ static int hook_uri2file(request_rec *r)
9a7cbf
                         r->filename));
9a7cbf
             return OK;
9a7cbf
         }
9a7cbf
-        else if ((skip = is_absolute_uri(r->filename, NULL)) > 0) {
9a7cbf
+        else if (skip_absolute > 0) {
9a7cbf
             int n;
9a7cbf
 
9a7cbf
             /* it was finally rewritten to a remote URL */
9a7cbf
@@ -4757,7 +4807,7 @@ static int hook_uri2file(request_rec *r)
9a7cbf
             if (rulestatus != ACTION_NOESCAPE) {
9a7cbf
                 rewritelog((r, 1, NULL, "escaping %s for redirect",
9a7cbf
                             r->filename));
9a7cbf
-                r->filename = escape_absolute_uri(r->pool, r->filename, skip);
9a7cbf
+                r->filename = escape_absolute_uri(r->pool, r->filename, skip_absolute);
9a7cbf
             }
9a7cbf
 
9a7cbf
             /* append the QUERY_STRING part */
9a7cbf
@@ -4981,7 +5031,26 @@ static int hook_fixup(request_rec *r)
9a7cbf
      */
9a7cbf
     rulestatus = apply_rewrite_list(r, dconf->rewriterules, dconf->directory);
9a7cbf
     if (rulestatus) {
9a7cbf
-        unsigned skip;
9a7cbf
+        unsigned skip_absolute = is_absolute_uri(r->filename, NULL);
9a7cbf
+        int to_proxyreq = 0;
9a7cbf
+        int will_escape = 0;
9a7cbf
+
9a7cbf
+        l = strlen(r->filename);
9a7cbf
+        to_proxyreq = l > 6 && strncmp(r->filename, "proxy:", 6) == 0;
9a7cbf
+        will_escape = skip_absolute && (rulestatus != ACTION_NOESCAPE);
9a7cbf
+
9a7cbf
+        if (r->args
9a7cbf
+               && !will_escape
9a7cbf
+               &&  *(ap_scan_vchar_obstext(r->args))) {
6a034d
+            /*
6a034d
+             * We have a raw control character or a ' ' in r->args.
6a034d
+             * Correct encoding was missed.
6a034d
+             */
6a034d
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10411)
6a034d
+                          "Rewritten query string contains control "
6a034d
+                          "characters or spaces");
6a034d
+            return HTTP_FORBIDDEN;
6a034d
+        }
9a7cbf
 
6a034d
         if (ACTION_STATUS == rulestatus) {
6a034d
             int n = r->status;
9a7cbf
@@ -4990,8 +5059,7 @@ static int hook_fixup(request_rec *r)
9a7cbf
             return n;
9a7cbf
         }
9a7cbf
 
9a7cbf
-        l = strlen(r->filename);
9a7cbf
-        if (l > 6 && strncmp(r->filename, "proxy:", 6) == 0) {
9a7cbf
+        if (to_proxyreq) {
9a7cbf
             /* it should go on as an internal proxy request */
9a7cbf
 
9a7cbf
             /* make sure the QUERY_STRING and
9a7cbf
@@ -5015,7 +5083,7 @@ static int hook_fixup(request_rec *r)
9a7cbf
                         "%s [OK]", r->filename));
9a7cbf
             return OK;
9a7cbf
         }
9a7cbf
-        else if ((skip = is_absolute_uri(r->filename, NULL)) > 0) {
9a7cbf
+        else if (skip_absolute > 0) {
9a7cbf
             /* it was finally rewritten to a remote URL */
6a034d
 
9a7cbf
             /* because we are in a per-dir context
9a7cbf
@@ -5024,7 +5092,7 @@ static int hook_fixup(request_rec *r)
9a7cbf
              */
9a7cbf
             if (dconf->baseurl != NULL) {
9a7cbf
                 /* skip 'scheme://' */
9a7cbf
-                cp = r->filename + skip;
9a7cbf
+                cp = r->filename + skip_absolute;
9a7cbf
 
9a7cbf
                 if ((cp = ap_strchr(cp, '/')) != NULL && *(++cp)) {
9a7cbf
                     rewritelog((r, 2, dconf->directory,
9a7cbf
@@ -5069,7 +5137,7 @@ static int hook_fixup(request_rec *r)
9a7cbf
             if (rulestatus != ACTION_NOESCAPE) {
9a7cbf
                 rewritelog((r, 1, dconf->directory, "escaping %s for redirect",
9a7cbf
                             r->filename));
9a7cbf
-                r->filename = escape_absolute_uri(r->pool, r->filename, skip);
9a7cbf
+                r->filename = escape_absolute_uri(r->pool, r->filename, skip_absolute);
9a7cbf
             }
9a7cbf
 
9a7cbf
             /* append the QUERY_STRING part */
6a034d
diff --git a/modules/proxy/mod_proxy_ajp.c b/modules/proxy/mod_proxy_ajp.c
9a7cbf
index 6faabea..59396a8 100644
6a034d
--- a/modules/proxy/mod_proxy_ajp.c
6a034d
+++ b/modules/proxy/mod_proxy_ajp.c
6a034d
@@ -69,6 +69,16 @@ static int proxy_ajp_canon(request_rec *r, char *url)
6a034d
         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
6a034d
                                  r->proxyreq);
6a034d
         search = r->args;
6a034d
+        if (search && *(ap_scan_vchar_obstext(search))) {
6a034d
+            /*
6a034d
+             * We have a raw control character or a ' ' in r->args.
6a034d
+             * Correct encoding was missed.
6a034d
+             */
6a034d
+             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10406)
6a034d
+                           "To be forwarded query string contains control "
6a034d
+                           "characters or spaces");
6a034d
+             return HTTP_FORBIDDEN;
6a034d
+        }
6a034d
     }
6a034d
     if (path == NULL)
6a034d
         return HTTP_BAD_REQUEST;
6a034d
diff --git a/modules/proxy/mod_proxy_balancer.c b/modules/proxy/mod_proxy_balancer.c
6a034d
index 3a28038..c599e1a 100644
6a034d
--- a/modules/proxy/mod_proxy_balancer.c
6a034d
+++ b/modules/proxy/mod_proxy_balancer.c
6a034d
@@ -106,6 +106,16 @@ static int proxy_balancer_canon(request_rec *r, char *url)
6a034d
         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
6a034d
                                  r->proxyreq);
6a034d
         search = r->args;
6a034d
+        if (search && *(ap_scan_vchar_obstext(search))) {
6a034d
+            /*
6a034d
+             * We have a raw control character or a ' ' in r->args.
6a034d
+             * Correct encoding was missed.
6a034d
+             */
6a034d
+             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10407)
6a034d
+                           "To be forwarded query string contains control "
6a034d
+                           "characters or spaces");
6a034d
+             return HTTP_FORBIDDEN;
6a034d
+        }
6a034d
     }
6a034d
     if (path == NULL)
6a034d
         return HTTP_BAD_REQUEST;
6a034d
diff --git a/modules/proxy/mod_proxy_http.c b/modules/proxy/mod_proxy_http.c
9a7cbf
index 7da9bde..2cdc61e 100644
6a034d
--- a/modules/proxy/mod_proxy_http.c
6a034d
+++ b/modules/proxy/mod_proxy_http.c
6a034d
@@ -90,6 +90,16 @@ static int proxy_http_canon(request_rec *r, char *url)
6a034d
             path = ap_proxy_canonenc(r->pool, url, strlen(url),
6a034d
                                      enc_path, 0, r->proxyreq);
6a034d
             search = r->args;
6a034d
+            if (search && *(ap_scan_vchar_obstext(search))) {
6a034d
+                /*
6a034d
+                 * We have a raw control character or a ' ' in r->args.
6a034d
+                 * Correct encoding was missed.
6a034d
+                 */
6a034d
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10408)
6a034d
+                              "To be forwarded query string contains control "
6a034d
+                              "characters or spaces");
6a034d
+                return HTTP_FORBIDDEN;
6a034d
+            }
6a034d
         }
6a034d
         break;
6a034d
     case PROXYREQ_PROXY:
6a034d
diff --git a/modules/proxy/mod_proxy_wstunnel.c b/modules/proxy/mod_proxy_wstunnel.c
6a034d
index e005a94..f5e27d9 100644
6a034d
--- a/modules/proxy/mod_proxy_wstunnel.c
6a034d
+++ b/modules/proxy/mod_proxy_wstunnel.c
6a034d
@@ -77,6 +77,16 @@ static int proxy_wstunnel_canon(request_rec *r, char *url)
6a034d
         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
6a034d
                                  r->proxyreq);
6a034d
         search = r->args;
6a034d
+        if (search && *(ap_scan_vchar_obstext(search))) {
6a034d
+            /*
6a034d
+             * We have a raw control character or a ' ' in r->args.
6a034d
+             * Correct encoding was missed.
6a034d
+             */
6a034d
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10409)
6a034d
+                          "To be forwarded query string contains control "
6a034d
+                          "characters or spaces");
6a034d
+            return HTTP_FORBIDDEN;
6a034d
+        }
6a034d
     }
6a034d
     if (path == NULL)
6a034d
         return HTTP_BAD_REQUEST;
9a7cbf
diff --git a/server/gen_test_char.c b/server/gen_test_char.c
9a7cbf
index 48ae6f4..6a153a3 100644
9a7cbf
--- a/server/gen_test_char.c
9a7cbf
+++ b/server/gen_test_char.c
9a7cbf
@@ -169,5 +169,15 @@ int main(int argc, char *argv[])
9a7cbf
 
9a7cbf
     printf("\n};\n");
9a7cbf
 
9a7cbf
+
9a7cbf
+    printf(
9a7cbf
+      "/* we assume the folks using this ensure 0 <= c < 256... which means\n"
9a7cbf
+      " * you need a cast to (unsigned char) first, you can't just plug a\n"
9a7cbf
+      " * char in here and get it to work, because if char is signed then it\n"
9a7cbf
+      " * will first be sign extended.\n"
9a7cbf
+      " */\n"
9a7cbf
+      "#define TEST_CHAR(c, f) (test_char_table[(unsigned char)(c)] & (f))\n"
9a7cbf
+    );
9a7cbf
+
9a7cbf
     return 0;
9a7cbf
 }
9a7cbf
diff --git a/server/util.c b/server/util.c
9a7cbf
index 2a5dd04..1d82fd8 100644
9a7cbf
--- a/server/util.c
9a7cbf
+++ b/server/util.c
9a7cbf
@@ -74,13 +74,6 @@
9a7cbf
  */
9a7cbf
 #include "test_char.h"
9a7cbf
 
9a7cbf
-/* we assume the folks using this ensure 0 <= c < 256... which means
9a7cbf
- * you need a cast to (unsigned char) first, you can't just plug a
9a7cbf
- * char in here and get it to work, because if char is signed then it
9a7cbf
- * will first be sign extended.
9a7cbf
- */
9a7cbf
-#define TEST_CHAR(c, f)        (test_char_table[(unsigned char)(c)] & (f))
9a7cbf
-
9a7cbf
 /* Win32/NetWare/OS2 need to check for both forward and back slashes
9a7cbf
  * in ap_getparents() and ap_escape_url.
9a7cbf
  */