mirror of
https://github.com/emikulic/darkhttpd.git
synced 2023-08-10 21:13:08 +03:00
Adding 404 error custom HTML page --e404
This commit is contained in:
39
darkhttpd.c
39
darkhttpd.c
@@ -293,6 +293,9 @@ static int max_connections = -1; /* kern.ipc.somaxconn */
|
|||||||
static const char *index_name = "index.html";
|
static const char *index_name = "index.html";
|
||||||
static int no_listing = 0;
|
static int no_listing = 0;
|
||||||
|
|
||||||
|
static char *e_404 = "";
|
||||||
|
static char *e_500 = "";
|
||||||
|
|
||||||
static int sockin = -1; /* socket to accept connections from */
|
static int sockin = -1; /* socket to accept connections from */
|
||||||
#ifdef HAVE_INET6
|
#ifdef HAVE_INET6
|
||||||
static int inet6 = 0; /* whether the socket uses inet6 */
|
static int inet6 = 0; /* whether the socket uses inet6 */
|
||||||
@@ -736,6 +739,20 @@ static void parse_extension_map_file(const char *filename) {
|
|||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Read the custom error HTML file
|
||||||
|
*/
|
||||||
|
static char *readFile(char *filename) {
|
||||||
|
char* buffer = NULL;
|
||||||
|
FILE *fp = fopen (filename, "rb");
|
||||||
|
if (fp == NULL)
|
||||||
|
err(1, "fopen(\"%s\")", filename);
|
||||||
|
size_t len;
|
||||||
|
ssize_t bytes_read = getdelim( &buffer, &len, '\0', fp);
|
||||||
|
if ( bytes_read != -1) {
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Uses the mime_map to determine a Content-Type: for a requested URL. This
|
/* Uses the mime_map to determine a Content-Type: for a requested URL. This
|
||||||
* bsearch()es mime_map, so make sure it's sorted first.
|
* bsearch()es mime_map, so make sure it's sorted first.
|
||||||
@@ -945,6 +962,8 @@ static void usage(const char *argv0) {
|
|||||||
"\t\tIf the client requested HTTP, forward to HTTPS.\n"
|
"\t\tIf the client requested HTTP, forward to HTTPS.\n"
|
||||||
"\t\tThis is useful if darkhttpd is behind a reverse proxy\n"
|
"\t\tThis is useful if darkhttpd is behind a reverse proxy\n"
|
||||||
"\t\tthat supports SSL.\n\n");
|
"\t\tthat supports SSL.\n\n");
|
||||||
|
printf("\t--e404 Custom 404 Error\n"
|
||||||
|
"\t\tEnable custom 404 page. Make file named e404.html in the same path of the server executable file\n\n");
|
||||||
#ifdef HAVE_INET6
|
#ifdef HAVE_INET6
|
||||||
printf("\t--ipv6\n"
|
printf("\t--ipv6\n"
|
||||||
"\t\tListen on IPv6 address.\n\n");
|
"\t\tListen on IPv6 address.\n\n");
|
||||||
@@ -1161,10 +1180,13 @@ static void parse_commandline(const int argc, char *argv[]) {
|
|||||||
else if (strcmp(argv[i], "--forward-https") == 0) {
|
else if (strcmp(argv[i], "--forward-https") == 0) {
|
||||||
forward_to_https = 1;
|
forward_to_https = 1;
|
||||||
}
|
}
|
||||||
|
else if (strcmp(argv[i], "--e404") == 0){
|
||||||
|
e_404 = readFile("e404.html");
|
||||||
|
}
|
||||||
#ifdef HAVE_INET6
|
#ifdef HAVE_INET6
|
||||||
else if (strcmp(argv[i], "--ipv6") == 0) {
|
else if (strcmp(argv[i], "--ipv6") == 0) {
|
||||||
inet6 = 1;
|
inet6 = 1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
else
|
else
|
||||||
errx(1, "unknown argument `%s'", argv[i]);
|
errx(1, "unknown argument `%s'", argv[i]);
|
||||||
@@ -1523,15 +1545,20 @@ static void default_reply(struct connection *conn,
|
|||||||
va_end(va);
|
va_end(va);
|
||||||
|
|
||||||
/* Only really need to calculate the date once. */
|
/* Only really need to calculate the date once. */
|
||||||
rfc1123_date(date, now);
|
rfc1123_date(date, now);
|
||||||
|
|
||||||
conn->reply_length = xasprintf(&(conn->reply),
|
char *defContent = "<html><head><title>%d %s</title></head><body style=\"background:tomato\">\n"
|
||||||
"<html><head><title>%d %s</title></head><body>\n"
|
|
||||||
"<h1>%s</h1>\n" /* errname */
|
"<h1>%s</h1>\n" /* errname */
|
||||||
"%s\n" /* reason */
|
"%s\n" /* reason */
|
||||||
"<hr>\n"
|
"<hr>\n"
|
||||||
"%s" /* generated on */
|
"%s" /* generated on */
|
||||||
"</body></html>\n",
|
"</body></html>\n";
|
||||||
|
if (errcode == 404 && e_404 != NULL){
|
||||||
|
defContent = e_404;
|
||||||
|
}
|
||||||
|
|
||||||
|
conn->reply_length = xasprintf(&(conn->reply),
|
||||||
|
defContent,
|
||||||
errcode, errname, errname, reason, generated_on(date));
|
errcode, errname, errname, reason, generated_on(date));
|
||||||
free(reason);
|
free(reason);
|
||||||
|
|
||||||
|
|||||||
15
e404.html
Normal file
15
e404.html
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf8">
|
||||||
|
<title>404 Not Found</title>
|
||||||
|
<style>
|
||||||
|
body{background:tomato;color:navy;padding:10px;font-family:sans-serif;font-size:24px;text-align:center;}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Error 404</h1>
|
||||||
|
<h4>The resource you have requested is not found.</h4>
|
||||||
|
<p>DarkHttpd Server</p>
|
||||||
|
<hr>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
BIN
favicon.ico
BIN
favicon.ico
Binary file not shown.
|
Before Width: | Height: | Size: 107 B After Width: | Height: | Size: 1.1 KiB |
Reference in New Issue
Block a user