Skip to content

Commit

Permalink
cleanup headers, exports and URL encoding parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ngn13 committed Jan 10, 2025
1 parent 387be46 commit 5670fbe
Show file tree
Hide file tree
Showing 44 changed files with 809 additions and 733 deletions.
14 changes: 7 additions & 7 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: DontAlign
AlignArrayOfStructures: Left
AlignArrayOfStructures: Left
AlignConsecutiveAssignments:
Enabled: true
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: true
AlignConsecutiveBitFields:
Enabled: true
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveDeclarations:
Enabled: true
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveMacros:
Enabled: true
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
Expand All @@ -48,8 +48,8 @@ AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
AttributeMacros:
- __capability
BinPackArguments: false
BinPackParameters: true
BinPackArguments: false
BinPackParameters: true
BitFieldColonSpacing: Both
BraceWrapping:
AfterCaseLabel: false
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# dist directory for examples and the library
compile_commands.json
.cache
dist/
48 changes: 27 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
PREFIX = /usr
CC = gcc
# paths & programs
PREFIX = /usr
DISTDIR = dist
CC = gcc

# sources
SRCS = $(shell find src/ -type f -name '*.c')
OBJS = $(patsubst src/%.c,dist/%.o,$(SRCS))
HDRS = $(wildcard include/*.h)
SRCS = $(shell find src/ -type f -name '*.c')
OBJS = $(patsubst src/%.c,$(DISTDIR)/%.o,$(SRCS))
HDRS = $(wildcard inc/*.h)

# dirs
SRCDIRS = $(shell find src/* -type d)
OBJDIRS = $(patsubst src/%,$(DISTDIR)/%,$(SRCDIRS))

# compiler flags
CFLAGS = -O3 -march=native -fstack-protector-strong -fcf-protection=full -fstack-clash-protection
LIBS = -lpthread
CFLAGS = -O3 -march=native -fstack-protector-strong -fcf-protection=full -fstack-clash-protection
INCLUDE = -I./inc
LIBS = -lpthread

# options
CTORM_DEBUG = 0
CTORM_DEBUG = 0
CTORM_JSON_SUPPORT = 1

ifeq ($(CTORM_JSON_SUPPORT), 1)
LIBS += -lcjson
endif

all: dist dist/libctorm.so

dist:
mkdir -pv dist/encoding
all: $(DISTDIR)/libctorm.so

dist/libctorm.so: $(OBJS)
$(CC) -shared -o $@ $^ $(LIBS) $(CFLAGS)

dist/%.o: src/%.c
$(CC) -c -Wall -fPIC -o $@ $^ $(LIBS) $(CFLAGS) \
-DCTORM_JSON_SUPPORT=$(CTORM_JSON_SUPPORT) \
$(DISTDIR)/%.o: src/%.c $(OBJDIRS)
$(CC) $(CFLAGS) $(INCLUDE) -c -Wall -fPIC -o $@ $< $(LIBS) \
-DCTORM_JSON_SUPPORT=$(CTORM_JSON_SUPPORT) \
-DCTORM_DEBUG=$(CTORM_DEBUG)

$(OBJDIRS):
@mkdir -pv $@

install:
install -m755 dist/libctorm.so $(DESTDIR)$(PREFIX)/lib/libctorm.so
mkdir -pv $(DESTDIR)/usr/include/ctorm
cp $(HDRS) $(DESTDIR)/usr/include/ctorm
install -Dm755 $(DISTDIR)/libctorm.so $(DESTDIR)/$(PREFIX)/lib/libctorm.so
install -Dm644 inc/ctorm.h $(DESTDIR)/$(PREFIX)/include/ctorm.h

uninstall:
rm $(DESTDIR)$(PREFIX)/lib/libctorm.so
rm -r $(DESTDIR)/usr/include/ctorm
rm -vf $(DESTDIR)/$(PREFIX)/lib/libctorm.so
rm -vf $(DESTDIR)/$(PREFIX)/include/ctorm.h

format:
clang-format -i -style=file $(SRCS) $(HDRS) example/*/*.c
Expand All @@ -49,4 +55,4 @@ clean:
example:
$(MAKE) -C $@

.PHONY: test install uninstall format clean example
.PHONY: install uninstall format clean example
8 changes: 4 additions & 4 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC = gcc

# you should run the binaries with LD_LIBRARY_PATH=../dist
# you should run the binaries with LD_LIBRARY_PATH=../dist

all: ../dist/example_hello ../dist/example_echo ../dist/example_middleware

Expand All @@ -12,12 +12,12 @@ endif

../dist/example_hello: hello/*.c ../dist/libctorm.so
mkdir -pv ../dist
$(CC) -L../dist hello/*.c -lctorm -o $@
$(CC) -I../inc -L../dist hello/*.c -lctorm -o $@

../dist/example_echo: echo/*.c ../dist/libctorm.so
mkdir -pv ../dist
$(CC) -L../dist echo/*.c -lctorm -o $@
$(CC) -I../inc -L../dist echo/*.c -lctorm -o $@

../dist/example_middleware: middleware/*.c ../dist/libctorm.so
mkdir -pv ../dist
$(CC) -L../dist middleware/*.c -lcjson -lctorm -o $@
$(CC) -I../inc -L../dist middleware/*.c -lctorm -lcjson -o $@
46 changes: 23 additions & 23 deletions example/echo/main.c
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
#include "../../include/all.h"
#include <ctorm.h>
#include <string.h>

void handle_notfound(req_t *req, res_t *res) {
res->code = 404;
void GET_notfound(ctorm_req_t *req, ctorm_res_t *res) {
RES_CODE(404);
RES_SENDFILE("./example/echo/html/404.html");
}

void handle_post(req_t *req, res_t *res) {
void POST_form(ctorm_req_t *req, ctorm_res_t *res) {
enc_url_t *form = NULL;
char *msg = NULL;

if ((form = REQ_FORM()) == NULL) {
res->code = 400;
error("Failed to parse the form data: %s", app_geterror());
RES_CODE(400);
ctorm_error("failed to parse the form data: %s", ctorm_geterror());
return RES_SEND("bad body");
}

if (NULL == (msg = enc_url_get(form, "msg"))) {
res->code = 400;
RES_CODE(400);
enc_url_free(form);
error("Form data does not contain the message");
ctorm_error("form data does not contain the message");
return RES_SEND("bad body");
}

info("Message: %s", msg);
RES_FMT("Message: %s", msg);
ctorm_info("message: %s", msg);
RES_FMT("message: %s", msg);

RES_SET("Cool", "yes");
RES_SET("cool", "yes");

enc_url_free(form);
}

void handle_get(req_t *req, res_t *res) {
void GET_index(ctorm_req_t *req, ctorm_res_t *res) {
if (!RES_SENDFILE("./example/echo/html/index.html"))
error("Failed to send index.html: %s", app_geterror());
ctorm_error("Failed to send index.html: %s", ctorm_geterror());
}

int main() {
// create the app configuration
app_config_t config;
app_config_new(&config);
ctorm_config_t config;
ctorm_config_new(&config);

// create the app
app_t *app = app_new(&config);
ctorm_app_t *app = ctorm_app_new(&config);

// setup the routes
GET(app, "/", handle_get);
POST(app, "/post", handle_post);
GET(app, "/", GET_index);
POST(app, "/post", POST_form);

// setup the static route
app_static(app, "/static", "./example/echo/static");
ctorm_app_static(app, "/static", "./example/echo/static");

// setup the non handled route handler
app_all(app, handle_notfound);
ctorm_app_all(app, GET_notfound);

// run the app
if (!app_run(app, "0.0.0.0:8080"))
error("Failed to start the app: %s", app_geterror());
if (!ctorm_app_run(app, "0.0.0.0:8080"))
ctorm_error("Failed to start the app: %s", ctorm_geterror());

// clean up
app_free(app);
ctorm_app_free(app);
}
20 changes: 10 additions & 10 deletions example/hello/main.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "../../include/all.h"
#include <ctorm.h>

void handle_get(req_t *req, res_t *res) {
RES_SEND("Hello world!");
void GET_index(ctorm_req_t *req, ctorm_res_t *res) {
RES_SEND("hello world!");
}

int main() {
// create the app configuration
app_config_t config;
app_config_new(&config);
ctorm_config_t config;
ctorm_config_new(&config);

// example: disable the server header
config.server_header = false;
Expand All @@ -16,15 +16,15 @@ int main() {
config.disable_logging = true;

// create the app
app_t *app = app_new(&config);
ctorm_app_t *app = ctorm_app_new(&config);

// setup the routes
GET(app, "/", handle_get);
GET(app, "/", GET_index);

// run the app
if (!app_run(app, "0.0.0.0:8080"))
error("Failed to start the app: %s", app_geterror());
if (!ctorm_app_run(app, "0.0.0.0:8080"))
ctorm_error("failed to start the app: %s", ctorm_geterror());

// clean up
app_free(app);
ctorm_app_free(app);
}
Loading

0 comments on commit 5670fbe

Please sign in to comment.