diff --git a/trunk/darkhttpd.c b/trunk/darkhttpd.c index 56a7998..29c1bf8 100644 --- a/trunk/darkhttpd.c +++ b/trunk/darkhttpd.c @@ -1437,6 +1437,55 @@ static int parse_request(struct connection *conn) +/* --------------------------------------------------------------------------- + * Check if a file exists. + */ +static int file_exists(const char *path) +{ + struct stat filestat; + if ((stat(path, &filestat) == -1) && (errno = ENOENT)) + return 0; + else + return 1; +} + + + +/* --------------------------------------------------------------------------- + * Generate directory listing. + */ +static void generate_dir_listing(struct connection *conn, const char *path) +{ + char date[DATE_LEN]; + + /* Only really need to calculate the date once. */ + (void)rfc1123_date(date, time(NULL)); + + conn->reply_length = xasprintf(&(conn->reply), + "%s\n" + "

%s

\n" + "Unimplemented.\n" + "
\n" + "Generated by %s on %s\n" + "\n", + conn->uri, conn->uri, pkgname, date); + + conn->header_length = xasprintf(&(conn->header), + "HTTP/1.1 200 OK\r\n" + "Date: %s\r\n" + "Server: %s\r\n" + "%s" /* keep-alive */ + "Content-Length: %d\r\n" + "Content-Type: text/html\r\n" + "\r\n", + date, pkgname, keep_alive(conn), conn->reply_length); + + conn->reply_type = REPLY_GENERATED; + conn->http_code = 200; +} + + + /* --------------------------------------------------------------------------- * Process a GET/HEAD request */ @@ -1464,6 +1513,14 @@ static void process_get(struct connection *conn) if (safe_url[strlen(safe_url)-1] == '/') { xasprintf(&target, "%s%s%s", wwwroot, safe_url, index_name); + if (!file_exists(target)) + { + safefree(target); + xasprintf(&target, "%s%s", wwwroot, safe_url); + generate_dir_listing(conn, target); + safefree(target); + return; + } mimetype = uri_content_type(index_name); } else /* points to a file */