]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] tools: add a "word_match()" function to match words and ignore spaces
authorWilly Tarreau <w@1wt.eu>
Mon, 18 Jan 2010 14:05:57 +0000 (15:05 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 18 Jan 2010 18:51:39 +0000 (19:51 +0100)
Some header values might be delimited with spaces, so it's not enough to
compare "close" or "keep-alive" with strncasecmp(). Use word_match() for
that.

include/common/standard.h
src/standard.c

index c1f3d1a733a9bbb674eb9ceebaa4809af98c1a04..b19f0ccd9f0ae497585bda4311007da48cb98171 100644 (file)
@@ -2,7 +2,7 @@
  * include/common/standard.h
  * This files contains some general purpose functions and macros.
  *
- * Copyright (C) 2000-2009 Willy Tarreau - w@1wt.eu
+ * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -363,4 +363,11 @@ char *my_strndup(const char *src, int n);
  */
 unsigned int get_next_id(struct eb_root *root, unsigned int key);
 
+/* This function compares a sample word possibly followed by blanks to another
+ * clean word. The compare is case-insensitive. 1 is returned if both are equal,
+ * otherwise zero. This intends to be used when checking HTTP headers for some
+ * values.
+ */
+int word_match(const char *sample, int slen, const char *word, int wlen);
+
 #endif /* _COMMON_STANDARD_H */
index 81cdda407b827b855f9f2312ce4fa27937647e25..f841c6dcda09d59725a65d0ebc043928716a2620 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * General purpose functions.
  *
- * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
+ * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -813,6 +813,35 @@ unsigned int get_next_id(struct eb_root *root, unsigned int key)
        return key;
 }
 
+/* This function compares a sample word possibly followed by blanks to another
+ * clean word. The compare is case-insensitive. 1 is returned if both are equal,
+ * otherwise zero. This intends to be used when checking HTTP headers for some
+ * values. Note that it validates a word followed only by blanks but does not
+ * validate a word followed by blanks then other chars.
+ */
+int word_match(const char *sample, int slen, const char *word, int wlen)
+{
+       if (slen < wlen)
+               return 0;
+
+       while (wlen) {
+               char c = *sample ^ *word;
+               if (c && c != ('A' ^ 'a'))
+                       return 0;
+               sample++;
+               word++;
+               slen--;
+               wlen--;
+       }
+
+       while (slen) {
+               if (*sample != ' ' && *sample != '\t')
+                       return 0;
+               sample++;
+               slen--;
+       }
+       return 1;
+}
 
 /*
  * Local variables: