From dd15b9d90676ffd7d7d02f5a512eda52a88ead3c Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Fri, 19 Mar 2021 17:55:52 +0100 Subject: [PATCH] Http and proxy refactor --- src/client.c | 46 ++++++++++++---------------------------------- src/http.c | 31 ++++++++++++++++++++++++++----- src/http.h | 43 ++++++++++++++++++++++++++----------------- 3 files changed, 64 insertions(+), 56 deletions(-) diff --git a/src/client.c b/src/client.c index 6288d2e..7edec06 100644 --- a/src/client.c +++ b/src/client.c @@ -366,14 +366,8 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int print("Reverse proxy for " BLD_STR "%s:%i" CLR_STR, conf->rev_proxy.hostname, conf->rev_proxy.port); http_remove_header_field(&res.hdr, "Date", HTTP_REMOVE_ALL); http_remove_header_field(&res.hdr, "Server", HTTP_REMOVE_ALL); - ret = rev_proxy_init(&req, &res, conf, client, &custom_status, err_msg); use_rev_proxy = ret == 0; - - if (http_get_header_field(&res.hdr, "Date") == NULL) - http_add_header_field(&res.hdr, "Date", http_get_date(buf0, sizeof(buf0))); - if (http_get_header_field(&res.hdr, "Server") == NULL) - http_add_header_field(&res.hdr, "Server", SERVER_STR); } else { print(ERR_STR "Unknown host type: %i" CLR_STR, conf->type); res.status = http_get_status(501); @@ -386,35 +380,19 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int } if (!use_fastcgi && !use_rev_proxy && file == NULL && ((res.status->code >= 400 && res.status->code < 600) || err_msg[0] != 0)) { + http_remove_header_field(&res.hdr, "Date", HTTP_REMOVE_ALL); + http_remove_header_field(&res.hdr, "Server", HTTP_REMOVE_ALL); + http_add_header_field(&res.hdr, "Date", http_get_date(buf0, sizeof(buf0))); + http_add_header_field(&res.hdr, "Server", SERVER_STR); + // TODO list Locations on 3xx Redirects - char color[16], mode[16]; - const char *icon, *document; - if (res.status->code >= 100 && res.status->code < 200) { - sprintf(mode, "info"); - sprintf(color, HTTP_COLOR_INFO); - icon = http_info_icon; - document = http_info_document; - } else if (res.status->code >= 200 && res.status->code < 300) { - sprintf(mode, "success"); - sprintf(color, HTTP_COLOR_SUCCESS); - icon = http_success_icon; - document = http_success_document; - } else if (res.status->code >= 300 && res.status->code < 400) { - sprintf(mode, "warning"); - sprintf(color, HTTP_COLOR_WARNING); - icon = http_warning_icon; - document = http_warning_document; - } else if (res.status->code >= 400 && res.status->code < 600) { - sprintf(mode, "error"); - sprintf(color, HTTP_COLOR_ERROR); - icon = http_error_icon; - document = http_error_document; - } - http_error_msg *http_msg = http_get_error_msg(res.status->code); - sprintf(msg_pre_buf, document, res.status->code, res.status->msg, - http_msg != NULL ? http_msg->err_msg : "", err_msg[0] != 0 ? err_msg : ""); - content_length = sprintf(msg_buf, http_default_document, res.status->code, res.status->msg, - msg_pre_buf, mode, icon, color, host); + const http_doc_info *info = http_get_status_info(res.status); + const http_status_msg *http_msg = http_get_error_msg(res.status); + + sprintf(msg_pre_buf, info->doc, res.status->code, res.status->msg, + http_msg != NULL ? http_msg->msg : "", err_msg[0] != 0 ? err_msg : ""); + content_length = snprintf(msg_buf, sizeof(msg_buf), http_default_document, res.status->code, + res.status->msg, msg_pre_buf, info->mode, info->icon, info->color, host); http_add_header_field(&res.hdr, "Content-Type", "text/html; charset=UTF-8"); } if (content_length >= 0) { diff --git a/src/http.c b/src/http.c index e4cb742..74a9083 100644 --- a/src/http.c +++ b/src/http.c @@ -244,7 +244,7 @@ int http_send_request(sock *server, http_req *req) { return 0; } -http_status *http_get_status(unsigned short status_code) { +const http_status *http_get_status(unsigned short status_code) { for (int i = 0; i < sizeof(http_statuses) / sizeof(http_status); i++) { if (http_statuses[i].code == status_code) { return &http_statuses[i]; @@ -253,16 +253,17 @@ http_status *http_get_status(unsigned short status_code) { return NULL; } -http_error_msg *http_get_error_msg(unsigned short status_code) { - for (int i = 0; i < sizeof(http_status_messages) / sizeof(http_error_msg); i++) { - if (http_status_messages[i].code == status_code) { +const http_status_msg *http_get_error_msg(const http_status *status) { + unsigned short code = status->code; + for (int i = 0; i < sizeof(http_status_messages) / sizeof(http_status_msg); i++) { + if (http_status_messages[i].code == code) { return &http_status_messages[i]; } } return NULL; } -const char *http_get_status_color(http_status *status) { +const char *http_get_status_color(const http_status *status) { unsigned short code = status->code; if (code >= 100 && code < 200) { return HTTP_1XX_STR; @@ -289,3 +290,23 @@ char *http_get_date(char *buf, size_t size) { time(&rawtime); return http_format_date(rawtime, buf, size); } + +const http_doc_info *http_get_status_info(const http_status *status) { + unsigned short code = status->code; + static http_doc_info info[] = { + {"info", HTTP_COLOR_INFO, http_info_icon, http_info_document}, + {"success", HTTP_COLOR_SUCCESS, http_success_icon, http_success_document}, + {"warning", HTTP_COLOR_WARNING, http_warning_icon, http_warning_document}, + {"error", HTTP_COLOR_ERROR, http_error_icon, http_error_document} + }; + if (code >= 100 && code < 200) { + return &info[0]; + } else if ((code >= 200 && code < 300) || code == 304) { + return &info[1]; + } else if (code >= 300 && code < 400) { + return &info[2]; + } else if (code >= 400 && code < 600) { + return &info[3]; + } + return NULL; +} diff --git a/src/http.h b/src/http.h index b5275c0..e520e95 100644 --- a/src/http.h +++ b/src/http.h @@ -33,8 +33,15 @@ typedef struct { typedef struct { unsigned short code; - char *err_msg; -} http_error_msg; + const char *msg; +} http_status_msg; + +typedef struct { + char mode[8]; + char color[8]; + const char *icon; + const char *doc; +} http_doc_info; typedef struct { char field_num; @@ -49,12 +56,12 @@ typedef struct { } http_req; typedef struct { - http_status *status; + const http_status *status; char version[3]; http_hdr hdr; } http_res; -http_status http_statuses[] = { +const http_status http_statuses[] = { {100, "Informational", "Continue"}, {101, "Informational", "Switching Protocols"}, @@ -102,7 +109,7 @@ http_status http_statuses[] = { {505, "Server Error", "HTTP Version Not Supported"}, }; -http_error_msg http_status_messages[] = { +const http_status_msg http_status_messages[] = { {100, "The client SHOULD continue with its request."}, {101, "The server understands and is willing to comply with the clients request, via the Upgrade message header field, for a change in the application protocol being used on this connection."}, @@ -149,7 +156,7 @@ http_error_msg http_status_messages[] = { {505, "The server does not support, or refuses to support, the HTTP protocol version that was used in the request message."} }; -const char *http_default_document = +const char http_default_document[] = "\n" "\n" "\n" @@ -189,52 +196,52 @@ const char *http_default_document = "\n" "\n"; -const char *http_error_document = +const char http_error_document[] = "\t\t\t

%1$i

\n" "\t\t\t

%2$s :(

\n" "\t\t\t

%3$s

\n" "\t\t\t

%4$s

\n"; -const char *http_error_icon = +const char http_error_icon[] = "\t\n"; -const char *http_warning_document = +const char http_warning_document[] = "\t\t\t

%1$i

\n" "\t\t\t

%2$s :o

\n" "\t\t\t

%3$s

\n" "\t\t\t

%4$s

\n"; -const char *http_warning_icon = +const char http_warning_icon[] = "\t\n"; -const char *http_success_document = +const char http_success_document[] = "\t\t\t

%1$i

\n" "\t\t\t

%2$s :)

\n" "\t\t\t

%3$s

\n" "\t\t\t

%4$s

\n"; -const char *http_success_icon = +const char http_success_icon[] = "\t\n"; -const char *http_info_document = +const char http_info_document[] = "\t\t\t

%1$i

\n" "\t\t\t

%2$s :)

\n" "\t\t\t

%3$s

\n" "\t\t\t

%4$s

\n"; -const char *http_info_icon = +const char http_info_icon[] = "\t