Refactored http default error document

This commit is contained in:
2020-12-15 19:53:21 +01:00
parent 97b04173ee
commit 93973c6e77
2 changed files with 19 additions and 42 deletions

View File

@ -40,6 +40,7 @@ int client_request_handler(sock *client, int req_num) {
int ret, client_keep_alive; int ret, client_keep_alive;
char buf[64]; char buf[64];
char msg_buf[4096]; char msg_buf[4096];
char msg_pre_buf[4096];
char err_msg[256]; char err_msg[256];
err_msg[0] = 0; err_msg[0] = 0;
char *host, *hdr_connection, *webroot; char *host, *hdr_connection, *webroot;
@ -107,9 +108,10 @@ int client_request_handler(sock *client, int req_num) {
int len = 0; int len = 0;
if (res.status->code >= 300 && res.status->code < 600) { if (res.status->code >= 300 && res.status->code < 600) {
http_error_msg *http_msg = http_get_error_msg(res.status->code); http_error_msg *http_msg = http_get_error_msg(res.status->code);
len = sprintf(msg_buf, http_error_document, res.status->code, res.status->msg, sprintf(msg_pre_buf, http_error_document, res.status->code, res.status->msg,
http_msg != NULL ? http_msg->err_msg : "", err_msg[0] != 0 ? err_msg : "", http_msg != NULL ? http_msg->err_msg : "", err_msg[0] != 0 ? err_msg : "");
res.status->code >= 300 && res.status->code < 400 ? "info" : "error"); len = sprintf(msg_buf, http_default_document, res.status->code, res.status->msg,
msg_pre_buf, res.status->code >= 300 && res.status->code < 400 ? "info" : "error");
sprintf(buf, "%i", len); sprintf(buf, "%i", len);
http_add_header_field(&res.hdr, "Content-Length", buf); http_add_header_field(&res.hdr, "Content-Length", buf);
http_add_header_field(&res.hdr, "Content-Type", "text/html; charset=UTF-8"); http_add_header_field(&res.hdr, "Content-Type", "text/html; charset=UTF-8");

View File

@ -111,7 +111,7 @@ http_error_msg http_error_messages[] = {
{505, "The server does not support, or refuses to support, the HTTP protocol version that was used in the request message."} {505, "The server does not support, or refuses to support, the HTTP protocol version that was used in the request message."}
}; };
const char *http_error_document = const char *http_default_document =
"<!DOCTYPE html>\n" "<!DOCTYPE html>\n"
"<html lang=\"en\">\n" "<html lang=\"en\">\n"
"<head>\n" "<head>\n"
@ -119,53 +119,28 @@ const char *http_error_document =
" <meta charset=\"UTF-8\"/>\n" " <meta charset=\"UTF-8\"/>\n"
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>\n" " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>\n"
" <style>\n" " <style>\n"
" html {\n" " html {font-family: \"Arial\", sans-serif; --error: #C00000; --info: #E0C000; --color: var(--%4$s);}\n"
" font-family: \"Arial\", sans-serif;\n" " body {background-color: #F0F0F0; margin: 0.5em;}\n"
" --error: #C00000;\n" " main {max-width: 600px; margin: 2em auto; background-color: #FFFFFF; border: 1px solid var(--color); border-radius: 4px; padding: 1em 2em;}\n"
" --info: #E0C000;\n" " h1, h2, h3, h4, h5, h6, h7 {text-align: center;color: var(--color);}\n"
" --color: var(--%5$s);\n" " h1 {margin: 0.5em 0; font-size: 1.5em;}\n"
" }\n" " p {text-align: center;}\n"
" body {\n" " div.footer {color: #808080; font-size: 0.75em; text-align: center; margin: 0.5em 0;}\n"
" background-color: #F0F0F0;\n"
" margin: 0.5em;\n"
" }\n"
" main {\n"
" max-width: 600px;\n"
" margin: 2em auto;\n"
" background-color: #FFFFFF;\n"
" border: 1px solid var(--color);\n"
" border-radius: 4px;\n"
" padding: 1em 2em;\n"
" }\n"
" h1, h2, h3, h4, h5, h6, h7 {\n"
" text-align: center;\n"
" color: var(--color);\n"
" }\n"
" h1 {\n"
" margin: 0.5em 0;\n"
" font-size: 1.5em;\n"
" }\n"
" p {\n"
" text-align: center;\n"
" }\n"
" div.footer {\n"
" color: #808080;\n"
" font-size: 0.75em;\n"
" text-align: center;\n"
" margin: 0.5em 0;\n"
" }\n"
" </style>\n" " </style>\n"
"</head>\n" "</head>\n"
"<body>\n" "<body>\n"
" <main>\n" " <main>\n"
" <h1>%1$i %2$s</h1>\n" "%3$s"
" <p>%3$s</p>\n"
" <p>%4$s</p>\n"
" <div class=\"footer\">Necronda web server " NECRONDA_VERSION "</div>\n" " <div class=\"footer\">Necronda web server " NECRONDA_VERSION "</div>\n"
" </main>\n" " </main>\n"
"</body>\n" "</body>\n"
"</html>\n"; "</html>\n";
const char *http_error_document =
" <h1>%1$i %2$s :(</h1>\n"
" <p>%3$s</p>\n"
" <p>%4$s</p>\n";
void http_to_camel_case(char *str); void http_to_camel_case(char *str);