Commit 8ca683df6f for ffmpeg

commit 8ca683df6f85732224d00c17a0400781d474789d
Author: Michael Niedermayer <michael@niedermayer.cc>
Date:   Sat Nov 7 00:23:53 2026 +0100

    avformat/httpcookie: support Max-Age

    It takes precedence over Expires, as specified by RFC 6265 section 5.3.

    Fixes: cookies expired by Max-Age being sent

diff --git a/libavformat/http.c b/libavformat/http.c
index 781a968bc4..cec796b46d 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -1452,6 +1452,7 @@ static int get_cookies(HTTPContext *s, char **cookies, const char *path)
         AVDictionary *cookie_pair = NULL, *cookie_params = NULL;
         const AVDictionaryEntry *cookie_entry, *e;
         const char *domain;
+        int64_t expiry;

         next = NULL;
         // store the cookie in a dict in case it is updated in the response
@@ -1467,13 +1468,9 @@ static int get_cookies(HTTPContext *s, char **cookies, const char *path)
             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};
-            if (!ff_http_parse_date(e->value, &tm_buf)) {
-                if (av_timegm(&tm_buf) < av_gettime() / 1000000)
-                    goto skip_cookie;
-            }
-        }
+        expiry = ff_http_cookie_expiry(cookie_params);
+        if (expiry && expiry < av_gettime() / 1000000)
+            goto skip_cookie;

         // if no domain in the cookie assume it applied to this request
         domain = ff_http_cookie_domain(cookie_params);
diff --git a/libavformat/http.h b/libavformat/http.h
index c3f52da721..0a1d8ad7e8 100644
--- a/libavformat/http.h
+++ b/libavformat/http.h
@@ -22,6 +22,7 @@
 #ifndef AVFORMAT_HTTP_H
 #define AVFORMAT_HTTP_H

+#include <stdint.h>
 #include <string.h>
 #include <time.h>

@@ -142,6 +143,13 @@ int ff_http_host_is_ip_literal(const char *host);
  */
 const char *ff_http_cookie_domain(const AVDictionary *cookie_params);

+/**
+ * @param attributes attributes parsed by ff_http_parse_set_cookie()
+ * @return the time the cookie expires at in seconds since the epoch,
+ *         0 if it does not expire
+ */
+int64_t ff_http_cookie_expiry(const AVDictionary *attributes);
+
 /**
  * Restrict the cookies without a Domain to host.
  *
diff --git a/libavformat/httpcookie.c b/libavformat/httpcookie.c
index aa4be75273..ff20d9b169 100644
--- a/libavformat/httpcookie.c
+++ b/libavformat/httpcookie.c
@@ -17,10 +17,13 @@
  */

 #include <stdint.h>
+#include <stdlib.h>
 #include <string.h>
+#include <time.h>

 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
+#include "libavutil/common.h"
 #include "libavutil/mem.h"
 #include "libavutil/parseutils.h"

@@ -73,6 +76,12 @@ static int has_control_char(const char *str)
     return 0;
 }

+static int is_delta_seconds(const char *str)
+{
+    str += *str == '-';
+    return *str && !str[strspn(str, "0123456789")];
+}
+
 int ff_http_parse_set_cookie(const char *set_cookie, AVDictionary **cookie, AVDictionary **attributes)
 {
     char *param, *next_param, *cstr, *eql;
@@ -106,8 +115,12 @@ int ff_http_parse_set_cookie(const char *set_cookie, AVDictionary **cookie, AVDi
         if (value)
             *value++ = '\0';
         param = trim(param);
+        value = value ? trim(value) : NULL;
+        // an invalid Max-Age is ignored (RFC 6265 5.2.2)
+        if (!av_strcasecmp(param, "max-age") && (!value || !is_delta_seconds(value)))
+            continue;
         if (*param)
-            ret = av_dict_set(attributes, param, value ? trim(value) : "", 0);
+            ret = av_dict_set(attributes, param, value ? value : "", 0);
     }

     av_free(cstr);
@@ -127,6 +140,22 @@ const char *ff_http_cookie_domain(const AVDictionary *cookie_params)
     return *domain ? domain : NULL;
 }

+int64_t ff_http_cookie_expiry(const AVDictionary *attributes)
+{
+    const AVDictionaryEntry *max_age = av_dict_get(attributes, "max-age", NULL, 0);
+    const AVDictionaryEntry *expires = av_dict_get(attributes, "expires", NULL, 0);
+    struct tm tm = { 0 };
+
+    // Max-Age takes precedence over Expires (RFC 6265 5.3)
+    if (max_age) {
+        int64_t delta = strtoll(max_age->value, NULL, 10);
+        return delta > 0 ? av_sat_add64(time(NULL), delta) : 1;
+    }
+    if (expires && !ff_http_parse_date(expires->value, &tm))
+        return FFMAX(av_timegm(&tm), 1);
+    return 0;
+}
+
 int ff_http_bind_cookies_to_host(char **cookies, const char *host)
 {
     char *copy, *cookie, *next, *saveptr = NULL, *bound;