diff options
| author | Garrett D'Amore <garrett@damore.org> | 2025-01-05 16:46:03 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2025-01-06 13:58:07 -0800 |
| commit | f42d0c6ef956d119e8762a3ecda37886fa055637 (patch) | |
| tree | 1744a559dafafdfecd906608888bf0cb9f6c4d10 /src/supplemental/http/http_msg.c | |
| parent | bce6a79fc55852032e9d653b099a121353aaa238 (diff) | |
| download | nng-f42d0c6ef956d119e8762a3ecda37886fa055637.tar.gz nng-f42d0c6ef956d119e8762a3ecda37886fa055637.tar.bz2 nng-f42d0c6ef956d119e8762a3ecda37886fa055637.zip | |
http: server callback API simplified
This simplified API lets callbacks obtain the response from the
connection objection directly, and does not require the aio to carry
it as a parameter. Further, the request and response are both
stored inline in the connection, reducing allocations.
This is at present only for the server; the client will get a similar
set of changes.
Diffstat (limited to 'src/supplemental/http/http_msg.c')
| -rw-r--r-- | src/supplemental/http/http_msg.c | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c index 08c594ac..6b12496c 100644 --- a/src/supplemental/http/http_msg.c +++ b/src/supplemental/http/http_msg.c @@ -263,8 +263,10 @@ static int http_entity_alloc_data(nni_http_entity *entity, size_t size) { void *newdata; - if ((newdata = nni_zalloc(size)) == NULL) { - return (NNG_ENOMEM); + if (size != 0) { + if ((newdata = nni_zalloc(size)) == NULL) { + return (NNG_ENOMEM); + } } http_entity_set_data(entity, newdata, size); entity->own = true; @@ -1057,25 +1059,37 @@ nni_http_alloc_html_error(char **html, uint16_t code, const char *details) } int -nni_http_res_alloc_error(nni_http_res **resp, uint16_t err) +nni_http_res_set_error(nni_http_res *res, uint16_t err) { - char *html = NULL; - nni_http_res *res = NULL; - int rv; - - if (((rv = nni_http_res_alloc(&res)) != 0) || - ((rv = nni_http_alloc_html_error(&html, err, NULL)) != 0) || + int rv; + char *html = NULL; + if (((rv = nni_http_alloc_html_error(&html, err, NULL)) != 0) || ((rv = nni_http_res_set_header( res, "Content-Type", "text/html; charset=UTF-8")) != 0) || ((rv = nni_http_res_copy_data(res, html, strlen(html))) != 0)) { nni_strfree(html); - nni_http_res_free(res); - } else { - nni_strfree(html); - res->code = err; - res->iserr = true; - *resp = res; + return (rv); } + nni_strfree(html); + res->code = err; + res->iserr = true; + return (0); +} - return (rv); +int +nni_http_res_alloc_error(nni_http_res **resp, uint16_t err) +{ + nni_http_res *res; + int rv; + + if ((rv = nni_http_res_alloc(&res)) != 0) { + return (rv); + } + rv = nni_http_res_set_error(res, err); + if (rv != 0) { + nni_http_res_free(res); + return (rv); + } + *resp = res; + return (0); } |
