aboutsummaryrefslogtreecommitdiff
path: root/src/supplemental
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-01-22 14:05:10 -0800
committerGarrett D'Amore <garrett@damore.org>2018-01-22 17:11:58 -0800
commit3d075fad7496ec126c5087d1c36ab7a4af73ce16 (patch)
treec5b5d6fe44eaa2996310683b5080de87160b9b41 /src/supplemental
parent5b1a3af7be4ae712868ae84b9a7d5a974d272b16 (diff)
downloadnng-3d075fad7496ec126c5087d1c36ab7a4af73ce16.tar.gz
nng-3d075fad7496ec126c5087d1c36ab7a4af73ce16.tar.bz2
nng-3d075fad7496ec126c5087d1c36ab7a4af73ce16.zip
fixes #219 transports should take URL structure instead of string address
This eliminates a bunch of redundant URL parsing, using the common URL logic we already have in place. While here I fixed a problem with the TLS and WSS test suites that was failing on older Ubuntu -- apparently older versions of mbedTLS were unhappy if selecting OPTIONAL verification without a validate certificate chain.
Diffstat (limited to 'src/supplemental')
-rw-r--r--src/supplemental/http/client.c15
-rw-r--r--src/supplemental/http/http.h5
-rw-r--r--src/supplemental/http/server.c47
-rw-r--r--src/supplemental/websocket/websocket.c21
-rw-r--r--src/supplemental/websocket/websocket.h4
5 files changed, 33 insertions, 59 deletions
diff --git a/src/supplemental/http/client.c b/src/supplemental/http/client.c
index 7542043d..4c54a708 100644
--- a/src/supplemental/http/client.c
+++ b/src/supplemental/http/client.c
@@ -24,7 +24,6 @@ struct nni_http_client {
bool closed;
nng_tls_config * tls;
nni_aio * connaio;
- nni_url * url;
nni_plat_tcp_ep *tep;
};
@@ -92,30 +91,21 @@ nni_http_client_fini(nni_http_client *c)
nni_tls_config_fini(c->tls);
}
#endif
- if (c->url != NULL) {
- nni_url_free(c->url);
- }
NNI_FREE_STRUCT(c);
}
int
-nni_http_client_init(nni_http_client **cp, const char *urlstr)
+nni_http_client_init(nni_http_client **cp, nni_url *url)
{
int rv;
- nni_url * url;
nni_http_client *c;
nni_aio * aio;
nni_sockaddr sa;
char * host;
char * port;
- if ((rv = nni_url_parse(&url, urlstr)) != 0) {
- return (rv);
- }
-
if (strlen(url->u_hostname) == 0) {
// We require a valid hostname.
- nni_url_free(url);
return (NNG_EADDRINVAL);
}
if ((strcmp(url->u_scheme, "http") != 0) &&
@@ -132,7 +122,6 @@ nni_http_client_init(nni_http_client **cp, const char *urlstr)
// imagines the ability to create a tcp dialer that does the
// necessary DNS lookups, etc. all asynchronously.
if ((rv = nni_aio_init(&aio, NULL, NULL)) != 0) {
- nni_url_free(url);
return (rv);
}
aio->a_addr = &sa;
@@ -143,7 +132,6 @@ nni_http_client_init(nni_http_client **cp, const char *urlstr)
rv = nni_aio_result(aio);
nni_aio_fini(aio);
if (rv != 0) {
- nni_url_free(url);
return (rv);
}
@@ -152,7 +140,6 @@ nni_http_client_init(nni_http_client **cp, const char *urlstr)
}
nni_mtx_init(&c->mtx);
nni_aio_list_init(&c->aios);
- c->url = url;
#ifdef NNG_SUPP_TLS
if ((strcmp(url->u_scheme, "https") == 0) ||
diff --git a/src/supplemental/http/http.h b/src/supplemental/http/http.h
index 08156714..93a94049 100644
--- a/src/supplemental/http/http.h
+++ b/src/supplemental/http/http.h
@@ -174,7 +174,7 @@ typedef struct nni_http_handler nni_http_handler;
// a restricted binding is required, we recommend using a URL consisting
// of an empty host name, such as http:// or https:// -- this would
// convert to binding to the default port on all interfaces on the host.
-extern int nni_http_server_init(nni_http_server **, const char *);
+extern int nni_http_server_init(nni_http_server **, nni_url *);
// nni_http_server_fini drops the reference count on the server, and
// if this was the last reference, closes down the server and frees
@@ -325,8 +325,7 @@ extern void *nni_http_handler_get_data(nni_http_handler *, unsigned);
typedef struct nni_http_client nni_http_client;
-// https vs. http; would also allow us to defer DNS lookups til later.
-extern int nni_http_client_init(nni_http_client **, const char *);
+extern int nni_http_client_init(nni_http_client **, nni_url *);
extern void nni_http_client_fini(nni_http_client *);
// nni_http_client_set_tls sets the TLS configuration. This wipes out
diff --git a/src/supplemental/http/server.c b/src/supplemental/http/server.c
index 88a3bd1a..ecb544ea 100644
--- a/src/supplemental/http/server.c
+++ b/src/supplemental/http/server.c
@@ -69,7 +69,8 @@ struct nni_http_server {
nng_tls_config * tls;
nni_aio * accaio;
nni_plat_tcp_ep *tep;
- nni_url * url;
+ char * port;
+ char * hostname;
};
int
@@ -789,9 +790,6 @@ http_server_fini(nni_http_server *s)
nni_http_handler_fini(h);
}
nni_mtx_unlock(&s->mtx);
- if (s->url != NULL) {
- nni_url_free(s->url);
- }
#ifdef NNG_SUPP_TLS
if (s->tls != NULL) {
nni_tls_config_fini(s->tls);
@@ -800,6 +798,8 @@ http_server_fini(nni_http_server *s)
nni_aio_fini(s->accaio);
nni_cv_fini(&s->cv);
nni_mtx_fini(&s->mtx);
+ nni_strfree(s->hostname);
+ nni_strfree(s->port);
NNI_FREE_STRUCT(s);
}
@@ -820,15 +820,9 @@ http_server_init(nni_http_server **serverp, nni_url *url)
{
nni_http_server *s;
int rv;
- const char * host;
const char * port;
nni_aio * aio;
- host = url->u_hostname;
- if (strlen(host) == 0) {
- host = NULL;
- }
-
port = url->u_port;
if ((strcmp(url->u_scheme, "http") != 0) &&
#ifdef NNG_SUPP_TLS
@@ -836,14 +830,11 @@ http_server_init(nni_http_server **serverp, nni_url *url)
(strcmp(url->u_scheme, "wss") != 0) &&
#endif
(strcmp(url->u_scheme, "ws") != 0)) {
- nni_url_free(url);
return (NNG_EADDRINVAL);
}
if ((s = NNI_ALLOC_STRUCT(s)) == NULL) {
- nni_url_free(url);
return (NNG_ENOMEM);
}
- s->url = url;
nni_mtx_init(&s->mtx);
nni_cv_init(&s->cv, &s->mtx);
NNI_LIST_INIT(&s->handlers, nni_http_handler, node);
@@ -852,6 +843,18 @@ http_server_init(nni_http_server **serverp, nni_url *url)
http_server_fini(s);
return (rv);
}
+
+ if ((strlen(url->u_port)) &&
+ ((s->port = nni_strdup(url->u_port)) == NULL)) {
+ http_server_fini(s);
+ return (NNG_ENOMEM);
+ }
+ if ((strlen(url->u_hostname)) &&
+ ((s->hostname = nni_strdup(url->u_hostname)) == NULL)) {
+ http_server_fini(s);
+ return (NNG_ENOMEM);
+ }
+
#ifdef NNG_SUPP_TLS
if ((strcmp(url->u_scheme, "https") == 0) ||
(strcmp(url->u_scheme, "wss") == 0)) {
@@ -872,9 +875,7 @@ http_server_init(nni_http_server **serverp, nni_url *url)
return (rv);
}
aio->a_addr = &s->addr;
- host = (strlen(url->u_hostname) != 0) ? url->u_hostname : NULL;
- port = (strlen(url->u_port) != 0) ? url->u_port : NULL;
- nni_plat_tcp_resolv(host, port, NNG_AF_UNSPEC, true, aio);
+ nni_plat_tcp_resolv(s->hostname, s->port, NNG_AF_UNSPEC, true, aio);
nni_aio_wait(aio);
rv = nni_aio_result(aio);
nni_aio_fini(aio);
@@ -888,23 +889,17 @@ http_server_init(nni_http_server **serverp, nni_url *url)
}
int
-nni_http_server_init(nni_http_server **serverp, const char *urlstr)
+nni_http_server_init(nni_http_server **serverp, nni_url *url)
{
int rv;
nni_http_server *s;
- nni_url * url;
-
- if ((rv = nni_url_parse(&url, urlstr)) != 0) {
- return (rv);
- }
nni_initialize(&http_server_initializer);
nni_mtx_lock(&http_servers_lk);
NNI_LIST_FOREACH (&http_servers, s) {
- if ((strcmp(url->u_port, s->url->u_port) == 0) &&
- (strcmp(url->u_hostname, s->url->u_hostname) == 0)) {
- nni_url_free(url);
+ if ((strcmp(url->u_port, s->port) == 0) &&
+ (strcmp(url->u_hostname, s->hostname) == 0)) {
*serverp = s;
s->refcnt++;
nni_mtx_unlock(&http_servers_lk);
@@ -916,8 +911,6 @@ nni_http_server_init(nni_http_server **serverp, const char *urlstr)
if ((rv = http_server_init(&s, url)) == 0) {
nni_list_append(&http_servers, s);
*serverp = s;
- } else {
- nni_url_free(url);
}
nni_mtx_unlock(&http_servers_lk);
diff --git a/src/supplemental/websocket/websocket.c b/src/supplemental/websocket/websocket.c
index df404b81..8e75490b 100644
--- a/src/supplemental/websocket/websocket.c
+++ b/src/supplemental/websocket/websocket.c
@@ -1522,12 +1522,11 @@ err:
}
int
-nni_ws_listener_init(nni_ws_listener **wslp, const char *addr)
+nni_ws_listener_init(nni_ws_listener **wslp, nni_url *url)
{
nni_ws_listener *l;
int rv;
char * host;
- char * serv;
if ((l = NNI_ALLOC_STRUCT(l)) == NULL) {
return (NNG_ENOMEM);
@@ -1539,7 +1538,8 @@ nni_ws_listener_init(nni_ws_listener **wslp, const char *addr)
NNI_LIST_INIT(&l->pend, nni_ws, node);
NNI_LIST_INIT(&l->reply, nni_ws, node);
- if ((rv = nni_url_parse(&l->url, addr)) != 0) {
+ // make a private copy of the url structure.
+ if ((rv = nni_url_clone(&l->url, url)) != 0) {
nni_ws_listener_fini(l);
return (rv);
}
@@ -1548,12 +1548,7 @@ nni_ws_listener_init(nni_ws_listener **wslp, const char *addr)
if (strlen(host) == 0) {
host = NULL;
}
- serv = l->url->u_port;
- if (strlen(serv) == 0) {
- serv = (strcmp(l->url->u_scheme, "wss") == 0) ? "443" : "80";
- }
-
- rv = nni_http_handler_init(&l->handler, l->url->u_path, ws_handler);
+ rv = nni_http_handler_init(&l->handler, url->u_path, ws_handler);
if (rv != 0) {
nni_ws_listener_fini(l);
return (rv);
@@ -1561,7 +1556,7 @@ nni_ws_listener_init(nni_ws_listener **wslp, const char *addr)
if (((rv = nni_http_handler_set_host(l->handler, host)) != 0) ||
((rv = nni_http_handler_set_data(l->handler, l, 0)) != 0) ||
- ((rv = nni_http_server_init(&l->server, addr)) != 0)) {
+ ((rv = nni_http_server_init(&l->server, url)) != 0)) {
nni_ws_listener_fini(l);
return (rv);
}
@@ -1841,7 +1836,7 @@ nni_ws_dialer_fini(nni_ws_dialer *d)
}
int
-nni_ws_dialer_init(nni_ws_dialer **dp, const char *addr)
+nni_ws_dialer_init(nni_ws_dialer **dp, nni_url *url)
{
nni_ws_dialer *d;
int rv;
@@ -1853,12 +1848,12 @@ nni_ws_dialer_init(nni_ws_dialer **dp, const char *addr)
NNI_LIST_INIT(&d->wspend, nni_ws, node);
nni_mtx_init(&d->mtx);
- if ((rv = nni_url_parse(&d->url, addr)) != 0) {
+ if ((rv = nni_url_clone(&d->url, url)) != 0) {
nni_ws_dialer_fini(d);
return (rv);
}
- if ((rv = nni_http_client_init(&d->client, addr)) != 0) {
+ if ((rv = nni_http_client_init(&d->client, url)) != 0) {
nni_ws_dialer_fini(d);
return (rv);
}
diff --git a/src/supplemental/websocket/websocket.h b/src/supplemental/websocket/websocket.h
index ddd09b72..2cabb9cc 100644
--- a/src/supplemental/websocket/websocket.h
+++ b/src/supplemental/websocket/websocket.h
@@ -29,7 +29,7 @@ typedef int (*nni_ws_listen_hook)(void *, nni_http_req *, nni_http_res *);
// on INADDR_ANY port 80, with path "/". For connect side, INADDR_ANY
// makes no sense. (TBD: return NNG_EADDRINVAL, or try loopback?)
-extern int nni_ws_listener_init(nni_ws_listener **, const char *);
+extern int nni_ws_listener_init(nni_ws_listener **, nni_url *);
extern void nni_ws_listener_fini(nni_ws_listener *);
extern void nni_ws_listener_close(nni_ws_listener *);
extern int nni_ws_listener_proto(nni_ws_listener *, const char *);
@@ -40,7 +40,7 @@ extern void nni_ws_listener_hook(
extern int nni_ws_listener_set_tls(nni_ws_listener *, nng_tls_config *);
extern int nni_ws_listener_get_tls(nni_ws_listener *, nng_tls_config **s);
-extern int nni_ws_dialer_init(nni_ws_dialer **, const char *);
+extern int nni_ws_dialer_init(nni_ws_dialer **, nni_url *);
extern void nni_ws_dialer_fini(nni_ws_dialer *);
extern void nni_ws_dialer_close(nni_ws_dialer *);
extern int nni_ws_dialer_proto(nni_ws_dialer *, const char *);