]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] redirect: in prefix mode a "/" means not to change the URI
authorWilly Tarreau <w@1wt.eu>
Wed, 19 Nov 2008 20:15:17 +0000 (21:15 +0100)
committerWilly Tarreau <w@1wt.eu>
Sun, 7 Dec 2008 22:48:39 +0000 (23:48 +0100)
If the prefix is set to "/", it means the user does not want to alter
the original URI, so we don't want to insert a new slash before the
original URI.

(cherry-picked from commit 02a35c74942c1bce762e996698add1270e6a5030)

doc/configuration.txt
src/proto_http.c
tests/test-redirect.cfg

index ca6f293ef03793de106ba9f03b7f4c3ca55f30e3..f92176a5e7400be7c0e73c1e42188de5235ebac5 100644 (file)
@@ -2350,7 +2350,10 @@ redirect prefix   <to> [code <code>] <option> {if | unless} <condition>
               the HTTP "Location" header. In case of "redirect prefix", the
               "Location" header is built from the concatenation of <to> and the
               complete URI, including the query string, unless the "drop-query"
-              option is specified (see below).
+              option is specified (see below). As a special case, if <to>
+              equals exactly "/" in prefix mode, then nothing is inserted
+              before the original URI. It allows one to redirect to the same
+              URL.
 
     <code>    The code is optional. It indicates which type of HTTP redirection
               is desired. Only codes 301, 302 and 303 are supported, and 302 is
index 2b72504c483a54d72b0ec5a53294d9500ac9da3e..ecbc8872df84be533baefd9ce6993c926735be30 100644 (file)
@@ -1912,9 +1912,14 @@ int http_process_request(struct session *s, struct buffer *req)
                                        if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
                                                goto return_bad_req;
 
-                                       /* add prefix */
-                                       memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
-                                       rdr.len += rule->rdr_len;
+                                       /* add prefix. Note that if prefix == "/", we don't want to
+                                        * add anything, otherwise it makes it hard for the user to
+                                        * configure a self-redirection.
+                                        */
+                                       if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
+                                               memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
+                                               rdr.len += rule->rdr_len;
+                                       }
 
                                        /* add path */
                                        memcpy(rdr.str + rdr.len, path, pathlen);
index 780132b0aa3f4282b11a39ef8b884faa4daf77ea..582a06967f5a2e0d869972e57ca242e5eaff0e44 100644 (file)
@@ -26,8 +26,8 @@ listen  sample1
        redirect   prefix   /pfx/test code 302 if url_test2
        redirect   prefix   /pfx/test code 303 drop-query if url_test3
 
-       redirect   location /test4 code 302 set-cookie   SEEN=1 if url_test4 !seen
-       redirect   location /      code 302 clear-cookie SEEN=  if url_test4 seen
+       redirect   prefix   /  code 302 set-cookie   SEEN=1 if url_test4 !seen
+       redirect   location /  code 302 clear-cookie SEEN=  if url_test4 seen
 
        ### unconditional redirection
        #redirect   location https://example.com/ if TRUE