Commit 3d34541443 for ffmpeg
commit 3d34541443dca0c851d96517ec8073c85d3417f3
Author: Jun Zhao <barryjzhao@tencent.com>
Date: Tue Nov 17 08:21:02 2026 +0800
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)). Return AVERROR_EOF directly from
http_read_stream() for client HEAD requests so resource-size metadata
(s->filesize / AVSEEK_SIZE) is preserved while body reading is skipped.
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 <barryjzhao@tencent.com>
diff --git a/libavformat/http.c b/libavformat/http.c
index 2a4481da59..786201c4ae 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -2100,15 +2100,19 @@ static int http_read_stream(URLContext *h, uint8_t *buf, int size)
if (s->end_of_resource)
return AVERROR_EOF;
- if (!s->hd)
- return s->off < requested_end(s) ? AVERROR(EIO) : AVERROR_EOF;
-
- if (s->end_chunked_post && !s->end_header) {
+ if (s->hd && s->end_chunked_post && !s->end_header) {
err = http_read_header(h);
if (err < 0)
return err;
}
+ /* A HEAD response describes the resource without carrying a body. */
+ if (!s->is_connected_server && s->method && !av_strcasecmp(s->method, "HEAD"))
+ return AVERROR_EOF;
+
+ if (!s->hd)
+ return s->off < requested_end(s) ? AVERROR(EIO) : AVERROR_EOF;
+
#if CONFIG_ZLIB
if (s->compressed)
return http_buf_read_compressed(h, buf, size);
@@ -2635,10 +2639,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,