From 1fd6d3c584678917bd0a262bfbbbfaa7181add08 Mon Sep 17 00:00:00 2001
From: Lucas de Andrade <lucas@mindello.com.br>
Date: Tue, 22 Sep 2015 00:58:03 -0300
Subject: [PATCH 1/8] Update Cookies Setcookie response

Update Cookies Setcookie response
---
 libavformat/hls.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libavformat/hls.c b/libavformat/hls.c
index 82dd744..e5c84e1 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -996,6 +996,8 @@ static int open_input(HLSContext *c, struct playlist *pls)
 
     if (seg->key_type == KEY_NONE) {
         ret = open_url(pls->parent->priv_data, &pls->input, seg->url, opts);
+        update_options(&c->cookies, "cookies", pls->input->priv_data);
+        av_dict_set(&opts, "cookies", c->cookies, 0);
     } else if (seg->key_type == KEY_AES_128) {
 //         HLSContext *c = var->parent->priv_data;
         char iv[33], key[33], url[MAX_URL_SIZE];

From d738d7b525eef2fc76b9d13e2b9ca5044d9a46d1 Mon Sep 17 00:00:00 2001
From: Lucas de Andrade <lucas@mindello.com.br>
Date: Tue, 22 Sep 2015 10:41:31 -0300
Subject: [PATCH 2/8] Update cookies on http response with setcookies.

If a playlist response sets a cookie, the cookies must be updated for future requests.
---
 libavformat/hls.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/libavformat/hls.c b/libavformat/hls.c
index e5c84e1..23e59a6 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -996,8 +996,6 @@ static int open_input(HLSContext *c, struct playlist *pls)
 
     if (seg->key_type == KEY_NONE) {
         ret = open_url(pls->parent->priv_data, &pls->input, seg->url, opts);
-        update_options(&c->cookies, "cookies", pls->input->priv_data);
-        av_dict_set(&opts, "cookies", c->cookies, 0);
     } else if (seg->key_type == KEY_AES_128) {
 //         HLSContext *c = var->parent->priv_data;
         char iv[33], key[33], url[MAX_URL_SIZE];
@@ -1044,16 +1042,21 @@ static int open_input(HLSContext *c, struct playlist *pls)
     else
       ret = AVERROR(ENOSYS);
 
-    /* Seek to the requested position. If this was a HTTP request, the offset
-     * should already be where want it to, but this allows e.g. local testing
-     * without a HTTP server. */
-    if (ret == 0 && seg->key_type == KEY_NONE) {
-        int seekret = ffurl_seek(pls->input, seg->url_offset, SEEK_SET);
-        if (seekret < 0) {
-            av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of HLS segment '%s'\n", seg->url_offset, seg->url);
-            ret = seekret;
-            ffurl_close(pls->input);
-            pls->input = NULL;
+    if(ret == 0) {
+        // update cookies on http response with setcookies.
+        update_options(&c->cookies, "cookies", pls->input->priv_data);
+        av_dict_set(&opts, "cookies", c->cookies, 0);
+        /* Seek to the requested position. If this was a HTTP request, the offset
+         * should already be where want it to, but this allows e.g. local testing
+         * without a HTTP server. */
+        if (seg->key_type == KEY_NONE) {
+            int seekret = ffurl_seek(pls->input, seg->url_offset, SEEK_SET);
+            if (seekret < 0) {
+                av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of HLS segment '%s'\n", seg->url_offset, seg->url);
+                ret = seekret;
+                ffurl_close(pls->input);
+                pls->input = NULL;
+            }
         }
     }
 

From adff5975ac8a34f83ba8d644d6d8ace5d036e01c Mon Sep 17 00:00:00 2001
From: Lucas de Andrade <lucas@mindello.com.br>
Date: Tue, 22 Sep 2015 13:56:18 -0300
Subject: [PATCH 3/8] Refactor on cookie update

Removed opts2 as it wasn't needed anymore. Cookies update moved to open_url function.
---
 libavformat/hls.c | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/libavformat/hls.c b/libavformat/hls.c
index 23e59a6..4894e4a 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -535,7 +535,11 @@ static int open_url(HLSContext *c, URLContext **uc, const char *url, AVDictionar
     av_dict_copy(&tmp, c->avio_opts, 0);
     av_dict_copy(&tmp, opts, 0);
 
-    ret = ffurl_open(uc, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
+    if(ret = ffurl_open(uc, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp) == 0) {
+        // update cookies on http response with setcookies.
+        update_options(&c->cookies, "cookies", uc->priv_data);
+        av_dict_set(&opts, "cookies", c->cookies, 0);
+    }
 
     av_dict_free(&tmp);
 
@@ -971,7 +975,6 @@ static void update_options(char **dest, const char *name, void *src)
 static int open_input(HLSContext *c, struct playlist *pls)
 {
     AVDictionary *opts = NULL;
-    AVDictionary *opts2 = NULL;
     int ret;
     struct segment *seg = pls->segments[pls->cur_seq_no - pls->start_seq_no];
 
@@ -981,9 +984,6 @@ static int open_input(HLSContext *c, struct playlist *pls)
     av_dict_set(&opts, "headers", c->headers, 0);
     av_dict_set(&opts, "seekable", "0", 0);
 
-    // Same opts for key request (ffurl_open mutilates the opts so it cannot be used twice)
-    av_dict_copy(&opts2, opts, 0);
-
     if (seg->size >= 0) {
         /* try to restrict the HTTP request to the part we want
          * (if this is in fact a HTTP request) */
@@ -1001,14 +1001,12 @@ static int open_input(HLSContext *c, struct playlist *pls)
         char iv[33], key[33], url[MAX_URL_SIZE];
         if (strcmp(seg->key, pls->key_url)) {
             URLContext *uc;
-            if (open_url(pls->parent->priv_data, &uc, seg->key, opts2) == 0) {
+            if (open_url(pls->parent->priv_data, &uc, seg->key, opts) == 0) {
                 if (ffurl_read_complete(uc, pls->key, sizeof(pls->key))
                     != sizeof(pls->key)) {
                     av_log(NULL, AV_LOG_ERROR, "Unable to read key file %s\n",
                            seg->key);
                 }
-                update_options(&c->cookies, "cookies", uc->priv_data);
-                av_dict_set(&opts, "cookies", c->cookies, 0);
                 ffurl_close(uc);
             } else {
                 av_log(NULL, AV_LOG_ERROR, "Unable to open key file %s\n",
@@ -1040,29 +1038,24 @@ static int open_input(HLSContext *c, struct playlist *pls)
         ret = AVERROR_PATCHWELCOME;
     }
     else
-      ret = AVERROR(ENOSYS);
-
-    if(ret == 0) {
-        // update cookies on http response with setcookies.
-        update_options(&c->cookies, "cookies", pls->input->priv_data);
-        av_dict_set(&opts, "cookies", c->cookies, 0);
-        /* Seek to the requested position. If this was a HTTP request, the offset
-         * should already be where want it to, but this allows e.g. local testing
-         * without a HTTP server. */
-        if (seg->key_type == KEY_NONE) {
-            int seekret = ffurl_seek(pls->input, seg->url_offset, SEEK_SET);
-            if (seekret < 0) {
-                av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of HLS segment '%s'\n", seg->url_offset, seg->url);
-                ret = seekret;
-                ffurl_close(pls->input);
-                pls->input = NULL;
-            }
+        
+        ret = AVERROR(ENOSYS);
+
+    /* Seek to the requested position. If this was a HTTP request, the offset
+     * should already be where want it to, but this allows e.g. local testing
+     * without a HTTP server. */
+    if (ret == 0 && seg->key_type == KEY_NONE) {
+        int seekret = ffurl_seek(pls->input, seg->url_offset, SEEK_SET);
+        if (seekret < 0) {
+            av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of HLS segment '%s'\n", seg->url_offset, seg->url);
+            ret = seekret;
+            ffurl_close(pls->input);
+            pls->input = NULL;
         }
     }
 
 cleanup:
     av_dict_free(&opts);
-    av_dict_free(&opts2);
     pls->cur_seg_offset = 0;
     return ret;
 }

From e6757d93fe55163a755f2fab0245dcc5c89af665 Mon Sep 17 00:00:00 2001
From: Lucas de Andrade <lucas@mindello.com.br>
Date: Tue, 22 Sep 2015 21:26:15 -0300
Subject: [PATCH 4/8] Remove CR

---
 libavformat/hls.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libavformat/hls.c b/libavformat/hls.c
index 75ac5fb..2f53d74 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -1036,7 +1036,6 @@ static int open_input(HLSContext *c, struct playlist *pls)
         ret = AVERROR_PATCHWELCOME;
     }
     else
-        
         ret = AVERROR(ENOSYS);
 
     /* Seek to the requested position. If this was a HTTP request, the offset

From 640aecf14e30402508cfb0ac6ef9b01755367751 Mon Sep 17 00:00:00 2001
From: Lucas de Andrade <lucas@mindello.com.br>
Date: Wed, 23 Sep 2015 08:28:07 -0300
Subject: [PATCH 5/8] Fixed wrong comparison

---
 libavformat/hls.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libavformat/hls.c b/libavformat/hls.c
index a654924..c5b902a 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -533,7 +533,8 @@ static int open_url(HLSContext *c, URLContext **uc, const char *url, AVDictionar
     av_dict_copy(&tmp, c->avio_opts, 0);
     av_dict_copy(&tmp, opts, 0);
 
-    if(ret = ffurl_open(uc, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp) == 0) {
+    ret = ffurl_open(uc, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
+    if( ret >= 0) {
         // update cookies on http response with setcookies.
         update_options(&c->cookies, "cookies", uc->priv_data);
         av_dict_set(&opts, "cookies", c->cookies, 0);

From 726d7bc011f8270dc095119961e0909f5f623f0c Mon Sep 17 00:00:00 2001
From: Lucas de Andrade <lucas@mindello.com.br>
Date: Wed, 23 Sep 2015 08:36:19 -0300
Subject: [PATCH 6/8] Ajusted pointers

---
 libavformat/hls.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libavformat/hls.c b/libavformat/hls.c
index c5b902a..1867a49 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -525,7 +525,7 @@ static int url_connect(struct playlist *pls, AVDictionary *opts, AVDictionary *o
     return ret;
 }
 
-static int open_url(HLSContext *c, URLContext **uc, const char *url, AVDictionary *opts)
+static int open_url(HLSContext *c, URLContext *uc, const char *url, AVDictionary *opts)
 {
     AVDictionary *tmp = NULL;
     int ret;
@@ -994,13 +994,13 @@ static int open_input(HLSContext *c, struct playlist *pls)
            seg->url, seg->url_offset, pls->index);
 
     if (seg->key_type == KEY_NONE) {
-        ret = open_url(pls->parent->priv_data, &pls->input, seg->url, opts);
+        ret = open_url(pls->parent->priv_data, pls->input, seg->url, opts);
     } else if (seg->key_type == KEY_AES_128) {
 //         HLSContext *c = var->parent->priv_data;
         char iv[33], key[33], url[MAX_URL_SIZE];
         if (strcmp(seg->key, pls->key_url)) {
             URLContext *uc;
-            if (open_url(pls->parent->priv_data, &uc, seg->key, opts) == 0) {
+            if (open_url(pls->parent->priv_data, uc, seg->key, opts) == 0) {
                 if (ffurl_read_complete(uc, pls->key, sizeof(pls->key))
                     != sizeof(pls->key)) {
                     av_log(NULL, AV_LOG_ERROR, "Unable to read key file %s\n",

From cf81fe8871f3eaa6b7ab0de7246fb959cf914ac5 Mon Sep 17 00:00:00 2001
From: Lucas de Andrade <lucas@mindello.com.br>
Date: Wed, 23 Sep 2015 08:43:51 -0300
Subject: [PATCH 7/8] Fixed ffurl_open parameters

---
 libavformat/hls.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavformat/hls.c b/libavformat/hls.c
index 1867a49..4ae56bc 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -533,7 +533,7 @@ static int open_url(HLSContext *c, URLContext *uc, const char *url, AVDictionary
     av_dict_copy(&tmp, c->avio_opts, 0);
     av_dict_copy(&tmp, opts, 0);
 
-    ret = ffurl_open(uc, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
+    ret = ffurl_open(&uc, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
     if( ret >= 0) {
         // update cookies on http response with setcookies.
         update_options(&c->cookies, "cookies", uc->priv_data);

From ea5345fa5592da9daa7fd19db7c277e960bdae5f Mon Sep 17 00:00:00 2001
From: Lucas Andrade <lucas@mindello.com.br>
Date: Thu, 24 Sep 2015 22:45:00 -0300
Subject: [PATCH 8/8] Fixed compile break

---
 libavformat/hls.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/libavformat/hls.c b/libavformat/hls.c
index 4ae56bc..f9f86af 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -525,7 +525,15 @@ static int url_connect(struct playlist *pls, AVDictionary *opts, AVDictionary *o
     return ret;
 }
 
-static int open_url(HLSContext *c, URLContext *uc, const char *url, AVDictionary *opts)
+static void update_options(char **dest, const char *name, void *src)
+{
+    av_freep(dest);
+    av_opt_get(src, name, 0, (uint8_t**)dest);
+    if (*dest && !strlen(*dest))
+        av_freep(dest);
+}
+
+static int open_url(HLSContext *c, URLContext **uc, const char *url, AVDictionary *opts)
 {
     AVDictionary *tmp = NULL;
     int ret;
@@ -533,10 +541,11 @@ static int open_url(HLSContext *c, URLContext *uc, const char *url, AVDictionary
     av_dict_copy(&tmp, c->avio_opts, 0);
     av_dict_copy(&tmp, opts, 0);
 
-    ret = ffurl_open(&uc, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
+    ret = ffurl_open(uc, url, AVIO_FLAG_READ, c->interrupt_callback, &tmp);
     if( ret >= 0) {
         // update cookies on http response with setcookies.
-        update_options(&c->cookies, "cookies", uc->priv_data);
+        URLContext *u = *uc;
+        update_options(&c->cookies, "cookies", u->priv_data);
         av_dict_set(&opts, "cookies", c->cookies, 0);
     }
 
@@ -963,14 +972,6 @@ static void intercept_id3(struct playlist *pls, uint8_t *buf,
         pls->is_id3_timestamped = (pls->id3_mpegts_timestamp != AV_NOPTS_VALUE);
 }
 
-static void update_options(char **dest, const char *name, void *src)
-{
-    av_freep(dest);
-    av_opt_get(src, name, 0, (uint8_t**)dest);
-    if (*dest && !strlen(*dest))
-        av_freep(dest);
-}
-
 static int open_input(HLSContext *c, struct playlist *pls)
 {
     AVDictionary *opts = NULL;
@@ -994,13 +995,13 @@ static int open_input(HLSContext *c, struct playlist *pls)
            seg->url, seg->url_offset, pls->index);
 
     if (seg->key_type == KEY_NONE) {
-        ret = open_url(pls->parent->priv_data, pls->input, seg->url, opts);
+        ret = open_url(pls->parent->priv_data, &pls->input, seg->url, opts);
     } else if (seg->key_type == KEY_AES_128) {
 //         HLSContext *c = var->parent->priv_data;
         char iv[33], key[33], url[MAX_URL_SIZE];
         if (strcmp(seg->key, pls->key_url)) {
             URLContext *uc;
-            if (open_url(pls->parent->priv_data, uc, seg->key, opts) == 0) {
+            if (open_url(pls->parent->priv_data, &uc, seg->key, opts) == 0) {
                 if (ffurl_read_complete(uc, pls->key, sizeof(pls->key))
                     != sizeof(pls->key)) {
                     av_log(NULL, AV_LOG_ERROR, "Unable to read key file %s\n",
