mirror of
https://github.com/emikulic/darkhttpd.git
synced 2023-08-10 21:13:08 +03:00
. Added file_exists().
. Added skeleton for generate_dir_listing(). . Tied it into process_get().
This commit is contained in:
parent
917e7a2cc4
commit
fb2e40cae5
@ -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),
|
||||||
|
"<html><head><title>%s</title></head><body>\n"
|
||||||
|
"<h1>%s</h1>\n"
|
||||||
|
"Unimplemented.\n"
|
||||||
|
"<hr>\n"
|
||||||
|
"Generated by %s on %s\n"
|
||||||
|
"</body></html>\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
|
* Process a GET/HEAD request
|
||||||
*/
|
*/
|
||||||
@ -1464,6 +1513,14 @@ static void process_get(struct connection *conn)
|
|||||||
if (safe_url[strlen(safe_url)-1] == '/')
|
if (safe_url[strlen(safe_url)-1] == '/')
|
||||||
{
|
{
|
||||||
xasprintf(&target, "%s%s%s", wwwroot, safe_url, index_name);
|
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);
|
mimetype = uri_content_type(index_name);
|
||||||
}
|
}
|
||||||
else /* points to a file */
|
else /* points to a file */
|
||||||
|
Loading…
Reference in New Issue
Block a user