Http and proxy refactor
This commit is contained in:
46
src/client.c
46
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) {
|
||||
|
31
src/http.c
31
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;
|
||||
}
|
||||
|
43
src/http.h
43
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[] =
|
||||
"<!DOCTYPE html>\n"
|
||||
"<html lang=\"en\">\n"
|
||||
"<head>\n"
|
||||
@ -189,52 +196,52 @@ const char *http_default_document =
|
||||
"</body>\n"
|
||||
"</html>\n";
|
||||
|
||||
const char *http_error_document =
|
||||
const char http_error_document[] =
|
||||
"\t\t\t<h1>%1$i</h1>\n"
|
||||
"\t\t\t<h2>%2$s :(</h2>\n"
|
||||
"\t\t\t<p>%3$s</p>\n"
|
||||
"\t\t\t<p>%4$s</p>\n";
|
||||
|
||||
const char *http_error_icon =
|
||||
const char http_error_icon[] =
|
||||
"\t<link rel=\"shortcut icon\" type=\"image/svg+xml\" sizes=\"any\" href=\"data:image/svg+xml;base64,"
|
||||
"PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAw"
|
||||
"L3N2ZyI+PHRleHQgeD0iNCIgeT0iMTIiIGZpbGw9IiNDMDAwMDAiIHN0eWxlPSJmb250LWZhbWls"
|
||||
"eTonQXJpYWwnLHNhbnMtc2VyaWYiPjooPC90ZXh0Pjwvc3ZnPgo=\"/>\n";
|
||||
|
||||
|
||||
const char *http_warning_document =
|
||||
const char http_warning_document[] =
|
||||
"\t\t\t<h1>%1$i</h1>\n"
|
||||
"\t\t\t<h2>%2$s :o</h2>\n"
|
||||
"\t\t\t<p>%3$s</p>\n"
|
||||
"\t\t\t<p>%4$s</p>\n";
|
||||
|
||||
const char *http_warning_icon =
|
||||
const char http_warning_icon[] =
|
||||
"\t<link rel=\"shortcut icon\" type=\"image/svg+xml\" sizes=\"any\" href=\"data:image/svg+xml;base64,"
|
||||
"PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAw"
|
||||
"L3N2ZyI+PHRleHQgeD0iNCIgeT0iMTIiIGZpbGw9IiNFMEMwMDAiIHN0eWxlPSJmb250LWZhbWls"
|
||||
"eTonQXJpYWwnLHNhbnMtc2VyaWYiPjpvPC90ZXh0Pjwvc3ZnPgo=\"/>\n";
|
||||
|
||||
|
||||
const char *http_success_document =
|
||||
const char http_success_document[] =
|
||||
"\t\t\t<h1>%1$i</h1>\n"
|
||||
"\t\t\t<h2>%2$s :)</h2>\n"
|
||||
"\t\t\t<p>%3$s</p>\n"
|
||||
"\t\t\t<p>%4$s</p>\n";
|
||||
|
||||
const char *http_success_icon =
|
||||
const char http_success_icon[] =
|
||||
"\t<link rel=\"shortcut icon\" type=\"image/svg+xml\" sizes=\"any\" href=\"data:image/svg+xml;base64,"
|
||||
"PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAw"
|
||||
"L3N2ZyI+PHRleHQgeD0iNCIgeT0iMTIiIGZpbGw9IiMwMDgwMDAiIHN0eWxlPSJmb250LWZhbWls"
|
||||
"eTonQXJpYWwnLHNhbnMtc2VyaWYiPjopPC90ZXh0Pjwvc3ZnPgo=\"/>\n";
|
||||
|
||||
|
||||
const char *http_info_document =
|
||||
const char http_info_document[] =
|
||||
"\t\t\t<h1>%1$i</h1>\n"
|
||||
"\t\t\t<h2>%2$s :)</h2>\n"
|
||||
"\t\t\t<p>%3$s</p>\n"
|
||||
"\t\t\t<p>%4$s</p>\n";
|
||||
|
||||
const char *http_info_icon =
|
||||
const char http_info_icon[] =
|
||||
"\t<link rel=\"shortcut icon\" type=\"image/svg+xml\" sizes=\"any\" href=\"data:image/svg+xml;base64,"
|
||||
"PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAw"
|
||||
"L3N2ZyI+PHRleHQgeD0iNCIgeT0iMTIiIGZpbGw9IiM2MDYwNjAiIHN0eWxlPSJmb250LWZhbWls"
|
||||
@ -263,14 +270,16 @@ int http_send_response(sock *client, http_res *res);
|
||||
|
||||
int http_send_request(sock *server, http_req *req);
|
||||
|
||||
http_status *http_get_status(unsigned short status_code);
|
||||
const http_status *http_get_status(unsigned short status_code);
|
||||
|
||||
http_error_msg *http_get_error_msg(unsigned short status_code);
|
||||
const http_status_msg *http_get_error_msg(const http_status *status);
|
||||
|
||||
const char *http_get_status_color(http_status *status);
|
||||
const char *http_get_status_color(const http_status *status);
|
||||
|
||||
char *http_format_date(time_t time, char *buf, size_t size);
|
||||
|
||||
char *http_get_date(char *buf, size_t size);
|
||||
|
||||
const http_doc_info *http_get_status_info(const http_status *status);
|
||||
|
||||
#endif //NECRONDA_SERVER_HTTP_H
|
||||
|
Reference in New Issue
Block a user