From 09f07ea259f14081d56f6cf3e1795af387007da9 Mon Sep 17 00:00:00 2001 From: Said Bakr Date: Wed, 16 Feb 2022 02:18:51 +0200 Subject: [PATCH 1/5] Adding initial run with mimetypes and index page --- index.html | 17 +++++++++++++++++ mimetypes | 1 + run.sh | 1 + 3 files changed, 19 insertions(+) create mode 100644 index.html create mode 100644 mimetypes create mode 100755 run.sh diff --git a/index.html b/index.html new file mode 100644 index 0000000..6254b90 --- /dev/null +++ b/index.html @@ -0,0 +1,17 @@ + + + + + DarkHttpd Works + + + +

DarkHttpd Server Works!

+

When you need a web server in a hurry.

+

DarkHttpd on GitHub | README

+ + diff --git a/mimetypes b/mimetypes new file mode 100644 index 0000000..81628bd --- /dev/null +++ b/mimetypes @@ -0,0 +1 @@ +text/HTML md diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..5d6d6c0 --- /dev/null +++ b/run.sh @@ -0,0 +1 @@ +./darkhttpd ./ --port 9090 --mimetypes mimetypes From e8865bda5261984826756033282c71cd7024476f Mon Sep 17 00:00:00 2001 From: Said Bakr Date: Wed, 16 Feb 2022 09:06:07 +0200 Subject: [PATCH 2/5] fix mimetype, add favicon --- favicon.ico | Bin 0 -> 107 bytes mimetypes | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 favicon.ico diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..8301846616aecac88c4bd4b06dc5cb4d0f802134 GIT binary patch literal 107 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`MxHK?Ar`&K76lIu)O#5)cPNPS zxHC&gNCag3l47{j(&xU{Az%Li(~UiBn;WV$CkpW}NUf+bOJpy*0@Tgm>FVdQ&MBb@ E07|kQMgRZ+ literal 0 HcmV?d00001 diff --git a/mimetypes b/mimetypes index 81628bd..cb334be 100644 --- a/mimetypes +++ b/mimetypes @@ -1 +1 @@ -text/HTML md +text/plain md From ec32d2b2845385bf25a60f7984fc5f50049b7bb8 Mon Sep 17 00:00:00 2001 From: Said Bakr Date: Wed, 16 Feb 2022 19:49:19 +0200 Subject: [PATCH 3/5] 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+ From 47168fb0c393548169a67e14677759d27ccdc71b Mon Sep 17 00:00:00 2001 From: Said Bakr Date: Wed, 16 Feb 2022 20:47:55 +0200 Subject: [PATCH 4/5] Fix without custom error condition --- darkhttpd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/darkhttpd.c b/darkhttpd.c index 6fbfe38..ebe2e3c 100644 --- a/darkhttpd.c +++ b/darkhttpd.c @@ -1553,7 +1553,7 @@ static void default_reply(struct connection *conn, "
\n" "%s" /* generated on */ "\n"; - if (errcode == 404 && e_404 != NULL){ + if (errcode == 404 && e_404 != ""){ defContent = e_404; } From cb548aef6ded6794b2a5bee06f40ec1ce415baad Mon Sep 17 00:00:00 2001 From: Said Bakr Date: Wed, 16 Feb 2022 21:17:08 +0200 Subject: [PATCH 5/5] Add hint about -e404 in README --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index d6654ab..5fcde66 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,11 @@ Commandline options can be combined: ./darkhttpd ~/public_html --port 8080 --addr 127.0.0.1 ``` +To use custom 404 error page, create file named `e404.html` in the root directory where `darkhttpd` executable run and use the following compination: +``` +./darkhttpd ./ --port 8080 --e404 +``` + To see a full list of commandline options, run darkhttpd without any arguments: