[vlc-commits] https: add helpers for token parsing

Rémi Denis-Courmont git at videolan.org
Sun Dec 20 14:25:37 CET 2015


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Dec 20 15:19:43 2015 +0200| [03b4ec183972b50f196173acab597efe1d471f8a] | committer: Rémi Denis-Courmont

https: add helpers for token parsing

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=03b4ec183972b50f196173acab597efe1d471f8a
---

 modules/access/http/message.c |   42 +++++++++++++++++++++++++++++++++++++----
 modules/access/http/message.h |   26 +++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 4 deletions(-)

diff --git a/modules/access/http/message.c b/modules/access/http/message.c
index 8405d9a..f7137de 100644
--- a/modules/access/http/message.c
+++ b/modules/access/http/message.c
@@ -31,7 +31,6 @@
 #include <time.h>
 
 #include <vlc_common.h>
-#include <vlc_strings.h>
 #include "message.h"
 #include "h2frame.h"
 
@@ -53,7 +52,7 @@ static ssize_t vlc_http_msg_find_header(const struct vlc_http_msg *m,
                                         const char *name)
 {
     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 -1;
 }
@@ -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
      * possible for Set-Cookie, while Cookie requires a special separator. */
     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 sep = vlc_ascii_strcasecmp(name, "Cookie") ? ',' : ';';
+        char sep = strcasecmp(name, "Cookie") ? ',' : ';';
 
         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)
     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)
 {   /* IETF RFC7230 §3.2.6 */
     if (*str != '(')
diff --git a/modules/access/http/message.h b/modules/access/http/message.h
index 1444b2b..7e57177 100644
--- a/modules/access/http/message.h
+++ b/modules/access/http/message.h
@@ -147,6 +147,32 @@ 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);
 
 /**
+ * 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.
  *
  * @return byte length, or (uintmax_t)-1 if unknown.



More information about the vlc-commits mailing list