aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2024-12-22 09:32:10 -0800
committerGarrett D'Amore <garrett@damore.org>2024-12-22 09:32:10 -0800
commit9ac2c5a0bb545a5dc48c7e0531346e40e426cf9b (patch)
treecedb57c29328b1e7e627e4979fac88bd5e37b7a5 /src
parentf0e0801f557abad86f4542243a5efa04fe9ecd85 (diff)
downloadnng-2.0.0-alpha.1.tar.gz
nng-2.0.0-alpha.1.tar.bz2
nng-2.0.0-alpha.1.zip
HTTP: intern the version and avoid allocationv2.0.0-alpha.1
There are only a few possible reasonable values, and we can intern them to avoid any allocations for it. (We will probably do the same for the HTTP method shortly as well.)
Diffstat (limited to 'src')
-rw-r--r--src/supplemental/http/http_msg.c58
1 files changed, 37 insertions, 21 deletions
diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c
index 670fb28e..afe2a3b6 100644
--- a/src/supplemental/http/http_msg.c
+++ b/src/supplemental/http/http_msg.c
@@ -16,6 +16,7 @@
#include "core/nng_impl.h"
#include "http_api.h"
+#include "nng/supplemental/http/http.h"
// Note that as we parse headers, the rule is that if a header is already
// present, then we can append it to the existing header, separated by
@@ -39,7 +40,7 @@ struct nng_http_req {
nni_http_entity data;
char *meth;
char *uri;
- char *vers;
+ const char *vers;
char *buf;
size_t bufsz;
bool parsed;
@@ -50,7 +51,7 @@ struct nng_http_res {
nni_http_entity data;
uint16_t code;
char *rsn;
- char *vers;
+ const char *vers;
char *buf;
size_t bufsz;
bool parsed;
@@ -99,14 +100,14 @@ nni_http_req_reset(nni_http_req *req)
{
http_headers_reset(&req->hdrs);
http_entity_reset(&req->data);
- nni_strfree(req->vers);
nni_strfree(req->meth);
nni_strfree(req->uri);
- req->vers = req->meth = req->uri = NULL;
+ req->meth = req->uri = NULL;
nni_free(req->buf, req->bufsz);
req->bufsz = 0;
req->buf = NULL;
req->parsed = false;
+ req->vers = NNG_HTTP_VERSION_1_1;
}
void
@@ -115,8 +116,7 @@ nni_http_res_reset(nni_http_res *res)
http_headers_reset(&res->hdrs);
http_entity_reset(&res->data);
nni_strfree(res->rsn);
- nni_strfree(res->vers);
- res->vers = NULL;
+ res->vers = NNG_HTTP_VERSION_1_1;
res->rsn = NULL;
res->code = 0;
res->parsed = false;
@@ -532,8 +532,7 @@ http_req_prepare(nni_http_req *req)
return (NNG_EINVAL);
}
rv = http_asprintf(&req->buf, &req->bufsz, &req->hdrs, "%s %s %s\r\n",
- req->meth != NULL ? req->meth : "GET", req->uri,
- req->vers != NULL ? req->vers : "HTTP/1.1");
+ req->meth != NULL ? req->meth : "GET", req->uri, req->vers);
return (rv);
}
@@ -542,7 +541,7 @@ http_res_prepare(nni_http_res *res)
{
int rv;
rv = http_asprintf(&res->buf, &res->bufsz, &res->hdrs, "%s %d %s\r\n",
- nni_http_res_get_version(res), nni_http_res_get_status(res),
+ res->vers, nni_http_res_get_status(res),
nni_http_res_get_reason(res));
return (rv);
}
@@ -612,7 +611,7 @@ nni_http_req_alloc(nni_http_req **reqp, const nng_url *url)
req->data.data = NULL;
req->data.size = 0;
req->data.own = false;
- req->vers = NULL;
+ req->vers = NNG_HTTP_VERSION_1_1;
req->meth = NULL;
req->uri = NULL;
if (url != NULL) {
@@ -664,7 +663,7 @@ nni_http_res_alloc(nni_http_res **resp)
res->data.data = NULL;
res->data.size = 0;
res->data.own = false;
- res->vers = NULL;
+ res->vers = NNG_HTTP_VERSION_1_1;
res->rsn = NULL;
res->code = 0;
*resp = res;
@@ -686,31 +685,48 @@ nni_http_req_get_uri(const nni_http_req *req)
const char *
nni_http_req_get_version(const nni_http_req *req)
{
- return (req->vers != NULL ? req->vers : "HTTP/1.1");
+ return (req->vers);
}
const char *
nni_http_res_get_version(const nni_http_res *res)
{
- return (res->vers != NULL ? res->vers : "HTTP/1.1");
+ return (res->vers);
+}
+
+static const char *http_versions[] = {
+ // for efficiency, we order in most likely first
+ "HTTP/1.1",
+ "HTTP/2",
+ "HTTP/3",
+ "HTTP/1.0",
+ "HTTP/0.9",
+ NULL,
+};
+
+static int
+http_set_version(const char **ptr, const char *vers)
+{
+ vers = vers != NULL ? vers : NNG_HTTP_VERSION_1_1;
+ for (int i = 0; http_versions[i] != NULL; i++) {
+ if (strcmp(vers, http_versions[i]) == 0) {
+ *ptr = http_versions[i];
+ return (0);
+ }
+ }
+ return (NNG_ENOTSUP);
}
int
nni_http_req_set_version(nni_http_req *req, const char *vers)
{
- if ((vers != NULL) && (strcmp(vers, "HTTP/1.1") == 0)) {
- vers = NULL;
- }
- return (http_set_string(&req->vers, vers));
+ return (http_set_version(&req->vers, vers));
}
int
nni_http_res_set_version(nni_http_res *res, const char *vers)
{
- if ((vers != NULL) && (strcmp(vers, "HTTP/1.1") == 0)) {
- vers = NULL;
- }
- return (http_set_string(&res->vers, vers));
+ return (http_set_version(&res->vers, vers));
}
int