From 497b8e22047fb0efa3397289d23656d6483fdd6d Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 22 Dec 2024 11:52:04 -0800 Subject: http: setting response status never fails (breaking API change) --- src/supplemental/http/http_api.h | 2 +- src/supplemental/http/http_msg.c | 8 ++++---- src/supplemental/http/http_public.c | 5 ++--- src/supplemental/http/http_server.c | 13 +++++++++---- src/supplemental/http/http_server_test.c | 4 ++-- src/supplemental/websocket/websocket.c | 6 +----- 6 files changed, 19 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/supplemental/http/http_api.h b/src/supplemental/http/http_api.h index 27559a48..349f3a49 100644 --- a/src/supplemental/http/http_api.h +++ b/src/supplemental/http/http_api.h @@ -139,7 +139,7 @@ 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 *); -extern int nni_http_res_set_status(nni_http_res *, uint16_t); +extern void nni_http_res_set_status(nni_http_res *, uint16_t); extern const char *nni_http_res_get_version(const nni_http_res *); extern int nni_http_res_set_version(nni_http_res *, const char *); extern const char *nni_http_res_get_reason(const nni_http_res *); diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c index 564a4763..1a288bee 100644 --- a/src/supplemental/http/http_msg.c +++ b/src/supplemental/http/http_msg.c @@ -746,11 +746,10 @@ nni_http_req_set_method(nni_http_req *req, const char *meth) (void) snprintf(req->meth, sizeof(req->meth), "%s", meth); } -int +void nni_http_res_set_status(nni_http_res *res, uint16_t status) { res->code = status; - return (0); } uint16_t @@ -849,8 +848,9 @@ http_res_parse_line(nni_http_res *res, uint8_t *line) return (NNG_EPROTO); } - if (((rv = nni_http_res_set_status(res, (uint16_t) status)) != 0) || - ((rv = nni_http_res_set_version(res, version)) != 0) || + nni_http_res_set_status(res, (uint16_t) status); + + if (((rv = nni_http_res_set_version(res, version)) != 0) || ((rv = nni_http_res_set_reason(res, reason)) != 0)) { return (rv); } diff --git a/src/supplemental/http/http_public.c b/src/supplemental/http/http_public.c index c9983df5..e3093d45 100644 --- a/src/supplemental/http/http_public.c +++ b/src/supplemental/http/http_public.c @@ -348,15 +348,14 @@ nng_http_res_get_reason(const nng_http_res *res) #endif } -int +void nng_http_res_set_status(nng_http_res *res, uint16_t status) { #ifdef NNG_SUPP_HTTP - return (nni_http_res_set_status(res, status)); + nni_http_res_set_status(res, status); #else NNI_ARG_UNUSED(res); NNI_ARG_UNUSED(status); - return (NNG_ENOTSUP); #endif } diff --git a/src/supplemental/http/http_server.c b/src/supplemental/http/http_server.c index ba89626b..fa9ad2f4 100644 --- a/src/supplemental/http/http_server.c +++ b/src/supplemental/http/http_server.c @@ -1416,7 +1416,6 @@ http_handle_file(nni_aio *aio) return; } if (((rv = nni_http_res_alloc(&res)) != 0) || - ((rv = nni_http_res_set_status(res, NNG_HTTP_STATUS_OK)) != 0) || ((rv = nni_http_res_set_header(res, "Content-Type", ctype)) != 0) || ((rv = nni_http_res_copy_data(res, data, size)) != 0)) { @@ -1426,6 +1425,8 @@ http_handle_file(nni_aio *aio) return; } + nni_http_res_set_status(res, NNG_HTTP_STATUS_OK); + nni_free(data, size); nni_aio_set_output(aio, 0, res); nni_aio_finish(aio, 0, 0); @@ -1610,7 +1611,6 @@ http_handle_dir(nni_aio *aio) } if (((rv = nni_http_res_alloc(&res)) != 0) || - ((rv = nni_http_res_set_status(res, NNG_HTTP_STATUS_OK)) != 0) || ((rv = nni_http_res_set_header(res, "Content-Type", ctype)) != 0) || ((rv = nni_http_res_copy_data(res, data, size)) != 0)) { @@ -1620,6 +1620,8 @@ http_handle_dir(nni_aio *aio) return; } + nni_http_res_set_status(res, NNG_HTTP_STATUS_OK); + nni_free(data, size); nni_aio_set_output(aio, 0, res); nni_aio_finish(aio, 0, 0); @@ -1707,7 +1709,6 @@ http_handle_redirect(nni_aio *aio) // discard it. if ((rv != 0) || ((rv = nni_http_res_alloc(&r)) != 0) || ((rv = nni_http_alloc_html_error(&html, hr->code, msg)) != 0) || - ((rv = nni_http_res_set_status(r, hr->code)) != 0) || ((rv = nni_http_res_set_header(r, "Connection", "close")) != 0) || ((rv = nni_http_res_set_header( r, "Content-Type", "text/html; charset=UTF-8")) != 0) || @@ -1722,6 +1723,9 @@ http_handle_redirect(nni_aio *aio) nni_aio_finish_error(aio, rv); return; } + + nni_http_res_set_status(r, hr->code); + if (loc != hr->where) { nni_strfree(loc); } @@ -1807,13 +1811,14 @@ http_handle_static(nni_aio *aio) if (((rv = nni_http_res_alloc(&r)) != 0) || ((rv = nni_http_res_set_header(r, "Content-Type", ctype)) != 0) || - ((rv = nni_http_res_set_status(r, NNG_HTTP_STATUS_OK)) != 0) || ((rv = nni_http_res_set_data(r, hs->data, hs->size)) != 0)) { nni_http_res_free(r); nni_aio_finish_error(aio, rv); return; } + nni_http_res_set_status(r, NNG_HTTP_STATUS_OK); + nni_aio_set_output(aio, 0, r); nni_aio_finish(aio, 0, 0); } diff --git a/src/supplemental/http/http_server_test.c b/src/supplemental/http/http_server_test.c index ddae77d3..f29fa52b 100644 --- a/src/supplemental/http/http_server_test.c +++ b/src/supplemental/http/http_server_test.c @@ -155,12 +155,12 @@ httpecho(nng_aio *aio) if (((rv = nng_http_res_alloc(&res)) != 0) || ((rv = nng_http_res_copy_data(res, body, len)) != 0) || ((rv = nng_http_res_set_header( - res, "Content-type", "text/plain")) != 0) || - ((rv = nng_http_res_set_status(res, NNG_HTTP_STATUS_OK)) != 0)) { + res, "Content-type", "text/plain")) != 0)) { nng_http_res_free(res); nng_aio_finish(aio, rv); return; } + nng_http_res_set_status(res, NNG_HTTP_STATUS_OK); nng_aio_set_output(aio, 0, res); nng_aio_finish(aio, 0); } diff --git a/src/supplemental/websocket/websocket.c b/src/supplemental/websocket/websocket.c index e7372a49..3dfb3e8e 100644 --- a/src/supplemental/websocket/websocket.c +++ b/src/supplemental/websocket/websocket.c @@ -1604,11 +1604,7 @@ ws_handler(nni_aio *aio) goto err; } - if (nni_http_res_set_status(res, NNG_HTTP_STATUS_SWITCHING) != 0) { - nni_http_res_free(res); - status = NNG_HTTP_STATUS_INTERNAL_SERVER_ERROR; - goto err; - } + nni_http_res_set_status(res, NNG_HTTP_STATUS_SWITCHING); if ((SETH("Connection", "Upgrade") != 0) || (SETH("Upgrade", "websocket") != 0) || -- cgit v1.2.3-70-g09d2