Skip to content

Commit 5670fbe

Browse files
committed
cleanup headers, exports and URL encoding parsing
1 parent 387be46 commit 5670fbe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+809
-733
lines changed

.clang-format

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ Language: Cpp
33
# BasedOnStyle: LLVM
44
AccessModifierOffset: -2
55
AlignAfterOpenBracket: DontAlign
6-
AlignArrayOfStructures: Left
6+
AlignArrayOfStructures: Left
77
AlignConsecutiveAssignments:
8-
Enabled: true
8+
Enabled: true
99
AcrossEmptyLines: false
1010
AcrossComments: false
1111
AlignCompound: false
1212
PadOperators: true
1313
AlignConsecutiveBitFields:
14-
Enabled: true
14+
Enabled: true
1515
AcrossEmptyLines: false
1616
AcrossComments: false
1717
AlignCompound: false
1818
PadOperators: false
1919
AlignConsecutiveDeclarations:
20-
Enabled: true
20+
Enabled: true
2121
AcrossEmptyLines: false
2222
AcrossComments: false
2323
AlignCompound: false
2424
PadOperators: false
2525
AlignConsecutiveMacros:
26-
Enabled: true
26+
Enabled: true
2727
AcrossEmptyLines: false
2828
AcrossComments: false
2929
AlignCompound: false
@@ -48,8 +48,8 @@ AlwaysBreakBeforeMultilineStrings: false
4848
AlwaysBreakTemplateDeclarations: MultiLine
4949
AttributeMacros:
5050
- __capability
51-
BinPackArguments: false
52-
BinPackParameters: true
51+
BinPackArguments: false
52+
BinPackParameters: true
5353
BitFieldColonSpacing: Both
5454
BraceWrapping:
5555
AfterCaseLabel: false

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# dist directory for examples and the library
1+
compile_commands.json
2+
.cache
23
dist/

Makefile

+27-21
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
1-
PREFIX = /usr
2-
CC = gcc
1+
# paths & programs
2+
PREFIX = /usr
3+
DISTDIR = dist
4+
CC = gcc
35

46
# sources
5-
SRCS = $(shell find src/ -type f -name '*.c')
6-
OBJS = $(patsubst src/%.c,dist/%.o,$(SRCS))
7-
HDRS = $(wildcard include/*.h)
7+
SRCS = $(shell find src/ -type f -name '*.c')
8+
OBJS = $(patsubst src/%.c,$(DISTDIR)/%.o,$(SRCS))
9+
HDRS = $(wildcard inc/*.h)
10+
11+
# dirs
12+
SRCDIRS = $(shell find src/* -type d)
13+
OBJDIRS = $(patsubst src/%,$(DISTDIR)/%,$(SRCDIRS))
814

915
# compiler flags
10-
CFLAGS = -O3 -march=native -fstack-protector-strong -fcf-protection=full -fstack-clash-protection
11-
LIBS = -lpthread
16+
CFLAGS = -O3 -march=native -fstack-protector-strong -fcf-protection=full -fstack-clash-protection
17+
INCLUDE = -I./inc
18+
LIBS = -lpthread
1219

1320
# options
14-
CTORM_DEBUG = 0
21+
CTORM_DEBUG = 0
1522
CTORM_JSON_SUPPORT = 1
1623

1724
ifeq ($(CTORM_JSON_SUPPORT), 1)
1825
LIBS += -lcjson
1926
endif
2027

21-
all: dist dist/libctorm.so
22-
23-
dist:
24-
mkdir -pv dist/encoding
28+
all: $(DISTDIR)/libctorm.so
2529

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

29-
dist/%.o: src/%.c
30-
$(CC) -c -Wall -fPIC -o $@ $^ $(LIBS) $(CFLAGS) \
31-
-DCTORM_JSON_SUPPORT=$(CTORM_JSON_SUPPORT) \
33+
$(DISTDIR)/%.o: src/%.c $(OBJDIRS)
34+
$(CC) $(CFLAGS) $(INCLUDE) -c -Wall -fPIC -o $@ $< $(LIBS) \
35+
-DCTORM_JSON_SUPPORT=$(CTORM_JSON_SUPPORT) \
3236
-DCTORM_DEBUG=$(CTORM_DEBUG)
3337

38+
$(OBJDIRS):
39+
@mkdir -pv $@
40+
3441
install:
35-
install -m755 dist/libctorm.so $(DESTDIR)$(PREFIX)/lib/libctorm.so
36-
mkdir -pv $(DESTDIR)/usr/include/ctorm
37-
cp $(HDRS) $(DESTDIR)/usr/include/ctorm
42+
install -Dm755 $(DISTDIR)/libctorm.so $(DESTDIR)/$(PREFIX)/lib/libctorm.so
43+
install -Dm644 inc/ctorm.h $(DESTDIR)/$(PREFIX)/include/ctorm.h
3844

3945
uninstall:
40-
rm $(DESTDIR)$(PREFIX)/lib/libctorm.so
41-
rm -r $(DESTDIR)/usr/include/ctorm
46+
rm -vf $(DESTDIR)/$(PREFIX)/lib/libctorm.so
47+
rm -vf $(DESTDIR)/$(PREFIX)/include/ctorm.h
4248

4349
format:
4450
clang-format -i -style=file $(SRCS) $(HDRS) example/*/*.c
@@ -49,4 +55,4 @@ clean:
4955
example:
5056
$(MAKE) -C $@
5157

52-
.PHONY: test install uninstall format clean example
58+
.PHONY: install uninstall format clean example

example/Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CC = gcc
22

3-
# you should run the binaries with LD_LIBRARY_PATH=../dist
3+
# you should run the binaries with LD_LIBRARY_PATH=../dist
44

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

@@ -12,12 +12,12 @@ endif
1212

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

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

2121
../dist/example_middleware: middleware/*.c ../dist/libctorm.so
2222
mkdir -pv ../dist
23-
$(CC) -L../dist middleware/*.c -lcjson -lctorm -o $@
23+
$(CC) -I../inc -L../dist middleware/*.c -lctorm -lcjson -o $@

example/echo/main.c

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
1-
#include "../../include/all.h"
1+
#include <ctorm.h>
22
#include <string.h>
33

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

9-
void handle_post(req_t *req, res_t *res) {
9+
void POST_form(ctorm_req_t *req, ctorm_res_t *res) {
1010
enc_url_t *form = NULL;
1111
char *msg = NULL;
1212

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

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

26-
info("Message: %s", msg);
27-
RES_FMT("Message: %s", msg);
26+
ctorm_info("message: %s", msg);
27+
RES_FMT("message: %s", msg);
2828

29-
RES_SET("Cool", "yes");
29+
RES_SET("cool", "yes");
3030

3131
enc_url_free(form);
3232
}
3333

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

3939
int main() {
4040
// create the app configuration
41-
app_config_t config;
42-
app_config_new(&config);
41+
ctorm_config_t config;
42+
ctorm_config_new(&config);
4343

4444
// create the app
45-
app_t *app = app_new(&config);
45+
ctorm_app_t *app = ctorm_app_new(&config);
4646

4747
// setup the routes
48-
GET(app, "/", handle_get);
49-
POST(app, "/post", handle_post);
48+
GET(app, "/", GET_index);
49+
POST(app, "/post", POST_form);
5050

5151
// setup the static route
52-
app_static(app, "/static", "./example/echo/static");
52+
ctorm_app_static(app, "/static", "./example/echo/static");
5353

5454
// setup the non handled route handler
55-
app_all(app, handle_notfound);
55+
ctorm_app_all(app, GET_notfound);
5656

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

6161
// clean up
62-
app_free(app);
62+
ctorm_app_free(app);
6363
}

example/hello/main.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
#include "../../include/all.h"
1+
#include <ctorm.h>
22

3-
void handle_get(req_t *req, res_t *res) {
4-
RES_SEND("Hello world!");
3+
void GET_index(ctorm_req_t *req, ctorm_res_t *res) {
4+
RES_SEND("hello world!");
55
}
66

77
int main() {
88
// create the app configuration
9-
app_config_t config;
10-
app_config_new(&config);
9+
ctorm_config_t config;
10+
ctorm_config_new(&config);
1111

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

1818
// create the app
19-
app_t *app = app_new(&config);
19+
ctorm_app_t *app = ctorm_app_new(&config);
2020

2121
// setup the routes
22-
GET(app, "/", handle_get);
22+
GET(app, "/", GET_index);
2323

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

2828
// clean up
29-
app_free(app);
29+
ctorm_app_free(app);
3030
}

0 commit comments

Comments
 (0)