mirror of
https://github.com/sigmasternchen/CFloor
synced 2025-03-15 20:28:56 +00:00
moved utilitiy functions to new files
This commit is contained in:
parent
9d9aa7fd10
commit
3c626303af
10 changed files with 101 additions and 78 deletions
2
Makefile
2
Makefile
|
@ -5,7 +5,7 @@ LDFLAGS = -lpthread -lrt
|
||||||
|
|
||||||
BIN_NAME = cfloor
|
BIN_NAME = cfloor
|
||||||
|
|
||||||
OBJS = obj/networking.o obj/linked.o obj/logging.o obj/signals.o obj/headers.o obj/misc.o obj/status.o obj/files.o obj/mime.o obj/cgi.o
|
OBJS = obj/networking.o obj/linked.o obj/logging.o obj/signals.o obj/headers.o obj/misc.o obj/status.o obj/files.o obj/mime.o obj/cgi.o obj/util.o
|
||||||
DEPS = $(OBJS:%.o=%.d)
|
DEPS = $(OBJS:%.o=%.d)
|
||||||
|
|
||||||
all: $(BIN_NAME)
|
all: $(BIN_NAME)
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include "cgi.h"
|
#include "cgi.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
#include "util.h"
|
||||||
#include "files.h"
|
#include "files.h"
|
||||||
#include "status.h"
|
#include "status.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
#include "files.h"
|
#include "files.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
#include "util.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "status.h"
|
#include "status.h"
|
||||||
#include "mime.h"
|
#include "mime.h"
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include "headers.h"
|
#include "headers.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
#include "util.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
||||||
struct headers headers_create() {
|
struct headers headers_create() {
|
||||||
|
|
16
src/main.c
16
src/main.c
|
@ -8,6 +8,7 @@
|
||||||
#include "headers.h"
|
#include "headers.h"
|
||||||
#include "files.h"
|
#include "files.h"
|
||||||
#include "cgi.h"
|
#include "cgi.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
struct handlerSettings {
|
struct handlerSettings {
|
||||||
struct fileSettings fileSettings;
|
struct fileSettings fileSettings;
|
||||||
|
@ -20,20 +21,7 @@ struct handler handlerGetter(struct metaData metaData, const char* host, struct
|
||||||
|
|
||||||
union userData data;
|
union userData data;
|
||||||
|
|
||||||
bool isCgiBin = false;
|
if (isInDir(metaData.path, settings->cgiBin)) {
|
||||||
int cgiBinLength = strlen(settings->cgiBin);
|
|
||||||
|
|
||||||
if (settings->cgiBin[cgiBinLength - 1] == '/')
|
|
||||||
cgiBinLength--;
|
|
||||||
|
|
||||||
if (strncmp(metaData.path, settings->cgiBin, cgiBinLength) == 0) {
|
|
||||||
if (metaData.path[cgiBinLength] == '\0')
|
|
||||||
isCgiBin = true;
|
|
||||||
if (metaData.path[cgiBinLength] == '/')
|
|
||||||
isCgiBin = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isCgiBin) {
|
|
||||||
data.ptr = &(settings->cgiSettings);
|
data.ptr = &(settings->cgiSettings);
|
||||||
|
|
||||||
return (struct handler) {
|
return (struct handler) {
|
||||||
|
|
51
src/misc.c
51
src/misc.c
|
@ -1,56 +1,5 @@
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
int startCopyThread(int from, int to, bool closeWriteFd, pthread_t* thread) {
|
|
||||||
struct fileCopy* files = malloc(sizeof(struct fileCopy));
|
|
||||||
if (files < 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
files->readFd = from;
|
|
||||||
files->writeFd = to;
|
|
||||||
files->closeWriteFd = closeWriteFd;
|
|
||||||
|
|
||||||
return pthread_create(thread, NULL, &fileCopyThread, files);
|
|
||||||
}
|
|
||||||
|
|
||||||
void* fileCopyThread(void* data) {
|
|
||||||
struct fileCopy* files = (struct fileCopy*) data;
|
|
||||||
char c;
|
|
||||||
|
|
||||||
while(read(files->readFd, &c, 1) > 0) {
|
|
||||||
write(files->writeFd, &c, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (files->closeWriteFd)
|
|
||||||
close(files->writeFd);
|
|
||||||
|
|
||||||
free(files);
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
char* strclone(const char* string) {
|
|
||||||
char* result = malloc(strlen(string) + 1);
|
|
||||||
if (result == NULL)
|
|
||||||
return NULL;
|
|
||||||
strcpy(result, string);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
int strlenOfNumber(long long number) {
|
|
||||||
int result = 1;
|
|
||||||
|
|
||||||
while(number > 9) {
|
|
||||||
number /= 10;
|
|
||||||
result++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
12
src/misc.h
12
src/misc.h
|
@ -76,16 +76,4 @@ struct handler {
|
||||||
union userData data;
|
union userData data;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct fileCopy {
|
|
||||||
int readFd;
|
|
||||||
int writeFd;
|
|
||||||
bool closeWriteFd;
|
|
||||||
};
|
|
||||||
int startCopyThread(int from, int to, bool closeWriteFd, pthread_t* thread);
|
|
||||||
void* fileCopyThread(void* data);
|
|
||||||
|
|
||||||
char* strclone(const char* string);
|
|
||||||
|
|
||||||
int strlenOfNumber(long long number);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "signals.h"
|
#include "signals.h"
|
||||||
#include "status.h"
|
#include "status.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
struct networkingConfig networkingConfig;
|
struct networkingConfig networkingConfig;
|
||||||
|
|
||||||
|
|
73
src/util.c
Normal file
73
src/util.c
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
bool isInDir(const char* file, const char* dir) {
|
||||||
|
int length = strlen(dir);
|
||||||
|
|
||||||
|
if (dir[length - 1] == '/')
|
||||||
|
length--;
|
||||||
|
|
||||||
|
if (strncmp(file, dir, length) == 0) {
|
||||||
|
if (file[length] == '\0')
|
||||||
|
return true;
|
||||||
|
if (file[length] == '/')
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int startCopyThread(int from, int to, bool closeWriteFd, pthread_t* thread) {
|
||||||
|
struct fileCopy* files = malloc(sizeof(struct fileCopy));
|
||||||
|
if (files < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
files->readFd = from;
|
||||||
|
files->writeFd = to;
|
||||||
|
files->closeWriteFd = closeWriteFd;
|
||||||
|
|
||||||
|
return pthread_create(thread, NULL, &fileCopyThread, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* fileCopyThread(void* data) {
|
||||||
|
struct fileCopy* files = (struct fileCopy*) data;
|
||||||
|
char c;
|
||||||
|
|
||||||
|
while(read(files->readFd, &c, 1) > 0) {
|
||||||
|
write(files->writeFd, &c, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (files->closeWriteFd)
|
||||||
|
close(files->writeFd);
|
||||||
|
|
||||||
|
free(files);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* strclone(const char* string) {
|
||||||
|
char* result = malloc(strlen(string) + 1);
|
||||||
|
if (result == NULL)
|
||||||
|
return NULL;
|
||||||
|
strcpy(result, string);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int strlenOfNumber(long long number) {
|
||||||
|
int result = 1;
|
||||||
|
|
||||||
|
while(number > 9) {
|
||||||
|
number /= 10;
|
||||||
|
result++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
21
src/util.h
Normal file
21
src/util.h
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
#ifndef UTIL_H
|
||||||
|
#define UTIL_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
bool isInDir(const char* filename, const char* dirname);
|
||||||
|
|
||||||
|
struct fileCopy {
|
||||||
|
int readFd;
|
||||||
|
int writeFd;
|
||||||
|
bool closeWriteFd;
|
||||||
|
};
|
||||||
|
int startCopyThread(int from, int to, bool closeWriteFd, pthread_t* thread);
|
||||||
|
void* fileCopyThread(void* data);
|
||||||
|
|
||||||
|
char* strclone(const char* string);
|
||||||
|
|
||||||
|
int strlenOfNumber(long long number);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue