diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-10-07 13:04:00 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-10-07 13:14:13 -0700 |
| commit | 617bb5112834eee40d7eaf00bfc7e98e0ae1ff01 (patch) | |
| tree | 686a5566b64d1cb79b495e00f5c106145f58b74b /src/supplemental/http/http_msg.c | |
| parent | 6c334f30cccaa9ddae81ee0865621b6695fb7e3a (diff) | |
| download | nng-617bb5112834eee40d7eaf00bfc7e98e0ae1ff01.tar.gz nng-617bb5112834eee40d7eaf00bfc7e98e0ae1ff01.tar.bz2 nng-617bb5112834eee40d7eaf00bfc7e98e0ae1ff01.zip | |
fixes #745 HTTP server redirect handler
Diffstat (limited to 'src/supplemental/http/http_msg.c')
| -rw-r--r-- | src/supplemental/http/http_msg.c | 65 |
1 files changed, 41 insertions, 24 deletions
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 @@ -1012,36 +1016,49 @@ nni_http_res_set_reason(nni_http_res *res, const char *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, + "<!DOCTYPE html>\n" + "<html><head><title>%d %s</title>\n" + "<style>" + "body { font-family: Arial, sans serif; text-align: center }\n" + "h1 { font-size: 36px; }" + "span { background-color: gray; color: white; padding: 7px; " + "border-radius: 5px }" + "h2 { font-size: 24px; }" + "p { font-size: 20px; }" + "</style></head>" + "<body><p> </p>" + "<h1><span>%d</span></h1>" + "<h2>%s</h2>" + "<p>%s</p>" + "</body></html>", + 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), - "<head><title>%d %s</title></head>" - "<body><p/><h1 align=\"center\">" - "<span style=\"font-size: 36px; border-radius: 5px; " - "background-color: black; color: white; padding: 7px; " - "font-family: Arial, sans serif;\">%d</span></h1>" - "<p align=\"center\">" - "<span style=\"font-size: 24px; font-family: Arial, sans serif;\">" - "%s</span></p></body>", - 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); } |
