Merge branch 'contrib/github_pr_15388' into 'master'

fix(esp_http_client): Fix host header for IPv6 address literal (GitHub PR)

Closes IDFGH-14640

See merge request espressif/esp-idf!37035
This commit is contained in:
Mahavir Jain 2025-02-24 16:02:27 +08:00
commit e2fc36349a

View File

@ -703,10 +703,13 @@ static char *_get_host_header(char *host, int port)
{ {
int err = 0; int err = 0;
char *host_name; char *host_name;
assert(host != NULL);
// Check if host is an IPv6 address literal that needs square brackets according to RFC3986
bool is_ipv6 = (host[0] != '[' && strchr(host, ':') != NULL);
if (port != DEFAULT_HTTP_PORT && port != DEFAULT_HTTPS_PORT) { if (port != DEFAULT_HTTP_PORT && port != DEFAULT_HTTPS_PORT) {
err = asprintf(&host_name, "%s:%d", host, port); err = asprintf(&host_name, is_ipv6 ? "[%s]:%d" : "%s:%d", host, port);
} else { } else {
err = asprintf(&host_name, "%s", host); err = asprintf(&host_name, is_ipv6 ? "[%s]" : "%s", host);
} }
if (err == -1) { if (err == -1) {
return NULL; return NULL;