[vlc-commits] http: helper for HTTP Basic authorization (refs #16166)

Rémi Denis-Courmont git at videolan.org
Sun Apr 3 17:26:57 CEST 2016


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Apr  3 18:26:00 2016 +0300| [2da34f3d59516cef1aa3d83dd4905f7837977fdf] | committer: Rémi Denis-Courmont

http: helper for HTTP Basic authorization (refs #16166)

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

 modules/access/http/message.c |   39 +++++++++++++++++++++++++++++++++++++++
 modules/access/http/message.h |   17 +++++++++++++++++
 2 files changed, 56 insertions(+)

diff --git a/modules/access/http/message.c b/modules/access/http/message.c
index 7bea981..b3dd48a 100644
--- a/modules/access/http/message.c
+++ b/modules/access/http/message.c
@@ -32,6 +32,7 @@
 
 #include <vlc_common.h>
 #include <vlc_http.h>
+#include <vlc_strings.h>
 #include "message.h"
 #include "h2frame.h"
 
@@ -901,3 +902,41 @@ int vlc_http_msg_add_cookies(struct vlc_http_msg *m,
     }
     return val;
 }
+
+int vlc_http_msg_add_creds_basic(struct vlc_http_msg *m, bool proxy,
+                                 const char *username, const char *password)
+{
+    char *str, *token;
+    int ret;
+    unsigned char c;
+
+    /* CTL characters and colons not permitted in username */
+    for (size_t len = 0; (c = username[len]) != '\0'; len++)
+        if (c < 32 || c == 127 || c == ':')
+        {
+            errno = EINVAL;
+            return -1;
+        }
+
+    /* CTL characters not permitted in password */
+    for (size_t len = 0; (c = password[len]) != '\0'; len++)
+        if (c < 32 || c == 127)
+        {
+            errno = EINVAL;
+            return -1;
+        }
+
+    ret = asprintf(&str, "%s:%s", username, password);
+    if (unlikely(ret < 0))
+        return -1;
+
+    token = vlc_b64_encode_binary((unsigned char *)str, ret);
+    free(str);
+    if (unlikely(token == NULL))
+        return -1;
+
+    ret = vlc_http_msg_add_header(m, proxy ? "Proxy-Authorization" :
+                                  "Authorization", "Basic %s", token);
+    free(token);
+    return ret;
+}
diff --git a/modules/access/http/message.h b/modules/access/http/message.h
index 10b7f11..302184e 100644
--- a/modules/access/http/message.h
+++ b/modules/access/http/message.h
@@ -149,6 +149,23 @@ int vlc_http_msg_add_cookies(struct vlc_http_msg *,
                              struct vlc_http_cookie_jar_t *);
 
 /**
+ * Adds Basic credentials.
+ *
+ * Formats a plain username and password pair using HTTP Basic (RFC7617)
+ * syntax.
+ *
+ * @param proxy true for proxy authentication,
+ *              false for origin server authentication
+ * @param username null-terminated username
+ * @param password null-terminated password
+ * @return 0 on success, -1 on out-of-memory (ENOMEM) or if username or
+ * password are invalid (EINVAL).
+ */
+int vlc_http_msg_add_creds_basic(struct vlc_http_msg *, bool proxy,
+                                 const char *username, const char *password);
+
+
+/**
  * Looks up an header field.
  *
  * Finds an HTTP header field by (case-insensitive) name inside an HTTP



More information about the vlc-commits mailing list