]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: http: Add standalone functions to parse a start-line or a header
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 22 Oct 2018 13:12:04 +0000 (15:12 +0200)
committerWilly Tarreau <w@1wt.eu>
Sun, 18 Nov 2018 20:45:49 +0000 (21:45 +0100)
These 2 functions are pretty naive. They only split a start-line into its 3
substrings or a header line into its name and value. Spaces before and after
each part are skipped. No CRLF at the end are expected.

include/common/http.h
src/http.c

index 3b1620df94d2caa6ddb0e38cae98b19a30ae4e97..0835d078d7e6357a786e42ccb94a4ef056cd4153 100644 (file)
@@ -153,6 +153,9 @@ int http_find_next_url_param(const char **chunks,
                              const char* url_param_name, size_t url_param_name_l,
                              const char **vstart, const char **vend, char delim);
 
+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);
+
 /*
  * 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 30d349a4b16dada539c92d83db48433fd0269677..8e5f4c87c77c3270161f0941d2b271b40f987552 100644 (file)
@@ -906,6 +906,72 @@ int http_find_next_url_param(const char **chunks,
        return 1;
 }
 
+/* Parses a single header line (without the CRLF) and splits it into its name
+ * and its value. The parsing is pretty naive and just skip spaces.
+ */
+int http_parse_header(const struct ist hdr, struct ist *name, struct ist *value)
+{
+        char *p   = hdr.ptr;
+        char *end = p + hdr.len;
+
+        name->len = value->len = 0;
+
+        /* Skip leading spaces */
+        for (; p < end && HTTP_IS_SPHT(*p); p++);
+
+        /* Set the header name */
+        name->ptr = p;
+        for (; p < end && HTTP_IS_TOKEN(*p); p++);
+        name->len = p - name->ptr;
+
+        /* Skip the ':' and spaces before and after it */
+        for (; p < end && HTTP_IS_SPHT(*p); p++);
+        if (p < end && *p == ':') p++;
+        for (; p < end && HTTP_IS_SPHT(*p); p++);
+
+        /* Set the header value */
+        value->ptr = p;
+        value->len = end - p;
+
+        return 1;
+}
+
+/* Parses a single start line (without the CRLF) and splits it into 3 parts. The
+ * parsing is pretty naive and just skip spaces.
+ */
+int http_parse_stline(const struct ist line, struct ist *p1, struct ist *p2, struct ist *p3)
+{
+        char *p   = line.ptr;
+        char *end = p + line.len;
+
+        p1->len = p2->len = p3->len = 0;
+
+        /* Skip leading spaces */
+        for (; p < end && HTTP_IS_SPHT(*p); p++);
+
+        /* Set the first part */
+        p1->ptr = p;
+        for (; p < end && HTTP_IS_TOKEN(*p); p++);
+        p1->len = p - p1->ptr;
+
+        /* Skip spaces between p1 and p2 */
+        for (; p < end && HTTP_IS_SPHT(*p); p++);
+
+        /* Set the second part */
+        p2->ptr = p;
+        for (; p < end && !HTTP_IS_SPHT(*p); p++);
+        p2->len = p - p2->ptr;
+
+        /* Skip spaces between p2 and p3 */
+        for (; p < end && HTTP_IS_SPHT(*p); p++);
+
+        /* The remaing is the third value */
+        p3->ptr = p;
+        p3->len = end - p;
+
+        return 1;
+}
+
 
 /* post-initializes the HTTP parts. Returns non-zero on error, with <err>
  * pointing to the error message.