aboutsummaryrefslogtreecommitdiff
path: root/src/supplemental/http
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/http
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/http')
-rw-r--r--src/supplemental/http/client.c15
-rw-r--r--src/supplemental/http/http.h5
-rw-r--r--src/supplemental/http/server.c47
3 files changed, 23 insertions, 44 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);