PR #26102 opened by Jun Zhao (mypopydev)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26102
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26102.patch

This series resolves issue #26071, where HTTP HEAD requests and cleanly
closed CONNECT tunnels were incorrectly treated as truncated responses.

In libavformat/http.c, HEAD replies legitimately carry no message body despite
declaring Content-Length or Content-Range. We set filesize to 0 and reset
chunksize to UINT64_MAX for HEAD requests so a clean EOF is returned instead of
AVERROR(EIO), while keeping truncation detection intact for genuine GET
responses. For httpproxy, we introduce a dedicated read callback that flushes
buffered handshake residue and passes EOF through directly, avoiding spurious
"Stream ends prematurely" errors caused by reusing HTTP body-length checks on
unbounded CONNECT tunnels.

A self-contained FATE regression test is added via a minimal loopback server in
libavformat/tests/http_nobody.c to verify HEAD EOF handling, GET truncation
checks, CONNECT tunnel EOF propagation, and handshake residue forwarding.


>From 29617d964c559e33dd97a6f41d1b7260f801aa1b Mon Sep 17 00:00:00 2001
From: Jun Zhao <[email protected]>
Date: Mon, 16 Nov 2026 20:24:04 +0800
Subject: [PATCH 1/2] avformat/http: do not treat HEAD replies and CONNECT
 tunnels as sized bodies

A HEAD response carries no message body even if Content-Length or
Content-Range is present. Because http_buf_read() expected the declared
body size, receiving EOF at offset 0 was treated as premature stream
truncation (AVERROR(EIO)). For HEAD requests in http_connect(), set
filesize to 0 and reset chunksize to UINT64_MAX so a clean EOF is
returned instead. Genuinely truncated GET responses remain unaffected.

Additionally, ff_httpproxy_protocol reused http_buf_read as its
.url_read, applying HTTP message body length checks to unbounded CONNECT
tunnels. Because a CONNECT 200 reply has no Content-Length, filesize and
chunksize remained UINT64_MAX, spuriously logging "Stream ends
prematurely... should be 18446744073709551615" upon tunnel closure.
Introduce http_proxy_read() to forward any data buffered during the
CONNECT handshake and pass through subsequent reads and EOF directly
from the underlying transport.

Fixes: https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/26071

Signed-off-by: Jun Zhao <[email protected]>
---
 libavformat/http.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/libavformat/http.c b/libavformat/http.c
index 2a4481da591..f9348f3e2fe 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -1922,6 +1922,13 @@ static int http_connect(URLContext *h, const char *path, 
const char *local_path,
     if (err < 0)
         goto done;
 
+    /* A HEAD response carries no body, even when Content-Length describes
+     * what a GET would have returned. Do not try to read that many bytes. */
+    if (!av_strcasecmp(method, "HEAD")) {
+        s->filesize  = 0;
+        s->chunksize = UINT64_MAX;
+    }
+
     s->nb_requests++;
     s->sum_latency += latency;
     s->max_latency = FFMAX(s->max_latency, latency);
@@ -2635,10 +2642,33 @@ static int http_proxy_write(URLContext *h, const 
uint8_t *buf, int size)
     return ffurl_write(s->hd, buf, size);
 }
 
+/* A CONNECT tunnel is an unbounded byte stream, so the Content-Length,
+ * chunked and premature-end handling in http_buf_read() does not apply.
+ * Forward any bytes buffered together with the CONNECT reply first, then
+ * pass reads (and EOF) through from the underlying connection. */
+static int http_proxy_read(URLContext *h, uint8_t *buf, int size)
+{
+    HTTPContext *s = h->priv_data;
+    int len;
+
+    if (!s->hd)
+        return AVERROR(EIO);
+
+    len = s->buf_end - s->buf_ptr;
+    if (len > 0) {
+        if (len > size)
+            len = size;
+        memcpy(buf, s->buf_ptr, len);
+        s->buf_ptr += len;
+        return len;
+    }
+    return ffurl_read(s->hd, buf, size);
+}
+
 const URLProtocol ff_httpproxy_protocol = {
     .name                = "httpproxy",
     .url_open            = http_proxy_open,
-    .url_read            = http_buf_read,
+    .url_read            = http_proxy_read,
     .url_write           = http_proxy_write,
     .url_close           = http_proxy_close,
     .url_get_file_handle = http_get_file_handle,
-- 
2.54.0


>From 4c02d4d41c6acca21d6bb16a2dcc5ead1073d8cb Mon Sep 17 00:00:00 2001
From: Jun Zhao <[email protected]>
Date: Mon, 16 Nov 2026 20:24:04 +0800
Subject: [PATCH 2/2] avformat/tests: add FATE coverage for HEAD no-body and
 CONNECT tunnel EOF

Add a self-contained loopback test exercising the URL protocol stack
via ffurl_open_whitelist() and ffurl_read(). A minimal HTTP/CONNECT
server runs in a pthread on an ephemeral port without requiring external
network access or fate-suite samples.

Adds two FATE targets:
  fate-http-head-no-body: verifies that a HEAD reply with Content-Length
  yields a clean EOF, a normal GET reads the full body, and a truncated
  GET is still flagged as an I/O error.

  fate-httpproxy-tunnel-eof: verifies that a cleanly closed CONNECT
  tunnel propagates EOF and that tunnel bytes buffered during the
  CONNECT handshake are properly forwarded.

The test program builds with CONFIG_HTTP_PROTOCOL or
CONFIG_HTTPPROXY_PROTOCOL, and skips cleanly when pthreads are unavailable.

Signed-off-by: Jun Zhao <[email protected]>
---
 libavformat/Makefile            |   4 +
 libavformat/tests/http_nobody.c | 355 ++++++++++++++++++++++++++++++++
 tests/fate/libavformat.mak      |  14 ++
 3 files changed, 373 insertions(+)
 create mode 100644 libavformat/tests/http_nobody.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 891d7f82d9a..8c1b93e12a4 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -791,6 +791,10 @@ FIFO-MUXER-TESTPROGS-$(CONFIG_NETWORK)   += fifo_muxer
 TESTPROGS-$(CONFIG_FIFO_MUXER)           += $(FIFO-MUXER-TESTPROGS-yes)
 TESTPROGS-$(CONFIG_FFRTMPCRYPT_PROTOCOL) += rtmpdh
 TESTPROGS-$(CONFIG_HTTP_PROTOCOL)        += http
+# http_nobody covers both the http and httpproxy read paths.
+HTTP_NOBODY_TESTPROG-$(CONFIG_HTTP_PROTOCOL)      = http_nobody
+HTTP_NOBODY_TESTPROG-$(CONFIG_HTTPPROXY_PROTOCOL) = http_nobody
+TESTPROGS                                       += $(HTTP_NOBODY_TESTPROG-yes)
 TESTPROGS-$(CONFIG_NETWORK)              += noproxy
 TESTPROGS-$(CONFIG_SRTP)                 += srtp
 TESTPROGS-$(CONFIG_IMF_DEMUXER)          += imf
diff --git a/libavformat/tests/http_nobody.c b/libavformat/tests/http_nobody.c
new file mode 100644
index 00000000000..44bfcf34675
--- /dev/null
+++ b/libavformat/tests/http_nobody.c
@@ -0,0 +1,355 @@
+/*
+ * Copyright (c) 2026 Jun Zhao
+ *
+ * Regression tests for HTTP / httpproxy premature-end handling (issue #26071).
+ *
+ * Two independent behaviours are covered, both through the real URL protocol
+ * stack rather than by poking at internals:
+ *   - A HEAD response has no body even when it carries a Content-Length
+ *     describing what a GET would have returned. A clean EOF must not be
+ *     reported as "Stream ends prematurely" / AVERROR(EIO).
+ *   - A CONNECT tunnel is an unbounded stream. A clean tunnel EOF must be
+ *     passed through, and bytes that arrived together with the CONNECT reply
+ *     must still be forwarded.
+ *
+ * A minimal loopback server runs in a pthread, so the test needs no external
+ * network, no fate-suite samples and no helper script.
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "config.h"
+#include "libavutil/dict.h"
+#include "libavutil/error.h"
+#include "libavformat/avio.h"
+#include "libavformat/network.h"
+#include "libavformat/url.h"
+
+#if HAVE_PTHREADS
+#include <pthread.h>
+#endif
+
+#if HAVE_WINSOCK2_H
+#define SHUT_WRITE SD_SEND
+#define SOCKLEN int
+#else
+#define SHUT_WRITE SHUT_WR
+#define SOCKLEN socklen_t
+#endif
+
+#define BODY_SIZE 1150
+#define MARK "TUNNEL_PREFIX_BYTES_0123456789_ABCDEFGHIJ"
+
+#define GET_HEADER "HTTP/1.1 200 OK\r\n"              \
+                   "Content-Length: 1150\r\n"        \
+                   "Connection: close\r\n"           \
+                   "\r\n"
+
+/* A HEAD reply: declares a length, legitimately sends no body. */
+static const char head_reply[] =
+    "HTTP/1.1 206 Partial Content\r\n"
+    "Content-Length: 1150\r\n"
+    "Content-Range: bytes 0-1149/1150\r\n"
+    "Accept-Ranges: bytes\r\n"
+    "Connection: close\r\n"
+    "\r\n";
+
+/* A lying GET reply: declares a length, sends no body. Must still be flagged. 
*/
+static const char lie_reply[] = GET_HEADER;
+
+/* A CONNECT 200 reply with the tunnel closed immediately afterwards. */
+static const char connect_reply[] =
+    "HTTP/1.1 200 OK\r\n"
+    "connection: close\r\n"
+    "\r\n";
+
+/* CONNECT 200 header and the first tunnel bytes in ONE write, so http_getc()
+ * buffers them past the header and they must be forwarded as residue. */
+static const char coalesced_reply[] =
+    "HTTP/1.1 200 OK\r\n"
+    "\r\n"
+    MARK;
+
+static char body_reply[sizeof(GET_HEADER) - 1 + BODY_SIZE];
+static int body_reply_len;
+static int failures;
+
+#define CHECK(cond, msg) do {                       \
+    if (cond) {                                    \
+        printf("PASS  %s\n", msg);                 \
+    } else {                                       \
+        printf("FAIL  %s\n", msg);                 \
+        failures++;                                \
+    }                                              \
+} while (0)
+
+typedef struct TestServer {
+    int fd;
+    int port;
+    const char *reply;
+    int reply_len;
+    int failed;
+#if HAVE_PTHREADS
+    pthread_t thread;
+#endif
+} TestServer;
+
+typedef struct Result {
+    int open_ret;
+    int read_ret;
+    int server_failed;
+    uint8_t buf[4096];
+} Result;
+
+static void set_timeouts(int fd)
+{
+#if HAVE_WINSOCK2_H
+    DWORD ms = 5000;
+    setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&ms, sizeof(ms));
+    setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const char *)&ms, sizeof(ms));
+#else
+    struct timeval tv = { 5, 0 };
+    setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
+    setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
+#endif
+}
+
+static void *server_main(void *arg)
+{
+    TestServer *s = arg;
+    int c = accept(s->fd, NULL, NULL);
+    char req[4096];
+    int n = 0;
+
+    if (c < 0) {
+        s->failed = 1;
+        return NULL;
+    }
+    set_timeouts(c);
+
+    /* Drain the request (GET/HEAD/CONNECT) before replying. */
+    req[0] = 0;
+    while (n < (int)sizeof(req) - 1 && !strstr(req, "\r\n\r\n")) {
+        int r = recv(c, req + n, sizeof(req) - 1 - n, 0);
+        if (r <= 0)
+            break;
+        n += r;
+        req[n] = 0;
+    }
+
+    if (send(c, s->reply, s->reply_len, 0) != s->reply_len)
+        s->failed = 1;
+    shutdown(c, SHUT_WRITE);
+    closesocket(c);
+    return NULL;
+}
+
+static int server_start(TestServer *s, const char *reply, int reply_len)
+{
+    struct sockaddr_in addr;
+    SOCKLEN alen = sizeof(addr);
+    int on = 1;
+
+    memset(s, 0, sizeof(*s));
+    s->reply     = reply;
+    s->reply_len = reply_len;
+
+    s->fd = ff_socket(AF_INET, SOCK_STREAM, 0, NULL);
+    if (s->fd < 0)
+        return -1;
+    setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+
+    memset(&addr, 0, sizeof(addr));
+    addr.sin_family      = AF_INET;
+    addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+    addr.sin_port        = 0;             /* let the kernel pick a free port */
+
+    if (bind(s->fd, (const struct sockaddr *)&addr, sizeof(addr)) < 0 ||
+        listen(s->fd, 1) < 0 ||
+        getsockname(s->fd, (struct sockaddr *)&addr, &alen) < 0) {
+        closesocket(s->fd);
+        return -1;
+    }
+    s->port = ntohs(addr.sin_port);
+
+#if HAVE_PTHREADS
+    if (pthread_create(&s->thread, NULL, server_main, s)) {
+        closesocket(s->fd);
+        return -1;
+    }
+#endif
+    return 0;
+}
+
+static void server_stop(TestServer *s)
+{
+#if HAVE_PTHREADS
+    pthread_join(s->thread, NULL);
+#endif
+    closesocket(s->fd);
+}
+
+static void exchange(Result *r, const char *reply, int reply_len,
+                     const char *url_fmt, const char *method)
+{
+    TestServer srv;
+    URLContext *uc = NULL;
+    AVDictionary *opts = NULL;
+    char url[256];
+
+    memset(r, 0, sizeof(*r));
+    if (server_start(&srv, reply, reply_len) < 0) {
+        r->open_ret = AVERROR(EIO);
+        return;
+    }
+
+    snprintf(url, sizeof(url), url_fmt, srv.port);
+    if (method)
+        av_dict_set(&opts, "method", method, 0);
+
+    r->open_ret = ffurl_open_whitelist(&uc, url, AVIO_FLAG_READ, NULL, &opts,
+                                       "http,httpproxy,tcp", NULL, NULL);
+    av_dict_free(&opts);
+    if (r->open_ret >= 0) {
+        r->read_ret = ffurl_read(uc, r->buf, sizeof(r->buf));
+        ffurl_closep(&uc);
+    }
+
+    server_stop(&srv);
+    r->server_failed = srv.failed;
+}
+
+static void test_head(void)
+{
+    Result r;
+
+    exchange(&r, head_reply, sizeof(head_reply) - 1,
+             "http://127.0.0.1:%d/x";, "HEAD");
+    printf("head: open=%d read=%d server_failed=%d\n",
+           r.open_ret, r.read_ret, r.server_failed);
+
+    CHECK(r.open_ret >= 0 && !r.server_failed,
+          "HEAD request exchanges headers successfully");
+    CHECK(r.read_ret == AVERROR_EOF,
+          "HEAD with Content-Length but no body yields a clean EOF");
+    CHECK(r.read_ret != AVERROR(EIO),
+          "HEAD no-body is not misreported as an I/O error");
+}
+
+static void test_get_body(void)
+{
+    Result r;
+
+    exchange(&r, body_reply, body_reply_len,
+             "http://127.0.0.1:%d/x";, NULL);
+    printf("get_body: open=%d read=%d server_failed=%d\n",
+           r.open_ret, r.read_ret, r.server_failed);
+
+    CHECK(r.open_ret >= 0 && !r.server_failed,
+          "GET request exchanges headers successfully");
+    CHECK(r.read_ret == BODY_SIZE,
+          "GET reads the full 1150-byte body");
+    CHECK(r.read_ret == BODY_SIZE && r.buf[0] == 'X' &&
+          r.buf[BODY_SIZE - 1] == 'X',
+          "GET body contents are intact");
+}
+
+static void test_get_lie(void)
+{
+    Result r;
+
+    exchange(&r, lie_reply, sizeof(lie_reply) - 1,
+             "http://127.0.0.1:%d/x";, NULL);
+    printf("get_lie: open=%d read=%d server_failed=%d\n",
+           r.open_ret, r.read_ret, r.server_failed);
+
+    CHECK(r.open_ret >= 0 && !r.server_failed,
+          "lying GET request exchanges headers successfully");
+    CHECK(r.read_ret == AVERROR(EIO),
+          "GET with declared length but no body is still flagged");
+}
+
+static void test_proxy_close(void)
+{
+    Result r;
+
+    exchange(&r, connect_reply, sizeof(connect_reply) - 1,
+             "httpproxy://127.0.0.1:%d/example.com:443", NULL);
+    printf("proxy_close: open=%d read=%d server_failed=%d\n",
+           r.open_ret, r.read_ret, r.server_failed);
+
+    CHECK(r.open_ret >= 0 && !r.server_failed,
+          "httpproxy CONNECT tunnel opens");
+    CHECK(r.read_ret == AVERROR_EOF,
+          "clean CONNECT tunnel EOF is passed through");
+    CHECK(r.read_ret != AVERROR(EIO),
+          "clean CONNECT tunnel EOF is not misreported as an I/O error");
+}
+
+static void test_proxy_residue(void)
+{
+    Result r;
+    const int mark_len = (int)sizeof(MARK) - 1;
+
+    exchange(&r, coalesced_reply, sizeof(coalesced_reply) - 1,
+             "httpproxy://127.0.0.1:%d/example.com:443", NULL);
+    printf("proxy_residue: open=%d read=%d server_failed=%d\n",
+           r.open_ret, r.read_ret, r.server_failed);
+
+    CHECK(r.open_ret >= 0 && !r.server_failed,
+          "coalesced CONNECT reply opens");
+    CHECK(r.read_ret == mark_len && !memcmp(r.buf, MARK, mark_len),
+          "tunnel bytes buffered with the CONNECT reply are forwarded");
+}
+
+int main(int argc, char **argv)
+{
+#if HAVE_PTHREADS
+    const char *group = argc > 1 ? argv[1] : "all";
+    int run_http  = !strcmp(group, "all") || !strcmp(group, "http");
+    int run_proxy = !strcmp(group, "all") || !strcmp(group, "proxy");
+
+    memcpy(body_reply, GET_HEADER, sizeof(GET_HEADER) - 1);
+    memset(body_reply + sizeof(GET_HEADER) - 1, 'X', BODY_SIZE);
+    body_reply_len = sizeof(body_reply);
+
+    if (ff_network_init() < 0)
+        return 1;
+
+    if (run_http) {
+        test_head();
+        test_get_body();
+        test_get_lie();
+    }
+    if (run_proxy) {
+        test_proxy_close();
+        test_proxy_residue();
+    }
+
+    ff_network_close();
+    printf("%s: %d failure(s)\n", group, failures);
+    return failures != 0;
+#else
+    (void)argc;
+    (void)argv;
+    printf("SKIP  no pthreads, loopback server unavailable\n");
+    return 0;
+#endif
+}
diff --git a/tests/fate/libavformat.mak b/tests/fate/libavformat.mak
index 70d5439437f..d9c5028c71a 100644
--- a/tests/fate/libavformat.mak
+++ b/tests/fate/libavformat.mak
@@ -60,6 +60,20 @@ FATE_LIBAVFORMAT-$(CONFIG_NETWORK) += fate-noproxy
 fate-noproxy: libavformat/tests/noproxy$(EXESUF)
 fate-noproxy: CMD = run libavformat/tests/noproxy$(EXESUF)
 
+# HEAD responses have no body, and CONNECT tunnels are unbounded streams.
+# Self-contained loopback test; it needs no samples and no external network.
+FATE_HTTP_NOBODY-$(CONFIG_HTTP_PROTOCOL)      += fate-http-head-no-body
+FATE_HTTP_NOBODY-$(CONFIG_HTTPPROXY_PROTOCOL) += fate-httpproxy-tunnel-eof
+FATE_LIBAVFORMAT-$(HAVE_PTHREADS)             += $(FATE_HTTP_NOBODY-yes)
+
+fate-http-head-no-body: libavformat/tests/http_nobody$(EXESUF)
+fate-http-head-no-body: CMD = run libavformat/tests/http_nobody$(EXESUF) http
+fate-http-head-no-body: CMP = null
+
+fate-httpproxy-tunnel-eof: libavformat/tests/http_nobody$(EXESUF)
+fate-httpproxy-tunnel-eof: CMD = run libavformat/tests/http_nobody$(EXESUF) 
proxy
+fate-httpproxy-tunnel-eof: CMP = null
+
 FATE_LIBAVFORMAT-$(CONFIG_FFRTMPCRYPT_PROTOCOL) += fate-rtmpdh
 fate-rtmpdh: libavformat/tests/rtmpdh$(EXESUF)
 fate-rtmpdh: CMD = run libavformat/tests/rtmpdh$(EXESUF)
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to