From 85aff44e00e836eda618d4f1cf013bce38b3fd44 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 17 Nov 2024 18:23:17 -0800 Subject: URL u_port should be a number not a string. The idea here is to reduce the dynamic allocations used for URLs, and also the back and forth with parsing begin strings and port numbers. We always resolve to a port number, and this is easier for everyone. The real goal in the long term is to eliminate dynamic allocation of the URL fields altogether, but that requires a little more work. This is a step in the right direction. --- src/supplemental/http/http_msg.c | 33 ++++++++++++++++----------------- src/supplemental/http/http_server.c | 8 +++----- 2 files changed, 19 insertions(+), 22 deletions(-) (limited to 'src/supplemental/http') diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c index 9f5996ca..28c89ec7 100644 --- a/src/supplemental/http/http_msg.c +++ b/src/supplemental/http/http_msg.c @@ -22,13 +22,13 @@ // a comma. From experience, for example, Firefox uses a Connection: // header with two values, "keepalive", and "upgrade". typedef struct http_header { - char * name; - char * value; + char *name; + char *value; nni_list_node node; } http_header; typedef struct nni_http_entity { - char * data; + char *data; size_t size; // allocated/expected size size_t len; // current length bool own; // if true, data is "ours", and should be freed @@ -37,10 +37,10 @@ typedef struct nni_http_entity { struct nng_http_req { nni_list hdrs; nni_http_entity data; - char * meth; - char * uri; - char * vers; - char * buf; + char *meth; + char *uri; + char *vers; + char *buf; size_t bufsz; bool parsed; }; @@ -49,9 +49,9 @@ struct nng_http_res { nni_list hdrs; nni_http_entity data; uint16_t code; - char * rsn; - char * vers; - char * buf; + char *rsn; + char *vers; + char *buf; size_t bufsz; bool parsed; bool iserr; @@ -492,7 +492,7 @@ http_asprintf(char **bufp, size_t *szp, nni_list *hdrs, const char *fmt, ...) va_list ap; size_t len; size_t n; - char * buf; + char *buf; va_start(ap, fmt); len = vsnprintf(NULL, 0, fmt, ap); @@ -550,7 +550,7 @@ http_res_prepare(nni_http_res *res) char * nni_http_req_headers(nni_http_req *req) { - char * s; + char *s; size_t len; len = http_sprintf_headers(NULL, 0, &req->hdrs) + 1; @@ -563,7 +563,7 @@ nni_http_req_headers(nni_http_req *req) char * nni_http_res_headers(nni_http_res *res) { - char * s; + char *s; size_t len; len = http_sprintf_headers(NULL, 0, &res->hdrs) + 1; @@ -625,8 +625,7 @@ nni_http_req_alloc(nni_http_req **reqp, const nni_url *url) // Add a Host: header since we know that from the URL. Also, // only include the :port portion if it isn't the default port. - if (strcmp(nni_url_default_port(url->u_scheme), url->u_port) == - 0) { + if (nni_url_default_port(url->u_scheme) == url->u_port) { host = url->u_hostname; } else { host = url->u_host; @@ -735,7 +734,7 @@ http_scan_line(void *vbuf, size_t n, size_t *lenp) { size_t len; char lc; - char * buf = vbuf; + char *buf = vbuf; lc = 0; for (len = 0; len < n; len++) { @@ -1042,7 +1041,7 @@ nni_http_alloc_html_error(char **html, uint16_t code, const char *details) int nni_http_res_alloc_error(nni_http_res **resp, uint16_t err) { - char * html = NULL; + char *html = NULL; nni_http_res *res = NULL; int rv; diff --git a/src/supplemental/http/http_server.c b/src/supplemental/http/http_server.c index ac57cf5b..03b3cf68 100644 --- a/src/supplemental/http/http_server.c +++ b/src/supplemental/http/http_server.c @@ -1,5 +1,5 @@ // -// Copyright 2024 Staysail Systems, Inc. +// Copyright 2023 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // Copyright 2018 QXSoftware // Copyright 2019 Devolutions @@ -962,9 +962,7 @@ http_server_init(nni_http_server **serverp, const nni_url *url) return (rv); } - // NB: We only support number port numbers, and the URL framework - // expands empty port numbers to 80 or 443 as appropriate. - s->port = atoi(url->u_port); + s->port = url->u_port; if ((s->hostname = nni_strdup(url->u_hostname)) == NULL) { http_server_fini(s); @@ -989,7 +987,7 @@ nni_http_server_init(nni_http_server **serverp, const nni_url *url) nni_mtx_lock(&http_servers_lk); NNI_LIST_FOREACH (&http_servers, s) { - if ((!s->closed) && (atoi(url->u_port) == s->port) && + if ((!s->closed) && (url->u_port == s->port) && (strcmp(url->u_hostname, s->hostname) == 0)) { *serverp = s; s->refcnt++; -- cgit v1.2.3-70-g09d2