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

https: add helpers for token parsing

parent 3a660dc6
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
#include <time.h> #include <time.h>
#include <vlc_common.h> #include <vlc_common.h>
#include <vlc_strings.h>
#include "message.h" #include "message.h"
#include "h2frame.h" #include "h2frame.h"
...@@ -53,7 +52,7 @@ static ssize_t vlc_http_msg_find_header(const struct vlc_http_msg *m, ...@@ -53,7 +52,7 @@ static ssize_t vlc_http_msg_find_header(const struct vlc_http_msg *m,
const char *name) const char *name)
{ {
for (unsigned i = 0; i < m->count; i++) for (unsigned i = 0; i < m->count; i++)
if (!vlc_ascii_strcasecmp(m->headers[i][0], name)) if (!strcasecmp(m->headers[i][0], name))
return i; return i;
return -1; return -1;
} }
...@@ -79,10 +78,10 @@ static int vlc_http_msg_vadd_header(struct vlc_http_msg *m, const char *name, ...@@ -79,10 +78,10 @@ static int vlc_http_msg_vadd_header(struct vlc_http_msg *m, const char *name,
/* Fold identically named header field values. This is unfortunately not /* Fold identically named header field values. This is unfortunately not
* possible for Set-Cookie, while Cookie requires a special separator. */ * possible for Set-Cookie, while Cookie requires a special separator. */
ssize_t idx = vlc_http_msg_find_header(m, name); ssize_t idx = vlc_http_msg_find_header(m, name);
if (idx >= 0 && vlc_ascii_strcasecmp(name, "Set-Cookie")) if (idx >= 0 && strcasecmp(name, "Set-Cookie"))
{ {
char *merged; char *merged;
char sep = vlc_ascii_strcasecmp(name, "Cookie") ? ',' : ';'; char sep = strcasecmp(name, "Cookie") ? ',' : ';';
int val = asprintf(&merged, "%s%c %s", m->headers[idx][1], sep, value); int val = asprintf(&merged, "%s%c %s", m->headers[idx][1], sep, value);
...@@ -549,6 +548,41 @@ static bool vlc_http_is_token(const char *str) ...@@ -549,6 +548,41 @@ static bool vlc_http_is_token(const char *str)
return len > 0 && str[len] == '\0'; return len > 0 && str[len] == '\0';
} }
const char *vlc_http_first_token(const char *value)
{
return value + strspn(value, "\t ");
}
const char *vlc_http_next_token(const char *value)
{
value = strchr(value, ',');
if (value == NULL)
return NULL;
return value + strspn(value, "\t ,");
}
const char *vlc_http_msg_get_token(const struct vlc_http_msg *msg,
const char *field, const char *token)
{
const char *value = vlc_http_msg_get_header(msg, field);
if (value == NULL)
return NULL;
const size_t length = strlen(token);
for (value = vlc_http_first_token(value);
value != NULL;
value = vlc_http_next_token(value))
{
if (vlc_http_token_length(value) == length
&& !strncasecmp(token, value, length))
return value;
}
return NULL;
}
static size_t vlc_http_comment_length(const char *str) static size_t vlc_http_comment_length(const char *str)
{ /* IETF RFC7230 §3.2.6 */ { /* IETF RFC7230 §3.2.6 */
if (*str != '(') if (*str != '(')
......
...@@ -146,6 +146,32 @@ const char *vlc_http_msg_get_scheme(const struct vlc_http_msg *m); ...@@ -146,6 +146,32 @@ const char *vlc_http_msg_get_scheme(const struct vlc_http_msg *m);
const char *vlc_http_msg_get_authority(const struct vlc_http_msg *m); const char *vlc_http_msg_get_authority(const struct vlc_http_msg *m);
const char *vlc_http_msg_get_path(const struct vlc_http_msg *m); const char *vlc_http_msg_get_path(const struct vlc_http_msg *m);
/**
* Looks up a token in a header field.
*
* Finds the first occurence of a token within a HTTP field header.
*
* @param field HTTP header field name
* @param token HTTP token name
* @return the first byte of the token if found, NULL if not found.
*/
const char *vlc_http_msg_get_token(const struct vlc_http_msg *,
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 the following token in a HTTP field value.
*/
const char *vlc_http_next_token(const char *);
/** /**
* Gets HTTP payload length. * Gets HTTP payload length.
* *
......
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