aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2019-02-16 12:40:41 -0800
committerGarrett D'Amore <garrett@damore.org>2019-02-16 19:22:27 -0800
commit60231f0600461a9593a8f876518874866df3387a (patch)
tree1a91f0b65b1ad2d5b995a3db21639f4bf7032066 /src/core
parent5cf750697624d4fd63cfe26921209d7c30e1a2d2 (diff)
downloadnng-60231f0600461a9593a8f876518874866df3387a.tar.gz
nng-60231f0600461a9593a8f876518874866df3387a.tar.bz2
nng-60231f0600461a9593a8f876518874866df3387a.zip
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.
Diffstat (limited to 'src/core')
-rw-r--r--src/core/url.c49
-rw-r--r--src/core/url.h2
2 files changed, 50 insertions, 1 deletions
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. <info@staysail.tech>
+// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// 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