Compare commits
No commits in common. "main" and "routing" have entirely different histories.
68
Makefile
68
Makefile
|
@ -3,7 +3,7 @@
|
|||
|
||||
-include config.mk
|
||||
|
||||
LIB_ARCHIVE := $(BUILD_DIR)/lib$(LIB).a
|
||||
LIB := $(BUILD_DIR)/$(LIB_FILENAME)
|
||||
|
||||
SRCS != find '$(SRC_DIR)' -iname '*.c'
|
||||
SRCS_H != find include -iname '*.h'
|
||||
|
@ -17,38 +17,54 @@ OBJS_EXAMPLE := $(SRCS_EXAMPLE:%=$(BUILD_DIR)/%.o)
|
|||
|
||||
DEPS := $(SRCS:%=$(BUILD_DIR)/%.d) $(SRCS_TEST:%=$(BUILD_DIR)/%.d)
|
||||
|
||||
BIN_TEST := $(BUILD_DIR)/$(TEST_DIR)/runner
|
||||
BINS_TEST := $(OBJS_TEST:%.c.o=%)
|
||||
BINS_EXAMPLE := $(OBJS_EXAMPLE:%.c.o=%)
|
||||
|
||||
TARGETS_TEST := $(BINS_TEST:%=test-%)
|
||||
TARGETS_MEM_TEST := $(BINS_TEST:%=test-mem-%)
|
||||
TARGETS_EXAMPLE := $(BINS_EXAMPLE:%=example-%)
|
||||
|
||||
_CFLAGS := $(addprefix -I,$(INC_DIRS)) $(CFLAGS) -Wall -Wextra
|
||||
|
||||
.PHONY: all
|
||||
all: lib
|
||||
|
||||
|
||||
# =====COMPILATION=====
|
||||
# Utility used by the CI to lint
|
||||
.PHONY: lib
|
||||
$(LIB_ARCHIVE): $(OBJS)
|
||||
ar -rcs $@ $^
|
||||
|
||||
.PHONY: objs
|
||||
objs: $(OBJS)
|
||||
|
||||
.PHONY: lib
|
||||
lib: $(LIB)
|
||||
$(LIB): $(OBJS)
|
||||
ar -rcs $@ $(OBJS)
|
||||
|
||||
$(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c
|
||||
mkdir -p $(dir $@)
|
||||
$(CC) -c $(_CFLAGS) $< -o $@
|
||||
|
||||
# =====TESTING=====
|
||||
.PHONY: test
|
||||
test: $(BIN_TEST)
|
||||
'./$^'
|
||||
test: $(TARGETS_TEST)
|
||||
|
||||
.PHONY: test-mem
|
||||
test-mem: $(BIN_TEST)
|
||||
valgrind --tool=memcheck --error-exitcode=1 --track-origins=yes --leak-check=full './$^'
|
||||
test-mem: $(TARGETS_MEM_TEST)
|
||||
|
||||
.PHONY: $(TARGETS_TEST)
|
||||
$(TARGETS_TEST): test-%: %
|
||||
./$^
|
||||
|
||||
.PHONY: $(TARGETS_MEM_TEST)
|
||||
$(TARGETS_MEM_TEST): test-mem-%: %
|
||||
valgrind --tool=memcheck --error-exitcode=1 --track-origins=yes --leak-check=full ./$^
|
||||
|
||||
.PHONY: build-test
|
||||
build-test: $(BIN_TEST)
|
||||
build-test: $(BINS_TEST)
|
||||
|
||||
$(BIN_TEST): $(OBJS_TEST) $(LIB_ARCHIVE)
|
||||
$(CC) -o $@ $^ $(_LDFLAGS)
|
||||
$(BINS_TEST): %: %.c.o $(LIB)
|
||||
$(CC) \
|
||||
$^ -o $@
|
||||
|
||||
# Along with the include directory, each test includes $(TEST_DIR) (which
|
||||
# contains the acutest.h header file), and the src directory of the module it's
|
||||
|
@ -64,8 +80,8 @@ $(BUILD_DIR)/$(TEST_DIR)/%.c.o: $(TEST_DIR)/%.c
|
|||
.PHONY: build-example
|
||||
build-example: $(BINS_EXAMPLE)
|
||||
|
||||
$(BINS_EXAMPLE): %: %.c.o $(LIB_ARCHIVE)
|
||||
$(CC) $(LDFLAGS) \
|
||||
$(BINS_EXAMPLE): %: %.c.o $(LIB)
|
||||
$(CC) \
|
||||
$^ -o $@
|
||||
|
||||
# Example binaries link the resulting library
|
||||
|
@ -76,22 +92,22 @@ $(BUILD_DIR)/$(EXAMPLE_DIR)/%.c.o: $(EXAMPLE_DIR)/%.c
|
|||
# =====MAINTENANCE=====
|
||||
.PHONY: lint
|
||||
lint:
|
||||
@ clang-format -n --Werror \
|
||||
$(shell find '$(SRC_DIR)/$(LIB)' -iname '*.c') \
|
||||
$(shell find '$(SRC_DIR)/_include/$(LIB)' -iname '*.h') \
|
||||
$(shell find 'include/$(LIB)' -iname '*.h')
|
||||
clang-format -n --Werror \
|
||||
$(filter-out $(THIRDPARTY),$(SRCS)) \
|
||||
$(filter-out $(THIRDPARTY),$(SRCS_H)) \
|
||||
$(filter-out $(THIRDPARTY),$(SRCS_H_INTERNAL))
|
||||
|
||||
.PHONY: fmt
|
||||
fmt:
|
||||
@ clang-format -i \
|
||||
$(shell find '$(SRC_DIR)/$(LIB)' -iname '*.c') \
|
||||
$(shell find '$(SRC_DIR)/_include/$(LIB)' -iname '*.h') \
|
||||
$(shell find 'include/$(LIB)' -iname '*.h')
|
||||
clang-format -i \
|
||||
$(filter-out $(THIRDPARTY),$(SRCS)) \
|
||||
$(filter-out $(THIRDPARTY),$(SRCS_H)) \
|
||||
$(filter-out $(THIRDPARTY),$(SRCS_H_INTERNAL))
|
||||
|
||||
.PHONY: check
|
||||
check:
|
||||
@ mkdir -p $(BUILD_DIR)/cppcheck
|
||||
@ cppcheck \
|
||||
mkdir -p $(BUILD_DIR)/cppcheck
|
||||
cppcheck \
|
||||
$(addprefix -I,$(INC_DIRS)) \
|
||||
--cppcheck-build-dir=$(BUILD_DIR)/cppcheck \
|
||||
--error-exitcode=1 \
|
||||
|
@ -100,7 +116,7 @@ check:
|
|||
--check-level=exhaustive \
|
||||
--quiet \
|
||||
-j$(shell nproc) \
|
||||
$(shell find '$(SRC_DIR)/$(LIB)' -iname '*.c')
|
||||
$(filter-out $(THIRDPARTY),$(SRCS))
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
LIB = lnm
|
||||
LIB_FILENAME = liblnm.a
|
||||
|
||||
BUILD_DIR = build
|
||||
SRC_DIR = src
|
||||
TEST_DIR = test
|
||||
EXAMPLE_DIR = example
|
||||
THIRDPARTY = src/picohttpparser.c include/picohttpparser.h
|
||||
|
||||
PUB_INC_DIR = include
|
||||
INC_DIRS = $(PUB_INC_DIR) src/_include
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
#include "lnm/http/res.h"
|
||||
#include "lnm/log.h"
|
||||
#include "lnm/http/loop.h"
|
||||
#include "lnm/loop.h"
|
||||
|
||||
lnm_err ctx_init(void **c_ctx, void *gctx) {
|
||||
*c_ctx = NULL;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
void ctx_reset(void *c_ctx) {}
|
||||
void ctx_free(void *c_ctx) {}
|
||||
|
||||
lnm_err data_streamer(uint64_t *written, char *buf,
|
||||
lnm_http_conn *conn, uint64_t offset,
|
||||
uint64_t len) {
|
||||
// Don't do anything, just let the application return random data stored in
|
||||
// the read buffer. The goal is to benchmark the networking pipeline
|
||||
*written = len;
|
||||
|
||||
return lnm_err_ok;
|
||||
}
|
||||
|
||||
lnm_http_step_err step_fn(lnm_http_conn *conn) {
|
||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||
uint64_t len = 1 << 30;
|
||||
lnm_http_res_body_set_fn(&ctx->res, data_streamer, len);
|
||||
|
||||
return lnm_http_step_err_done;
|
||||
}
|
||||
|
||||
int main() {
|
||||
lnm_http_loop *hl;
|
||||
|
||||
lnm_http_loop_init(&hl, NULL, ctx_init,
|
||||
ctx_reset,
|
||||
ctx_free);
|
||||
|
||||
lnm_http_router *router;
|
||||
lnm_http_router_init(&router);
|
||||
|
||||
lnm_http_route *route;
|
||||
lnm_http_router_add(&route, router, lnm_http_method_get, "/");
|
||||
lnm_http_route_step_append(route, step_fn, false);
|
||||
|
||||
lnm_http_loop_router_set(hl, router);
|
||||
|
||||
lnm_log_init_global();
|
||||
lnm_log_register_stdout(lnm_log_level_warning);
|
||||
|
||||
printf("res = %i\n", lnm_http_loop_run(hl, 8080, 1, 0));
|
||||
}
|
|
@ -40,6 +40,7 @@ typedef struct lnm_http_req {
|
|||
struct {
|
||||
size_t o;
|
||||
size_t len;
|
||||
regmatch_t groups[LNM_HTTP_MAX_REGEX_GROUPS];
|
||||
} path;
|
||||
struct {
|
||||
size_t o;
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include "lnm/http/req.h"
|
||||
#include "lnm/log.h"
|
||||
#include "lnm/loop.h"
|
||||
#include "lnm/loop_internal.h"
|
||||
|
||||
static const char *section = "http";
|
||||
|
|
@ -65,9 +65,6 @@ void lnm_vlog(lnm_log_level level, const char *section, const char *fmt,
|
|||
continue;
|
||||
}
|
||||
|
||||
va_list aq;
|
||||
va_copy(aq, ap);
|
||||
|
||||
switch (stream->type) {
|
||||
case lnm_logger_stream_type_file:
|
||||
fprintf(stream->ptr, "[%s][%s][%s] ", date_str,
|
||||
|
@ -77,7 +74,7 @@ void lnm_vlog(lnm_log_level level, const char *section, const char *fmt,
|
|||
break;
|
||||
}
|
||||
|
||||
va_end(aq);
|
||||
va_end(ap);
|
||||
}
|
||||
}
|
||||
|
|
@ -59,8 +59,8 @@ uint64_t lnm_digits(uint64_t num) {
|
|||
bool lnm_is_ascii(const char *s) {
|
||||
bool valid = true;
|
||||
|
||||
while (valid && (*s != '\0')) {
|
||||
valid = *s >= 0;
|
||||
while (valid && *s != '\0') {
|
||||
valid = *s < 0;
|
||||
|
||||
s++;
|
||||
}
|
|
@ -1,9 +1,8 @@
|
|||
#define TEST_NO_MAIN
|
||||
#include "acutest.h"
|
||||
#include "tests.h"
|
||||
|
||||
#include "lnm/common.h"
|
||||
#include "lnm/http/route.h"
|
||||
#include "test.h"
|
||||
|
||||
#include "lnm/http/loop.h"
|
||||
|
||||
void test_routing_simple() {
|
||||
lnm_http_router *router;
|
||||
|
@ -102,3 +101,11 @@ void test_routing_nest() {
|
|||
|
||||
lnm_http_router_free(r2);
|
||||
}
|
||||
|
||||
TEST_LIST = {
|
||||
{ "routing simple", test_routing_simple },
|
||||
{ "routing star", test_routing_star },
|
||||
{ "routing merge", test_routing_merge },
|
||||
{ "routing nest", test_routing_nest },
|
||||
{ NULL, NULL }
|
||||
};
|
|
@ -1,10 +0,0 @@
|
|||
#include "acutest.h"
|
||||
#include "tests.h"
|
||||
|
||||
TEST_LIST = {
|
||||
{ "routing simple", test_routing_simple },
|
||||
{ "routing star", test_routing_star },
|
||||
{ "routing merge", test_routing_merge },
|
||||
{ "routing nest", test_routing_nest },
|
||||
{ NULL, NULL }
|
||||
};
|
File diff suppressed because it is too large
Load Diff
|
@ -1,9 +0,0 @@
|
|||
#ifndef TEST_TESTS
|
||||
#define TEST_TESTS
|
||||
|
||||
void test_routing_simple();
|
||||
void test_routing_star();
|
||||
void test_routing_merge();
|
||||
void test_routing_nest();
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue