updated for list directory and show hidden files
This commit is contained in:
1 parent
2cb0ad8535
commit
716e7df03a
4 files changed
+55
-32
No files matched your search
+15
-8
@@ -56,10 +56,10 @@ static void write_full(SSL *ssl, const char *data, size_t len) {
|
||||
}
|
||||
}
|
||||
|
||||
static void list_files(SSL *ssl) {
|
||||
static void list_files(SSL *ssl, char *directory) {
|
||||
char buf[8192] = "<html><head><title>File Server</title></head><body><h1>Files</h1><ul>";
|
||||
size_t pos = strlen(buf);
|
||||
DIR *dir = opendir(ROOT_DIR);
|
||||
DIR *dir = opendir(directory);
|
||||
if (!dir) {
|
||||
send_response(ssl, "500 Internal Server Error", "text/plain", "Cannot open directory", 20);
|
||||
return;
|
||||
@@ -67,13 +67,13 @@ static void list_files(SSL *ssl) {
|
||||
|
||||
struct dirent *entry;
|
||||
while ((entry = readdir(dir)) && pos < sizeof(buf) - 100) {
|
||||
if (entry->d_name[0] == '.') continue;
|
||||
if (!strcmp(entry->d_name, ".")) continue;
|
||||
if (!strcmp(entry->d_name, "..") && !strcmp(directory, ROOT_DIR)) continue;
|
||||
struct stat st;
|
||||
char path[MAX_PATH];
|
||||
snprintf(path, sizeof(path), "%s/%s", ROOT_DIR, entry->d_name);
|
||||
if (stat(path, &st) < 0 || S_ISDIR(st.st_mode)) continue;
|
||||
snprintf(path, sizeof(path), "%s/%s", directory, entry->d_name);
|
||||
pos += snprintf(buf + pos, sizeof(buf) - pos,
|
||||
"<li><a href=\"%s\">%s</a></li>", entry->d_name, entry->d_name);
|
||||
"<li><a href=\"%s/%s\">%s</a></li>", directory, entry->d_name, entry->d_name);
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
@@ -120,7 +120,7 @@ void *handle_request(void *param) {
|
||||
}
|
||||
|
||||
if (!strcmp(path, "/")) {
|
||||
list_files(ssl);
|
||||
list_files(ssl, ROOT_DIR);
|
||||
SSL_shutdown(ssl);
|
||||
SSL_free(ssl);
|
||||
close(client_sock);
|
||||
@@ -132,9 +132,16 @@ void *handle_request(void *param) {
|
||||
|
||||
struct stat st;
|
||||
if (stat(file_path, &st) < 0 || S_ISDIR(st.st_mode)) {
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
list_files(ssl, file_path);
|
||||
SSL_shutdown(ssl);
|
||||
SSL_free(ssl);
|
||||
close(client_sock);
|
||||
pthread_exit(0);
|
||||
}
|
||||
send_response(ssl, st.st_mode ? "403 Forbidden" : "404 Not Found",
|
||||
"text/plain", st.st_mode ? "Directory listing not allowed" : "Not Found",
|
||||
st.st_mode ? 28 : 9);
|
||||
st.st_mode ? 29 : 9);
|
||||
SSL_shutdown(ssl);
|
||||
SSL_free(ssl);
|
||||
close(client_sock);
|
||||
|
||||
Reference in new issue
Block a user