diff options
| author | Garrett D'Amore <garrett@damore.org> | 2024-11-17 21:46:14 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2024-11-17 22:14:07 -0800 |
| commit | 6333c9cefb847231397128bb7b99f0055acd1632 (patch) | |
| tree | 8bbcd814a88a29d373e68ae3481d76482f75a46a | |
| parent | 85aff44e00e836eda618d4f1cf013bce38b3fd44 (diff) | |
| download | nng-6333c9cefb847231397128bb7b99f0055acd1632.tar.gz nng-6333c9cefb847231397128bb7b99f0055acd1632.tar.bz2 nng-6333c9cefb847231397128bb7b99f0055acd1632.zip | |
nni_url_sprintf
| -rw-r--r-- | src/core/url.c | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/core/url.c b/src/core/url.c index 9bdcfd23..bae5a491 100644 --- a/src/core/url.c +++ b/src/core/url.c @@ -568,7 +568,7 @@ nni_url_free(nni_url *url) } int -nni_url_asprintf(char **str, const nni_url *url) +nni_url_sprintf(char *str, size_t size, const nni_url *url) { const char *scheme = url->u_scheme; const char *host = url->u_hostname; @@ -579,7 +579,7 @@ nni_url_asprintf(char **str, const nni_url *url) if ((strcmp(scheme, "ipc") == 0) || (strcmp(scheme, "inproc") == 0) || (strcmp(scheme, "unix") == 0) || (strcmp(scheme, "abstract") == 0)) { - return (nni_asprintf(str, "%s://%s", scheme, url->u_path)); + return (snprintf(str, size, "%s://%s", scheme, url->u_path)); } if (url->u_port == nni_url_default_port(scheme)) { @@ -598,10 +598,25 @@ nni_url_asprintf(char **str, const nni_url *url) } else { portstr[0] = 0; } - return (nni_asprintf(str, "%s://%s%s%s%s%s", scheme, hostob, host, + return (snprintf(str, size, "%s://%s%s%s%s%s", scheme, hostob, host, hostcb, portstr, url->u_requri != NULL ? url->u_requri : "")); } +int +nni_url_asprintf(char **str, const nni_url *url) +{ + char *result; + size_t sz; + + sz = nni_url_sprintf(NULL, 0, url) + 1; + if ((result = nni_alloc(sz)) == NULL) { + return (NNG_ENOMEM); + } + nni_url_sprintf(result, sz, url); + *str = result; + return (0); +} + // nni_url_asprintf_port is like nni_url_asprintf, but includes a port // override. If non-zero, this port number replaces the port number // in the port string. |
