moved utilitiy functions to new files

This commit is contained in:
overflowerror 2019-03-08 14:41:12 +01:00
parent 9d9aa7fd10
commit 3c626303af
10 changed files with 101 additions and 78 deletions

View file

@ -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)

View file

@ -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"

View file

@ -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"

View file

@ -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() {

View file

@ -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) {

View file

@ -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;
}

View file

@ -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

View file

@ -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
View 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
View 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