avformat: Add ff_format_check_set_url()

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
This commit is contained in:
Michael Niedermayer
2026-02-26 03:05:36 +01:00
parent e24b9820b4
commit 394a7ff8ce
2 changed files with 42 additions and 0 deletions

View File

@@ -41,6 +41,7 @@
#include "demux.h"
#include "mux.h"
#include "internal.h"
#include "url.h"
void ff_free_stream(AVStream **pst)
{
@@ -868,6 +869,37 @@ void ff_format_set_url(AVFormatContext *s, char *url)
s->url = url;
}
int ff_format_check_set_url(AVFormatContext *s, const char *url)
{
URLComponents uc;
av_assert0(url);
char proto[64];
int ret = ff_url_decompose(&uc, url, NULL);
if (ret < 0)
return ret;
av_strlcpy(proto, uc.scheme, FFMIN(sizeof(proto), uc.url_component_end_scheme - uc.scheme));
if (s->protocol_whitelist && av_match_list(proto, s->protocol_whitelist, ',') <= 0) {
av_log(s, AV_LOG_ERROR, "Protocol '%s' not on whitelist '%s'!\n", proto, s->protocol_whitelist);
return AVERROR(EINVAL);
}
if (s->protocol_blacklist && av_match_list(proto, s->protocol_blacklist, ',') > 0) {
av_log(s, AV_LOG_ERROR, "Protocol '%s' on blacklist '%s'!\n", proto, s->protocol_blacklist);
return AVERROR(EINVAL);
}
url = av_strdup(url);
if (!url)
return AVERROR(ENOMEM);
av_freep(&s->url);
s->url = url;
return 0;
}
int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
{
int ret = 0;

View File

@@ -630,6 +630,16 @@ int ff_bprint_to_codecpar_extradata(AVCodecParameters *par, struct AVBPrint *buf
*/
void ff_format_set_url(AVFormatContext *s, char *url);
/**
* Set AVFormatContext url field to a av_strdup of the provided pointer. The pointer must
* point to a valid string. The existing url field is freed if necessary.
*
* Checks protocol_whitelist/blacklist
*
* @returns a AVERROR code or non negative on success
*/
int ff_format_check_set_url(AVFormatContext *s, const char *url);
/**
* Return a positive value if the given url has one of the given
* extensions, negative AVERROR on error, 0 otherwise.