[vlc-commits] https: fetch/store cookies in the cookie jar

Rémi Denis-Courmont git at videolan.org
Sat Dec 19 15:59:10 CET 2015


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Dec 19 16:51:23 2015 +0200| [c7d4d5c7576a301a57fa3c90401c5099fa7bb686] | committer: Rémi Denis-Courmont

https: fetch/store cookies in the cookie jar

If present - this is currently only supported by the playlist.

Header field folding is not implemented.

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

 modules/access/http/access.c    |   10 +++++++---
 modules/access/http/connmgr.c   |   38 +++++++++++++++++++++++++++++++++++++-
 modules/access/http/connmgr.h   |   11 ++++++++++-
 modules/access/http/file.c      |    6 ++++++
 modules/access/http/file_test.c |   23 +++++++++++++++++++++++
 5 files changed, 83 insertions(+), 5 deletions(-)

diff --git a/modules/access/http/access.c b/modules/access/http/access.c
index b71be40..0252a44 100644
--- a/modules/access/http/access.c
+++ b/modules/access/http/access.c
@@ -113,8 +113,6 @@ static int Open(vlc_object_t *obj)
 
     if (var_InheritBool(obj, "http-continuous"))
         return VLC_EGENERIC; /* FIXME not implemented yet */
-    if (var_InheritBool(obj, "http-forward-cookies"))
-        return VLC_EGENERIC; /* FIXME not implemented yet */
 
     char *proxy = vlc_getProxyUrl(access->psz_url);
     free(proxy);
@@ -130,7 +128,13 @@ static int Open(vlc_object_t *obj)
     sys->manager = NULL;
     sys->file = NULL;
 
-    sys->manager = vlc_http_mgr_create(obj, var_InheritBool(obj, "http2"));
+    void *jar = NULL;
+    if (var_InheritBool(obj, "http-forward-cookies"))
+        jar = var_InheritAddress(obj, "http-cookies");
+
+    bool h2c = var_InheritBool(obj, "http2");
+
+    sys->manager = vlc_http_mgr_create(obj, jar, h2c);
     if (sys->manager == NULL)
         goto error;
 
diff --git a/modules/access/http/connmgr.c b/modules/access/http/connmgr.c
index f110057..ba64f04 100644
--- a/modules/access/http/connmgr.c
+++ b/modules/access/http/connmgr.c
@@ -25,6 +25,7 @@
 #include <assert.h>
 #include <vlc_common.h>
 #include <vlc_tls.h>
+#include <vlc_http.h>
 #include <vlc_interrupt.h>
 #include "transport.h"
 #include "conn.h"
@@ -134,6 +135,7 @@ struct vlc_http_mgr
 {
     vlc_object_t *obj;
     vlc_tls_creds_t *creds;
+    vlc_http_cookie_jar_t *jar;
     struct vlc_http_conn *conn;
     bool use_h2c;
 };
@@ -270,7 +272,40 @@ struct vlc_http_msg *vlc_http_mgr_request(struct vlc_http_mgr *mgr, bool https,
     return (https ? vlc_https_request : vlc_http_request)(mgr, host, port, m);
 }
 
-struct vlc_http_mgr *vlc_http_mgr_create(vlc_object_t *obj, bool h2c)
+int vlc_http_mgr_send_cookies(struct vlc_http_mgr *mgr, bool https,
+                              const char *host, const char *path,
+                              struct vlc_http_msg *req)
+{
+    int ret = 0;
+
+    if (mgr->jar != NULL)
+    {
+        char *cookies = vlc_http_cookies_fetch(mgr->jar, https, host, path);
+        if (cookies != NULL)
+        {
+            msg_Err(mgr->obj, "retrieved cookies: %s", cookies);
+            ret = vlc_http_msg_add_header(req, "Cookie", "%s", cookies);
+            free(cookies);
+        }
+    }
+    return ret;
+}
+
+void vlc_http_mgr_recv_cookies(struct vlc_http_mgr *mgr, bool https,
+                               const char *host, const char *path,
+                               const struct vlc_http_msg *resp)
+{
+    if (mgr->jar != NULL)
+    {   /* FIXME: fold multiple Set-Cookies headers with ';' */
+        const char *cookies = vlc_http_msg_get_header(resp, "Set-Cookie");
+        if (cookies != NULL
+         && vlc_http_cookies_store(mgr->jar, cookies, https, host, path))
+            msg_Err(mgr->obj, "stored cookie: %s", cookies);
+    }
+}
+
+struct vlc_http_mgr *vlc_http_mgr_create(vlc_object_t *obj,
+                                         vlc_http_cookie_jar_t *jar, bool h2c)
 {
     struct vlc_http_mgr *mgr = malloc(sizeof (*mgr));
     if (unlikely(mgr == NULL))
@@ -278,6 +313,7 @@ struct vlc_http_mgr *vlc_http_mgr_create(vlc_object_t *obj, bool h2c)
 
     mgr->obj = obj;
     mgr->creds = NULL;
+    mgr->jar = jar;
     mgr->conn = NULL;
     mgr->use_h2c = h2c;
     return mgr;
diff --git a/modules/access/http/connmgr.h b/modules/access/http/connmgr.h
index d84778e..efe4e5b 100644
--- a/modules/access/http/connmgr.h
+++ b/modules/access/http/connmgr.h
@@ -20,10 +20,19 @@
 
 struct vlc_http_mgr;
 struct vlc_http_msg;
+struct vlc_http_cookie_jar_t;
 
 struct vlc_http_msg *vlc_http_mgr_request(struct vlc_http_mgr *mgr, bool https,
                                           const char *host, unsigned port,
                                           const struct vlc_http_msg *req);
+int vlc_http_mgr_send_cookies(struct vlc_http_mgr *mgr, bool https,
+                              const char *host, const char *path,
+                              struct vlc_http_msg *req);
+void vlc_http_mgr_recv_cookies(struct vlc_http_mgr *mgr, bool https,
+                               const char *host, const char *path,
+                               const struct vlc_http_msg *resp);
 
-struct vlc_http_mgr *vlc_http_mgr_create(vlc_object_t *obj, bool try_h2c);
+struct vlc_http_mgr *vlc_http_mgr_create(vlc_object_t *obj,
+                                         struct vlc_http_cookie_jar_t *jar,
+                                         bool h2c);
 void vlc_http_mgr_destroy(struct vlc_http_mgr *mgr);
diff --git a/modules/access/http/file.c b/modules/access/http/file.c
index 14e5833..7a72d4f 100644
--- a/modules/access/http/file.c
+++ b/modules/access/http/file.c
@@ -123,6 +123,9 @@ static struct vlc_http_msg *vlc_http_file_open(struct vlc_http_file *file,
     if (unlikely(req == NULL))
         return NULL;
 
+    vlc_http_mgr_send_cookies(file->manager, file->secure, file->host,
+                              file->path, req);
+
     struct vlc_http_msg *resp = vlc_http_mgr_request(file->manager,
                                     file->secure, file->host, file->port, req);
     vlc_http_msg_destroy(req);
@@ -131,6 +134,9 @@ static struct vlc_http_msg *vlc_http_file_open(struct vlc_http_file *file,
     if (resp == NULL)
         return NULL;
 
+    vlc_http_mgr_recv_cookies(file->manager, file->secure, file->host,
+                              file->path, resp);
+
     int status = vlc_http_msg_get_status(resp);
     if (status < 200 || status >= 599)
         goto fail;
diff --git a/modules/access/http/file_test.c b/modules/access/http/file_test.c
index 40b83b0..1699aec 100644
--- a/modules/access/http/file_test.c
+++ b/modules/access/http/file_test.c
@@ -284,3 +284,26 @@ struct vlc_http_msg *vlc_http_mgr_request(struct vlc_http_mgr *mgr, bool https,
 
     return vlc_http_stream_read_headers(&stream);
 }
+
+int vlc_http_mgr_send_cookies(struct vlc_http_mgr *mgr, bool https,
+                              const char *host, const char *path,
+                              struct vlc_http_msg *req)
+{
+    assert(https);
+    assert(!strcmp(host, "www.example.com"));
+    assert(!strcmp(path, "/dir/file.ext?a=b"));
+    assert(mgr == NULL);
+    (void) req;
+    return 0;
+}
+
+void vlc_http_mgr_recv_cookies(struct vlc_http_mgr *mgr, bool https,
+                               const char *host, const char *path,
+                               const struct vlc_http_msg *resp)
+{
+    assert(https);
+    assert(!strcmp(host, "www.example.com"));
+    assert(!strcmp(path, "/dir/file.ext?a=b"));
+    assert(mgr == NULL);
+    (void) resp;
+}



More information about the vlc-commits mailing list