Commit a0c369f64c for ffmpeg

commit a0c369f64c871a1999925b95453061f0baae365e
Author: Michael Niedermayer <michael@niedermayer.cc>
Date:   Sat Nov 7 00:21:32 2026 +0100

    avformat/httpcookie: keep the name and value of a cookie apart from its attributes

    A cookie named like an attribute was taken for that attribute, e.g.
    "domain=localhost" was sent to localhost instead of the host it was given
    for, and an attribute named like the cookie replaced the cookie.

    Fixes: cookies named like an attribute

diff --git a/libavformat/http.c b/libavformat/http.c
index 4cb9db8c36..781a968bc4 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -1116,24 +1116,19 @@ static int host_in_cookie_domain(const char *host, const char *domain)
 static int parse_cookie(HTTPContext *s, const char *p, const char *host,
                         AVDictionary **cookies)
 {
-    AVDictionary *new_params = NULL;
+    AVDictionary *new_cookie = NULL, *new_params = NULL;
     const AVDictionaryEntry *e, *cookie_entry;
     const char *eql;
     char *name, *value;
     int len;

     // ensure the cookie is parsable
-    if (ff_http_parse_set_cookie(p, &new_params)) {
-        av_dict_free(&new_params);
-        return -1;
-    }
-
-    // if there is no cookie value there is nothing to parse
-    cookie_entry = av_dict_iterate(new_params, NULL);
-    if (!cookie_entry || !cookie_entry->value) {
+    if (ff_http_parse_set_cookie(p, &new_cookie, &new_params)) {
+        av_dict_free(&new_cookie);
         av_dict_free(&new_params);
         return -1;
     }
+    cookie_entry = av_dict_iterate(new_cookie, NULL);

     // ensure the cookie is not expired or older than an existing value
     if ((e = av_dict_get(new_params, "expires", NULL, 0)) && e->value) {
@@ -1143,6 +1138,7 @@ static int parse_cookie(HTTPContext *s, const char *p, const char *host,

             // if the cookie has already expired ignore it
             if (av_timegm(&new_tm) < av_gettime() / 1000000) {
+                av_dict_free(&new_cookie);
                 av_dict_free(&new_params);
                 return 0;
             }
@@ -1150,20 +1146,23 @@ static int parse_cookie(HTTPContext *s, const char *p, const char *host,
             // only replace an older cookie with the same name
             e2 = av_dict_get(*cookies, cookie_entry->key, NULL, 0);
             if (e2 && e2->value) {
-                AVDictionary *old_params = NULL;
-                if (!ff_http_parse_set_cookie(p, &old_params)) {
+                AVDictionary *old_cookie = NULL, *old_params = NULL;
+                if (!ff_http_parse_set_cookie(p, &old_cookie, &old_params)) {
                     e2 = av_dict_get(old_params, "expires", NULL, 0);
                     if (e2 && e2->value) {
                         struct tm old_tm = {0};
                         if (!ff_http_parse_date(e->value, &old_tm)) {
                             if (av_timegm(&new_tm) < av_timegm(&old_tm)) {
+                                av_dict_free(&new_cookie);
                                 av_dict_free(&new_params);
+                                av_dict_free(&old_cookie);
                                 av_dict_free(&old_params);
                                 return -1;
                             }
                         }
                     }
                 }
+                av_dict_free(&old_cookie);
                 av_dict_free(&old_params);
             }
         }
@@ -1172,9 +1171,11 @@ static int parse_cookie(HTTPContext *s, const char *p, const char *host,
     int host_only = host && !domain;
     if (domain && !host_in_cookie_domain(host, domain)) {
         av_log(s, AV_LOG_WARNING, "Ignoring cookie for domain %s set by %s\n", domain, host);
+        av_dict_free(&new_cookie);
         av_dict_free(&new_params);
         return 0;
     }
+    av_dict_free(&new_cookie);
     av_dict_free(&new_params);

     // duplicate the cookie name (dict will dupe the value)
@@ -1448,9 +1449,9 @@ static int get_cookies(HTTPContext *s, char **cookies, const char *path)

     *cookies = NULL;
     while ((cookie = av_strtok(next, "\n", &saveptr)) && !ret) {
-        AVDictionary *cookie_params = NULL;
+        AVDictionary *cookie_pair = NULL, *cookie_params = NULL;
         const AVDictionaryEntry *cookie_entry, *e;
-        const char *domain, *eql;
+        const char *domain;

         next = NULL;
         // store the cookie in a dict in case it is updated in the response
@@ -1458,20 +1459,13 @@ static int get_cookies(HTTPContext *s, char **cookies, const char *path)
             av_log(s, AV_LOG_WARNING, "Unable to parse '%s'\n", cookie);

         // continue on to the next cookie if this one cannot be parsed
-        if (ff_http_parse_set_cookie(cookie, &cookie_params))
+        if (ff_http_parse_set_cookie(cookie, &cookie_pair, &cookie_params))
             goto skip_cookie;
+        cookie_entry = av_dict_iterate(cookie_pair, NULL);

-        // if the cookie has no value, skip it
-        cookie_entry = av_dict_iterate(cookie_params, NULL);
-        eql = strchr(cookie, '=');
-        if (!cookie_entry || !eql || eql == cookie + strspn(cookie, WHITESPACES) ||
-            memchr(cookie, ';', eql - cookie))
+        if (av_dict_get(cookie_params, "secure", NULL, 0) && !av_stristart(s->location, "https:", NULL))
             goto skip_cookie;

-        for (e = cookie_entry; (e = av_dict_iterate(cookie_params, e)); )
-            if (!av_strcasecmp(e->key, "secure") && !av_stristart(s->location, "https:", NULL))
-                goto skip_cookie;
-
         // if the cookie has expired, don't add it
         if ((e = av_dict_get(cookie_params, "expires", NULL, 0)) && e->value) {
             struct tm tm_buf = {0};
@@ -1511,6 +1505,7 @@ static int get_cookies(HTTPContext *s, char **cookies, const char *path)
             ret = AVERROR(ENOMEM);

     skip_cookie:
+        av_dict_free(&cookie_pair);
         av_dict_free(&cookie_params);
     }

diff --git a/libavformat/http.h b/libavformat/http.h
index a7a4218727..c3f52da721 100644
--- a/libavformat/http.h
+++ b/libavformat/http.h
@@ -126,11 +126,14 @@ const char* ff_http_get_new_location(URLContext *h);
 int ff_http_parse_date(const char *date_str, struct tm *buf);

 /**
- * Parse a Set-Cookie style line into its cookie and attributes.
+ * Parse a Set-Cookie style line.
  *
- * @return 0 on success, a negative value otherwise
+ * @param cookie     the name and value of the cookie are added to it
+ * @param attributes the attributes of the cookie are added to it
+ * @return 0 on success, AVERROR_INVALIDDATA if the line holds no cookie,
+ *         another negative error code otherwise
  */
-int ff_http_parse_set_cookie(const char *set_cookie, AVDictionary **dict);
+int ff_http_parse_set_cookie(const char *set_cookie, AVDictionary **cookie, AVDictionary **attributes);

 int ff_http_host_is_ip_literal(const char *host);

diff --git a/libavformat/httpcookie.c b/libavformat/httpcookie.c
index a32ab54692..f0d3f61bb9 100644
--- a/libavformat/httpcookie.c
+++ b/libavformat/httpcookie.c
@@ -53,44 +53,48 @@ int ff_http_parse_date(const char *date_str, struct tm *buf)
     return av_small_strptime(date, "%d%b%Y%H%M%S", buf) ? 0 : AVERROR(EINVAL);
 }

-int ff_http_parse_set_cookie(const char *set_cookie, AVDictionary **dict)
+static char *trim(char *str)
 {
-    char *param, *next_param, *cstr, *back;
-    char *saveptr = NULL;
+    char *end;
+
+    str += strspn(str, WHITESPACES);
+    end  = str + strlen(str);
+    while (end > str && strchr(WHITESPACES, end[-1]))
+        *--end = '\0';
+    return str;
+}

-    if (!set_cookie[0])
-        return 0;
+int ff_http_parse_set_cookie(const char *set_cookie, AVDictionary **cookie, AVDictionary **attributes)
+{
+    char *param, *next_param, *cstr, *eql;
+    char *saveptr = NULL;
+    int ret;

     if (!(cstr = av_strdup(set_cookie)))
-        return AVERROR(EINVAL);
+        return AVERROR(ENOMEM);

-    // strip any trailing whitespace
-    back = &cstr[strlen(cstr)-1];
-    while (strchr(WHITESPACES, *back)) {
-        *back='\0';
-        if (back == cstr)
-            break;
-        back--;
+    // the name and value of the cookie come before the first ';'
+    param      = trim(cstr);
+    next_param = strchr(param, ';');
+    if (next_param)
+        *next_param++ = '\0';
+    if (!(eql = strchr(param, '=')) || eql == param) {
+        av_free(cstr);
+        return AVERROR_INVALIDDATA;
     }
+    *eql = '\0';
+    ret = av_dict_set(cookie, trim(param), eql + 1, 0);

-    next_param = cstr;
-    while ((param = av_strtok(next_param, ";", &saveptr))) {
+    while (ret >= 0 && (param = av_strtok(next_param, ";", &saveptr))) {
         char *name, *value;
         next_param = NULL;
         param += strspn(param, WHITESPACES);
-        if ((name = av_strtok(param, "=", &value))) {
-            char *end = name + strlen(name);
-            while (end > name && strchr(WHITESPACES, end[-1]))
-                *--end = '\0';
-            if (av_dict_set(dict, name, value ? value : "", 0) < 0) {
-                av_free(cstr);
-                return -1;
-            }
-        }
+        if ((name = av_strtok(param, "=", &value)))
+            ret = av_dict_set(attributes, trim(name), value ? value : "", 0);
     }

     av_free(cstr);
-    return 0;
+    return ret < 0 ? ret : 0;
 }

 int ff_http_host_is_ip_literal(const char *host)
@@ -118,18 +122,19 @@ int ff_http_bind_cookies_to_host(char **cookies, const char *host)

     av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
     while ((cookie = av_strtok(next, "\n", &saveptr))) {
-        AVDictionary *params = NULL;
+        AVDictionary *pair = NULL, *params = NULL;
         int len = strlen(cookie), host_only;

         next = NULL;
         while (len && strchr(WHITESPACES, cookie[len - 1]))
             len--;
-        if ((ret = ff_http_parse_set_cookie(cookie, &params)) < 0) {
-            av_dict_free(&params);
-            break;
-        }
+        ret = ff_http_parse_set_cookie(cookie, &pair, &params);
         host_only = !ff_http_cookie_domain(params) && !av_dict_get(params, "@hostonly", NULL, 0);
+        av_dict_free(&pair);
         av_dict_free(&params);
+        if (ret == AVERROR(ENOMEM))
+            break;
+        ret = 0;
         av_bprintf(&bp, "%.*s%s%s\n", len, cookie, host_only ? "; @hostonly=" : "", host_only ? host : "");
     }
     av_free(copy);