]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
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)
committerWilly Tarreau <w@1wt.eu>
Tue, 17 Aug 2021 08:16:22 +0000 (10:16 +0200)
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.

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

index 150ebdb7f9eefcac51d9a2d5f429c1b8c2ae358a..a213f5bfd223ffef2f00f09719d1839e67a9973f 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_parse_scheme(struct http_uri_parser *parser);
 struct ist http_parse_authority(struct http_uri_parser *parser, int no_userinfo);
 struct ist http_parse_path(struct http_uri_parser *parser);
index 75c899d774210626c1b3246e6dddbe2639f26453..0d6e92d911cb3f9fc89792e5442f7928e2c1278b 100644 (file)
@@ -468,6 +468,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 scheme. If not found, an empty ist is
  * returned. Otherwise, the ist pointing to the scheme is returned.
  *