From ec32d2b2845385bf25a60f7984fc5f50049b7bb8 Mon Sep 17 00:00:00 2001 From: Said Bakr Date: Wed, 16 Feb 2022 19:49:19 +0200 Subject: [PATCH] Adding 404 error custom HTML page --e404 --- darkhttpd.c | 39 +++++++++++++++++++++++++++++++++------ e404.html | 15 +++++++++++++++ favicon.ico | Bin 107 -> 1150 bytes 3 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 e404.html diff --git a/darkhttpd.c b/darkhttpd.c index 350a56a..6fbfe38 100644 --- a/darkhttpd.c +++ b/darkhttpd.c @@ -293,6 +293,9 @@ static int max_connections = -1; /* kern.ipc.somaxconn */ static const char *index_name = "index.html"; static int no_listing = 0; +static char *e_404 = ""; +static char *e_500 = ""; + static int sockin = -1; /* socket to accept connections from */ #ifdef HAVE_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); } +/** + * 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 * 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\tThis is useful if darkhttpd is behind a reverse proxy\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 printf("\t--ipv6\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) { forward_to_https = 1; } + else if (strcmp(argv[i], "--e404") == 0){ + e_404 = readFile("e404.html"); + } #ifdef HAVE_INET6 else if (strcmp(argv[i], "--ipv6") == 0) { inet6 = 1; - } + } #endif else errx(1, "unknown argument `%s'", argv[i]); @@ -1523,15 +1545,20 @@ static void default_reply(struct connection *conn, va_end(va); /* Only really need to calculate the date once. */ - rfc1123_date(date, now); - - conn->reply_length = xasprintf(&(conn->reply), - "%d %s\n" + rfc1123_date(date, now); + + char *defContent = "%d %s\n" "

%s

\n" /* errname */ "%s\n" /* reason */ "
\n" "%s" /* generated on */ - "\n", + "\n"; + if (errcode == 404 && e_404 != NULL){ + defContent = e_404; + } + + conn->reply_length = xasprintf(&(conn->reply), + defContent, errcode, errname, errname, reason, generated_on(date)); free(reason); diff --git a/e404.html b/e404.html new file mode 100644 index 0000000..f77950b --- /dev/null +++ b/e404.html @@ -0,0 +1,15 @@ + + + + 404 Not Found + + + +

Error 404

+

The resource you have requested is not found.

+

DarkHttpd Server

+
+ + diff --git a/favicon.ico b/favicon.ico index 8301846616aecac88c4bd4b06dc5cb4d0f802134..d8994a302ab007f3540cff08d64aaa663862b034 100644 GIT binary patch literal 1150 zcmc(bu?@md3`8%2GKf^vDcOQ`7=j%bAbr+h94bi8`ntxl6$^?a@<08vpOp*H@f`;6 z9C1Ga7XVilEjrDo0POwyocjJVPtQkXkIiOP*^#mGw{~bQ(k(X2XS*Vn{u1>v-|si} z7hYUd{kK;+&3Rrq*w6BZxx`8L;+3naK7WFVdQ&MBb@ E07|kQMgRZ+