From 617bb5112834eee40d7eaf00bfc7e98e0ae1ff01 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 7 Oct 2018 13:04:00 -0700 Subject: fixes #745 HTTP server redirect handler --- src/supplemental/http/http_msg.c | 65 +++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 24 deletions(-) (limited to 'src/supplemental/http/http_msg.c') diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c index 3b78a2a9..da60c746 100644 --- a/src/supplemental/http/http_msg.c +++ b/src/supplemental/http/http_msg.c @@ -128,15 +128,19 @@ nni_http_res_reset(nni_http_res *res) void nni_http_req_free(nni_http_req *req) { - nni_http_req_reset(req); - NNI_FREE_STRUCT(req); + if (req != NULL) { + nni_http_req_reset(req); + NNI_FREE_STRUCT(req); + } } void nni_http_res_free(nni_http_res *res) { - nni_http_res_reset(res); - NNI_FREE_STRUCT(res); + if (res != NULL) { + nni_http_res_reset(res); + NNI_FREE_STRUCT(res); + } } static int @@ -1011,37 +1015,50 @@ nni_http_res_set_reason(nni_http_res *res, const char *reason) return (http_set_string(&res->rsn, reason)); } +int +nni_http_alloc_html_error(char **html, uint16_t code, const char *details) +{ + const char *rsn = nni_http_reason(code); + + return (nni_asprintf(html, + "\n" + "%d %s\n" + "" + "

 

" + "

%d

" + "

%s

" + "

%s

" + "", + code, rsn, code, rsn, details != NULL ? details : "")); +} + int nni_http_res_alloc_error(nni_http_res **resp, uint16_t err) { - char html[512]; + char * html = NULL; + nni_http_res *res = NULL; int rv; - nni_http_res *res; - if ((rv = nni_http_res_alloc(&res)) != 0) { - return (rv); - } - - // very simple builtin error page - (void) snprintf(html, sizeof(html), - "%d %s" - "

" - "%d

" - "

" - "" - "%s

", - err, nni_http_reason(err), err, nni_http_reason(err)); - - res->code = err; - if (((rv = nni_http_res_set_header( + if (((rv = nni_http_res_alloc(&res)) != 0) || + ((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); } -- cgit v1.2.3-70-g09d2