MINOR: http: add a new function http_validate_scheme() to validate a scheme
authorWilly Tarreau <w@1wt.eu>
Tue, 10 Aug 2021 13:35:36 +0000 (15:35 +0200)
committerSalvatore Bonaccorso <carnil@debian.org>
Sat, 23 Dec 2023 10:02:19 +0000 (11:02 +0100)
While http_parse_scheme() extracts a scheme from a URI by extracting
exactly the valid characters and stopping on delimiters, this new
function performs the same on a fixed-size string.

(cherry picked from commit adfc08e717db600c3ac44ca8f3178d861699b67c)
[wt: context adj]
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit 073e9c9c10897a05117f29cb9d3ebdbc13ff03b5)
[wt: context adj]
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit 0fb53c3c025fb158c51c515532f3f52bb2abcdea)
Signed-off-by: Willy Tarreau <w@1wt.eu>
Gbp-Pq: Name 2.2-0001-MINOR-http-add-a-new-function-http_validate_scheme-t.patch

include/haproxy/http.h
src/http.c

index 1bd9da9adbc9551e1f2503c89ef35d85ffe5da40..00a64c7b7d3624414eda7b40129b8c2a539e059b 100644 (file)
@@ -36,6 +36,7 @@ extern const uint8_t http_char_classes[256];
 enum http_meth_t find_http_meth(const char *str, const int len);
 int http_get_status_idx(unsigned int status);
 const char *http_get_reason(unsigned int status);
+int http_validate_scheme(const struct ist schm);
 struct ist http_get_authority(const struct ist uri, int no_userinfo);
 struct ist http_get_path(const struct ist uri);
 int http_header_match2(const char *hdr, const char *end,
index 3d88aa274404e71d6f620837157d96a7ea5fde72..12a72accef2f35454c8741502196497d2bb7bfb2 100644 (file)
@@ -458,6 +458,29 @@ const char *http_get_reason(unsigned int status)
        }
 }
 
+/* Returns non-zero if the scheme <schm> is syntactically correct according to
+ * RFC3986#3.1, otherwise zero. It expects only the scheme and nothing else
+ * (particularly not the following "://").
+ *     Scheme = alpha *(alpha|digit|'+'|'-'|'.')
+ */
+int http_validate_scheme(const struct ist schm)
+{
+       size_t i;
+
+       for (i = 0; i < schm.len; i++) {
+               if (likely((schm.ptr[i] >= 'a' && schm.ptr[i] <= 'z') ||
+                          (schm.ptr[i] >= 'A' && schm.ptr[i] <= 'Z')))
+                       continue;
+               if (unlikely(!i)) // first char must be alpha
+                       return 0;
+               if ((schm.ptr[i] >= '0' && schm.ptr[i] <= '9') ||
+                   schm.ptr[i] == '+' || schm.ptr[i] == '-' || schm.ptr[i] == '.')
+                       continue;
+               return 0;
+       }
+       return !!i;
+}
+
 /* Parse the uri and looks for the authority, between the scheme and the
  * path. if no_userinfo is not zero, the part before the '@' (including it) is
  * skipped. If not found, an empty ist is returned. Otherwise, the ist pointing