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.
This commit is contained in:
parent
72d9125efb
commit
8277ca72b1
|
@ -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.
|
||||
|
|
66
src/http.c
66
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 <err>
|
||||
* pointing to the error message.
|
||||
|
|
Loading…
Reference in New Issue