Skip to content

Commit

Permalink
http: remove separate request allocation and free
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Jan 7, 2025
1 parent 196deb3 commit a278744
Show file tree
Hide file tree
Showing 25 changed files with 19 additions and 114 deletions.
4 changes: 2 additions & 2 deletions demo/http_client/http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ main(int argc, char **argv)
if (((rv = nng_init(NULL)) != 0) ||
((rv = nng_url_parse(&url, argv[1])) != 0) ||
((rv = nng_http_client_alloc(&client, url)) != 0) ||
((rv = nng_http_req_alloc(&req, url)) != 0) ||
((rv = nng_aio_alloc(&aio, NULL, NULL)) != 0)) {
fatal(rv);
}
Expand All @@ -85,12 +84,13 @@ main(int argc, char **argv)
// Get the connection, at the 0th output.
conn = nng_aio_get_output(aio, 0);
res = nng_http_conn_res(conn);
req = nng_http_conn_req(conn);

// Request is already set up with URL, and for GET via HTTP/1.1.
// The Host: header is already set up too.

// Send the request, and wait for that to finish.
nng_http_conn_write_req(conn, req, aio);
nng_http_conn_write_req(conn, aio);
nng_aio_wait(aio);

if ((rv = nng_aio_result(aio)) != 0) {
Expand Down
1 change: 0 additions & 1 deletion docs/man/libnng.3.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ and connections.
|xref:nng_http_conn_write_req.3http.adoc[nng_http_conn_write_req()]|write HTTP request
|xref:nng_http_conn_write_res.3http.adoc[nng_http_conn_write_res()]|write HTTP response
|xref:nng_http_req_add_header.3http.adoc[nng_http_req_add_header()]|add HTTP request header
|xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc()]|allocate HTTP request structure
|xref:nng_http_req_copy_data.3http.adoc[nng_http_req_copy_data()]|copy HTTP request body
|xref:nng_http_req_del_header.3http.adoc[nng_http_req_del_header()]|delete HTTP request header
|xref:nng_http_req_free.3http.adoc[nng_http_req_free()]|free HTTP request structure
Expand Down
3 changes: 1 addition & 2 deletions docs/man/nng_http_conn_write_req.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ nng_http_conn_write_req - write HTTP request
#include <nng/nng.h>
#include <nng/supplemental/http/http.h>
void nng_http_conn_write_req(nng_http_conn *conn, nng_http_req *req,
nng_aio *aio);
void nng_http_conn_write_req(nng_http_conn *conn, nng_aio *aio);
----

== DESCRIPTION
Expand Down
1 change: 0 additions & 1 deletion docs/man/nng_http_get_version.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ Request version as a string.
== SEE ALSO

[.text-left]
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_req_set_version.3http.adoc[nng_http_req_set_version(3http)],
xref:nng.7.adoc[nng(7)]
1 change: 0 additions & 1 deletion docs/man/nng_http_req_add_header.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ This function returns 0 on success, and non-zero otherwise.
== SEE ALSO

[.text-left]
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_req_del_header.3http.adoc[nng_http_req_del_header(3http)],
xref:nng_http_req_get_header.3http.adoc[nng_http_req_get_header(3http)],
xref:nng_http_req_set_header.3http.adoc[nng_http_req_set_header(3http)],
Expand Down
1 change: 0 additions & 1 deletion docs/man/nng_http_req_copy_data.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ This function returns 0 on success, and non-zero otherwise.

[.text-left]
xref:nng_http_conn_write_req.3http.adoc[nng_http_conn_write_req(3http)],
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_req_set_data.3http.adoc[nng_http_req_set_data(3http)],
xref:nng_http_req_set_header.3http.adoc[nng_http_req_set_header(3http)],
xref:nng.7.adoc[nng(7)]
1 change: 0 additions & 1 deletion docs/man/nng_http_req_del_header.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ This function returns 0 on success, and non-zero otherwise.
== SEE ALSO

[.text-left]
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_req_add_header.3http.adoc[nng_http_req_add_header(3http)],
xref:nng_http_req_del_header.3http.adoc[nng_http_req_del_header(3http)],
xref:nng_http_req_get_header.3http.adoc[nng_http_req_get_header(3http)],
Expand Down
1 change: 0 additions & 1 deletion docs/man/nng_http_req_get_data.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ None.
== SEE ALSO

[.text-left]
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_req_set_data.3http.adoc[nng_http_req_copy_data(3http)],
xref:nng_http_req_copy_data.3http.adoc[nng_http_req_copy_data(3http)],
xref:nng.7.adoc[nng(7)]
1 change: 0 additions & 1 deletion docs/man/nng_http_req_get_header.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ None.
== SEE ALSO

[.text-left]
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_req_add_header.3http.adoc[nng_http_req_add_header(3http)],
xref:nng_http_req_set_header.3http.adoc[nng_http_req_set_header(3http)],
xref:nng.7.adoc[nng(7)]
1 change: 0 additions & 1 deletion docs/man/nng_http_req_get_method.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ None.
== SEE ALSO

[.text-left]
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_req_set_method.3http.adoc[nng_http_req_set_method(3http)],
xref:nng.7.adoc[nng(7)]
1 change: 0 additions & 1 deletion docs/man/nng_http_req_get_uri.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,5 @@ None.
== SEE ALSO

[.text-left]
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_req_set_uri.3http.adoc[nng_http_req_set_uri(3http)],
xref:nng.7.adoc[nng(7)]
1 change: 0 additions & 1 deletion docs/man/nng_http_req_set_data.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ This function returns 0 on success, and non-zero otherwise.

[.text-left]
xref:nng_http_conn_write_req.3http.adoc[nng_http_conn_write_req(3http)],
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_req_copy_data.3http.adoc[nng_http_req_copy_data(3http)],
xref:nng_http_req_set_header.3http.adoc[nng_http_req_set_header(3http)],
xref:nng.7.adoc[nng(7)]
1 change: 0 additions & 1 deletion docs/man/nng_http_req_set_header.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ This function returns 0 on success, and non-zero otherwise.
== SEE ALSO

[.text-left]
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_req_add_header.3http.adoc[nng_http_req_add_header(3http)],
xref:nng_http_req_del_header.3http.adoc[nng_http_req_del_header(3http)],
xref:nng_http_req_get_header.3http.adoc[nng_http_req_get_header(3http)],
Expand Down
1 change: 0 additions & 1 deletion docs/man/nng_http_req_set_method.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,5 @@ A local copy of the _method_ is made in the request _req_.
== SEE ALSO

[.text-left]
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_req_get_method.3http.adoc[nng_http_req_get_method(3http)],
xref:nng.7.adoc[nng(7)]
1 change: 0 additions & 1 deletion docs/man/nng_http_req_set_uri.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ This function returns 0 on success, and non-zero otherwise.
== SEE ALSO

[.text-left]
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_req_get_uri.3http.adoc[nng_http_req_get_uri(3http)],
xref:nng_url_parse.3.adoc[nng_url_parse(3)],
xref:nng.7.adoc[nng(7)]
2 changes: 0 additions & 2 deletions docs/man/nng_http_res_alloc.3http.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@ This function returns 0 on success, and non-zero otherwise.
[.text-left]
xref:nng_http_conn_read_res.3http.adoc[nng_http_conn_read_res(3http)],
xref:nng_http_conn_write_res.3http.adoc[nng_http_conn_write_res(3http)],
xref:nng_http_req_alloc.3http.adoc[nng_http_req_alloc(3http)],
xref:nng_http_res_alloc_error.3http.adoc[nng_http_res_alloc_error(3http)],
xref:nng_http_res_add_header.3http.adoc[nng_http_res_add_header(3http)],
xref:nng_http_res_copy_data.3http.adoc[nng_http_res_copy_data(3http)],
xref:nng_http_res_del_header.3http.adoc[nng_http_res_del_header(3http)],
xref:nng_http_res_get_header.3http.adoc[nng_http_res_get_header(3http)],
xref:nng_http_res_get_reason.3http.adoc[nng_http_res_get_reason(3http)],
xref:nng_http_res_get_status.3http.adoc[nng_http_res_get_status(3http)],
xref:nng_http_res_get_version.3http.adoc[nng_http_res_get_version(3http)],
xref:nng_http_res_reset.3http.adoc[nng_http_res_reset(3http)],
xref:nng_http_res_set_data.3http.adoc[nng_http_res_set_data(3http)],
xref:nng_http_res_set_reason.3http.adoc[nng_http_res_set_reason(3http)],
Expand Down
13 changes: 1 addition & 12 deletions include/nng/supplemental/http/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,6 @@ enum nng_http_status {
// nng_http_req represents an HTTP request.
typedef struct nng_http_req nng_http_req;

// nng_http_req_alloc creates a vanilla HTTP request object. The object is
// initialized with the given URL object for an HTTP/1.1 GET request by
// default. It also adds the Host: header required for HTTP/1.1. If the
// url is NULL, then the uri and Host: header are uninitialized, and will
// need to be set explicitly.
NNG_DECL int nng_http_req_alloc(nng_http_req **, const nng_url *);

// nng_http_req_free frees an HTTP request object.
NNG_DECL void nng_http_req_free(nng_http_req *);

// nng_http_req_get_method returns the method.
NNG_DECL const char *nng_http_req_get_method(const nng_http_req *);

Expand Down Expand Up @@ -244,8 +234,7 @@ NNG_DECL void nng_http_conn_write_all(nng_http_conn *, nng_aio *);

// nng_http_conn_write_req writes the entire request. It will also write any
// data that has been attached.
NNG_DECL void nng_http_conn_write_req(
nng_http_conn *, nng_http_req *, nng_aio *);
NNG_DECL void nng_http_conn_write_req(nng_http_conn *, nng_aio *);

// nng_http_conn_write_res writes the entire response. It will also write any
// data that has been attached. It uses the res object in the conn.
Expand Down
2 changes: 1 addition & 1 deletion src/supplemental/http/http_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ extern int nni_http_req_alloc(nni_http_req **, const nng_url *);
extern int nni_http_res_set_error(nni_http_res *, uint16_t);
extern void nni_http_req_free(nni_http_req *);
extern void nni_http_res_free(nni_http_res *);
extern void nni_http_write_req(nni_http_conn *, nni_http_req *, nni_aio *);
extern void nni_http_write_req(nni_http_conn *, nni_aio *);
extern void nni_http_read_res(nni_http_conn *, nni_aio *);
extern void nni_http_read_req(nni_http_conn *, nni_aio *);
extern void nni_http_write_res(nni_http_conn *, nni_aio *);
Expand Down
10 changes: 1 addition & 9 deletions src/supplemental/http/http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ nni_http_client_connect(nni_http_client *c, nni_aio *aio)
}

typedef enum http_txn_state {
HTTP_CONNECTING,
HTTP_SENDING,
HTTP_RECVING,
HTTP_RECVING_BODY,
Expand Down Expand Up @@ -269,13 +268,6 @@ http_txn_cb(void *arg)
return;
}
switch (txn->state) {
case HTTP_CONNECTING:
txn->conn = nni_aio_get_output(&txn->aio, 0);
txn->state = HTTP_SENDING;
nni_http_write_req(txn->conn, txn->req, &txn->aio);
nni_mtx_unlock(&http_txn_lk);
return;

case HTTP_SENDING:
txn->state = HTTP_RECVING;
nni_http_read_res(txn->conn, &txn->aio);
Expand Down Expand Up @@ -402,6 +394,6 @@ nni_http_transact_conn(nni_http_conn *conn, nni_aio *aio)
return;
}
nni_list_append(&txn->aios, aio);
nni_http_write_req(conn, txn->req, &txn->aio);
nni_http_write_req(conn, &txn->aio);
nni_mtx_unlock(&http_txn_lk);
}
6 changes: 3 additions & 3 deletions src/supplemental/http/http_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ nni_http_read(nni_http_conn *conn, nni_aio *aio)
}

void
nni_http_write_req(nni_http_conn *conn, nni_http_req *req, nni_aio *aio)
nni_http_write_req(nni_http_conn *conn, nni_aio *aio)
{
int rv;
void *buf;
Expand All @@ -568,11 +568,11 @@ nni_http_write_req(nni_http_conn *conn, nni_http_req *req, nni_aio *aio)
nni_iov iov[2];
int niov;

if ((rv = nni_http_req_get_buf(req, &buf, &bufsz)) != 0) {
if ((rv = nni_http_req_get_buf(&conn->req, &buf, &bufsz)) != 0) {
nni_aio_finish_error(aio, rv);
return;
}
nni_http_req_get_data(req, &data, &size);
nni_http_req_get_data(&conn->req, &data, &size);
niov = 1;
iov[0].iov_len = bufsz;
iov[0].iov_buf = buf;
Expand Down
37 changes: 0 additions & 37 deletions src/supplemental/http/http_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,6 @@ nni_http_res_reset(nni_http_res *res)
res->bufsz = 0;
}

void
nni_http_req_free(nni_http_req *req)
{
if (req != NULL) {
nni_http_req_reset(req);
NNI_FREE_STRUCT(req);
}
}

void
nni_http_res_free(nni_http_res *res)
{
if (res != NULL) {
nni_http_res_reset(res);
NNI_FREE_STRUCT(res);
}
}

static int
http_del_header(nni_list *hdrs, const char *key)
{
Expand Down Expand Up @@ -615,25 +597,6 @@ nni_http_req_set_url(nni_http_req *req, const nng_url *url)
return (0);
}

int
nni_http_req_alloc(nni_http_req **reqp, const nng_url *url)
{
nni_http_req *req;
if ((req = NNI_ALLOC_STRUCT(req)) == NULL) {
return (NNG_ENOMEM);
}
nni_http_req_init(req);
if (url != NULL) {
int rv;
if ((rv = nni_http_req_set_url(req, url)) != 0) {
nni_http_req_free(req);
return (rv);
}
}
*reqp = req;
return (0);
}

void
nni_http_res_init(nni_http_res *res)
{
Expand Down
27 changes: 2 additions & 25 deletions src/supplemental/http/http_public.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,6 @@
// Symbols in this file are "public" versions of the HTTP API.
// These are suitable for exposure to applications.

int
nng_http_req_alloc(nng_http_req **reqp, const nng_url *url)
{
#ifdef NNG_SUPP_HTTP
return (nni_http_req_alloc(reqp, url));
#else
NNI_ARG_UNUSED(reqp);
NNI_ARG_UNUSED(url);
return (NNG_ENOTSUP);
#endif
}

void
nng_http_req_free(nng_http_req *req)
{
#ifdef NNG_SUPP_HTTP
nni_http_req_free(req);
#else
NNI_ARG_UNUSED(req);
#endif
}

const char *
nng_http_req_get_header(const nng_http_req *req, const char *key)
{
Expand Down Expand Up @@ -460,13 +438,12 @@ nng_http_conn_write_all(nng_http_conn *conn, nng_aio *aio)
}

void
nng_http_conn_write_req(nng_http_conn *conn, nng_http_req *req, nng_aio *aio)
nng_http_conn_write_req(nng_http_conn *conn, nng_aio *aio)
{
#ifdef NNG_SUPP_HTTP
nni_http_write_req(conn, req, aio);
nni_http_write_req(conn, aio);
#else
NNI_ARG_UNUSED(conn);
NNI_ARG_UNUSED(req);
nni_aio_finish_error(aio, NNG_ENOTSUP);
#endif
}
Expand Down
12 changes: 6 additions & 6 deletions src/supplemental/http/http_server_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ httpdo(struct server_test *st, void **datap, size_t *sizep)
const char *ptr;
nng_http_res *res;

nng_http_conn_write_req(st->conn, st->req, st->aio);
nng_http_conn_write_req(st->conn, st->aio);
nng_aio_wait(st->aio);
if ((rv = nng_aio_result(st->aio)) != 0) {
return (rv);
Expand Down Expand Up @@ -220,7 +220,7 @@ test_server_basic(void)
server_setup(&st, h);

NUTS_PASS(nng_http_req_set_uri(st.req, "/home.html"));
nng_http_conn_write_req(st.conn, st.req, st.aio);
nng_http_conn_write_req(st.conn, st.aio);

nng_aio_wait(st.aio);
NUTS_PASS(nng_aio_result(st.aio));
Expand Down Expand Up @@ -257,7 +257,7 @@ test_server_404(void)
server_setup(&st, NULL);

NUTS_PASS(nng_http_req_set_uri(st.req, "/bogus"));
nng_http_conn_write_req(st.conn, st.req, st.aio);
nng_http_conn_write_req(st.conn, st.aio);

nng_aio_wait(st.aio);
NUTS_PASS(nng_aio_result(st.aio));
Expand All @@ -281,7 +281,7 @@ test_server_bad_version(void)

NUTS_PASS(nng_http_set_version(st.conn, "HTTP/0.9"));
NUTS_PASS(nng_http_req_set_uri(st.req, "/bogus"));
nng_http_conn_write_req(st.conn, st.req, st.aio);
nng_http_conn_write_req(st.conn, st.aio);

nng_aio_wait(st.aio);
NUTS_PASS(nng_aio_result(st.aio));
Expand All @@ -303,7 +303,7 @@ test_server_missing_host(void)

nng_http_req_del_header(st.req, "Host");
NUTS_PASS(nng_http_req_set_uri(st.req, "/bogus"));
nng_http_conn_write_req(st.conn, st.req, st.aio);
nng_http_conn_write_req(st.conn, st.aio);

nng_aio_wait(st.aio);
NUTS_PASS(nng_aio_result(st.aio));
Expand Down Expand Up @@ -345,7 +345,7 @@ test_server_wrong_method(void)

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);
nng_http_conn_write_req(st.conn, st.aio);

nng_aio_wait(st.aio);
NUTS_PASS(nng_aio_result(st.aio));
Expand Down
2 changes: 1 addition & 1 deletion src/supplemental/websocket/websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -2220,7 +2220,7 @@ ws_conn_cb(void *arg)

ws->req = req;

nni_http_write_req(ws->http, req, &ws->httpaio);
nni_http_write_req(ws->http, &ws->httpaio);
nni_mtx_unlock(&ws->mtx);
return;

Expand Down
Loading

0 comments on commit a278744

Please sign in to comment.