Commit a9f3ddf4 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

https: improve header field list iteration

parent eeab18ae
...@@ -552,23 +552,46 @@ static size_t vlc_http_token_length(const char *str) ...@@ -552,23 +552,46 @@ static size_t vlc_http_token_length(const char *str)
return i; return i;
} }
static bool vlc_http_is_token(const char *str) static size_t vlc_http_quoted_length(const char *str)
{ {
size_t len = vlc_http_token_length(str); size_t i = 0;
return len > 0 && str[len] == '\0'; unsigned char c;
if (str[i++] != '"')
return 0;
do
{
c = str[i++];
if (c == '\0')
return 0;
if (c == '\\') /* Quoted pair */
{
unsigned char q = str[i++];
if (q < 32 && q != '\t')
return 0;
}
}
while (c != '"');
return i;
} }
const char *vlc_http_first_token(const char *value) static bool vlc_http_is_token(const char *str)
{ {
return value + strspn(value, "\t "); size_t len = vlc_http_token_length(str);
return len > 0 && str[len] == '\0';
} }
const char *vlc_http_next_token(const char *value) const char *vlc_http_next_token(const char *value)
{ { /* We handle either token or token = token / quoted-string */
value = strchr(value, ','); value += strcspn(value, ",\"");
if (value == NULL) if (!*value)
return NULL; return NULL;
value += vlc_http_quoted_length(value);
return value + strspn(value, "\t ,"); return value + strspn(value, "\t ,");
} }
...@@ -576,18 +599,15 @@ const char *vlc_http_msg_get_token(const struct vlc_http_msg *msg, ...@@ -576,18 +599,15 @@ const char *vlc_http_msg_get_token(const struct vlc_http_msg *msg,
const char *field, const char *token) const char *field, const char *token)
{ {
const char *value = vlc_http_msg_get_header(msg, field); const char *value = vlc_http_msg_get_header(msg, field);
if (value == NULL)
return NULL;
const size_t length = strlen(token); const size_t length = strlen(token);
for (value = vlc_http_first_token(value); while (value != NULL)
value != NULL;
value = vlc_http_next_token(value))
{ {
if (vlc_http_token_length(value) == length if (vlc_http_token_length(value) == length
&& !strncasecmp(token, value, length)) && !strncasecmp(token, value, length))
return value; return value;
value = vlc_http_next_token(value);
} }
return NULL; return NULL;
......
...@@ -165,13 +165,6 @@ const char *vlc_http_msg_get_path(const struct vlc_http_msg *m); ...@@ -165,13 +165,6 @@ const char *vlc_http_msg_get_path(const struct vlc_http_msg *m);
const char *vlc_http_msg_get_token(const struct vlc_http_msg *, const char *vlc_http_msg_get_token(const struct vlc_http_msg *,
const char *field, const char *token); const char *field, const char *token);
/**
* Finds first token.
*
* Finds the first token in a HTTP field value.
*/
const char *vlc_http_first_token(const char *);
/** /**
* Finds next token. * Finds next token.
* *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment