]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] stick-tables: add support for "stick on hdr"
authorWilly Tarreau <w@1wt.eu>
Wed, 12 May 2010 06:08:50 +0000 (08:08 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 13 May 2010 20:10:02 +0000 (22:10 +0200)
It is now possible to stick on an IP address found in a HTTP header. Right
now only the last occurrence of the header can be used, which is generally
enough for most uses. Also, the header extraction rule only knows how to
convert the header to IP. Later it will be usable as a plain string with
an implicit conversion, and the syntax will not change.

doc/configuration.txt
src/proto_http.c

index 93914a57031c4569899d5cc63cf81f8fb42832f2..b8636acc8fa6af6ef942ba7b98a391f3145823e3 100644 (file)
@@ -6555,6 +6555,11 @@ The list of currently supported pattern fetch functions is the following :
                ports to some clients for a whole application session. It is of
                type integer and only works with such tables.
 
+  hdr(name)    This extracts the last occurrence of header <name> in an HTTP
+               request and converts it to an IP address. This IP address is
+               then used to match the table. A typical use is with the
+               x-forwarded-for header.
+
 
 The currently available list of transformations include :
 
index 1f134385f15b9756594951763c16a1c9a2c94440..0209391b178be9b93fbd53800a4bdcb0133f986e 100644 (file)
@@ -49,6 +49,7 @@
 #include <proto/fd.h>
 #include <proto/log.h>
 #include <proto/hdr_idx.h>
+#include <proto/pattern.h>
 #include <proto/proto_tcp.h>
 #include <proto/proto_http.h>
 #include <proto/proxy.h>
@@ -7429,11 +7430,40 @@ static struct acl_kw_list acl_kws = {{ },{
        { NULL, NULL, NULL, NULL },
 }};
 
+/************************************************************************/
+/*     The code below is dedicated to pattern fetching and matching     */
+/************************************************************************/
+
+/* extract the IP address from the last occurrence of specified header. Note
+ * that we should normally first extract the string then convert it to IP,
+ * but right now we have all the functions to do this seemlessly, and we will
+ * be able to change that later without touching the configuration.
+ */
+static int
+pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
+                  const char *arg, int arg_len, union pattern_data *data)
+{
+       struct http_txn *txn = l7;
+
+       data->ip.s_addr = htonl(get_ip_from_hdr2(&txn->req, arg, arg_len, &txn->hdr_idx, -1));
+       return data->ip.s_addr != 0;
+}
+
+/************************************************************************/
+/*             All supported keywords must be declared here.            */
+/************************************************************************/
+/* Note: must not be declared <const> as its list will be overwritten */
+static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
+       { "hdr",       pattern_fetch_hdr_ip,   PATTERN_TYPE_IP,   PATTERN_FETCH_REQ },
+       { NULL, NULL, 0, 0 },
+}};
+
 
 __attribute__((constructor))
 static void __http_protocol_init(void)
 {
        acl_register_keywords(&acl_kws);
+       pattern_register_fetches(&pattern_fetch_keywords);
 }