mirror of
https://github.com/sigmasternchen/CFloor
synced 2025-03-15 20:28:56 +00:00
we now have index files
This commit is contained in:
parent
e8cc8cafab
commit
fa637c4dc4
3 changed files with 57 additions and 13 deletions
57
src/files.c
57
src/files.c
|
@ -114,7 +114,7 @@ void fileHandler(struct request request, struct response response) {
|
||||||
if (path == NULL)
|
if (path == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
struct stat statObj;
|
struct stat statObj, statObjFile;
|
||||||
if (stat(path, &statObj) < 0) {
|
if (stat(path, &statObj) < 0) {
|
||||||
free(path);
|
free(path);
|
||||||
|
|
||||||
|
@ -129,21 +129,54 @@ void fileHandler(struct request request, struct response response) {
|
||||||
if (S_ISDIR(statObj.st_mode)) {
|
if (S_ISDIR(statObj.st_mode)) {
|
||||||
// TODO check for index files
|
// TODO check for index files
|
||||||
|
|
||||||
if (!indexes) {
|
char* filepath = NULL;
|
||||||
status(request, response, 403);
|
for (int i = 0; i < settings->indexfiles.number; i++) {
|
||||||
return;
|
filepath = malloc(strlen(path) + 1 + strlen(settings->indexfiles.files[i]) + 1);
|
||||||
|
if (filepath == NULL) {
|
||||||
|
error("files: Couldn't allocate memory for index file check: %s", strerror(errno));
|
||||||
|
warn("files: ignoring");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
strcpy(filepath, path);
|
||||||
|
strcat(filepath, "/");
|
||||||
|
strcat(filepath, settings->indexfiles.files[i]);
|
||||||
|
|
||||||
|
debug("files: searching for index: %s", filepath);
|
||||||
|
|
||||||
|
if (access(filepath, F_OK | R_OK) == 0 && stat(filepath, &statObjFile) == 0) {
|
||||||
|
if (S_ISREG(statObjFile.st_mode)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(filepath);
|
||||||
|
filepath = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct headers headers = headers_create();
|
if (filepath != NULL) {
|
||||||
headers_mod(&headers, "Content-Type", "text/html; charset=utf-8");
|
debug("files: found index file: %s", filepath);
|
||||||
int fd = response.sendHeader(200, &headers, &request);
|
overrideStat = true;
|
||||||
headers_free(&headers);
|
free(path);
|
||||||
|
path = filepath;
|
||||||
|
statObj = statObjFile;
|
||||||
|
} else {
|
||||||
|
|
||||||
if (showIndex(fd, path, documentRoot) < 0) {
|
if (!indexes) {
|
||||||
// TODO error
|
free(path);
|
||||||
}
|
status(request, response, 403);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct headers headers = headers_create();
|
||||||
|
headers_mod(&headers, "Content-Type", "text/html; charset=utf-8");
|
||||||
|
int fd = response.sendHeader(200, &headers, &request);
|
||||||
|
headers_free(&headers);
|
||||||
|
|
||||||
|
if (showIndex(fd, path, documentRoot) < 0) {
|
||||||
|
// TODO error
|
||||||
|
}
|
||||||
|
|
||||||
done = true;
|
done = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (S_ISREG(statObj.st_mode) || overrideStat) {
|
if (S_ISREG(statObj.st_mode) || overrideStat) {
|
||||||
|
|
|
@ -9,6 +9,10 @@
|
||||||
struct fileSettings {
|
struct fileSettings {
|
||||||
const char* documentRoot;
|
const char* documentRoot;
|
||||||
bool index;
|
bool index;
|
||||||
|
struct {
|
||||||
|
int number;
|
||||||
|
const char** files;
|
||||||
|
} indexfiles;
|
||||||
};
|
};
|
||||||
|
|
||||||
void fileHandler(struct request request, struct response response);
|
void fileHandler(struct request request, struct response response);
|
||||||
|
|
|
@ -46,7 +46,14 @@ int main(int argc, char** argv) {
|
||||||
struct handlerSettings handlerSettings = {
|
struct handlerSettings handlerSettings = {
|
||||||
.fileSettings = {
|
.fileSettings = {
|
||||||
.documentRoot = documentRoot,
|
.documentRoot = documentRoot,
|
||||||
.index = true
|
.index = true,
|
||||||
|
.indexfiles = {
|
||||||
|
.number = 2,
|
||||||
|
.files = (const char* []) {
|
||||||
|
"index.html",
|
||||||
|
"index.htm"
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
.cgiSettings = {
|
.cgiSettings = {
|
||||||
.documentRoot = documentRoot
|
.documentRoot = documentRoot
|
||||||
|
|
Loading…
Reference in a new issue