aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2024-12-22 11:40:36 -0800
committerGarrett D'Amore <garrett@damore.org>2024-12-22 11:40:36 -0800
commit8d1b52931b1d7ad8fabffe0098b9bc31c0b61a9b (patch)
treeb6bf2966a0fa4dbabcd06bdaf0135776f11f6563 /src
parent6800bdd8b9e3fd4488bab56cd9fb12206d0631f6 (diff)
downloadnng-8d1b52931b1d7ad8fabffe0098b9bc31c0b61a9b.tar.gz
nng-8d1b52931b1d7ad8fabffe0098b9bc31c0b61a9b.tar.bz2
nng-8d1b52931b1d7ad8fabffe0098b9bc31c0b61a9b.zip
http: method on request structure is now static
This saves yet another allocation. It also no longer returns a value making this a breaking change.
Diffstat (limited to 'src')
-rw-r--r--src/supplemental/http/http_api.h2
-rw-r--r--src/supplemental/http/http_msg.c30
-rw-r--r--src/supplemental/http/http_public.c5
-rw-r--r--src/supplemental/http/http_server_test.c14
4 files changed, 26 insertions, 25 deletions
diff --git a/src/supplemental/http/http_api.h b/src/supplemental/http/http_api.h
index 46a02211..27559a48 100644
--- a/src/supplemental/http/http_api.h
+++ b/src/supplemental/http/http_api.h
@@ -135,7 +135,7 @@ extern int nni_http_res_alloc_data(nni_http_res *, size_t);
extern const char *nni_http_req_get_method(const nni_http_req *);
extern const char *nni_http_req_get_version(const nni_http_req *);
extern const char *nni_http_req_get_uri(const nni_http_req *);
-extern int nni_http_req_set_method(nni_http_req *, const char *);
+extern void nni_http_req_set_method(nni_http_req *, const char *);
extern int nni_http_req_set_version(nni_http_req *, const char *);
extern int nni_http_req_set_uri(nni_http_req *, const char *);
extern uint16_t nni_http_res_get_status(const nni_http_res *);
diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c
index afe2a3b6..564a4763 100644
--- a/src/supplemental/http/http_msg.c
+++ b/src/supplemental/http/http_msg.c
@@ -38,7 +38,7 @@ typedef struct nni_http_entity {
struct nng_http_req {
nni_list hdrs;
nni_http_entity data;
- char *meth;
+ char meth[32];
char *uri;
const char *vers;
char *buf;
@@ -100,14 +100,14 @@ nni_http_req_reset(nni_http_req *req)
{
http_headers_reset(&req->hdrs);
http_entity_reset(&req->data);
- nni_strfree(req->meth);
nni_strfree(req->uri);
- req->meth = req->uri = NULL;
+ req->uri = NULL;
nni_free(req->buf, req->bufsz);
+ nni_http_req_set_method(req, NULL);
+ nni_http_req_set_version(req, NNG_HTTP_VERSION_1_1);
req->bufsz = 0;
req->buf = NULL;
req->parsed = false;
- req->vers = NNG_HTTP_VERSION_1_1;
}
void
@@ -532,7 +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);
+ req->meth, req->uri, req->vers);
return (rv);
}
@@ -611,9 +611,9 @@ 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 = NNG_HTTP_VERSION_1_1;
- req->meth = NULL;
req->uri = NULL;
+ nni_http_req_set_version(req, NNG_HTTP_VERSION_1_1);
+ nni_http_req_set_method(req, "GET");
if (url != NULL) {
const char *host;
char host_buf[264]; // 256 + 8 for port
@@ -673,7 +673,7 @@ nni_http_res_alloc(nni_http_res **resp)
const char *
nni_http_req_get_method(const nni_http_req *req)
{
- return (req->meth != NULL ? req->meth : "GET");
+ return (req->meth);
}
const char *
@@ -735,13 +735,15 @@ nni_http_req_set_uri(nni_http_req *req, const char *uri)
return (http_set_string(&req->uri, uri));
}
-int
+void
nni_http_req_set_method(nni_http_req *req, const char *meth)
{
- if ((meth != NULL) && (strcmp(meth, "GET") == 0)) {
- meth = NULL;
+ if (meth == NULL) {
+ meth = "GET";
}
- return (http_set_string(&req->meth, meth));
+ // this may truncate the method, but nobody should be sending
+ // methods so long.
+ (void) snprintf(req->meth, sizeof(req->meth), "%s", meth);
}
int
@@ -811,8 +813,8 @@ http_req_parse_line(nni_http_req *req, void *line)
*version = '\0';
version++;
- if (((rv = nni_http_req_set_method(req, method)) != 0) ||
- ((rv = nni_http_req_set_uri(req, uri)) != 0) ||
+ nni_http_req_set_method(req, method);
+ if (((rv = nni_http_req_set_uri(req, uri)) != 0) ||
((rv = nni_http_req_set_version(req, version)) != 0)) {
return (rv);
}
diff --git a/src/supplemental/http/http_public.c b/src/supplemental/http/http_public.c
index 161aefd8..c9983df5 100644
--- a/src/supplemental/http/http_public.c
+++ b/src/supplemental/http/http_public.c
@@ -280,15 +280,14 @@ nng_http_req_get_uri(const nng_http_req *req)
#endif
}
-int
+void
nng_http_req_set_method(nng_http_req *req, const char *meth)
{
#ifdef NNG_SUPP_HTTP
- return (nni_http_req_set_method(req, meth));
+ nni_http_req_set_method(req, meth);
#else
NNI_ARG_UNUSED(req);
NNI_ARG_UNUSED(meth);
- return (NNG_ENOTSUP);
#endif
}
diff --git a/src/supplemental/http/http_server_test.c b/src/supplemental/http/http_server_test.c
index 2ed04c4d..ddae77d3 100644
--- a/src/supplemental/http/http_server_test.c
+++ b/src/supplemental/http/http_server_test.c
@@ -363,7 +363,7 @@ test_server_wrong_method(void)
server_setup(&st, h);
- NUTS_PASS(nng_http_req_set_method(st.req, "POST"));
+ nng_http_req_set_method(st.req, "POST");
NUTS_PASS(nng_http_req_set_uri(st.req, "/home.html"));
nng_http_conn_write_req(st.conn, st.req, st.aio);
@@ -400,7 +400,7 @@ test_server_post_handler(void)
snprintf(txdata, sizeof(txdata), "1234");
nng_http_req_set_uri(st.req, "/post");
nng_http_req_set_data(st.req, txdata, strlen(txdata));
- NUTS_PASS(nng_http_req_set_method(st.req, "POST"));
+ nng_http_req_set_method(st.req, "POST");
NUTS_PASS(httpdo(st.url, st.req, st.res, (void **) &rxdata, &size));
NUTS_TRUE(nng_http_res_get_status(st.res) == NNG_HTTP_STATUS_OK);
NUTS_TRUE(size == strlen(txdata));
@@ -410,7 +410,7 @@ test_server_post_handler(void)
server_reset(&st);
NUTS_PASS(nng_http_req_set_uri(st.req, "/post"));
- NUTS_PASS(nng_http_req_set_method(st.req, "GET"));
+ nng_http_req_set_method(st.req, "GET");
NUTS_PASS(nng_http_req_set_data(st.req, txdata, strlen(txdata)));
NUTS_PASS(httpdo(st.url, st.req, st.res, &data, &size));
@@ -501,7 +501,7 @@ test_server_post_redirect(void)
snprintf(txdata, sizeof(txdata), "1234");
NUTS_PASS(nng_http_req_set_uri(st.req, "/here"));
nng_http_req_set_data(st.req, txdata, strlen(txdata));
- NUTS_PASS(nng_http_req_set_method(st.req, "POST"));
+ nng_http_req_set_method(st.req, "POST");
NUTS_PASS(httpdo(st.url, st.req, st.res, (void **) &data, &size));
NUTS_TRUE(nng_http_res_get_status(st.res) == 301);
dest = nng_http_res_get_header(st.res, "Location");
@@ -521,14 +521,14 @@ test_server_post_echo_tree(void)
char *rxdata;
NUTS_PASS(nng_http_handler_alloc(&h, "/", httpecho));
- NUTS_PASS(nng_http_handler_set_method(h, "POST"));
+ nng_http_handler_set_method(h, "POST");
NUTS_PASS(nng_http_handler_set_tree(h));
server_setup(&st, h);
snprintf(txdata, sizeof(txdata), "1234");
nng_http_req_set_data(st.req, txdata, strlen(txdata));
- NUTS_PASS(nng_http_req_set_method(st.req, "POST"));
+ nng_http_req_set_method(st.req, "POST");
NUTS_PASS(nng_http_req_set_uri(st.req, "/some_sub/directory"));
NUTS_PASS(httpdo(st.url, st.req, st.res, (void **) &rxdata, &size));
NUTS_TRUE(nng_http_res_get_status(st.res) == NNG_HTTP_STATUS_OK);
@@ -866,7 +866,7 @@ test_serve_index_not_post(void)
server_setup(&st, h);
NUTS_PASS(nng_http_req_set_uri(st.req, "/subdir2/index.html"));
- NUTS_PASS(nng_http_req_set_method(st.req, "POST"));
+ nng_http_req_set_method(st.req, "POST");
NUTS_PASS(httpget(&st, &data, &size, &stat, &ctype));
NUTS_TRUE(stat == NNG_HTTP_STATUS_METHOD_NOT_ALLOWED);
nng_strfree(ctype);