]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: http: Add function to get port part of a host
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 5 Jul 2022 07:48:39 +0000 (09:48 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 6 Jul 2022 15:54:03 +0000 (17:54 +0200)
http_get_host_port() function can be used to get the port part of a host. It
will be used to get the port of an uri authority or a host header
value. This function only look for a port starting from the end of the
host. It is the caller responsibility to call it with a valid host value. An
indirect string is returned.

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

index a213f5bfd223ffef2f00f09719d1839e67a9973f..aba8e65a3bcc61d9ba171c5c5730f3ae0fbfa821 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);
+struct ist http_get_host_port(const struct ist host);
 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);
index bc0a8085f284f72b9b001de935e06ca59e7b2c4e..7a7fb0cbb983cc50952e52bd795cf9ba593c010c 100644 (file)
@@ -478,6 +478,24 @@ const char *http_get_reason(unsigned int status)
        }
 }
 
+/* Returns the ist string corresponding to port part (without ':') in the host
+ * <host> or IST_NULL if not found.
+*/
+struct ist http_get_host_port(const struct ist host)
+{
+       char *start, *end, *ptr;
+
+       start = istptr(host);
+       end = istend(host);
+       for (ptr = end; ptr > start && isdigit((unsigned char)*--ptr););
+
+       /* no port found */
+       if (likely(*ptr != ':' || ptr+1 == end || ptr == start))
+               return IST_NULL;
+
+       return istnext(ist2(ptr, end - ptr));
+}
+
 /* 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 "://").