From 2762ae74c53b058345660019ecce4ba41a85a050 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Tue, 26 Aug 2025 00:23:16 +0200 Subject: [PATCH] avformat/tls: use ff_parse_opts_from_query_string() to set URL parameters Note that this changes the code to work the same way as other protocols where an URL parameter can override an AVOption. Signed-off-by: Marton Balint --- doc/protocols.texi | 10 +++++++-- libavformat/tls.c | 46 +++++---------------------------------- libavformat/tls_mbedtls.c | 14 ------------ 3 files changed, 14 insertions(+), 56 deletions(-) diff --git a/doc/protocols.texi b/doc/protocols.texi index 3d9685ed6d..9f88f005b9 100644 --- a/doc/protocols.texi +++ b/doc/protocols.texi @@ -1984,8 +1984,14 @@ The required syntax for a TLS/SSL url is: tls://@var{hostname}:@var{port}[?@var{options}] @end example -The following parameters can be set via command line options -(or in code via @code{AVOption}s): +@var{options} contains a list of &-separated options of the form +@var{key}=@var{val}. Standard percent-encoding (and using the plus sign for +space) can be used to escape keys and values. + +Options can also can be specified via command line options (or in code via +@code{AVOption}s). + +The list of supported options follows. @table @option diff --git a/libavformat/tls.c b/libavformat/tls.c index bd9c05e6dc..2cf0c99a6b 100644 --- a/libavformat/tls.c +++ b/libavformat/tls.c @@ -31,41 +31,6 @@ #include "libavutil/mem.h" #include "libavutil/parseutils.h" -static int set_options(TLSShared *c, const char *uri) -{ - char buf[1024]; - const char *p = strchr(uri, '?'); - if (!p) - return 0; - - if (!c->ca_file && av_find_info_tag(buf, sizeof(buf), "cafile", p)) { - c->ca_file = av_strdup(buf); - if (!c->ca_file) - return AVERROR(ENOMEM); - } - - if (!c->verify && av_find_info_tag(buf, sizeof(buf), "verify", p)) { - char *endptr = NULL; - c->verify = strtol(buf, &endptr, 10); - if (buf == endptr) - c->verify = 1; - } - - if (!c->cert_file && av_find_info_tag(buf, sizeof(buf), "cert", p)) { - c->cert_file = av_strdup(buf); - if (!c->cert_file) - return AVERROR(ENOMEM); - } - - if (!c->key_file && av_find_info_tag(buf, sizeof(buf), "key", p)) { - c->key_file = av_strdup(buf); - if (!c->key_file) - return AVERROR(ENOMEM); - } - - return 0; -} - int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options) { int port; @@ -77,17 +42,18 @@ int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AV int use_proxy; int ret; - ret = set_options(c, uri); - if (ret < 0) - return ret; + p = strchr(uri, '?'); + if (p) { + ret = ff_parse_opts_from_query_string(c, p, 1); + if (ret < 0) + return ret; + } if (c->listen && !c->is_dtls) snprintf(opts, sizeof(opts), "?listen=1"); av_url_split(NULL, 0, NULL, 0, c->underlying_host, sizeof(c->underlying_host), &port, NULL, 0, uri); - p = strchr(uri, '?'); - if (!p) { p = opts; } else { diff --git a/libavformat/tls_mbedtls.c b/libavformat/tls_mbedtls.c index 2bcd3cca63..8aa142b9d2 100644 --- a/libavformat/tls_mbedtls.c +++ b/libavformat/tls_mbedtls.c @@ -174,17 +174,6 @@ static void handle_handshake_error(URLContext *h, int ret) } } -static void parse_options(TLSContext *tls_ctxc, const char *uri) -{ - char buf[1024]; - const char *p = strchr(uri, '?'); - if (!p) - return; - - if (!tls_ctxc->priv_key_pw && av_find_info_tag(buf, sizeof(buf), "key_password", p)) - tls_ctxc->priv_key_pw = av_strdup(buf); -} - static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **options) { TLSContext *tls_ctx = h->priv_data; @@ -192,9 +181,6 @@ static int tls_open(URLContext *h, const char *uri, int flags, AVDictionary **op uint32_t verify_res_flags; int ret; - // parse additional options - parse_options(tls_ctx, uri); - if ((ret = ff_tls_open_underlying(shr, h, uri, options)) < 0) goto fail;