diff options
| author | Garrett D'Amore <garrett@damore.org> | 2020-11-17 22:46:42 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2020-11-17 22:49:59 -0800 |
| commit | cda4885676f009e2e7f2ad5e6c52743efc8b8924 (patch) | |
| tree | 62dfc33e0559cbf84efb014dd17b687247cadda8 | |
| parent | 04974cc135ca461d8fce50fa8def0ca5fe13b101 (diff) | |
| download | nng-cda4885676f009e2e7f2ad5e6c52743efc8b8924.tar.gz nng-cda4885676f009e2e7f2ad5e6c52743efc8b8924.tar.bz2 nng-cda4885676f009e2e7f2ad5e6c52743efc8b8924.zip | |
Centralize the scheme handling for HTTP schemes.
| -rw-r--r-- | src/supplemental/http/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/supplemental/http/http_api.h | 4 | ||||
| -rw-r--r-- | src/supplemental/http/http_client.c | 38 | ||||
| -rw-r--r-- | src/supplemental/http/http_schemes.c | 85 | ||||
| -rw-r--r-- | src/supplemental/http/http_server.c | 43 |
5 files changed, 109 insertions, 62 deletions
diff --git a/src/supplemental/http/CMakeLists.txt b/src/supplemental/http/CMakeLists.txt index 324484d8..db6bcdac 100644 --- a/src/supplemental/http/CMakeLists.txt +++ b/src/supplemental/http/CMakeLists.txt @@ -24,4 +24,5 @@ nng_sources_if(NNG_SUPP_HTTP http_conn.c http_msg.c http_public.c + http_schemes.c http_server.c) diff --git a/src/supplemental/http/http_api.h b/src/supplemental/http/http_api.h index 33947abf..207f0230 100644 --- a/src/supplemental/http/http_api.h +++ b/src/supplemental/http/http_api.h @@ -382,4 +382,8 @@ extern void nni_http_transact_conn( extern void nni_http_transact( nni_http_client *, nni_http_req *, nni_http_res *, nni_aio *); +// nni_http_stream_scheme returns the underlying stream scheme for a given +// upper layer scheme. +extern const char *nni_http_stream_scheme(const char *); + #endif // NNG_SUPPLEMENTAL_HTTP_HTTP_API_H diff --git a/src/supplemental/http/http_client.c b/src/supplemental/http/http_client.c index 2efb97a8..36446b63 100644 --- a/src/supplemental/http/http_client.c +++ b/src/supplemental/http/http_client.c @@ -100,30 +100,14 @@ nni_http_client_init(nni_http_client **cp, const nni_url *url) int rv; nni_http_client *c; nng_url my_url; + const char * scheme; - // Rewrite URLs to either TLS or TCP. - memcpy(&my_url, url, sizeof(my_url)); - if ((strcmp(url->u_scheme, "http") == 0) || - (strcmp(url->u_scheme, "ws") == 0)) { - my_url.u_scheme = "tcp"; - } else if ((strcmp(url->u_scheme, "https") == 0) || - (strcmp(url->u_scheme, "wss") == 0)) { - my_url.u_scheme = "tls+tcp"; - } else if ((strcmp(url->u_scheme, "ws4") == 0) || - (strcmp(url->u_scheme, "http4") == 0)) { - my_url.u_scheme = "tcp4"; - } else if ((strcmp(url->u_scheme, "ws6") == 0) || - (strcmp(url->u_scheme, "http6") == 0)) { - my_url.u_scheme = "tcp6"; - } else if ((strcmp(url->u_scheme, "wss4") == 0) || - (strcmp(url->u_scheme, "https4") == 0)) { - my_url.u_scheme = "tls+tcp4"; - } else if ((strcmp(url->u_scheme, "wss6") == 0) || - (strcmp(url->u_scheme, "https6") == 0)) { - my_url.u_scheme = "tls+tcp6"; - } else { + if ((scheme = nni_http_stream_scheme(url->u_scheme)) == NULL) { return (NNG_EADDRINVAL); } + // Rewrite URLs to either TLS or TCP. + memcpy(&my_url, url, sizeof(my_url)); + my_url.u_scheme = (char *) scheme; if (strlen(url->u_hostname) == 0) { // We require a valid hostname. @@ -153,20 +137,14 @@ nni_http_client_init(nni_http_client **cp, const nni_url *url) int nni_http_client_set_tls(nni_http_client *c, nng_tls_config *tls) { - int rv; - rv = nni_stream_dialer_set(c->dialer, NNG_OPT_TLS_CONFIG, &tls, - sizeof(tls), NNI_TYPE_POINTER); - return (rv); + return (nng_stream_dialer_set_ptr(c->dialer, NNG_OPT_TLS_CONFIG, tls)); } int nni_http_client_get_tls(nni_http_client *c, nng_tls_config **tlsp) { - size_t sz = sizeof(*tlsp); - int rv; - rv = nni_stream_dialer_get( - c->dialer, NNG_OPT_TLS_CONFIG, tlsp, &sz, NNI_TYPE_POINTER); - return (rv); + return (nng_stream_dialer_get_ptr( + c->dialer, NNG_OPT_TLS_CONFIG, (void **) tlsp)); } int diff --git a/src/supplemental/http/http_schemes.c b/src/supplemental/http/http_schemes.c new file mode 100644 index 00000000..df8b9208 --- /dev/null +++ b/src/supplemental/http/http_schemes.c @@ -0,0 +1,85 @@ +// +// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech> +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include <ctype.h> +#include <stdbool.h> +#include <string.h> + +#include "core/nng_impl.h" + +#include "http_api.h" + +static struct { + const char *upper; + const char *lower; +} http_schemes[] = { + { + .upper = "http", + .lower = "tcp", + }, + { + .upper = "ws", + .lower = "tcp", + }, + { + .upper = "https", + .lower = "tls+tcp", + }, + { + .upper = "wss", + .lower = "tls+tcp", + }, + { + .upper = "http4", + .lower = "tcp4", + }, + { + .upper = "ws4", + .lower = "tcp4", + }, + { + .upper = "http6", + .lower = "tcp6", + }, + { + .upper = "ws6", + .lower = "tcp6", + }, + { + .upper = "https4", + .lower = "tls+tcp4", + }, + { + .upper = "wss4", + .lower = "tls+tcp4", + }, + { + .upper = "https6", + .lower = "tls+tcp6", + }, + { + .upper = "wss6", + .lower = "tls+tcp6", + }, + { + .upper = NULL, + .lower = NULL, + }, +}; + +const char * +nni_http_stream_scheme(const char *upper) +{ + for (int i = 0; http_schemes[i].upper != NULL; i++) { + if (strcmp(http_schemes[i].upper, upper) == 0) { + return (http_schemes[i].lower); + } + } + return (NULL); +}
\ No newline at end of file diff --git a/src/supplemental/http/http_server.c b/src/supplemental/http/http_server.c index 33e49c09..49c236e4 100644 --- a/src/supplemental/http/http_server.c +++ b/src/supplemental/http/http_server.c @@ -931,31 +931,15 @@ http_server_init(nni_http_server **serverp, const nni_url *url) { nni_http_server *s; int rv; - nng_url myurl; + nng_url my_url; + const char * scheme; - // Rewrite URLs to either TLS or TCP. - memcpy(&myurl, url, sizeof(myurl)); - if ((strcmp(url->u_scheme, "http") == 0) || - (strcmp(url->u_scheme, "ws") == 0)) { - myurl.u_scheme = "tcp"; - } else if ((strcmp(url->u_scheme, "https") == 0) || - (strcmp(url->u_scheme, "wss") == 0)) { - myurl.u_scheme = "tls+tcp"; - } else if ((strcmp(url->u_scheme, "ws4") == 0) || - (strcmp(url->u_scheme, "http4")) == 0) { - myurl.u_scheme = "tcp4"; - } else if ((strcmp(url->u_scheme, "ws6") == 0) || - (strcmp(url->u_scheme, "http6") == 0)) { - myurl.u_scheme = "tcp6"; - } else if ((strcmp(url->u_scheme, "wss4") == 0) || - (strcmp(url->u_scheme, "https4") == 0)) { - myurl.u_scheme = "tls+tcp4"; - } else if ((strcmp(url->u_scheme, "wss6") == 0) || - (strcmp(url->u_scheme, "https6") == 0)) { - myurl.u_scheme = "tls+tcp6"; - } else { + if ((scheme = nni_http_stream_scheme(url->u_scheme)) == NULL) { return (NNG_EADDRINVAL); } + // Rewrite URLs to either TLS or TCP. + memcpy(&my_url, url, sizeof(my_url)); + my_url.u_scheme = (char *) scheme; if ((s = NNI_ALLOC_STRUCT(s)) == NULL) { return (NNG_ENOMEM); @@ -983,7 +967,7 @@ http_server_init(nni_http_server **serverp, const nni_url *url) return (NNG_ENOMEM); } - if ((rv = nng_stream_listener_alloc_url(&s->listener, &myurl)) != 0) { + if ((rv = nng_stream_listener_alloc_url(&s->listener, &my_url)) != 0) { http_server_fini(s); return (rv); } @@ -1882,20 +1866,15 @@ nni_http_handler_init_static(nni_http_handler **hpp, const char *uri, int nni_http_server_set_tls(nni_http_server *s, nng_tls_config *tls) { - int rv; - rv = nni_stream_listener_set(s->listener, NNG_OPT_TLS_CONFIG, &tls, - sizeof(tls), NNI_TYPE_POINTER); - return (rv); + return ( + nng_stream_listener_set_ptr(s->listener, NNG_OPT_TLS_CONFIG, tls)); } int nni_http_server_get_tls(nni_http_server *s, nng_tls_config **tlsp) { - size_t sz = sizeof(*tlsp); - int rv; - rv = nni_stream_listener_get( - s->listener, NNG_OPT_TLS_CONFIG, tlsp, &sz, NNI_TYPE_POINTER); - return (rv); + return (nng_stream_listener_get_ptr( + s->listener, NNG_OPT_TLS_CONFIG, (void **) tlsp)); } int |
