aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2024-12-22 12:18:33 -0800
committerGarrett D'Amore <garrett@damore.org>2024-12-22 12:18:33 -0800
commit10f6fc5141a15e368dac813a38942cb66d5ddef4 (patch)
treed6ebd5663b2aeb876840e5e2560cae77264d1abc /src
parent497b8e22047fb0efa3397289d23656d6483fdd6d (diff)
downloadnng-10f6fc5141a15e368dac813a38942cb66d5ddef4.tar.gz
nng-10f6fc5141a15e368dac813a38942cb66d5ddef4.tar.bz2
nng-10f6fc5141a15e368dac813a38942cb66d5ddef4.zip
HTTP handler: limit host names to 256 bytes (RFC 1035 specifies 253.)
This also makes `nng_http_handler_set_host` never fail (API break).
Diffstat (limited to 'src')
-rw-r--r--src/supplemental/http/http_api.h2
-rw-r--r--src/supplemental/http/http_public.c5
-rw-r--r--src/supplemental/http/http_server.c46
-rw-r--r--src/supplemental/websocket/websocket.c5
4 files changed, 22 insertions, 36 deletions
diff --git a/src/supplemental/http/http_api.h b/src/supplemental/http/http_api.h
index 349f3a49..d759e27b 100644
--- a/src/supplemental/http/http_api.h
+++ b/src/supplemental/http/http_api.h
@@ -322,7 +322,7 @@ extern int nni_http_handler_set_tree_exclusive(nni_http_handler *);
// on port number as we assume that clients MUST have gotten that part right
// as we do not support virtual hosting on multiple separate ports; the
// server only listens on a single port.
-extern int nni_http_handler_set_host(nni_http_handler *, const char *);
+extern void nni_http_handler_set_host(nni_http_handler *, const char *);
// nni_http_handler_set_method limits the handler to only being called
// for the given HTTP method. By default a handler is called for GET
diff --git a/src/supplemental/http/http_public.c b/src/supplemental/http/http_public.c
index e3093d45..a60743fd 100644
--- a/src/supplemental/http/http_public.c
+++ b/src/supplemental/http/http_public.c
@@ -609,15 +609,14 @@ nng_http_handler_collect_body(nng_http_handler *h, bool want, size_t len)
#endif
}
-int
+void
nng_http_handler_set_host(nng_http_handler *h, const char *host)
{
#ifdef NNG_SUPP_HTTP
- return (nni_http_handler_set_host(h, host));
+ nni_http_handler_set_host(h, host);
#else
NNI_ARG_UNUSED(h);
NNI_ARG_UNUSED(host);
- return (NNG_ENOTSUP);
#endif
}
diff --git a/src/supplemental/http/http_server.c b/src/supplemental/http/http_server.c
index fa9ad2f4..e9c8bea3 100644
--- a/src/supplemental/http/http_server.c
+++ b/src/supplemental/http/http_server.c
@@ -26,7 +26,7 @@ struct nng_http_handler {
nni_list_node node;
char *uri;
char method[32];
- char *host;
+ char host[256]; // RFC 1035
nng_sockaddr host_addr;
bool host_ip;
bool tree;
@@ -122,12 +122,12 @@ nni_http_handler_init(
h->cb = cb;
h->data = NULL;
h->dtor = NULL;
- h->host = NULL;
h->tree = false;
h->tree_exclusive = false;
h->maxbody = 1024 * 1024; // Up to 1MB of body
h->getbody = true;
- strcpy(h->method, "GET");
+ (void) strcpy(h->method, "GET");
+ (void) strcpy(h->host, "");
*hp = h;
return (0);
}
@@ -143,7 +143,6 @@ nni_http_handler_fini(nni_http_handler *h)
if (h->dtor != NULL) {
h->dtor(h->data);
}
- nni_strfree(h->host);
nni_strfree(h->uri);
NNI_FREE_STRUCT(h);
}
@@ -203,19 +202,15 @@ nni_http_handler_set_tree_exclusive(nni_http_handler *h)
return (0);
}
-int
+void
nni_http_handler_set_host(nni_http_handler *h, const char *host)
{
- char *dup;
+ NNI_ASSERT(!nni_atomic_get_bool(&h->busy));
- if (nni_atomic_get_bool(&h->busy) != 0) {
- return (NNG_EBUSY);
- }
if ((host == NULL) || (strcmp(host, "*") == 0) ||
strcmp(host, "") == 0) {
- nni_strfree(h->host);
- h->host = NULL;
- return (0);
+ (void) strcpy(h->host, "");
+ return;
}
if (nni_parse_ip(host, &h->host_addr) == 0) {
uint8_t wild[16] = { 0 };
@@ -224,28 +219,21 @@ nni_http_handler_set_host(nni_http_handler *h, const char *host)
switch (h->host_addr.s_family) {
case NNG_AF_INET:
if (h->host_addr.s_in.sa_addr == 0) {
- nni_strfree(h->host);
- h->host = NULL;
- return (0);
+ (void) strcpy(h->host, "");
+ return;
}
break;
case NNG_AF_INET6:
if (memcmp(h->host_addr.s_in6.sa_addr, wild, 16) ==
0) {
- nni_strfree(h->host);
- h->host = NULL;
- return (0);
+ (void) strcpy(h->host, "");
+ return;
}
break;
}
h->host_ip = true;
}
- if ((dup = nni_strdup(host)) == NULL) {
- return (NNG_ENOMEM);
- }
- nni_strfree(h->host);
- h->host = dup;
- return (0);
+ (void) snprintf(h->host, sizeof(h->host), "%s", host);
}
int
@@ -499,7 +487,7 @@ http_handler_host_match(nni_http_handler *h, const char *host)
nng_sockaddr sa;
size_t len;
- if (h->host == NULL) {
+ if ((len = strlen(h->host)) == '\0') {
return (true);
}
if (host == NULL) {
@@ -529,8 +517,6 @@ http_handler_host_match(nni_http_handler *h, const char *host)
}
}
- len = strlen(h->host);
-
if ((nni_strncasecmp(host, h->host, len) != 0)) {
return (false);
}
@@ -1205,13 +1191,13 @@ nni_http_server_add_handler(nni_http_server *s, nni_http_handler *h)
NNI_LIST_FOREACH (&s->handlers, h2) {
size_t len2;
- if ((h2->host != NULL) && (h->host != NULL) &&
+ if ((h2->host[0] != 0) && (h->host[0] != 0) &&
(nni_strcasecmp(h2->host, h->host) != 0)) {
// Hosts don't match, so we are safe.
continue;
}
- if (((h2->host == NULL) && (h->host != NULL)) ||
- ((h->host == NULL) && (h2->host != NULL))) {
+ if (((h2->host[0] == 0) && (h->host[0] != 0)) ||
+ ((h->host[0] == 0) && (h2->host[0] != 0))) {
continue; // Host specified for just one.
}
if (((h->method[0] == 0) && (h2->method[0] != 0)) ||
diff --git a/src/supplemental/websocket/websocket.c b/src/supplemental/websocket/websocket.c
index 3dfb3e8e..ba304149 100644
--- a/src/supplemental/websocket/websocket.c
+++ b/src/supplemental/websocket/websocket.c
@@ -2150,8 +2150,9 @@ nni_ws_listener_alloc(nng_stream_listener **wslp, const nng_url *url)
return (rv);
}
- if (((rv = nni_http_handler_set_host(l->handler, host)) != 0) ||
- ((rv = nni_http_handler_set_data(l->handler, l, 0)) != 0) ||
+ nni_http_handler_set_host(l->handler, host);
+
+ if (((rv = nni_http_handler_set_data(l->handler, l, 0)) != 0) ||
((rv = nni_http_server_init(&l->server, url)) != 0)) {
ws_listener_free(l);
return (rv);