From ca6cfe359fa55a5a7f4b6ae73500ffd98e6ee968 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Mon, 18 Nov 2024 01:17:24 -0800 Subject: URL refactor part 1. This eliminates most (but not all) of the dynamic allocations associated with URL objects. A number of convenience fields on the URL are removed, but we are able to use common buffer for most of the details. --- src/supplemental/http/http_msg.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/supplemental/http/http_msg.c') diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c index 28c89ec7..a2ab218f 100644 --- a/src/supplemental/http/http_msg.c +++ b/src/supplemental/http/http_msg.c @@ -617,8 +617,13 @@ nni_http_req_alloc(nni_http_req **reqp, const nni_url *url) req->uri = NULL; if (url != NULL) { const char *host; + char host_buf[264]; // 256 + 8 for port int rv; - if ((req->uri = nni_strdup(url->u_requri)) == NULL) { + rv = nni_asprintf(&req->uri, "%s%s%s%s%s", url->u_path, + url->u_query ? "?" : "", url->u_query ? url->u_query : "", + url->u_fragment ? "#" : "", + url->u_fragment ? url->u_fragment : ""); + if (rv != 0) { NNI_FREE_STRUCT(req); return (NNG_ENOMEM); } @@ -628,7 +633,14 @@ nni_http_req_alloc(nni_http_req **reqp, const nni_url *url) if (nni_url_default_port(url->u_scheme) == url->u_port) { host = url->u_hostname; } else { - host = url->u_host; + if (strchr(url->u_hostname, ':')) { + snprintf(host_buf, sizeof(host_buf), "[%s]:%u", + url->u_hostname, url->u_port); + } else { + snprintf(host_buf, sizeof(host_buf), "%s:%u", + url->u_hostname, url->u_port); + } + host = host_buf; } if ((rv = nni_http_req_add_header(req, "Host", host)) != 0) { nni_http_req_free(req); -- cgit v1.2.3-70-g09d2