]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: http: Add etag comparison function
authorRemi Tricot-Le Breton <rlebreton@haproxy.com>
Thu, 22 Oct 2020 08:40:03 +0000 (10:40 +0200)
committerWilliam Lallemand <wlallemand@haproxy.org>
Thu, 22 Oct 2020 14:06:20 +0000 (16:06 +0200)
Add a function that compares two etags that might be of different types.
If any of them is weak, the 'W/' prefix is discarded and a strict string
comparison is performed.

Co-authored-by: Tim Duesterhus <tim@bastelstu.be>
include/haproxy/http.h
src/http.c

index a84f6db45592beb8fb7ed3af3e2d80d3c30c4332..fb47abdd454d28479872b2d296b47dcaddde6f35 100644 (file)
@@ -57,6 +57,8 @@ int http_parse_header(const struct ist hdr, struct ist *name, struct ist *value)
 int http_parse_stline(const struct ist line, struct ist *p1, struct ist *p2, struct ist *p3);
 int http_parse_status_val(const struct ist value, struct ist *status, struct ist *reason);
 
+int http_compare_etags(struct ist etag1, struct ist etag2);
+
 /*
  * Given a path string and its length, find the position of beginning of the
  * query string. Returns NULL if no query string is found in the path.
index 4b1b9cda4e66943e8a5e94c799a6e8f667d66fa0..bb99c50989823ba20eeb6715489ae4908d7a17d1 100644 (file)
@@ -1047,3 +1047,29 @@ int http_parse_status_val(const struct ist value, struct ist *status, struct ist
        code = strl2ui(status->ptr, status->len);
        return code;
 }
+
+
+/* Returns non-zero if the two ETags are comparable (see RFC 7232#2.3.2).
+ * If any of them is a weak ETag, we discard the weakness prefix and perform
+ * a strict string comparison.
+ * Returns 0 otherwise.
+ */
+int http_compare_etags(struct ist etag1, struct ist etag2)
+{
+       enum http_etag_type etag_type1;
+       enum http_etag_type etag_type2;
+
+       etag_type1 = http_get_etag_type(etag1);
+       etag_type2 = http_get_etag_type(etag2);
+
+       if (etag_type1 == ETAG_INVALID || etag_type2 == ETAG_INVALID)
+               return 0;
+
+       /* Discard the 'W/' prefix an ETag is a weak one. */
+       if (etag_type1 == ETAG_WEAK)
+               etag1 = istadv(etag1, 2);
+       if (etag_type2 == ETAG_WEAK)
+               etag2 = istadv(etag2, 2);
+
+       return isteq(etag1, etag2);
+}