git-svn-id: file:///raid/svn-personal/tewi/trunk@6 8739d7e6-ffea-ec47-b151-bdff447c6205
This commit is contained in:
Nishi 2024-09-13 10:28:20 +00:00
parent e8ba2c3751
commit b8c2411b4c
10 changed files with 100 additions and 44 deletions

View File

@ -10,7 +10,7 @@ OBJS = string.o log.o
all: common.a
common.a: $(OBJS)
ar rcs $@ $(OBJS)
$(AR) rcs $@ $(OBJS)
.c.o:
$(CC) $(CFLAGS) -c -o $@ $<

View File

@ -39,6 +39,13 @@ void cm_log(const char* name, const char* log, ...) {
char* tmp = result;
result = cm_strcat(tmp, va_arg(args, char*));
free(tmp);
} else if(log[i] == 'd') {
int a = va_arg(args, int);
char buf[128];
sprintf(buf, "%d", a);
char* tmp = result;
result = cm_strcat(tmp, buf);
free(tmp);
}
} else {
cbuf[0] = log[i];

View File

@ -15,21 +15,21 @@ char* cm_strcat(const char* a, const char* b) {
char* cm_strdup(const char* str) { return cm_strcat(str, ""); }
char* cm_trimstart(const char* str){
char* cm_trimstart(const char* str) {
int i;
for(i = 0; str[i] != 0; i++){
if(str[i] != ' ' && str[i] != '\t'){
for(i = 0; str[i] != 0; i++) {
if(str[i] != ' ' && str[i] != '\t') {
return cm_strdup(str + i);
}
}
return cm_strdup("");
}
char* cm_trimend(const char* str){
char* cm_trimend(const char* str) {
char* s = cm_strdup(str);
int i;
for(i = strlen(s) - 1; i >= 0; i--){
if(s[i] != '\t' && s[i] != ' '){
for(i = strlen(s) - 1; i >= 0; i--) {
if(s[i] != '\t' && s[i] != ' ') {
s[i + 1] = 0;
break;
}
@ -37,14 +37,14 @@ char* cm_trimend(const char* str){
return s;
}
char* cm_trim(const char* str){
char* cm_trim(const char* str) {
char* tmp = cm_trimstart(str);
char* s = cm_trimend(tmp);
free(tmp);
return s;
}
char** cm_split(const char* str, const char* by){
char** cm_split(const char* str, const char* by) {
int i;
char** r = malloc(sizeof(*r));
r[0] = NULL;
@ -54,20 +54,21 @@ char** cm_split(const char* str, const char* by){
cbuf[1] = 0;
bool dq = false;
bool sq = false;
for(i = 0;; i++){
for(i = 0;; i++) {
int j;
bool has = false;
for(j = 0; by[j] != 0; j++){
if(by[j] == str[i]){
for(j = 0; by[j] != 0; j++) {
if(by[j] == str[i]) {
has = true;
break;
}
}
if(!(dq || sq) && (has || str[i] == 0)){
if(strlen(b) > 0){
if(!(dq || sq) && (has || str[i] == 0)) {
if(strlen(b) > 0) {
char** old = r;
int j;
for(j = 0; old[j] != NULL; j++);
for(j = 0; old[j] != NULL; j++)
;
r = malloc(sizeof(*r) * (j + 2));
for(j = 0; old[j] != NULL; j++) r[j] = old[j];
r[j] = b;
@ -77,12 +78,12 @@ char** cm_split(const char* str, const char* by){
b = malloc(1);
b[0] = 0;
if(str[i] == 0) break;
}else{
if(str[i] == '"' && !sq){
} else {
if(str[i] == '"' && !sq) {
dq = !dq;
}else if(str[i] == '\'' && !dq){
} else if(str[i] == '\'' && !dq) {
sq = !sq;
}else{
} else {
cbuf[0] = str[i];
char* tmp = b;
b = cm_strcat(tmp, cbuf);
@ -94,12 +95,12 @@ char** cm_split(const char* str, const char* by){
return r;
}
bool cm_strcaseequ(const char* a, const char* b){
bool cm_strcaseequ(const char* a, const char* b) {
if(a == NULL) return false;
if(b == NULL) return false;
if(strlen(a) != strlen(b)) return false;
int i;
for(i = 0; a[i] != 0; i++){
for(i = 0; a[i] != 0; i++) {
if(tolower(a[i]) != tolower(b[i])) return false;
}
return true;

View File

@ -17,7 +17,7 @@ all: ./Server
$(MAKE) -C $@ $(FLAGS)
format:
clang-format --verbose -i `find . -name "*.c" -or -name "*.h"`
clang-format --verbose -i `find ./Server ./Common -name "*.c" -or -name "*.h"`
clean:
$(MAKE) -C ./Server $(FLAGS) clean

View File

@ -5,3 +5,4 @@ AR = ar
CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common
LDFLAGS =
LIBS =
EXEC =

8
Platform/win64.mk Normal file
View File

@ -0,0 +1,8 @@
# $Id$
CC = x86_64-w64-mingw32-gcc
AR = x86_64-w64-mingw32-ar
CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -I $(PWD)/openssl/include
LDFLAGS = -L $(PWD)/openssl/lib
LIBS =
EXEC = .exe

View File

@ -10,7 +10,7 @@ OBJS = version.o main.o config.o
all: tewi$(EXEC)
tewi$(EXEC): $(OBJS) ../Common/common.a
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) ../Common/common.a
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) ../Common/common.a -lssl -lcrypto
.c.o:
$(CC) $(CFLAGS) -c -o $@ $<

View File

@ -9,32 +9,61 @@
#include <cm_string.h>
#include <cm_log.h>
int tw_config_read(const char* path){
struct tw_config config;
void tw_config_init(void) {}
int tw_config_read(const char* path) {
cm_log("Config", "Reading %s", path);
char cbuf[2];
cbuf[1] = 0;
int ln = 0;
FILE* f = fopen(path, "r");
if(f != NULL){
if(f != NULL) {
char* line = malloc(1);
line[0] = 0;
while(1){
int stop = 0;
char* vhost = NULL;
while(stop == 0) {
int c = fread(cbuf, 1, 1, f);
if(cbuf[0] == '\n' || c <= 0){
if(cbuf[0] == '\n' || c <= 0) {
ln++;
char* l = cm_trim(line);
if(strlen(l) > 0 && l[0] != '#'){
if(strlen(l) > 0 && l[0] != '#') {
char** r = cm_split(l, " \t");
int i;
if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")){
for(i = 1; r[i] != NULL; i++){
if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")){
for(i = 0; r[i] != NULL; i++) free(r[i]);
free(r);
free(line);
free(l);
fclose(f);
return 1;
if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
for(i = 1; r[i] != NULL; i++) {
if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
stop = 1;
break;
}
}
} else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
if(vhost != NULL) {
cm_log("Config", "Already in virtual host section");
stop = 1;
} else {
if(r[1] == NULL) {
cm_log("Config", "Missing virtual host");
stop = 1;
} else {
vhost = cm_strdup(r[1]);
}
}
} else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
if(vhost == NULL) {
cm_log("Config", "Not in virtual host section");
stop = 1;
} else {
free(vhost);
vhost = NULL;
}
} else {
if(r[0] != NULL) {
cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
}
stop = 1;
}
for(i = 0; r[i] != NULL; i++) free(r[i]);
free(r);
@ -44,7 +73,7 @@ int tw_config_read(const char* path){
line = malloc(1);
line[0] = 0;
if(c <= 0) break;
}else if(cbuf[0] != '\r'){
} else if(cbuf[0] != '\r') {
char* tmp = line;
line = cm_strcat(tmp, cbuf);
free(tmp);
@ -52,8 +81,8 @@ int tw_config_read(const char* path){
}
free(line);
fclose(f);
return 0;
}else{
return stop;
} else {
cm_log("Config", "Could not open the file");
return 1;
}

View File

@ -4,6 +4,8 @@
#include <stdbool.h>
#include <string.h>
#include <openssl/opensslv.h>
#include <cm_log.h>
#include "tw_config.h"
@ -19,13 +21,13 @@ int main(int argc, char** argv) {
if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
if(!cm_do_log) {
cm_do_log = true;
cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
} else {
cm_do_log = true;
}
} else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0){
} else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
i++;
if(argv[i] == NULL){
if(argv[i] == NULL) {
fprintf(stderr, "Missing argument\n");
return 1;
}
@ -36,7 +38,8 @@ int main(int argc, char** argv) {
}
}
}
if(tw_config_read(config) != 0){
tw_config_init();
if(tw_config_read(config) != 0) {
fprintf(stderr, "Could not read the config\n");
return 1;
}

View File

@ -3,6 +3,13 @@
#ifndef __TW_CONFIG_H__
#define __TW_CONFIG_H__
struct tw_config_entry {};
struct tw_config {
struct tw_config_entry root;
};
void tw_config_init(void);
int tw_config_read(const char* path);
#endif