Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding initial run with mimetypes and index page #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
39 changes: 33 additions & 6 deletions darkhttpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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),
"<html><head><title>%d %s</title></head><body>\n"
rfc1123_date(date, now);

char *defContent = "<html><head><title>%d %s</title></head><body style=\"background:tomato\">\n"
"<h1>%s</h1>\n" /* errname */
"%s\n" /* reason */
"<hr>\n"
"%s" /* generated on */
"</body></html>\n",
"</body></html>\n";
if (errcode == 404 && e_404 != ""){
defContent = e_404;
}

conn->reply_length = xasprintf(&(conn->reply),
defContent,
errcode, errname, errname, reason, generated_on(date));
free(reason);

Expand Down
15 changes: 15 additions & 0 deletions e404.html
Original file line number Diff line number Diff line change
@@ -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>
Binary file added favicon.ico
Binary file not shown.
17 changes: 17 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>
<head>
<meta charset="utf-8">
<meta content="DarkHttpd demo page">
<title>DarkHttpd Works</title>
<style>
body{background:#cfcfcf; color:#3300FF; padding: 10px;}
a{color:red;font-weight:bold}
a:hover{color:green}
</style>
</head>
<body>
<h1>DarkHttpd Server Works!</h1>
<p>When you need a web server in a hurry.</p>
<p>DarkHttpd on <a href="https://github.com/emikulic/darkhttpd">GitHub</a> | <a href="README.md">README</a></p>
</body>
</html>
1 change: 1 addition & 0 deletions mimetypes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text/plain md
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./darkhttpd ./ --port 9090 --mimetypes mimetypes