Added error documents and 203 response if webroot does not exist
This commit is contained in:
41
src/client.c
41
src/client.c
@ -93,7 +93,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
|
|||||||
|
|
||||||
hdr_connection = http_get_header_field(&req.hdr, "Connection");
|
hdr_connection = http_get_header_field(&req.hdr, "Connection");
|
||||||
client_keep_alive = hdr_connection != NULL &&
|
client_keep_alive = hdr_connection != NULL &&
|
||||||
(strcmp(hdr_connection, "keep-alive") == 0 || strcmp(hdr_connection, "Keep-Alive") == 0);
|
(strcmp(hdr_connection, "keep-alive") == 0 || strcmp(hdr_connection, "Keep-Alive") == 0);
|
||||||
host_ptr = http_get_header_field(&req.hdr, "Host");
|
host_ptr = http_get_header_field(&req.hdr, "Host");
|
||||||
if (host_ptr != NULL && strlen(host_ptr) > 255) {
|
if (host_ptr != NULL && strlen(host_ptr) > 255) {
|
||||||
host[0] = 0;
|
host[0] = 0;
|
||||||
@ -132,10 +132,14 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
|
|||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
if (ret == 1) {
|
if (ret == 1) {
|
||||||
sprintf(err_msg, "Invalid URI: has to start with slash.");
|
sprintf(err_msg, "Invalid URI: has to start with slash.");
|
||||||
|
res.status = http_get_status(400);
|
||||||
} else if (ret == 2) {
|
} else if (ret == 2) {
|
||||||
sprintf(err_msg, "Invalid URI: contains relative path change (/../).");
|
sprintf(err_msg, "Invalid URI: contains relative path change (/../).");
|
||||||
|
res.status = http_get_status(400);
|
||||||
|
} else if (ret == 3) {
|
||||||
|
sprintf(err_msg, "The specified webroot directory does not exist.");
|
||||||
|
res.status = http_get_status(203);
|
||||||
}
|
}
|
||||||
res.status = http_get_status(400);
|
|
||||||
goto respond;
|
goto respond;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,7 +215,8 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
|
|||||||
char *if_modified_since = http_get_header_field(&req.hdr, "If-Modified-Since");
|
char *if_modified_since = http_get_header_field(&req.hdr, "If-Modified-Since");
|
||||||
char *if_none_match = http_get_header_field(&req.hdr, "If-None-Match");
|
char *if_none_match = http_get_header_field(&req.hdr, "If-None-Match");
|
||||||
if ((if_none_match != NULL && strstr(if_none_match, uri.meta->etag) == NULL) ||
|
if ((if_none_match != NULL && strstr(if_none_match, uri.meta->etag) == NULL) ||
|
||||||
(accept_if_modified_since && if_modified_since != NULL && strcmp(if_modified_since, last_modified) == 0)) {
|
(accept_if_modified_since && if_modified_since != NULL &&
|
||||||
|
strcmp(if_modified_since, last_modified) == 0)) {
|
||||||
res.status = http_get_status(304);
|
res.status = http_get_status(304);
|
||||||
goto respond;
|
goto respond;
|
||||||
}
|
}
|
||||||
@ -360,13 +365,35 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
|
|||||||
http_add_header_field(&res.hdr, "Accept-Ranges", "none");
|
http_add_header_field(&res.hdr, "Accept-Ranges", "none");
|
||||||
}
|
}
|
||||||
if (!use_fastcgi && !use_rev_proxy && file == NULL &&
|
if (!use_fastcgi && !use_rev_proxy && file == NULL &&
|
||||||
res.status->code >= 400 && res.status->code < 600) {
|
((res.status->code >= 400 && res.status->code < 600) || err_msg[0] != 0)) {
|
||||||
|
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);
|
http_error_msg *http_msg = http_get_error_msg(res.status->code);
|
||||||
sprintf(msg_pre_buf, http_error_document, res.status->code, res.status->msg,
|
sprintf(msg_pre_buf, 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 : "");
|
||||||
content_length = sprintf(msg_buf, http_default_document, res.status->code, res.status->msg,
|
content_length = 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",
|
msg_pre_buf, mode, icon, color, host);
|
||||||
http_error_icon, "#C00000", host);
|
|
||||||
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");
|
||||||
}
|
}
|
||||||
if (content_length >= 0) {
|
if (content_length >= 0) {
|
||||||
|
17
src/debug.c
Normal file
17
src/debug.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
//
|
||||||
|
// Created by lorenz on 17.01.21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "fastcgi.c"
|
||||||
|
#include "http.c"
|
||||||
|
#include "sock.c"
|
||||||
|
|
||||||
|
const char *msg = "PHP message: PHP Notice: Undefined index: Verifiedd in /srv/necronda/www.necronda.net/admin/users.php on line 58PHP message: PHP Notice: Undefined index: Verifiedd in /srv/necronda/www.necronda.net/admin/users.php on line 58PHP message: PHP Notice: Undefined index: Verifiedd in /srv/necronda/www.necronda.net/admin/users.php on line 58PHP message: PHP Notice: Undefined index: Verifiedd in /srv/necronda/www.necronda.net/admin/users.php on line 58PHP message: PHP Notice: Undefined index: Verifiedd in /srv/necronda/www.necronda.net/admin/users.php on line 58PHP message: PHP Notice: Undefined index: Verifiedd in /srv/necronda/www.necronda.net/admin/users.php on line 58PHP message: PHP Notice: Undefined index: Verifiedd in /srv/necronda/www.necronda.net/admin/users.php on line 58PHP message: PHP Notice: Undefined index: Verifiedd in /srv/necronda/www.necronda.net/admin/users.php on line 58";
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char err[256];
|
||||||
|
int ret = fastcgi_php_error(msg, strlen(msg), err);
|
||||||
|
printf("%i\n", ret);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -254,9 +254,9 @@ http_status *http_get_status(unsigned short status_code) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
http_error_msg *http_get_error_msg(unsigned short status_code) {
|
http_error_msg *http_get_error_msg(unsigned short status_code) {
|
||||||
for (int i = 0; i < sizeof(http_error_messages) / sizeof(http_error_msg); i++) {
|
for (int i = 0; i < sizeof(http_status_messages) / sizeof(http_error_msg); i++) {
|
||||||
if (http_error_messages[i].code == status_code) {
|
if (http_status_messages[i].code == status_code) {
|
||||||
return &http_error_messages[i];
|
return &http_status_messages[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
72
src/http.h
72
src/http.h
@ -16,6 +16,11 @@
|
|||||||
#define HTTP_REMOVE_ALL 1
|
#define HTTP_REMOVE_ALL 1
|
||||||
#define HTTP_REMOVE_LAST 2
|
#define HTTP_REMOVE_LAST 2
|
||||||
|
|
||||||
|
#define HTTP_COLOR_SUCCESS "#008000"
|
||||||
|
#define HTTP_COLOR_INFO "#606060"
|
||||||
|
#define HTTP_COLOR_WARNING "#E0C000"
|
||||||
|
#define HTTP_COLOR_ERROR "#C00000"
|
||||||
|
|
||||||
#include "sock.h"
|
#include "sock.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
@ -65,7 +70,7 @@ http_status http_statuses[] = {
|
|||||||
{301, "Redirection", "Moved Permanently"},
|
{301, "Redirection", "Moved Permanently"},
|
||||||
{302, "Redirection", "Found"},
|
{302, "Redirection", "Found"},
|
||||||
{303, "Redirection", "See Other"},
|
{303, "Redirection", "See Other"},
|
||||||
{304, "Redirection", "Not Modified"},
|
{304, "Success", "Not Modified"},
|
||||||
{305, "Redirection", "Use Proxy"},
|
{305, "Redirection", "Use Proxy"},
|
||||||
{307, "Redirection", "Temporary Redirect"},
|
{307, "Redirection", "Temporary Redirect"},
|
||||||
{308, "Redirection", "Permanent Redirect"},
|
{308, "Redirection", "Permanent Redirect"},
|
||||||
@ -97,7 +102,27 @@ http_status http_statuses[] = {
|
|||||||
{505, "Server Error", "HTTP Version Not Supported"},
|
{505, "Server Error", "HTTP Version Not Supported"},
|
||||||
};
|
};
|
||||||
|
|
||||||
http_error_msg http_error_messages[] = {
|
http_error_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."},
|
||||||
|
|
||||||
|
{200, "The request has succeeded."},
|
||||||
|
{201, "The request has been fulfilled and resulted in a new resource being created."},
|
||||||
|
{202, "The request has been accepted for processing, but the processing has not been completed."},
|
||||||
|
{203, "The returned meta information in the entity-header is not the definitive set as available from the origin server, but is gathered from a local or a third-party copy."},
|
||||||
|
{204, "The server has fulfilled the request but does not need to return an entity-body, and might want to return updated meta information."},
|
||||||
|
{205, "The server has fulfilled the request and the user agent SHOULD reset the document view which caused the request to be sent."},
|
||||||
|
{206, "The server has fulfilled the partial GET request for the resource."},
|
||||||
|
|
||||||
|
{300, "The requested resource corresponds to any one of a set of representations, each with its own specific location, and agent-driven negotiation information is being provided so that the user (or user agent) can select a preferred representation and redirect its request to that location."},
|
||||||
|
{301, "The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs."},
|
||||||
|
{302, "The requested resource resides temporarily under a different URI."},
|
||||||
|
{303, "The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource."},
|
||||||
|
{304, "The request has been fulfilled and the requested resource has not been modified."},
|
||||||
|
{305, "The requested resource MUST be accessed through the proxy given by the Location field."},
|
||||||
|
{307, "The requested resource resides temporarily under a different URI."},
|
||||||
|
{308, "The requested resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs."},
|
||||||
|
|
||||||
{400, "The request could not be understood by the server due to malformed syntax."},
|
{400, "The request could not be understood by the server due to malformed syntax."},
|
||||||
{401, "The request requires user authentication."},
|
{401, "The request requires user authentication."},
|
||||||
{403, "The server understood the request, but is refusing to fulfill it."},
|
{403, "The server understood the request, but is refusing to fulfill it."},
|
||||||
@ -113,7 +138,7 @@ http_error_msg http_error_messages[] = {
|
|||||||
{413, "The server is refusing to process a request because the request entity is larger than the server is willing or able to process."},
|
{413, "The server is refusing to process a request because the request entity is larger than the server is willing or able to process."},
|
||||||
{414, "The server is refusing to service the request because the Request-URI is longer than the server is willing to interpret."},
|
{414, "The server is refusing to service the request because the Request-URI is longer than the server is willing to interpret."},
|
||||||
{415, "The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method."},
|
{415, "The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method."},
|
||||||
{416, "None of the ranges in the request's Range header field overlap the current extent of the selected resource or that the set of ranges requested has been rejected due to invalid ranges or an excessive request of small or overlapping ranges."},
|
{416, "None of the ranges in the requests Range header field overlap the current extent of the selected resource or that the set of ranges requested has been rejected due to invalid ranges or an excessive request of small or overlapping ranges."},
|
||||||
{417, "The expectation given in an Expect request-header field could not be met by this server, or, if the server is a proxy, the server has unambiguous evidence that the request could not be met by the next-hop server."},
|
{417, "The expectation given in an Expect request-header field could not be met by this server, or, if the server is a proxy, the server has unambiguous evidence that the request could not be met by the next-hop server."},
|
||||||
|
|
||||||
{500, "The server encountered an unexpected condition which prevented it from fulfilling the request."},
|
{500, "The server encountered an unexpected condition which prevented it from fulfilling the request."},
|
||||||
@ -136,7 +161,7 @@ const char *http_default_document =
|
|||||||
"\t<meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\"/>\n"
|
"\t<meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\"/>\n"
|
||||||
"%5$s"
|
"%5$s"
|
||||||
"\t<style>\n"
|
"\t<style>\n"
|
||||||
"\t\thtml{font-family:\"Arial\",sans-serif;--error:#C00000;--info:#E0C000;--color:var(--%4$s);}\n"
|
"\t\thtml{font-family:\"Arial\",sans-serif;--error:" HTTP_COLOR_ERROR ";--warning:" HTTP_COLOR_WARNING ";--success:" HTTP_COLOR_SUCCESS ";--info:" HTTP_COLOR_INFO ";--color:var(--%4$s);}\n"
|
||||||
"\t\tbody{background-color:#F0F0F0;margin:0;}\n"
|
"\t\tbody{background-color:#F0F0F0;margin:0;}\n"
|
||||||
"\t\tmain{max-width:650px;margin:2em auto;}\n"
|
"\t\tmain{max-width:650px;margin:2em auto;}\n"
|
||||||
"\t\tsection{margin:1em;background-color:#FFFFFF;border: 1px solid var(--color);border-radius:4px;padding:1em;}\n"
|
"\t\tsection{margin:1em;background-color:#FFFFFF;border: 1px solid var(--color);border-radius:4px;padding:1em;}\n"
|
||||||
@ -176,6 +201,45 @@ const char *http_error_icon =
|
|||||||
"eTonQXJpYWwnLHNhbnMtc2VyaWYiPjooPC90ZXh0Pjwvc3ZnPgo=\"/>\n";
|
"eTonQXJpYWwnLHNhbnMtc2VyaWYiPjooPC90ZXh0Pjwvc3ZnPgo=\"/>\n";
|
||||||
|
|
||||||
|
|
||||||
|
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 =
|
||||||
|
"\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 =
|
||||||
|
"\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 =
|
||||||
|
"\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 =
|
||||||
|
"\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 =
|
||||||
|
"\t<link rel=\"shortcut icon\" type=\"image/svg+xml\" sizes=\"any\" href=\"data:image/svg+xml;base64,"
|
||||||
|
"PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAw"
|
||||||
|
"L3N2ZyI+PHRleHQgeD0iNCIgeT0iMTIiIGZpbGw9IiM2MDYwNjAiIHN0eWxlPSJmb250LWZhbWls"
|
||||||
|
"eTonQXJpYWwnLHNhbnMtc2VyaWYiPjopPC90ZXh0Pjwvc3ZnPgo=\"/>\n";
|
||||||
|
|
||||||
|
|
||||||
void http_to_camel_case(char *str, int mode);
|
void http_to_camel_case(char *str, int mode);
|
||||||
|
|
||||||
void http_free_hdr(http_hdr *hdr);
|
void http_free_hdr(http_hdr *hdr);
|
||||||
|
@ -89,6 +89,11 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo
|
|||||||
} else {
|
} else {
|
||||||
strcpy(uri->pathinfo, "");
|
strcpy(uri->pathinfo, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!path_exists(uri->webroot)) {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
sprintf(buf0, "%s%s", uri->webroot, uri->path);
|
sprintf(buf0, "%s%s", uri->webroot, uri->path);
|
||||||
sprintf(buf1, "%s.php", buf0);
|
sprintf(buf1, "%s.php", buf0);
|
||||||
|
Reference in New Issue
Block a user