[vlc-commits] http: add resource-level Basic authentication

Rémi Denis-Courmont git at videolan.org
Tue Aug 30 20:54:41 CEST 2016


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Aug 30 20:16:38 2016 +0300| [bfb087f8d846a9fbef92bdf08f4a662e16ec6500] | committer: Rémi Denis-Courmont

http: add resource-level Basic authentication

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

 modules/access/http/resource.c | 43 +++++++++++++++++++++++++++++++++++++++++-
 modules/access/http/resource.h |  5 +++++
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/modules/access/http/resource.c b/modules/access/http/resource.c
index 8688d23..c8363fb 100644
--- a/modules/access/http/resource.c
+++ b/modules/access/http/resource.c
@@ -58,7 +58,8 @@ vlc_http_res_req(const struct vlc_http_resource *res, void *opaque)
     }
 
     /* Authentication */
-    /* TODO: authentication */
+    if (res->username != NULL)
+        vlc_http_msg_add_creds_basic(req, false, res->username, res->password);
 
     /* Request context */
     if (res->agent != NULL)
@@ -148,6 +149,8 @@ static void vlc_http_res_deinit(struct vlc_http_resource *res)
 {
     free(res->referrer);
     free(res->agent);
+    free(res->password);
+    free(res->username);
     free(res->path);
     free(res->authority);
     free(res->host);
@@ -207,6 +210,10 @@ int vlc_http_res_init(struct vlc_http_resource *restrict res,
     res->host = strdup(url.psz_host);
     res->port = url.i_port;
     res->authority = vlc_http_authority(url.psz_host, url.i_port);
+    res->username = (url.psz_username != NULL) ? strdup(url.psz_username)
+                                               : NULL;
+    res->password = (url.psz_password != NULL) ? strdup(url.psz_password)
+                                               : NULL;
     res->agent = (ua != NULL) ? strdup(ua) : NULL;
     res->referrer = (ref != NULL) ? strdup(ref) : NULL;
 
@@ -315,3 +322,37 @@ struct block_t *vlc_http_res_read(struct vlc_http_resource *res)
 
     return vlc_http_msg_read(res->response);
 }
+
+int vlc_http_res_set_login(struct vlc_http_resource *res,
+                           const char *username, const char *password)
+{
+    char *user = NULL;
+    char *pass = NULL;
+
+    if (username != NULL)
+    {
+        user = strdup(username);
+        if (unlikely(user == NULL))
+            return -1;
+
+        pass = strdup((password != NULL) ? password : "");
+        if (unlikely(pass == NULL))
+        {
+            free(user);
+            return -1;
+        }
+    }
+
+    free(res->password);
+    free(res->username);
+    res->username = user;
+    res->password = pass;
+
+    if (res->response != NULL && vlc_http_msg_get_status(res->response) == 401)
+    {
+        vlc_http_msg_destroy(res->response);
+        res->response = NULL;
+    }
+
+    return 0;
+}
diff --git a/modules/access/http/resource.h b/modules/access/http/resource.h
index 5f42dd4..1a5b21e 100644
--- a/modules/access/http/resource.h
+++ b/modules/access/http/resource.h
@@ -52,6 +52,8 @@ struct vlc_http_resource
     unsigned port;
     char *authority;
     char *path;
+    char *username;
+    char *password;
     char *agent;
     char *referrer;
 };
@@ -94,5 +96,8 @@ char *vlc_http_res_get_type(struct vlc_http_resource *);
  */
 struct block_t *vlc_http_res_read(struct vlc_http_resource *);
 
+int vlc_http_res_set_login(struct vlc_http_resource *res,
+                           const char *username, const char *password);
+
 /** @} */
 #endif



More information about the vlc-commits mailing list