From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 626EB4BE97 for ; Thu, 22 May 2025 18:38:52 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 6C37468DD80; Thu, 22 May 2025 21:38:47 +0300 (EEST) Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id EE4E768DD10 for ; Thu, 22 May 2025 21:38:40 +0300 (EEST) Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-3a366843fa6so3790731f8f.1 for ; Thu, 22 May 2025 11:38:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747939120; x=1748543920; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=LI8GJd5RmJrR5fyN3fW1esMhDPG5rmB+J3irg/Qj8eY=; b=envg/QhVYEcT/X5C9lhSpU80tX7/xt6YXHXk2wr7VNYfymx6xz8Q4Ek6WwRLlVgadq iFs5d+/m3nF3zJ8LnJUcl139ApZws2Y19OVC8ELwuQ/8MzjXXLC5hvxwxXFkegIHgUJx GK7mZokIPdnKkF1Ey4JPGwBsCgdgYe0gyC+d00BPuv8TV+GFKo2ltMjyAV7ES9X3yRUJ pUZpDOqHqKWZpuzGSoqcnRe3aEIf0ShA8zZCb93rSm2+WY7Ux3gF7qcfsO87Co/50jRk aWgd520IPjFgfUGarrKnzA2AekqCCUQvUyZ/hTFE88L6zjDfTRMMO8mH3XgVQTod97ti wTQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747939120; x=1748543920; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LI8GJd5RmJrR5fyN3fW1esMhDPG5rmB+J3irg/Qj8eY=; b=PZVPEsQAbgVG3MXd5PSgkPORxIUSNsnYJtSPLa9Ngoqg/9/TFLFH35ktr6ggtUICba o4XPcGBm5c5MW8PCJhDa9cbYw/vvxYLS5trmtfz1WhUmwK3RLWybmAsSY8pNsKiDqvXM cAOb8mQEHRmLP5ZKSBH8VoegOWzWe+Z8FdleQvO9wn1bsrK0dEXaGEbNAkZPtPXIKkBe dYp0rfASi79TiIQxlI5spGGodAelCXJnf+ladtR5AviJvi6gKKc2D8BslsB2DeV6//C2 npXu0GfXhxBACkVkhzCQtBmjHcT78KxtgDw1BulUc3j12x4F7lGMZK/evAaJDgCWZAAm Cixw== X-Gm-Message-State: AOJu0YzxVI9FH7mkxRrF2uUsyl/FY2wSQsndqiIymaMtuP+qBZjkzfLd EtxKv0ab0XxfA2CFuXgNhnhvyfYO5jLbRwwNfTVSeiUujwzwKSlBoRkNC+bDRg== X-Gm-Gg: ASbGnctghhdhrFL1y+U2dDsGRcfuQQGcgEgYwBbGAeNVpWa/EEncHTVi/YQxyjpFv6r WSYPJjROQDgTZNrsjUE4PlouajqpWZBOsZe3M8JqDlJRKhCdW/UV5IkkGYUFPsYsARqaR1EGfDR rREtyErTu7/zieeuisyjNGgnxtPkalPWoPSRZ2A3XSEEd3pRtFLOyE/jl1vi5hg9mZWQMPacmse JB+zdMw3ul7wR+P1phPnP8x0DhaJ870rxhLyaly8rxjI81rj+vsGFRstA5TVOvVi6pg6EWCz60C 6vIddqtQ4rVc3cjpbPKQNeH8YgUYFzL2XT/w6RVN3ol/k9pwr1Mq6hPaHRvfdFWWRQziDaKWCxX VrSLFRct4PU9FU2ZMtnqkINTRj19R3phgB01rRwnLthGbS0BLFZ1QcHVVhQl2tAPuwMw= X-Google-Smtp-Source: AGHT+IGNh/kbXXTDB23dcLGt21csi5U/fLLzdnpcztHna7KDnTaZeOXXzjkp3+suMkJ2uTexqjEd4g== X-Received: by 2002:a5d:5f46:0:b0:3a0:b816:5a44 with SMTP id ffacd0b85a97d-3a35fe964dcmr23377118f8f.35.1747939120027; Thu, 22 May 2025 11:38:40 -0700 (PDT) Received: from ArmedBeast.fritz.box (p200300cccf0693000d6ef30d9e6b5c39.dip0.t-ipconnect.de. [2003:cc:cf06:9300:d6e:f30d:9e6b:5c39]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a3620dbc6asm22624268f8f.88.2025.05.22.11.38.39 for (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 22 May 2025 11:38:39 -0700 (PDT) From: Marvin Scholz To: ffmpeg-devel@ffmpeg.org Date: Thu, 22 May 2025 20:38:32 +0200 Message-Id: <20250522183832.10120-1-epirat07@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avformat/http: Handle IPv6 Zone ID in hostname X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: When using a literal IPv6 address as hostname, it can contain a Zone ID especially in the case of link-local addresses. Sending this to the server in the Host header is not useful to the server and in some cases servers refuse such requests. To prevent any such issues, strip the Zone ID from the address if it's an IPv6 address. This also removes it for the Cookies lookup. Based on a patch by: Daniel N Pettersson --- libavformat/http.c | 60 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/libavformat/http.c b/libavformat/http.c index f7b2a8a029..3bde616b43 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -24,6 +24,7 @@ #include "config.h" #include "config_components.h" +#include #include #if CONFIG_ZLIB #include @@ -209,6 +210,63 @@ void ff_http_init_auth_state(URLContext *dest, const URLContext *src) sizeof(HTTPAuthState)); } +static bool host_is_numeric_ipv6(const char *host) +{ + bool res = false; +#if defined(AF_INET6) + struct addrinfo hints = { .ai_flags = AI_NUMERICHOST }, *ai; + if (getaddrinfo(host, NULL, &hints, &ai) == 0) { + if (ai->ai_family == AF_INET6) + res = true; + freeaddrinfo(ai); + } +#else + // Just guess based on if the host contains a ':' + if (strchr(host, ':') != NULL) + res = true; +#endif + return res; +} + +/** + * Copy the normalized host to the given buffer + * + * If the host is a normal hostname, this just returns + * host:port. However in case of an IPv6 address, it + * ensures proper escaping with [] and removes the + * zone identifier, if any, making the return suitable + * for example for use in the HTTP Host header. + */ +static unsigned copy_normalized_host(char *out, unsigned size, + const char *host, const int port) +{ + AVBPrint bp; + av_bprint_init_for_buffer(&bp, out, size); + + if (host_is_numeric_ipv6(host)) { + // This is an IPv6 address, so we need to strip the Zone ID, + // if any. + // While technically we could have percent encoding even in + // the Zone ID, this doesn't seem to be a relevant case in + // the real world on any platform. + char *percent = strrchr(host, '%'); + if (percent) { + int len = (percent - host); + av_bprintf(&bp, "[%.*s]", len, host); + } else { + av_bprintf(&bp, "[%s]", host); + } + } else { + // Host is not an IPv6 address, so just use as-is + av_bprintf(&bp, "%s", host); + } + + if (port >= 0) + av_bprintf(&bp, ":%d", port); + + return bp.len; +} + static int http_open_cnx_internal(URLContext *h, AVDictionary **options) { const char *path, *proxy_path, *lower_proto = "tcp", *local_path; @@ -224,7 +282,7 @@ static int http_open_cnx_internal(URLContext *h, AVDictionary **options) av_url_split(proto, sizeof(proto), auth, sizeof(auth), hostname, sizeof(hostname), &port, path1, sizeof(path1), s->location); - ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL); + copy_normalized_host(hoststr, sizeof(hoststr), hostname, port); env_http_proxy = getenv_utf8("http_proxy"); proxy_path = s->http_proxy ? s->http_proxy : env_http_proxy; -- 2.39.5 (Apple Git-154) _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".