Skip to content

Commit

Permalink
http: no more need to pass request for http_conn_read_req
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Jan 6, 2025
1 parent 6f34e31 commit 651696e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 26 deletions.
3 changes: 1 addition & 2 deletions include/nng/supplemental/http/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ NNG_DECL void nng_http_conn_write_res(nng_http_conn *, nng_aio *);
// nng_http_conn_read_req reads an entire request, EXCEPT for any entity
// data. The caller is responsible for processing the headers in the request
// and reading any submitted entity data itself.
NNG_DECL void nng_http_conn_read_req(
nng_http_conn *, nng_http_req *, nng_aio *);
NNG_DECL void nng_http_conn_read_req(nng_http_conn *, nng_aio *);

// nng_http_conn_read_res reads an entire response, EXCEPT for any entity
// data. The caller is responsible for processing the headers in the response
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 @@ -118,8 +118,8 @@ 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_read_req(nni_http_conn *, nni_http_req *, nni_aio *);
extern void nni_http_read_res(nni_http_conn *, nni_http_res *, nni_aio *);
extern void nni_http_read_req(nni_http_conn *, nni_aio *);
extern void nni_http_write_res(nni_http_conn *, nni_aio *);

extern const char *nni_http_req_get_header(const nni_http_req *, const char *);
Expand Down
5 changes: 3 additions & 2 deletions src/supplemental/http/http_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,13 @@ http_wr_submit(nni_http_conn *conn, nni_aio *aio, enum write_flavor flavor)
}

void
nni_http_read_req(nni_http_conn *conn, nni_http_req *req, nni_aio *aio)
nni_http_read_req(nni_http_conn *conn, nni_aio *aio)
{
nni_aio_set_prov_data(aio, req);
nni_aio_set_prov_data(aio, &conn->req);

// clear the sent flag (used for the server)
conn->res_sent = false;
nni_http_req_reset(&conn->req);
nni_mtx_lock(&conn->mtx);
http_rd_submit(conn, aio, HTTP_RD_REQ);
nni_mtx_unlock(&conn->mtx);
Expand Down
5 changes: 2 additions & 3 deletions src/supplemental/http/http_public.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,12 @@ nng_http_conn_write_res(nng_http_conn *conn, nng_aio *aio)
}

void
nng_http_conn_read_req(nng_http_conn *conn, nng_http_req *req, nng_aio *aio)
nng_http_conn_read_req(nng_http_conn *conn, nng_aio *aio)
{
#ifdef NNG_SUPP_HTTP
nni_http_read_req(conn, req, aio);
nni_http_read_req(conn, aio);
#else
NNI_ARG_UNUSED(conn);
NNI_ARG_UNUSED(req);
nni_aio_finish_error(aio, NNG_ENOTSUP);
#endif
}
Expand Down
26 changes: 8 additions & 18 deletions src/supplemental/http/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ typedef struct http_sconn {
nni_list_node node;
nni_http_conn *conn;
nni_http_server *server;
nni_http_req *req;
nni_http_res *res;
nni_http_handler *handler; // set if we deferred to read body
nni_http_handler *release; // set if we dispatched handler
bool close;
Expand Down Expand Up @@ -307,16 +305,13 @@ http_sconn_txdatdone(void *arg)
return;
}

sc->res = NULL;

if (sc->close) {
http_sconn_close(sc);
return;
}

sc->handler = NULL;
nni_http_req_reset(sc->req);
nni_http_read_req(sc->conn, sc->req, &sc->rxaio);
nni_http_read_req(sc->conn, &sc->rxaio);
}

static void
Expand All @@ -335,10 +330,8 @@ http_sconn_txdone(void *arg)
return;
}

sc->res = NULL;
sc->handler = NULL;
nni_http_req_reset(sc->req);
nni_http_read_req(sc->conn, sc->req, &sc->rxaio);
nni_http_read_req(sc->conn, &sc->rxaio);
}

static char
Expand Down Expand Up @@ -451,7 +444,6 @@ nni_http_hijack(nni_http_conn *conn)

nni_mtx_lock(&s->mtx);
sc->conn = NULL;
sc->req = NULL;
nni_mtx_unlock(&s->mtx);
}
return (0);
Expand Down Expand Up @@ -523,7 +515,7 @@ http_sconn_rxdone(void *arg)
nni_http_handler *h = NULL;
nni_http_handler *head = NULL;
const char *val;
nni_http_req *req = sc->req;
nni_http_req *req = nng_http_conn_req(sc->conn);
char *uri;
size_t urisz;
char *path;
Expand Down Expand Up @@ -685,8 +677,6 @@ http_sconn_rxdone(void *arg)
finish:
sc->release = h;
sc->handler = NULL;
nni_aio_set_input(&sc->cbaio, 0, sc->req);
nni_aio_set_input(&sc->cbaio, 1, h);

// Set a reference -- this because the callback may be running
// asynchronously even after it gets removed from the server.
Expand Down Expand Up @@ -744,7 +734,9 @@ http_sconn_cbdone(void *arg)
if (sc->close) {
nni_http_res_set_header(res, "Connection", "close");
}
if (strcmp(nni_http_req_get_method(sc->req), "HEAD") == 0) {
if (strcmp(
nni_http_req_get_method(nng_http_conn_req(sc->conn)),
"HEAD") == 0) {
void *data;
size_t size;
// prune off the data, but preserve the content-length
Expand All @@ -763,8 +755,7 @@ http_sconn_cbdone(void *arg)
// Presumably client already sent a response.
// Wait for another request.
sc->handler = NULL;
nni_http_req_reset(sc->req);
nni_http_read_req(sc->conn, sc->req, &sc->rxaio);
nni_http_read_req(sc->conn, &sc->rxaio);
}
}

Expand All @@ -789,7 +780,6 @@ http_sconn_init(http_sconn **scp, nng_stream *stream)
http_sconn_close(sc);
return (rv);
}
sc->req = nni_http_conn_req(sc->conn);

nni_http_conn_set_ctx(sc->conn, sc);
*scp = sc;
Expand Down Expand Up @@ -832,7 +822,7 @@ http_server_acccb(void *arg)
nni_list_append(&s->conns, sc);

sc->handler = NULL;
nni_http_read_req(sc->conn, sc->req, &sc->rxaio);
nni_http_read_req(sc->conn, &sc->rxaio);
nng_stream_listener_accept(s->listener, aio);
nni_mtx_unlock(&s->mtx);
}
Expand Down

0 comments on commit 651696e

Please sign in to comment.