From a24b4f11799806e08638162901039fd23efe48be Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 22 Dec 2024 12:57:19 -0800 Subject: http: limit handler uri to 1K This is just the part of the tree that will be matched when looking up a handler. Requests may come in with very much longer URIs, and be matched to the handler as a "subdirectory". This approach makes it possible to avoid a dynamic allocation on the handler, at the cost of pre-allocating 1KB with the handler object. This size can be overridden using a NNG_HTTP_MAX_URI at compile time. --- src/supplemental/http/http_server.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/supplemental/http/http_server.c b/src/supplemental/http/http_server.c index 82f11eac..e068ae98 100644 --- a/src/supplemental/http/http_server.c +++ b/src/supplemental/http/http_server.c @@ -22,9 +22,13 @@ #include "http_api.h" +#ifndef NNG_HTTP_MAX_URI +#define NNG_HTTP_MAX_URI 1024 +#endif + struct nng_http_handler { nni_list_node node; - char *uri; + char uri[NNG_HTTP_MAX_URI]; char method[32]; char host[256]; // RFC 1035 nng_sockaddr host_addr; @@ -114,10 +118,7 @@ nni_http_handler_init( if ((uri == NULL) || (strlen(uri) == 0) || (strcmp(uri, "/") == 0)) { uri = ""; } - if ((h->uri = nni_strdup(uri)) == NULL) { - nni_http_handler_fini(h); - return (NNG_ENOMEM); - } + (void) snprintf(h->uri, sizeof(h->uri), "%s", uri); NNI_LIST_NODE_INIT(&h->node); h->cb = cb; h->data = NULL; @@ -143,7 +144,6 @@ nni_http_handler_fini(nni_http_handler *h) if (h->dtor != NULL) { h->dtor(h->data); } - nni_strfree(h->uri); NNI_FREE_STRUCT(h); } -- cgit v1.2.3-70-g09d2