From 89784ab90a12c2ec0ddfb87482f487a1466b6942 Mon Sep 17 00:00:00 2001 From: cpq Date: Tue, 19 Jan 2021 14:21:50 +0000 Subject: [PATCH] Restore to a simple version --- examples/desktop-server/Makefile | 17 +-- examples/desktop-server/main.c | 124 ++++---------------- examples/desktop-server/win32/res.rc | 1 - examples/desktop-server/win32/systray.ico | Bin 15086 -> 0 bytes examples/desktop-server/win32/winmain.c | 131 ---------------------- 5 files changed, 26 insertions(+), 247 deletions(-) delete mode 100644 examples/desktop-server/win32/res.rc delete mode 100644 examples/desktop-server/win32/systray.ico delete mode 100644 examples/desktop-server/win32/winmain.c diff --git a/examples/desktop-server/Makefile b/examples/desktop-server/Makefile index 9085b907..2e32522c 100644 --- a/examples/desktop-server/Makefile +++ b/examples/desktop-server/Makefile @@ -4,28 +4,15 @@ 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 -GCC = docker run --rm -v $(ROOT):$(ROOT) -w $(CURDIR) mdashnet/cc2 all: $(PROG) $(DEBUGGER) ./$(PROG) $(ARGS) $(PROG): main.c Makefile - $(CC) ../../mongoose.c main.c -I../.. $(CFLAGS) $(EXTRA) -o $@ - -mongoose_mac: $(PROG) - cp $(PROG) $@ - -mongoose_linux: main.c - $(GCC) $(CC) ../../mongoose.c main.c $(CFLAGS) -s -o $@ + $(CC) ../../mongoose.c main.c -I../.. $(CFLAGS) -o $@ mongoose.exe: main.c - $(VC98) wine rc /fomongoose.res win32/res.rc - $(VC98) wine cl ../../mongoose.c main.c $(VCFLAGS) mongoose.res shell32.lib ws2_32.lib user32.lib /out:$@ - -zip: mongoose.exe mongoose_linux mongoose_mac - rm -rf mongoose; mkdir mongoose - cp mongoose.exe mongoose_mac mongoose_linux mongoose - zip -r mongoose.zip mongoose + $(VC98) wine cl ../../mongoose.c main.c $(VCFLAGS) ws2_32.lib /out:$@ clean: rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb mongoose mongoose_* mongoose.* diff --git a/examples/desktop-server/main.c b/examples/desktop-server/main.c index f1e634d3..e4956785 100644 --- a/examples/desktop-server/main.c +++ b/examples/desktop-server/main.c @@ -9,9 +9,6 @@ static const char *s_listening_address = "http://localhost:8000"; static const char *s_enable_hexdump = "no"; static const char *s_ssi_pattern = "#.shtml"; -static struct mg_mgr s_mgr; -static struct mg_timer s_timer; - 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}; @@ -34,114 +31,41 @@ static void usage(const char *prog) { exit(EXIT_FAILURE); } -static void cfg(const char *progname, const char *optname, const char *value) { - if (value == NULL) { - usage(progname); - } else if (strcmp(optname, "-D") == 0) { - s_debug_level = value; - } else if (strcmp(optname, "-H") == 0) { - s_enable_hexdump = value; - } else if (strcmp(optname, "-S") == 0) { - s_ssi_pattern = value; - } else if (strcmp(optname, "-d") == 0) { - s_root_dir = value; - } else if (strcmp(optname, "-l") == 0) { - s_listening_address = value; - } else { - usage(progname); - } - LOG(LL_INFO, ("Setting option [%s] to [%s]", optname, value)); -} - -static FILE *opencfg(const char *exe, const char *mode) { - char path[1024]; - const char *p = strrchr(exe, MG_DIRSEP); - if (p == NULL) p = strrchr(exe, '/'); - snprintf(path, sizeof(path), "%.*s%c%s", p == NULL ? 1 : (int) (p - exe), - p == NULL ? "." : exe, MG_DIRSEP, "mongoose.conf"); - return fopen(path, mode); -} - -static void parse_config_file(const char *exe) { - FILE *fp = opencfg(exe, "rb"); - if (fp != NULL) { - char line[8192], *name; - int i, line_no = 0; - LOG(LL_INFO, ("Processing config file...")); - while (fgets(line, sizeof(line), fp) != NULL) { - line_no++; - // Trim whitespaces at the end - for (i = strlen(line); i > 0 && isspace(line[i - 1]);) line[--i] = '\0'; - // Ignore empty lines and comments - for (i = 0; isspace(line[i]);) i++; - if (line[i] == '#' || line[i] == '\0') continue; - name = &line[i]; - while (line[i] != '\0' && !isspace(line[i])) i++; - if (line[i] != ' ') { - LOG(LL_ERROR, ("Ignoring invalid line %d: %s", line_no, line)); - } else { - line[i++] = '\0'; // nul-terminate name - while (isspace(line[i])) i++; - cfg(exe, name, strdup(&line[i])); - } - } - fclose(fp); - } -} - -static void ccb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - if (ev == MG_EV_HTTP_MSG) { - struct mg_http_message *hm = ev_data; - LOG(LL_DEBUG, ("New version: %.*s", (int) hm->body.len, hm->body.ptr)); - c->is_closing = 1; - } - (void) fn_data; -} - -// Check latest version, alert user if upgrade is available -static void timer_fn(void *arg) { - struct mg_connection *c = - mg_http_connect(&s_mgr, "http://cesanta.com", ccb, NULL); - if (c != NULL) - mg_printf(c, "%s", - "GET /downloads/mws/version.json HTTP/1.0\r\n" - "Host: cesanta.com\r\n" - "\r\n"); - (void) arg; -} - -static void init_mongoose(int argc, char *argv[]) { +int main(int argc, char *argv[]) { + struct mg_mgr mgr; struct mg_connection *c; int i; - // Configure options from a command-line flags and configuration file - for (i = 1; i < argc; i += 2) cfg(argv[0], argv[i], argv[i + 1]); - parse_config_file(argv[0]); + // Parse command-line flags + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-d") == 0) { + s_root_dir = argv[++i]; + } else if (strcmp(argv[i], "-D") == 0) { + s_debug_level = argv[++i]; + } else if (strcmp(argv[i], "-H") == 0) { + s_enable_hexdump = argv[++i]; + } else if (strcmp(argv[i], "-S") == 0) { + s_ssi_pattern = argv[++i]; + } else if (strcmp(argv[i], "-l") == 0) { + s_listening_address = argv[++i]; + } else { + usage(argv[0]); + } + } // Initialise stuff mg_log_set(s_debug_level); - mg_mgr_init(&s_mgr); - if ((c = mg_http_listen(&s_mgr, s_listening_address, cb, &s_mgr)) == NULL) { + mg_mgr_init(&mgr); + if ((c = mg_http_listen(&mgr, s_listening_address, cb, &mgr)) == NULL) { LOG(LL_ERROR, ("Cannot listen on %s. Use http://ADDR:PORT or :PORT", s_listening_address)); exit(EXIT_FAILURE); } - mg_timer_init(&s_timer, 3600000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, - &s_mgr); if (mg_casecmp(s_enable_hexdump, "yes") == 0) c->is_hexdumping = 1; - s_root_dir = realpath(s_root_dir, NULL); - if (s_root_dir == NULL) s_root_dir = "."; - LOG(LL_INFO, ("Starting Mongoose v%s, serving %s", MG_VERSION, s_root_dir)); -} -#ifdef _WIN32 -#include "win32/winmain.c" -#else -int main(int argc, char *argv[]) { - init_mongoose(argc, argv); - for (;;) mg_mgr_poll(&s_mgr, 1000); - mg_timer_free(&s_timer); - mg_mgr_free(&s_mgr); + // Start infinite event loop + LOG(LL_INFO, ("Starting Mongoose web server v%s", MG_VERSION)); + for (;;) mg_mgr_poll(&mgr, 1000); + mg_mgr_free(&mgr); return 0; } -#endif diff --git a/examples/desktop-server/win32/res.rc b/examples/desktop-server/win32/res.rc deleted file mode 100644 index 52dba67b..00000000 --- a/examples/desktop-server/win32/res.rc +++ /dev/null @@ -1 +0,0 @@ -150 ICON DISCARDABLE "systray.ico" diff --git a/examples/desktop-server/win32/systray.ico b/examples/desktop-server/win32/systray.ico deleted file mode 100644 index 2c7c97eb0b44734abe1d8aade4d8f2e0a28c6177..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 15086 zcmd5@X>gTS89qoI)b8mm9XEAwUR8$VOhD=X~F}`R={nm%At2nK$QK z&UxSGdCz;6@0=5*2B?8*$Pk5eqBPAoAc9 zN?6a4D205z{norTSlKcPX%?i#7GCq2e($EkyhEA}DS}9d@EPBQ=`hdi_r*v<%bI(> z3(^roSyvrPpH(VF`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K2Yap@O3)s@8GU;IZm!+t z;YC zbwTWfiyy@reU7wTtw9W54aBhJ_i(gvOpdbto_o z)z?jzj$?l)J**DcPPvcJ*>+tjut%}_ubJ@Ed1$+|0*3%u65Q^hs5s^KLLJ`IfUSG#t(7(%m1t6hw*TF z?{b_i`&w`;yTni3@giLxdse>~sn56w{2}eviQQa#*Td#F#^`>7xOLygI&idK`n~&R zr*4nkH@hqjvn&quJ==6}`#mcdU$M{ms@CheC)4Bq0^oP_p9Zg6>c*j4_j5dhS94$$ z<|vO_^#x4i!+7UB7@6y_jGW}=%{0FSFaL^F}yXimg z0SB(>1;wTNeK?2qmdjFiqFDEP+)FH8`oo_1pl{)0o=0?Y;26)nUG86VFMHk0;Q_3s z4&ChsVZ@jlSWA5l*Id(!jsx-?DV?uWGH47AUA$k9m|?%z z(bj}L!HIg0wtCAP#CemoZy3^!B4FYCforH-N272pbs?k+5|IPXd+y2nwLVk9eZkDh zpT)R&zcgPtgMHlhd;IUvMGoBG<#@(D{mjX?>3HM044(JY`+JW3z2IPf$+c5Gf8!(+ z*A%WpE`{{cpIY3ArQq%F>6qdEZ&}Xobsx`hYB%=%b-N6~!e^{2;;r5?oAY_>7iXc) zF3Fa6A9F1OETO(6ws#<5Yo>Tc4r!36I3ORe{tEl!M>VF_ri)TtJWKEAy0$yC1&*^; zf6DVPEyA|LGq={+pfIdaV>cf&Asiyd5-_p9C_(Jwn@#sQuGozqk)M0!+gFU0ec3O` z7MxLt@?4Xr-wk$?hH!`~mKYnMZI<=pE>@q!cz264moiafkC%gsO%Lo|oIIb$jNOAn z${l7`)bLn>n_>DWCuq#~A5hP@9sq#U~khh)T3 zpJDglkVq``Irb!S=r`;k95@%`+&jwe2R)u~{@$mqBF?{iVF;|8_A24ui|!G`I~9P|~~tJWFlfe%lYmFUC*CZ-K*EC=Mlo J17Wt`{|_E@fYty2 diff --git a/examples/desktop-server/win32/winmain.c b/examples/desktop-server/win32/winmain.c deleted file mode 100644 index 4185ff70..00000000 --- a/examples/desktop-server/win32/winmain.c +++ /dev/null @@ -1,131 +0,0 @@ -#include -#include - -static const char *s_server_name = "Mongoose v" MG_VERSION; -static HANDLE hThread; // Serving thread -static NOTIFYICONDATA TrayIcon; - -enum { - ID_QUIT = 100, // Menu item IDs - ID_SEPARATOR, - ID_SET_DIR, - ID_ICON_MONGOOSE = 150, // Icons for mongoose menu -}; - -static const char *get_my_url(void) { - static char buf[100]; - char local_ip[50] = "", hostname[200]; - struct hostent *he; - if (gethostname(hostname, sizeof(hostname)) == 0 && - (he = gethostbyname(hostname)) != NULL) { - snprintf(local_ip, sizeof(local_ip), "%s", - inet_ntoa(*(struct in_addr *) he->h_addr_list[0])); - } - if (local_ip[0] == '\0') snprintf(local_ip, sizeof(local_ip), "127.0.0.1"); - snprintf(buf, sizeof(buf), "http://%s:%d", local_ip, - mg_url_port(s_listening_address)); - return buf; -} - -static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, - LPARAM lParam) { - POINT pt; - HMENU hMenu; - switch (msg) { - case WM_COMMAND: - switch (LOWORD(wParam)) { - case ID_QUIT: - Shell_NotifyIcon(NIM_DELETE, &TrayIcon); - PostQuitMessage(0); - return 0; - case ID_SET_DIR: { - char path[PATH_MAX] = ""; - BROWSEINFO bi; - memset(&bi, 0, sizeof(bi)); - bi.hwndOwner = hWnd; - bi.lpszTitle = "Choose root directory:"; - bi.ulFlags = BIF_RETURNONLYFSDIRS; - SHGetPathFromIDList(SHBrowseForFolder(&bi), path); - if (path[0] != '\0') { - FILE *fp = opencfg(__argv[0], "w"); - s_root_dir = strdup(path); - if (fp != NULL) { - fprintf(fp, "-d %s\n", path); - fclose(fp); - } - } - break; - } - } - break; - case WM_USER: - switch (lParam) { - case WM_RBUTTONUP: - case WM_LBUTTONUP: - case WM_LBUTTONDBLCLK: { - char buf[1024]; - hMenu = CreatePopupMenu(); - AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, s_server_name); - AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, ""); - snprintf(buf, sizeof(buf), "Shared directory: %s", s_root_dir); - AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, buf); - snprintf(buf, sizeof(buf), "URL: %s", get_my_url()); - AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, buf); - AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, ""); - AppendMenu(hMenu, MF_STRING, ID_SET_DIR, - "Change shared directory ..."); - AppendMenu(hMenu, MF_STRING, ID_QUIT, "Exit"); - GetCursorPos(&pt); - SetForegroundWindow(hWnd); - TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL); - PostMessage(hWnd, WM_NULL, 0, 0); - DestroyMenu(hMenu); - break; - } - } - break; - case WM_CLOSE: - Shell_NotifyIcon(NIM_DELETE, &TrayIcon); - PostQuitMessage(0); - return 0; // We've just sent our own quit message, with proper hwnd. - default: - break; - } - return DefWindowProc(hWnd, msg, wParam, lParam); -} - -int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show) { - WNDCLASS cls; - HWND hWnd; - MSG msg = {0}; - - init_mongoose(__argc, __argv); - memset(&cls, 0, sizeof(cls)); - cls.lpfnWndProc = (WNDPROC) WindowProc; - cls.hIcon = LoadIcon(NULL, IDI_APPLICATION); - cls.lpszClassName = s_server_name; - RegisterClass(&cls); - hWnd = CreateWindow(cls.lpszClassName, s_server_name, WS_OVERLAPPEDWINDOW, 0, - 0, 0, 0, NULL, NULL, NULL, NULL); - ShowWindow(hWnd, SW_HIDE); - - TrayIcon.cbSize = sizeof(TrayIcon); - TrayIcon.uID = ID_ICON_MONGOOSE; - TrayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; - TrayIcon.hIcon = - LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ICON_MONGOOSE)); - TrayIcon.hWnd = hWnd; - snprintf(TrayIcon.szTip, sizeof(TrayIcon.szTip), "%s", s_server_name); - TrayIcon.uCallbackMessage = WM_USER; - Shell_NotifyIcon(NIM_ADD, &TrayIcon); - - while (msg.message != WM_QUIT) { - mg_mgr_poll(&s_mgr, 1); - if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE) > 0) { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - } - mg_mgr_free(&s_mgr); - return msg.wParam; -}