From 60231f0600461a9593a8f876518874866df3387a Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 16 Feb 2019 12:40:41 -0800 Subject: fixes #879 Desire NNG_OPT_TCP_BOUND_PORT We also have made some support changes, including new APIs for printing URLs, and some improvements to the NNG_OPT_URL to make use of this new property. --- src/core/url.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- src/core/url.h | 2 ++ 2 files changed, 50 insertions(+), 1 deletion(-) (limited to 'src/core') diff --git a/src/core/url.c b/src/core/url.c index b2cf77f8..2f1e3a78 100644 --- a/src/core/url.c +++ b/src/core/url.c @@ -1,5 +1,5 @@ // -// Copyright 2018 Staysail Systems, Inc. +// Copyright 2019 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a @@ -491,6 +491,53 @@ nni_url_free(nni_url *url) } } +int +nni_url_asprintf(char **str, const nni_url *url) +{ + const char *scheme = url->u_scheme; + const char *port = url->u_port; + const char *host = url->u_hostname; + const char *hostob = ""; + const char *hostcb = ""; + + if ((strcmp(scheme, "ipc") == 0) || (strcmp(scheme, "inproc") == 0)) { + return (nni_asprintf(str, "%s://%s", scheme, url->u_path)); + } + + if (port != NULL) { + if ((strlen(port) == 0) || + (strcmp(nni_url_default_port(scheme), port) == 0)) { + port = NULL; + } + } + if (strcmp(host, "*") == 0) { + host = ""; + } + if (strchr(host, ':') != 0) { + hostob = "["; + hostcb = "]"; + } + return (nni_asprintf(str, "%s://%s%s%s%s%s%s", scheme, hostob, host, + hostcb, port != NULL ? ":" : "", port != NULL ? port : "", + url->u_requri != NULL ? url->u_requri : "")); +} + +// 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. +int +nni_url_asprintf_port(char **str, const nni_url *url, int port) +{ + char portstr[16]; + nni_url myurl = *url; + + if (port > 0) { + (void) snprintf(portstr, sizeof(portstr), "%d", port); + myurl.u_port = portstr; + } + return (nni_url_asprintf(str, &myurl)); +} + #define URL_COPYSTR(d, s) ((s != NULL) && ((d = nni_strdup(s)) == NULL)) int diff --git a/src/core/url.h b/src/core/url.h index 2358f6ba..afb8a882 100644 --- a/src/core/url.h +++ b/src/core/url.h @@ -17,5 +17,7 @@ extern int nni_url_parse(nni_url **, const char *path); extern void nni_url_free(nni_url *); extern int nni_url_clone(nni_url **, const nni_url *); extern const char *nni_url_default_port(const char *); +extern int nni_url_asprintf(char **, const nni_url *); +extern int nni_url_asprintf_port(char **, const nni_url *, int); #endif // CORE_URL_H -- cgit v1.2.3-70-g09d2