souphttpsrc: cookie jar and context query support

Use a volatile Cookie jar to store cookies and handle the context
query so that session data can be shared with other elements (like
adaptivedemux).

https://bugzilla.gnome.org/show_bug.cgi?id=726314
diff --git a/ext/soup/gstsouphttpsrc.c b/ext/soup/gstsouphttpsrc.c
index 28642e3..d23ec4c 100644
--- a/ext/soup/gstsouphttpsrc.c
+++ b/ext/soup/gstsouphttpsrc.c
@@ -489,6 +489,7 @@
   src->session = NULL;
   src->external_session = NULL;
   src->forced_external_session = FALSE;
+  src->cookie_jar = NULL;
   src->msg = NULL;
   src->timeout = DEFAULT_TIMEOUT;
   src->log_level = DEFAULT_SOUP_LOG_LEVEL;
@@ -1026,6 +1027,9 @@
     GST_DEBUG_OBJECT (src, "Re-using session");
   }
 
+  src->cookie_jar = soup_cookie_jar_new ();
+  soup_session_add_feature (src->session,
+      SOUP_SESSION_FEATURE (src->cookie_jar));
   return TRUE;
 }
 
@@ -1041,6 +1045,11 @@
     src->msg = NULL;
   }
 
+  if (src->cookie_jar) {
+    g_object_unref (src->cookie_jar);
+    src->cookie_jar = NULL;
+  }
+
   if (src->session) {
     if (!src->session_is_shared)
       soup_session_abort (src->session);
@@ -1493,11 +1502,12 @@
   }
   if (src->cookies) {
     gchar **cookie;
+    SoupURI *uri = soup_uri_new (src->location);
 
     for (cookie = src->cookies; *cookie != NULL; cookie++) {
-      soup_message_headers_append (src->msg->request_headers, "Cookie",
-          *cookie);
+      soup_cookie_jar_set_cookie (src->cookie_jar, uri, *cookie);
     }
+    soup_uri_free (uri);
   }
 
   if (!src->compress)
@@ -2046,6 +2056,12 @@
   gboolean ret;
   GstSchedulingFlags flags;
   gint minsize, maxsize, align;
+  GstContext *context;
+  GstStructure *context_structure;
+  char *cookie;
+  const gchar *cookies[2];
+  const gchar *context_type;
+  SoupURI *uri;
 
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_URI:
@@ -2057,6 +2073,27 @@
       }
       ret = TRUE;
       break;
+    case GST_QUERY_CONTEXT:
+      if (gst_query_parse_context_type (query, &context_type)
+          && !g_strcmp0 (context_type, "http-headers")) {
+        uri = soup_uri_new (src->location);
+        cookie = soup_cookie_jar_get_cookies (src->cookie_jar, uri, TRUE);
+        context = gst_context_new ("http-headers", FALSE);
+        gst_context_make_writable (context);
+        context_structure = gst_context_writable_structure (context);
+        if (cookie != NULL) {
+          cookies[0] = cookie;
+          cookies[1] = NULL;
+          gst_structure_set (context_structure, "cookies", G_TYPE_STRV, cookies,
+              NULL);
+          g_free (cookie);
+        }
+        gst_query_set_context (query, context);
+        soup_uri_free (uri);
+        ret = TRUE;
+        break;
+      }
+
     default:
       ret = FALSE;
       break;
diff --git a/ext/soup/gstsouphttpsrc.h b/ext/soup/gstsouphttpsrc.h
index 06851dd..cd0397a 100644
--- a/ext/soup/gstsouphttpsrc.h
+++ b/ext/soup/gstsouphttpsrc.h
@@ -63,6 +63,7 @@
   gboolean session_is_shared;
   SoupSession *external_session; /* Shared via GstContext */
   gboolean forced_external_session; /* If session was explicitly set from application */
+  SoupCookieJar *cookie_jar;   /* Volatile HTTP cookie storage */
   SoupMessage *msg;            /* Request message. */
   gint retry_count;            /* Number of retries since we received data */
   gint max_retries;            /* Maximum number of retries */