diff --git a/examples/http-server/Makefile b/examples/http-server/Makefile index 2e32522c..0e6bae63 100644 --- a/examples/http-server/Makefile +++ b/examples/http-server/Makefile @@ -3,7 +3,8 @@ ROOT ?= $(realpath $(CURDIR)/../..) DEFS ?= -DMG_ENABLE_LINES=1 -DMG_ENABLE_DIRECTORY_LISTING=1 -DMG_ENABLE_SSI=1 CFLAGS ?= -I../.. -W -Wall -DMG_ENABLE_IPV6=1 $(DEFS) $(EXTRA) VCFLAGS = /nologo /W3 /O2 /I../.. $(DEFS) $(EXTRA) /link /incremental:no /machine:IX86 -VC98 = docker run --rm -e Tmp=. -v $(ROOT):$(ROOT) -w $(CURDIR) docker.io/mdashnet/vc98 +VC98 = docker run -it --rm -e Tmp=. -v $(ROOT):$(ROOT) -w $(CURDIR) mdashnet/vc98 +LIN = docker run -it --rm -v $(ROOT):$(ROOT) -w $(CURDIR) mdashnet/cc2 all: $(PROG) $(DEBUGGER) ./$(PROG) $(ARGS) @@ -14,5 +15,10 @@ $(PROG): main.c Makefile mongoose.exe: main.c $(VC98) wine cl ../../mongoose.c main.c $(VCFLAGS) ws2_32.lib /out:$@ +linux: all +linux: CFLAGS += -O2 -fsanitize=address,undefined,shift,null,return,bounds,alignment,object-size,bool,enum -static-libasan +#linux: CC = $(LIN) cc +#linux: DEBUGGER = $(LIN) + clean: rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb mongoose mongoose_* mongoose.* diff --git a/examples/http-server/main.c b/examples/http-server/main.c index a311e063..a984c4e0 100644 --- a/examples/http-server/main.c +++ b/examples/http-server/main.c @@ -1,6 +1,7 @@ // Copyright (c) 2020 Cesanta Software Limited // All rights reserved +#include #include "mongoose.h" static const char *s_debug_level = "2"; @@ -9,6 +10,14 @@ static const char *s_listening_address = "http://localhost:8000"; static const char *s_enable_hexdump = "no"; static const char *s_ssi_pattern = "#.shtml"; +// Handle interrupts, like Ctrl-C +static int s_signo; +static void signal_handler(int signo) { + s_signo = signo; +} + +// Event handler for the listening connection. +// Simply serve static files from `s_root_dir` static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_serve_opts opts = {s_root_dir, s_ssi_pattern, NULL}; @@ -54,6 +63,8 @@ int main(int argc, char *argv[]) { } // Initialise stuff + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); mg_log_set(s_debug_level); mg_mgr_init(&mgr); if ((c = mg_http_listen(&mgr, s_listening_address, cb, &mgr)) == NULL) { @@ -65,7 +76,8 @@ int main(int argc, char *argv[]) { // Start infinite event loop LOG(LL_INFO, ("Starting Mongoose v%s, serving [%s]", MG_VERSION, s_root_dir)); - for (;;) mg_mgr_poll(&mgr, 1000); + while (s_signo == 0) mg_mgr_poll(&mgr, 1000); mg_mgr_free(&mgr); + LOG(LL_INFO, ("Exiting on signal %d", s_signo)); return 0; } diff --git a/mongoose.c b/mongoose.c index d4dc69d7..2314f90e 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1378,7 +1378,8 @@ int mg_iobuf_resize(struct mg_iobuf *io, size_t new_size) { // porting to some obscure platforms like FreeRTOS void *p = malloc(new_size); if (p != NULL) { - memcpy(p, io->buf, io->size < new_size ? io->size : new_size); + size_t len = new_size < io->len ? new_size : io->len; + if (len > 0) memcpy(p, io->buf, len); free(io->buf); io->buf = (unsigned char *) p; io->size = new_size; @@ -4041,9 +4042,8 @@ void mg_random(void *buf, size_t len) { #if MG_ENABLE_FS FILE *fp = mg_fopen("/dev/urandom", "rb"); if (fp != NULL) { - fread(buf, 1, len, fp); + if (fread(buf, 1, len, fp) == len) done = true; fclose(fp); - done = true; } #endif if (!done) {