Commit 4325111907 for ffmpeg

commit 4325111907ad57f1f159101bc4ae1558d8187876
Author: Jun Zhao <barryjzhao@tencent.com>
Date:   Wed Nov 18 07:08:12 2026 +0800

    avformat/libcurl: support the HTTP method option and HEAD

    CurlContext had no "method" AVOption, so -method HEAD -prefer_libcurl 1
    was silently dropped and sent a GET.  Add the option and use
    CURLOPT_NOBODY only for the case-sensitive token HEAD.
    CUSTOMREQUEST="HEAD" alone would still expect a body and trigger
    CURLE_PARTIAL_FILE retries.  Other methods, including a lowercase
    "head", are passed through CURLOPT_CUSTOMREQUEST.

    HEAD suppresses CURLOPT_RANGE, accepts an empty Content-Length: 0
    representation at offset 0, and completes with a clean EOF without the
    recoverable-error retry path.  Some libcurl versions (e.g. 8.7.1)
    report CURLINFO_CONTENT_LENGTH_DOWNLOAD_T = -1 throughout
    header_callback(), where content_size is latched, so parse the full
    len-bounded Content-Length header value and keep it even when
    Content-Encoding is set.  Document the option in doc/protocols.texi.

    Signed-off-by: Jun Zhao <barryjzhao@tencent.com>

diff --git a/doc/protocols.texi b/doc/protocols.texi
index dc7f48f038..4eb9e9a4c5 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -1139,6 +1139,11 @@ The libcurl protocol accepts the following options.
 @item user_agent
 Override the @code{User-Agent} header. Defaults to @code{Lavf/<version>}.

+@item method
+Override the HTTP method. @code{HEAD} retrieves the response headers and
+the resource size without a response body. Any other method name is sent
+as supplied; method tokens are case-sensitive.
+
 @item referer
 Set the @code{Referer} header.

diff --git a/libavformat/libcurl.c b/libavformat/libcurl.c
index 18185e1fd7..fa802bc7c2 100644
--- a/libavformat/libcurl.c
+++ b/libavformat/libcurl.c
@@ -118,6 +118,7 @@ struct CurlContext {

     /* AVOptions. */
     char           *user_agent;
+    char           *method;      /* client HTTP method (GET/HEAD/...), or NULL */
     char           *referer;
     char           *headers;
     char           *http_proxy;
@@ -154,12 +155,14 @@ struct CurlContext {
     int             icy_block_filled;
     uint8_t         icy_block[ICY_MAX_BLOCK + 1];

-    /* Producer bookkeeping, touched only by the loop thread. */
+    /* Producer bookkeeping, touched only by the loop thread.  no_body is
+     * set once by setup_curl() before the handle is added, then read-only. */
     int             active;          /* currently added to the multi */
     int64_t         request_start;   /* absolute offset the current request began at */
     int64_t         request_received;/* bytes delivered in the current request */
     int64_t         request_end;     /* expected end of request, or -1 if unknown */
     int             is_initial;      /* using reduced request size */
+    int             no_body;         /* HEAD request: CURLOPT_NOBODY is set */
     int             seek_queued;     /* soft seeking; drain remaining bytes until done */

     /* Per-response-block header scratch, loop thread only. */
@@ -378,25 +381,26 @@ static void parse_content_range(CurlContext *c, const char *v)
 }

 /* Parse a decimal header value, bounded by len since curl does not promise a
- * NUL terminated header buffer. Returns -1 if absent, malformed or too large. */
-static int64_t parse_metaint(const char *p, size_t len)
+ * NUL terminated header buffer. Returns -1 if absent, malformed or > max. */
+static int64_t parse_header_uint(const char *p, size_t len, int64_t max)
 {
     int64_t v = 0;
     size_t i = 0;

-    while (i < len && av_isspace(p[i]))
+    while (i < len && av_isspace((unsigned char)p[i]))
         i++;
-    if (i == len || !av_isdigit(p[i]))
+    if (i == len || !av_isdigit((unsigned char)p[i]))
         return -1;

-    for (; i < len && av_isdigit(p[i]); i++) {
-        if (v > (INT_MAX - (p[i] - '0')) / 10)
+    for (; i < len && av_isdigit((unsigned char)p[i]); i++) {
+        int d = p[i] - '0';
+        if (v > (max - d) / 10)
             return -1;
-        v = v * 10 + (p[i] - '0');
+        v = v * 10 + d;
     }

     // Reject 4junk
-    while (i < len && av_isspace(p[i]))
+    while (i < len && av_isspace((unsigned char)p[i]))
         i++;
     if (i != len)
         return -1;
@@ -469,6 +473,10 @@ static int verify_content_range(CurlContext *c, int64_t start, int64_t end,
         return 0;
     }

+    /* A 200 HEAD reply for an empty representation has start=0, end=-1, total=0. */
+    if (c->no_body && !total && end < 0)
+        return 1;
+
     if (total >= 0 && (start >= total || end >= total)) {
         av_log(c->h, AV_LOG_ERROR, "Server sent back content range "
                "%"PRId64"-%"PRId64" that exceeds the total size "
@@ -522,8 +530,17 @@ static size_t header_callback(char *ptr, size_t size, size_t nitems, void *userd
         parse_content_range(c, ptr + 14);
         return len;
     }
+    if (c->no_body && av_strncasecmp(ptr, "Content-Length:", 15) == 0) {
+        /* Some libcurl versions (e.g. 8.7.1) still report
+         * CURLINFO_CONTENT_LENGTH_DOWNLOAD_T = -1 for HEAD throughout
+         * header_callback(), including the final blank line where
+         * content_size is latched.  Parse Content-Length here so
+         * avio_size()/AVSEEK_SIZE stays populated. */
+        c->hdr_content_length = parse_header_uint(ptr + 15, len - 15, INT64_MAX);
+        return len;
+    }
     if (av_strncasecmp(ptr, "icy-metaint:", 12) == 0) {
-        c->hdr_icy_metaint = parse_metaint(ptr + 12, len - 12);
+        c->hdr_icy_metaint = parse_header_uint(ptr + 12, len - 12, INT_MAX);
         return len;
     }
     /* Collected per block so that headers from a redirect hop, which the
@@ -547,14 +564,19 @@ static size_t header_callback(char *ptr, size_t size, size_t nitems, void *userd
         return len;

     curl_off_t cl = -1;
-    if (curl_easy_getinfo(c->easy, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &cl) == CURLE_OK)
-        c->hdr_content_length = cl >= 0 ? cl : -1;
+    /* Do not clobber a Content-Length parsed above when getinfo is still -1. */
+    if (curl_easy_getinfo(c->easy, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &cl) == CURLE_OK &&
+        cl >= 0)
+        c->hdr_content_length = cl;

     pthread_mutex_lock(&c->mutex);
     if (status >= 200 && status < 300) {
         int64_t content_start = status == 206 ? c->hdr_content_start : 0;
         int64_t content_end   = status == 206 ? c->hdr_content_end : -1;
         int64_t content_total = status == 206 ? c->hdr_content_total : c->hdr_content_length;
+        /* Content-Encoding describes a body. HEAD has none, so its
+         * Content-Length is the resource size. */
+        int keep_size;
         if (content_end < 0 && content_total > 0)
             content_end = content_total - 1;

@@ -567,11 +589,13 @@ static size_t header_callback(char *ptr, size_t size, size_t nitems, void *userd
             return len;
         }

+        keep_size = !c->hdr_compressed || c->no_body;
+
         /* Don't unlearn a known size when a reply omits it. */
-        if (!c->hdr_compressed && content_total >= 0)
+        if (keep_size && content_total >= 0)
             c->content_size = content_total;

-        if (!c->hdr_compressed) {
+        if (keep_size) {
             int64_t total = content_total;
             if (total < 0 && status != 206)
                 total = c->hdr_content_length;
@@ -583,7 +607,7 @@ static size_t header_callback(char *ptr, size_t size, size_t nitems, void *userd
         }

         c->stream_ok = 1;
-        c->request_end = c->hdr_compressed ? -1 : content_end;
+        c->request_end = keep_size ? content_end : -1;
         /* Capture the post-redirect URL, this is exposed as "location" AVOption
          * for compatibility with http.c. */
         if (!c->probed) {
@@ -676,7 +700,7 @@ static void start_request(CurlContext *c)
         return;
     }

-    if (!c->probed || c->seekable) {
+    if (!c->no_body && (!c->probed || c->seekable)) {
         int64_t start = c->request_start;
         int64_t request_size = c->request_size;
         if (c->is_initial && c->initial_request_size > 0)
@@ -780,6 +804,24 @@ static void on_done(CurlContext *c, CURLcode code)
     if (aborted)
         return;

+    if (c->no_body) {
+        /* A HEAD reply carries no body.  Do not restart the request or
+         * escalate an empty transfer to EIO ("Server sent back no data at
+         * offset"), and do not treat the clean EOF as a recoverable error. */
+        pthread_mutex_lock(&c->mutex);
+        if (code == CURLE_OK && c->stream_ok) {
+            c->status      = AVERROR_EOF;
+            c->curl_status = CURLE_OK;
+        } else if (!c->status) {
+            c->loop->num_errors++;
+            update_status_locked(c, code == CURLE_OK ? AVERROR(EIO)
+                                                     : curlcode_to_averror(code), code);
+        }
+        pthread_cond_broadcast(&c->cond);
+        pthread_mutex_unlock(&c->mutex);
+        return;
+    }
+
     if (c->seek_queued) {
         /* previous soft seek drain finished; can start new request now */
         c->seek_queued = 0;
@@ -1362,6 +1404,17 @@ static int setup_curl(CurlContext *c)

     if (c->user_agent && c->user_agent[0])
         CURL_SETOPT(CURLOPT_USERAGENT, c->user_agent);
+    if (c->method && c->method[0]) {
+        if (!strcmp(c->method, "HEAD")) {
+            /* NOBODY issues HEAD and skips the body.  CUSTOMREQUEST="HEAD"
+             * alone would leave curl expecting Content-Length bytes and
+             * raise CURLE_PARTIAL_FILE, which is retryable here. */
+            c->no_body = 1;
+            CURL_SETOPT(CURLOPT_NOBODY, 1L);
+        } else {
+            CURL_SETOPT(CURLOPT_CUSTOMREQUEST, c->method);
+        }
+    }
     if (c->http_proxy && c->http_proxy[0])
         CURL_SETOPT(CURLOPT_PROXY, c->http_proxy);

@@ -1672,7 +1725,7 @@ static int libcurl_read(URLContext *h, unsigned char *buf, int size)
         }

         if (c->status) {
-            if (c->seekable && is_recoverable(c->curl_status))
+            if (c->seekable && !c->no_body && is_recoverable(c->curl_status))
                 return retry_request_locked(h, nonblock);
             if (c->status == AVERROR_EOF && c->icy_block_len >= 0)
                 av_log(h, AV_LOG_WARNING,
@@ -1792,6 +1845,7 @@ static int libcurl_get_short_seek(URLContext *h)
 #define E AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
     { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
+    { "method", "Override the HTTP method used for the request", OFFSET(method), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
     { "referer", "override Referer header", OFFSET(referer), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
     { "headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
     { "http_proxy", "set HTTP proxy to tunnel through", OFFSET(http_proxy), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },