From 8277ca72b152f36d8561c60dd8e7ea2d5bd2c2fc Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Mon, 22 Oct 2018 15:12:04 +0200 Subject: [PATCH] MINOR: http: Add standalone functions to parse a start-line or a header 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 | 3 ++ src/http.c | 66 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/include/common/http.h b/include/common/http.h index 3b1620df9..0835d078d 100644 --- a/include/common/http.h +++ b/include/common/http.h @@ -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. diff --git a/src/http.c b/src/http.c index 30d349a4b..8e5f4c87c 100644 --- a/src/http.c +++ b/src/http.c @@ -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 * pointing to the error message.