Compare commits

..
87 Commits
Author SHA1 Message Date
Sergey Lyubka 13d0b67c00 More updates to User Manual 2013-02-05 18:00:49 +00:00
Sergey Lyubka bda8e90733 Added protection against running under launchd 2013-02-05 17:59:56 +00:00
Sergey Lyubka fed2057a33 Added comment 2013-02-05 17:59:16 +00:00
Sergey Lyubka a27f62f89f More options 2013-02-05 14:04:38 +00:00
Sergey Lyubka d53902844c Squashed warning for lua+sqlite code path 2013-02-04 22:14:42 +00:00
Sergey Lyubka 9870b80c65 Updated README 2013-02-04 20:18:07 +00:00
Sergey Lyubka cde25a8b82 Removed embedded test 2013-02-03 19:56:24 +00:00
Sergey Lyubka 1b7f036199 Removed embedded test 2013-02-03 19:56:16 +00:00
Sergey Lyubka b80157b7ea Setting user data pointer properly 2013-02-03 19:15:27 +00:00
Sergey Lyubka f9311f0b30 Building cocoa version with lua + sqlite 2013-02-02 16:42:01 +00:00
Sergey Lyubka bdd933f597 Fixed UNIX code path for handle_lsp_request() 2013-02-02 16:41:43 +00:00
Sergey Lyubka ee55d38b55 API CHANGE: using struct mg_callbacks 2013-02-01 16:48:30 +00:00
Sergey Lyubka 62162ff3e4 Added /DNO_SSL_DL to w target 2013-02-01 13:53:31 +00:00
Sergey Lyubka 98f66c8867 Updated User Manual 2013-01-31 22:33:08 +00:00
Sergey Lyubka c132191cad Prepare to change the API to use explicit callbacks 2013-01-31 18:29:22 +00:00
Sergey Lyubka d73aa1ad09 Added skeleton for regex checks 2013-01-31 18:06:05 +00:00
Sergey Lyubka d5bc58c38d Using sqlite as an example 2013-01-31 15:17:40 +00:00
Sergey Lyubka 51a80e4844 Do not output headers when serving Lua server pages 2013-01-31 15:17:19 +00:00
Sergey Lyubka 54f15a8fe3 Adding lua+sqlite functionality in the default windows binary build 2013-01-31 13:38:57 +00:00
Sergey Lyubka ce0826e285 Adding lua+sqlite functionality in the default windows binary build 2013-01-31 13:38:32 +00:00
Sergey Lyubka b21a8b2193 Removed wait_until_socket_is_readable(). Rely on socket timeout. 2013-01-29 11:21:23 +00:00
Sergey Lyubka 8bb49642da Merge pull request #86 from thekvs/master
I've just fixed a typo.
2013-01-28 12:06:28 -08:00
Konstantin Sorokin 3f78df86ac fixed typo 2013-01-28 18:49:19 +04:00
Sergey Lyubka c30f7a5258 Merge pull request #77 from bamiaux/add_request_timeout
Add request_timeout option
2013-01-28 03:13:28 -08:00
Sergey Lyubka 7ed94d1918 Unit test call fixed 2013-01-27 20:49:30 +00:00
Sergey Lyubka dc513a9a15 Wrapped SSL code into #ifndef NO_SSL 2013-01-27 20:49:17 +00:00
Sergey Lyubka 8ce6cf61b7 Using relative path to yassl 2013-01-27 20:48:52 +00:00
Sergey Lyubka e63f7fe33a Always initializing struct file on stack 2013-01-25 21:50:00 +00:00
Sergey Lyubka b589e0cdf4 Fixed mg_upload() and some SSL related issues 2013-01-25 20:28:09 +00:00
Sergey Lyubka 2b87f909ce Makefile refactored 2013-01-25 20:27:52 +00:00
Sergey Lyubka ec7d0b1abd Added test_mg_upload() 2013-01-25 20:27:08 +00:00
Sergey Lyubka 5bb3ede46d Removed mongoose.1 2013-01-25 13:21:19 +00:00
Sergey Lyubka 5105717f78 Added printf checkers for mg_download 2013-01-24 09:35:54 +00:00
Sergey Lyubka 9d1e356709 changed README 2013-01-24 09:35:11 +00:00
Sergey Lyubka 3d7192d9fa Typo in mongoose.h fixed 2013-01-24 01:16:23 +00:00
Sergey Lyubka 1b3cbb17c1 Updated README 2013-01-23 23:48:07 +00:00
Sergey Lyubka f1f03e4378 Updated README 2013-01-23 23:43:59 +00:00
Sergey Lyubka 625a2a3652 Updated README 2013-01-23 23:42:46 +00:00
Sergey Lyubka 33a31d9308 Updated README 2013-01-23 23:39:55 +00:00
Sergey Lyubka 5e9bd1f05e Update README.md 2013-01-23 23:34:01 +00:00
Sergey Lyubka 75d9a6c844 Removed mg_connect() and mg_fetch(). Added mg_download() 2013-01-23 21:54:27 +00:00
Sergey Lyubka 179761fd99 Removed LUA_FLAGS from CL flags 2013-01-23 21:14:11 +00:00
Sergey Lyubka b783813e54 Always sending MG_NEW_REQUEST, as first step in processing the request 2013-01-22 12:28:22 +00:00
Sergey Lyubka 4b29689a42 Binding to 127.0.0.1 to prevent popup windows on mac 2013-01-22 12:26:56 +00:00
Sergey Lyubka 9c4853471c Added stub for test_mg_upload() 2013-01-22 09:08:00 +00:00
Sergey Lyubka 81e21a3a0b Using get_url_to_first_open_port() in cocoa build 2013-01-22 09:00:17 +00:00
Sergey Lyubka bec6ea00ea Using unit test targets 2013-01-22 08:59:42 +00:00
Sergey Lyubka ed32e3be0f Added unix_unit_test and windows_unit_test targets 2013-01-22 08:59:13 +00:00
Sergey Lyubka 83fae879bf Binding to 127.0.0.1 to prevent annoying popups on mac 2013-01-21 22:29:31 +00:00
Sergey Lyubka 2169e6d77f Added -pthread to the CFLAGS 2013-01-21 17:51:56 +00:00
Sergey Lyubka 850e0a5922 Updated readme 2013-01-21 09:11:58 +00:00
Sergey Lyubka 322ed3cc6f fflush()-ing uploaded file before calling MG_UPLOAD callback 2013-01-21 01:39:14 +00:00
Sergey Lyubka 6d4d53c247 Squashed linux warning in process_new_connection() 2013-01-20 10:11:57 +00:00
Sergey Lyubka e6f8d40942 SSL redirect, using Host: header 2013-01-20 10:06:31 +00:00
Sergey Lyubka 7e051206ad Removed obsolete manual reference 2013-01-20 00:21:57 +00:00
Sergey Lyubka 76d913796c Getting first SSL listener dynamically 2013-01-19 22:32:26 +00:00
Sergey Lyubka 4c2609df2c Merge branch 'master' of ssh://github.com/valenok/mongoose 2013-01-19 10:46:10 +00:00
Sergey Lyubka f79bd71fd4 Implemented ssl redirection 2013-01-19 10:45:58 +00:00
Sergey Lyubka 5d4f5b6000 Merge pull request #83 from rogerz/mingw-build-error
Fix building error under mingw
2013-01-18 05:36:50 -08:00
Rogerz Zhang fe7c245a6b Fix building error under mingw 2013-01-18 21:22:44 +08:00
Sergey Lyubka f24bebaca8 3.6 -> 3.7 2013-01-18 11:08:20 +00:00
Sergey Lyubka 08afdfa8b6 better cleanup 2013-01-18 10:43:52 +00:00
Sergey Lyubka 05f7d65fc9 Added "Start browser" menu item 2013-01-18 10:38:58 +00:00
Sergey Lyubka a194e07545 2011 -> 2013 2013-01-18 09:45:54 +00:00
Sergey Lyubka dbb8a5bbf4 put_delete_passwords_file -> put_delete_auth_file 2013-01-18 09:45:36 +00:00
Sergey Lyubka d3005df019 Bug fix in Win32 mg_mkdir 2013-01-17 17:51:59 +00:00
Sergey Lyubka ef9b103a00 Started adding ssl redirection support 2013-01-17 17:50:18 +00:00
Sergey Lyubka f0a2924a7c Added config editor for Win32 2013-01-17 17:14:41 +00:00
Sergey Lyubka f109030cd0 Renamed: global_passwords_file -> global_auth_file, put_delete_passwords_file -> put_delete_auth_file. 2013-01-17 17:14:11 +00:00
Sergey Lyubka eebdd06532 Added comdlg32.lib to GUILIB 2013-01-17 17:13:15 +00:00
Sergey Lyubka ca2fc71a01 Changed systray icon 2013-01-17 11:26:50 +00:00
Sergey Lyubka e61e371c16 Changed user manual 2013-01-17 11:20:59 +00:00
Sergey Lyubka d5f07f5b9b Changed README 2013-01-17 11:07:53 +00:00
Sergey Lyubka 0a8d5dde51 Changed README 2013-01-17 10:59:42 +00:00
Sergey Lyubka 5646caa572 Using poll() instead of select() to prevent FD_SETSIZE overflows, for programs that open large number of descriptors 2013-01-16 23:45:03 +00:00
Sergey Lyubka c7e5573d08 Not building DLLs. Using separate target for cocoa mac app. 2013-01-16 23:43:06 +00:00
Sergey Lyubka b9a226d109 Adding command line options 2013-01-15 13:53:25 +00:00
Sergey Lyubka fcf8b86288 Adding command line options 2013-01-15 13:51:35 +00:00
Sergey Lyubka a8b08c6784 Fixing http://code.google.com/p/mongoose/issues/detail?id=392 2013-01-15 13:24:53 +00:00
Sergey Lyubka 1fb6f391a1 In mg_upload(), use fopen() with "wb" mode instead of "wbx", cause "x" is not understood by Mac and Windows. 2013-01-13 10:55:42 +00:00
Sergey Lyubka fcbc36ecde return 0; after PostQuitMessage() 2013-01-10 16:26:01 +00:00
Sergey Lyubka 70771f29ce Config edit implemented for mac 2013-01-10 16:24:42 +00:00
Sergey Lyubka 121391504f mongoose.app -> Mongoose.app 2013-01-10 16:24:12 +00:00
Sergey Lyubka 8455742440 Pass reply status code to MG_REQUEST_COMPLETE 2013-01-10 09:56:24 +00:00
Sergey Lyubka 5b786a5089 make release corrected 2013-01-09 21:21:04 +00:00
Sergey Lyubka a6e74e7707 3.5 -> 3.6 2013-01-09 20:55:21 +00:00
Benoît Amiaux acf72a6109 Add request_timeout option 2013-01-09 17:09:04 +01:00
22 changed files with 148168 additions and 1367 deletions

No files matched your search

+104 -103
View File
@@ -1,12 +1,10 @@
# This file is part of Mongoose project, http://code.google.com/p/mongoose # This Makefile is part of Mongoose web server project,
# $Id: Makefile 473 2009-09-02 11:20:06Z valenok $ # https://github.com/valenok/mongoose
#
PROG= mongoose # Example custom build:
# COPT="-g -O0 -DNO_SSL_DL -DUSE_LUA -llua -lcrypto -lssl" make linux
all: #
@echo "make (linux|bsd|solaris|mac|windows|mingw|cygwin)" # Flags are:
# Possible COPT values: (in brackets are rough numbers for 'gcc -O2' on i386)
# -DHAVE_MD5 - use system md5 library (-2kb) # -DHAVE_MD5 - use system md5 library (-2kb)
# -DNDEBUG - strip off all debug code (-5kb) # -DNDEBUG - strip off all debug code (-5kb)
# -DDEBUG - build debug version (very noisy) (+7kb) # -DDEBUG - build debug version (very noisy) (+7kb)
@@ -18,61 +16,12 @@ all:
# -DCRYPTO_LIB=\"libcrypto.so.<version>\" - use system versioned CRYPTO so # -DCRYPTO_LIB=\"libcrypto.so.<version>\" - use system versioned CRYPTO so
# -DUSE_LUA - embed Lua in Mongoose (+100kb) # -DUSE_LUA - embed Lua in Mongoose (+100kb)
PROG = mongoose
########################################################################## CFLAGS = -std=c99 -O2 -W -Wall -pedantic -pthread $(COPT)
### UNIX build: linux, bsd, mac, rtems
##########################################################################
# To build with Lua, download and unzip Lua 5.2.1 source code into the # To build with Lua, download and unzip Lua 5.2.1 source code into the
# mongoose directory, and then add $(LUA_FLAGS) to CFLAGS below # mongoose directory, and then add $(LUA_SOURCES) to CFLAGS
LUA = lua-5.2.1/src LUA = lua-5.2.1/src
LUA_FLAGS = -DUSE_LUA -I$(LUA) -L$(LUA) -llua -lm
CFLAGS = -std=c99 -O2 -W -Wall -pedantic $(COPT)
LIB = lib$(PROG).so$(MONGOOSE_LIB_SUFFIX)
# Make sure that the compiler flags come last in the compilation string.
# If not so, this can break some on some Linux distros which use
# "-Wl,--as-needed" turned on by default in cc command.
# Also, this is turned in many other distros in static linkage builds.
linux:
$(CC) mongoose.c -shared -fPIC -fpic -o $(LIB) -Wl,-soname,$(LIB) -ldl -pthread $(CFLAGS)
$(CC) mongoose.c main.c -o $(PROG) -ldl -pthread $(CFLAGS)
bsd:
$(CC) mongoose.c -shared -pthread -fpic -fPIC -o $(LIB) $(CFLAGS)
$(CC) mongoose.c main.c -pthread -o $(PROG) $(CFLAGS)
mac:
$(CC) mongoose.c -pthread -o $(LIB) -flat_namespace -bundle -undefined suppress $(CFLAGS)
$(CC) mongoose.c main.c -DUSE_COCOA -pthread $(CFLAGS) -framework Cocoa -ObjC -arch i386 -arch x86_64 -o $(PROG)
V=`perl -lne '/define\s+MONGOOSE_VERSION\s+"(\S+)"/ and print $$1' mongoose.c`; DIR=dmg/$(PROG).app && rm -rf $$DIR && mkdir -p $$DIR/Contents/{MacOS,Resources} && install -m 644 build/mongoose_*.png $$DIR/Contents/Resources/ && install -m 644 build/Info.plist $$DIR/Contents/ && install -m 755 $(PROG) $$DIR/Contents/MacOS/ && ln -fs /Applications dmg/ ; hdiutil create $(PROG)_$$V.dmg -volname "Mongoose $$V" -srcfolder dmg -ov #; rm -rf dmg
solaris:
$(CC) mongoose.c -pthread -lnsl -lsocket -fpic -fPIC -shared -o $(LIB) $(CFLAGS)
$(CC) mongoose.c main.c -pthread -lnsl -lsocket -o $(PROG) $(CFLAGS)
##########################################################################
### WINDOWS build: Using Visual Studio or Mingw
##########################################################################
# Using Visual Studio 6.0. To build Mongoose:
# o Set MSVC variable below to where VS 6.0 is installed on your system
# o Run "PATH_TO_VC6\bin\nmake windows"
MSVC = e:/vc6
CYA = e:/cyassl-2.0.0rc2
#DBG = /Zi /DDEBUG /Od
DBG = /DNDEBUG /O1
CL = $(MSVC)/bin/cl /MD /TC /nologo $(DBG) /Gz /W3 /DNO_SSL_DL \
/I$(MSVC)/include /DUSE_LUA /I$(LUA)
GUILIB= user32.lib shell32.lib
LINK = /link /incremental:no /libpath:$(MSVC)/lib /machine:IX86 \
/subsystem:windows ws2_32.lib advapi32.lib cyassl.lib lua.lib
CYAFL = /c /I $(CYA)/include -I $(CYA)/include/openssl /I$(MSVC)/INCLUDE \
/I $(CYA)/ctaocrypt/include /D _LIB /D OPENSSL_EXTRA
LUA_SOURCES = $(LUA)/lapi.c $(LUA)/lcode.c $(LUA)/lctype.c \ LUA_SOURCES = $(LUA)/lapi.c $(LUA)/lcode.c $(LUA)/lctype.c \
$(LUA)/ldebug.c $(LUA)/ldo.c $(LUA)/ldump.c \ $(LUA)/ldebug.c $(LUA)/ldo.c $(LUA)/ldump.c \
$(LUA)/lfunc.c $(LUA)/lgc.c $(LUA)/llex.c \ $(LUA)/lfunc.c $(LUA)/lgc.c $(LUA)/llex.c \
@@ -86,47 +35,104 @@ LUA_SOURCES = $(LUA)/lapi.c $(LUA)/lcode.c $(LUA)/lctype.c \
$(LUA)/loadlib.c $(LUA)/linit.c $(LUA)/loadlib.c $(LUA)/linit.c
LUA_OBJECTS = $(LUA_SOURCES:%.c=%.o) LUA_OBJECTS = $(LUA_SOURCES:%.c=%.o)
CYA_SOURCES = $(CYA)/src/cyassl_int.c $(CYA)/src/cyassl_io.c \ # Using Visual Studio 6.0. To build Mongoose:
$(CYA)/src/keys.c $(CYA)/src/tls.c $(CYA)/src/ssl.c \ # Set MSVC variable below to where VS 6.0 is installed on your system
$(CYA)/ctaocrypt/src/aes.c $(CYA)/ctaocrypt/src/arc4.c \ # Run "PATH_TO_VC6\bin\nmake windows"
$(CYA)/ctaocrypt/src/asn.c $(CYA)/ctaocrypt/src/coding.c \ MSVC = ../vc6
$(CYA)/ctaocrypt/src/ctc_asm.c $(CYA)/ctaocrypt/src/ctc_misc.c \ #DBG = /Zi /Od
$(CYA)/ctaocrypt/src/cyassl_memory.c $(CYA)/ctaocrypt/src/des3.c \ DBG = /DNDEBUG /O1
$(CYA)/ctaocrypt/src/dh.c $(CYA)/ctaocrypt/src/dsa.c \ CL = $(MSVC)/bin/cl /MD /TC /nologo $(DBG) /Gz /W3 \
$(CYA)/ctaocrypt/src/ecc.c $(CYA)/ctaocrypt/src/hc128.c \ /I$(MSVC)/include /I$(LUA) /I. /I$(YASSL) /I$(YASSL)/cyassl /GA
$(CYA)/ctaocrypt/src/hmac.c $(CYA)/ctaocrypt/src/integer.c \ MSLIB = /link /incremental:no /libpath:$(MSVC)/lib /machine:IX86 \
$(CYA)/ctaocrypt/src/md4.c $(CYA)/ctaocrypt/src/md5.c \ user32.lib shell32.lib comdlg32.lib ws2_32.lib advapi32.lib
$(CYA)/ctaocrypt/src/pwdbased.c $(CYA)/ctaocrypt/src/rabbit.c \
$(CYA)/ctaocrypt/src/random.c $(CYA)/ctaocrypt/src/ripemd.c \
$(CYA)/ctaocrypt/src/rsa.c $(CYA)/ctaocrypt/src/sha.c \
$(CYA)/ctaocrypt/src/sha256.c $(CYA)/ctaocrypt/src/sha512.c \
$(CYA)/ctaocrypt/src/tfm.c
cyassl.lib: # Stock windows binary builds with Lua and YASSL library.
$(CL) /Fo$(CYA)/ $(CYA_SOURCES) $(CYAFL) $(DEF) YASSL = ../cyassl-2.4.6
$(MSVC)/bin/lib $(CYA)/*.obj /out:$@ YASSL_FLAGS = -I $(YASSL) -I $(YASSL)/cyassl \
-D _LIB -D OPENSSL_EXTRA -D HAVE_ERRNO_H \
-D HAVE_GETHOSTBYNAME -D HAVE_INET_NTOA -D HAVE_LIMITS_H \
-D HAVE_MEMSET -D HAVE_SOCKET -D HAVE_STDDEF_H -D HAVE_STDLIB_H \
-D HAVE_STRING_H -D HAVE_SYS_STAT_H -D HAVE_SYS_TYPES_H
YASSL_SOURCES = \
$(YASSL)/src/internal.c $(YASSL)/src/io.c $(YASSL)/src/keys.c \
$(YASSL)/src/ssl.c $(YASSL)/src/tls.c $(YASSL)/ctaocrypt/src/hmac.c \
$(YASSL)/ctaocrypt/src/random.c $(YASSL)/ctaocrypt/src/sha.c \
$(YASSL)/ctaocrypt/src/sha256.c $(YASSL)/ctaocrypt/src/logging.c \
$(YASSL)/ctaocrypt/src/error.c $(YASSL)/ctaocrypt/src/rsa.c \
$(YASSL)/ctaocrypt/src/des3.c $(YASSL)/ctaocrypt/src/asn.c \
$(YASSL)/ctaocrypt/src/coding.c $(YASSL)/ctaocrypt/src/arc4.c \
$(YASSL)/ctaocrypt/src/md4.c $(YASSL)/ctaocrypt/src/md5.c \
$(YASSL)/ctaocrypt/src/dh.c $(YASSL)/ctaocrypt/src/dsa.c \
$(YASSL)/ctaocrypt/src/pwdbased.c $(YASSL)/ctaocrypt/src/aes.c \
$(YASSL)/ctaocrypt/src/md2.c $(YASSL)/ctaocrypt/src/ripemd.c \
$(YASSL)/ctaocrypt/src/sha512.c $(YASSL)/src/sniffer.c \
$(YASSL)/ctaocrypt/src/rabbit.c $(YASSL)/ctaocrypt/src/misc.c \
$(YASSL)/ctaocrypt/src/tfm.c $(YASSL)/ctaocrypt/src/integer.c \
$(YASSL)/ctaocrypt/src/ecc.c $(YASSL)/src/ocsp.c $(YASSL)/src/crl.c \
$(YASSL)/ctaocrypt/src/hc128.c $(YASSL)/ctaocrypt/src/memory.c
lua.lib: all:
$(CL) /c /Fo$(LUA)/ $(LUA_SOURCES) @echo "make (linux|bsd|solaris|mac|windows|mingw|cygwin)"
$(MSVC)/bin/lib $(LUA_SOURCES:%.c=%.obj) /out:$@
windows: cyassl.lib lua.lib # Make sure that the compiler flags come last in the compilation string.
# If not so, this can break some on some Linux distros which use
# "-Wl,--as-needed" turned on by default in cc command.
# Also, this is turned in many other distros in static linkage builds.
linux:
$(CC) mongoose.c main.c -o $(PROG) -ldl $(CFLAGS)
mac: bsd
bsd:
$(CC) mongoose.c main.c -o $(PROG) $(CFLAGS)
bsd_yassl:
$(CC) mongoose.c main.c build/lsqlite3.c build/sqlite3.c -o $(PROG) \
$(CFLAGS) -I$(LUA) -Ibuild \
$(YASSL_SOURCES) $(YASSL_FLAGS) -DNO_SSL_DL \
$(LUA_SOURCES) -DUSE_LUA -DUSE_LUA_SQLITE3 -DLUA_COMPAT_ALL
solaris:
$(CC) mongoose.c main.c -lnsl -lsocket -o $(PROG) $(CFLAGS)
# For codesign to work in non-interactive mode, unlock login keychain:
# security unlock ~/Library/Keychains/login.keychain
# See e.g. http://lists.apple.com/archives/apple-cdsa/2008/Jan/msg00027.html
cocoa:
$(CC) mongoose.c main.c build/lsqlite3.c build/sqlite3.c \
-DUSE_COCOA $(CFLAGS) -I$(LUA) -Ibuild \
$(YASSL_SOURCES) $(YASSL_FLAGS) -DNO_SSL_DL \
$(LUA_SOURCES) -DUSE_LUA -DUSE_LUA_SQLITE3 -DLUA_COMPAT_ALL \
-framework Cocoa -ObjC -arch i386 -arch x86_64 -o Mongoose
V=`perl -lne '/define\s+MONGOOSE_VERSION\s+"(\S+)"/ and print $$1' mongoose.c`; DIR=dmg/Mongoose.app && rm -rf $$DIR && mkdir -p $$DIR/Contents/{MacOS,Resources} && install -m 644 build/mongoose_*.png $$DIR/Contents/Resources/ && install -m 644 build/Info.plist $$DIR/Contents/ && install -m 755 Mongoose $$DIR/Contents/MacOS/ && ln -fs /Applications dmg/ ; hdiutil create Mongoose_$$V.dmg -volname "Mongoose $$V" -srcfolder dmg -ov #; rm -rf dmg
u:
$(CC) test/unit_test.c -o unit_test -I. -I$(LUA) $(LUA_SOURCES) \
$(CFLAGS) -g -O0
./unit_test
w:
$(CL) test/unit_test.c $(LUA_SOURCES) \
$(YASSL_SOURCES) $(YASSL_FLAGS) /DNO_SSL_DL \
$(MSLIB) /out:unit_test.exe
./unit_test.exe
windows:
$(MSVC)/bin/rc build\res.rc $(MSVC)/bin/rc build\res.rc
$(CL) /Ibuild main.c mongoose.c /GA $(LINK) build\res.res \ $(CL) main.c mongoose.c build/lsqlite3.c build/sqlite3.c \
$(GUILIB) /out:$(PROG).exe $(YASSL_SOURCES) $(YASSL_FLAGS) /DNO_SSL_DL \
$(CL) mongoose.c /GD $(LINK) /DLL /DEF:build\dll.def /out:$(PROG).dll $(LUA_SOURCES) /DUSE_LUA /DUSE_LUA_SQLITE3 /DLUA_COMPAT_ALL \
$(MSLIB) build\res.res /out:$(PROG).exe /subsystem:windows
# Build for Windows under MinGW # Build for Windows under MinGW
#MINGWDBG= -DDEBUG -O0 -ggdb #MINGWDBG= -DDEBUG -O0 -ggdb
MINGWDBG= -DNDEBUG -Os MINGWDBG= -DNDEBUG -Os
MINGWOPT= -W -Wall -mthreads -Wl,--subsystem,console $(MINGWDBG) -DHAVE_STDINT $(GCC_WARNINGS) $(COPT) MINGWOPT= -W -Wall -mthreads -Wl,--subsystem,console $(MINGWDBG) -DHAVE_STDINT $(GCC_WARNINGS) $(COPT)
#MINGWOPT= -W -Wall -mthreads -Wl,--subsystem,windows $(MINGWDBG) -DHAVE_STDINT $(GCC_WARNINGS) $(COPT)
mingw: mingw:
windres build\res.rc build\res.o windres build\res.rc build\res.o
$(CC) $(MINGWOPT) mongoose.c -lws2_32 \ $(CC) $(MINGWOPT) mongoose.c -lws2_32 \
-shared -Wl,--out-implib=$(PROG).lib -o $(PROG).dll -shared -Wl,--out-implib=$(PROG).lib -o $(PROG).dll
$(CC) $(MINGWOPT) -build mongoose.c main.c build\res.o \ $(CC) $(MINGWOPT) mongoose.c main.c build\res.o \
-lws2_32 -ladvapi32 -o $(PROG).exe -lws2_32 -ladvapi32 -lcomdlg32 -o $(PROG).exe
# Build for Windows under Cygwin # Build for Windows under Cygwin
#CYGWINDBG= -DDEBUG -O0 -ggdb #CYGWINDBG= -DDEBUG -O0 -ggdb
@@ -139,22 +145,17 @@ cygwin:
$(CC) $(CYGWINOPT) -Ibuild mongoose.c main.c ./build/res.o \ $(CC) $(CYGWINOPT) -Ibuild mongoose.c main.c ./build/res.o \
-lws2_32 -ladvapi32 -o $(PROG).exe -lws2_32 -ladvapi32 -o $(PROG).exe
##########################################################################
### Manuals, cleanup, test, release
##########################################################################
man:
groff -man -T ascii mongoose.1 | col -b > mongoose.txt
groff -man -T ascii mongoose.1 | less
# "TEST=unit make test" - perform unit test only
# "TEST=embedded" - test embedded API by building and testing test/embed.c
# "TEST=basic_tests" - perform basic tests only (no CGI, SSI..)
tests: tests:
perl test/test.pl $(TEST) perl test/test.pl $(TEST)
release: clean tarball: clean
F=mongoose-`perl -lne '/define\s+MONGOOSE_VERSION\s+"(\S+)"/ and print $$1' mongoose.c`.tgz ; cd .. && tar -czf x mongoose/{LICENSE,Makefile,examples,test,build,mongoose.c,mongoose.h,mongoose.1} && mv x mongoose/$$F F=mongoose-`perl -lne '/define\s+MONGOOSE_VERSION\s+"(\S+)"/ and print $$1' mongoose.c`.tgz ; cd .. && tar -czf x mongoose/{LICENSE,Makefile,examples,test,build,*.[ch],*.md} && mv x mongoose/$$F
release: tarball cocoa
wine make windows
V=`perl -lne '/define\s+MONGOOSE_VERSION\s+"(\S+)"/ and print $$1' mongoose.c`; upx mongoose.exe; cp mongoose.exe mongoose-$$V.exe; cp mongoose.exe mongoose_php_bundle/; zip -r mongoose_php_bundle_$$V.zip mongoose_php_bundle/
clean: clean:
rm -rf *.o *.core $(PROG) *.obj *.so $(PROG).txt *.dSYM *.tgz $(PROG).exe *.dll *.lib build/res.o build/res.RES cd examples && $(MAKE) clean
rm -rf *.o *.core $(PROG) *.obj *.so $(PROG).txt *.dSYM *.tgz \
$(PROG).exe *.dll *.lib build/res.o build/res.RES *.dSYM
+37 -12
View File
@@ -2,8 +2,9 @@ Project Mission
--------------- ---------------
Project mission is to provide simple, functional, embeddable web server to Project mission is to provide simple, functional, embeddable web server to
make it easy for application and device developers to implement web interface for their make it easy for application and device developers to implement web interface
application and devices, and to offer a simple web development environment. for their application and devices, and to offer a simple web development
environment.
Overview Overview
-------- --------
@@ -11,32 +12,56 @@ Overview
To accomplish it's mission, Mongoose keeps balance on functionality and To accomplish it's mission, Mongoose keeps balance on functionality and
simplicity by carefully selected list of features: simplicity by carefully selected list of features:
- Liberal, commercial-friendly [MIT license](http://en.wikipedia.org/wiki/MIT_License) - Liberal, commercial-friendly
[MIT license](http://en.wikipedia.org/wiki/MIT_License)
- Works on Windows, Mac, UNIX, iPhone, Android, and many other platforms - Works on Windows, Mac, UNIX, iPhone, Android, and many other platforms
- Support for CGI, SSL, SSI, Digest (MD5) authorization, Websocket, WEbDAV - Support for CGI, SSL, SSI, Digest (MD5) authorization, Websocket, WEbDAV
- Lua server pages (PHP-like functionality using Lua), see [page.lp](https://github.com/valenok/mongoose/blob/master/test/page.lp) - Lua server pages (PHP-like functionality using Lua), see
[page.lp](https://github.com/valenok/mongoose/blob/master/test/page.lp)
- Resumed download, URL rewrite, IP-based ACL, Windows service - Resumed download, URL rewrite, IP-based ACL, Windows service
- Excluding files from serving by URI pattern (file blacklist) - Excluding files from serving by URI pattern (file blacklist)
- Download speed limit based on client subnet or URI pattern - Download speed limit based on client subnet or URI pattern
- Small footprint: executable size is 50 kB on Linux 2.6 i386 system - Small footprint: executable size is 50 kB on Linux 2.6 i386 system
- 130 kilobytes Windows executable with all of the above and no dependencies - 130 kilobytes Windows executable with all of the above and no dependencies
- Simple and clean embedding API ([mongoose.h](https://github.com/valenok/mongoose/blob/master/mongoose.h)). The source is in single [mongoose.c](https://github.com/valenok/mongoose/blob/master/mongoose.c) file to make things easy. - Simple and clean embedding API,
- Embedding examples: [hello.c](https://github.com/valenok/mongoose/blob/master/examples/hello.c), [post.c](https://github.com/valenok/mongoose/blob/master/examples/post.c), [upload.c](https://github.com/valenok/mongoose/blob/master/examples/upload.c), [websocket.c](https://github.com/valenok/mongoose/blob/master/examples/websocket.c) [mongoose.h](https://github.com/valenok/mongoose/blob/master/mongoose.h).
- Extensive documentation in form of [User Manual](https://github.com/valenok/mongoose/blob/master/UserManual.md) The source is in single
[mongoose.c](https://github.com/valenok/mongoose/blob/master/mongoose.c) file
to make things easy
- Embedding examples:
[hello.c](https://github.com/valenok/mongoose/blob/master/examples/hello.c),
[post.c](https://github.com/valenok/mongoose/blob/master/examples/post.c),
[upload.c](https://github.com/valenok/mongoose/blob/master/examples/upload.c),
[websocket.c](https://github.com/valenok/mongoose/blob/master/examples/websocket.c)
- HTTP client functionality for embedded usage, capable of
sending arbitrary HTTP/HTTPS requests
- Extensive documentation in form of
[User Manual](https://github.com/valenok/mongoose/blob/master/UserManual.md)
Questions can be asked at Questions can be asked at
[mongoose-users@google.com](http://groups.google.com/group/mongoose-users) mailing list. [mongoose-users@google.com](http://groups.google.com/group/mongoose-users)
mailing list.
Keep Sergey happy Keep Sergey happy
----------------- -----------------
Since 2004, Mongoose is being constantly improved by me, Sergey Lyubka, a software engineer I am Sergey Lyubka, a software engineer from Galway, Ireland. I started
from Galway, Ireland. My other software I give to the community for free is working on Mongoose in 2004, and since then continuously improving it,
investing thousands of hours of work. My other project I'm contributing to the
community for free is
[Super Light Regular Expression library](http://code.google.com/p/slre). [Super Light Regular Expression library](http://code.google.com/p/slre).
If you feel grateful for the stuff I've done, you can buy me a book from my If you feel grateful for the stuff I've done, you can buy me a book from my
[Amazon wishlist](http://amzn.com/w/1OC2ZCPTQYIEP?sort=priority). Many thanks to all who [Amazon wishlist](http://amzn.com/w/1OC2ZCPTQYIEP?sort=priority). Many thanks
already did so: T.Barmann, D.Hughes, J.C.Sloan, R.Romeo and 4 others. to all who already did so: T.Barmann, D.Hughes, J.C.Sloan, R.Romeo,
L.E.Spencer, S.Kotay, B.Fjukstad and 6 others.
Appreciated guys, you keep my brains going! Appreciated guys, you keep my brains going!
Cash is also appreciated indeed. Press
[<img src="http://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif">](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=DGZ2FMP95TAL6)
button to donate. Donation progress: 4/1000 &euro;
(thanks to O.M.Vilhunen, C.Radik)
![Progress](http://chart.googleapis.com/chart?chxr=0,0,1000&chxt=x&chbh=30,0,0&chs=300x35&cht=bhs&chco=90c0f0&chd=s:B)
+152 -12
View File
@@ -1,8 +1,7 @@
**NOTE: THIS MANUAL IS WORK IN PROGRESS** **NOTE: THIS MANUAL IS WORK IN PROGRESS**
Overview ## Overview
--------
Mongoose is small and easy to use web server. It is self-contained, and does Mongoose is small and easy to use web server. It is self-contained, and does
not require any external software to run. not require any external software to run.
@@ -45,27 +44,168 @@ Mongoose can also be used to modify `.htpasswd` passwords file:
mongoose -A <htpasswd_file> <realm> <user> <passwd> mongoose -A <htpasswd_file> <realm> <user> <passwd>
Usage Examples ## Usage Examples
--------------
- How to share a Windows folder: copy mongoose executable to a folder and - How to share a Windows folder: copy mongoose executable to a folder and
double-click the executable. The folder should be accessible via double-click the executable. The folder should be accessible via
[http://localhost:8080](http://localhost:8080) in any browser. [http://localhost:8080](http://localhost:8080) in any browser.
- How to start mongoose at UNIX startup time in daemon mode, serving - How to start mongoose at UNIX startup time in daemon mode, serving
directory `/var/www`: put this line in the system startup script, directory `/var/www`: put this line in the system startup script:
`/path/to/mongoose -listening_ports 80 -document_root /var/www &` `/path/to/mongoose -listening_ports 80 -document_root /var/www &`
Command Line Options - How to serve user home directories using URL rewrite:
-------------------- `mongoose -url_rewrite_patterns /~joe/=/home/joe/,/~bill=/home/bill/`
Common Problems ## Configuration Options
```
-C cgi_pattern
All files that fully match cgi_pattern are treated as CGI.
Default pattern allows CGI files be anywhere. To restrict CGIs to
certain directory, use e.g. "-C /cgi-bin/**.cgi". Default:
"**.cgi$|**.pl$|**.php$"
-E cgi_environment
Extra environment variables to be passed to the CGI script in
addition to standard ones. The list must be comma-separated list
of X=Y pairs, like this: "VARIABLE1=VALUE1,VARIABLE2=VALUE2".
Default: ""
-G put_delete_passwords_file
PUT and DELETE passwords file. This must be specified if PUT or
DELETE methods are used. Default: ""
-I cgi_interpreter
Use cgi_interpreter as a CGI interpreter for all CGI scripts
regardless script extension. Mongoose decides which interpreter
to use by looking at the first line of a CGI script. Default:
"".
-P protect_uri
Comma separated list of URI=PATH pairs, specifying that given
URIs must be protected with respected password files. Default: ""
-R authentication_domain
Authorization realm. Default: "mydomain.com"
-S ssi_pattern
All files that fully match ssi_pattern are treated as SSI.
Unknown SSI directives are silently ignored. Currently, two SSI
directives are supported, "include" and "exec". Default:
"**.shtml$|**.shtm$"
-T throttle
Limit download speed for clients. throttle is a comma-separated
list of key=value pairs, where key could be a '*' character
(limit for all connections), a subnet in form x.x.x.x/mask (limit
for a given subnet, for example 10.0.0.0/8), or an URI prefix
pattern (limit for the set of URIs, for example /foo/**). The
value is a floating-point number of bytes per second, optionally
followed by a `k' or `m' character, meaning kilobytes and
megabytes respectively. A limit of 0 means unlimited rate. The
last matching rule wins. For example, "*=1k,10.0.0.0/8" means
limit everybody to 1 kilobyte per second, but give people from
10/8 subnet unlimited speed. Default: ""
-a access_log_file
Access log file. Default: "", no logging is done.
-d enable_directory_listing
Enable/disable directory listing. Default: "yes"
-e error_log_file
Error log file. Default: "", no errors are logged.
-g global_passwords_file
Location of a global passwords file. If set, per-directory
.htpasswd files are ignored, and all requests must be authorised
against that file. Default: ""
-i index_files
Comma-separated list of files to be treated as directory index
files. Default: "index.html,index.htm,index.cgi"
-l access_control_list
Specify access control list (ACL). ACL is a comma separated list
of IP subnets, each subnet is prepended by '-' or '+' sign. Plus
means allow, minus means deny. If subnet mask is omitted, like
"-1.2.3.4", then it means single IP address. Mask may vary from 0
to 32 inclusive. On each request, full list is traversed, and
last match wins. Default setting is to allow all. For example, to
allow only 192.168/16 subnet to connect, run "mongoose
-0.0.0.0/0,+192.168/16". Default: ""
-m extra_mime_types
Extra mime types to recognize, in form "extension1=type1,exten-
sion2=type2,...". Extension must include dot. Example: "mongoose
-m .cpp=plain/text,.java=plain/text". Default: ""
-p listening_ports
Comma-separated list of ports to listen on. If the port is SSL, a
letter 's' must be appeneded, for example, "-p 80,443s" will open
port 80 and port 443, and connections on port 443 will be SSL-ed.
It is possible to specify an IP address to bind to. In this case,
an IP address and a colon must be prepended to the port number.
For example, to bind to a loopback interface on port 80 and to
all interfaces on HTTPS port 443, use "mongoose -p
127.0.0.1:80,443s". Default: "8080"
-r document_root
Location of the WWW root directory. Default: "."
-s ssl_certificate
Location of SSL certificate file. Default: ""
-t num_threads
Number of worker threads to start. Default: "10"
-u run_as_user
Switch to given user's credentials after startup. Default: ""
-w url_rewrite_patterns
Comma-separated list of URL rewrites in the form of "pattern=sub-
stitution,..." If the "pattern" matches some prefix of the
requested URL, then matched prefix gets substituted with "substi-
tution". For example, "-w /con-
fig=/etc,**.doc|**.rtf=/path/to/cgi-bin/handle_doc.cgi" will
serve all URLs that start with "/config" from the "/etc" direc-
tory, and call handle_doc.cgi script for .doc and .rtf file
requests. If some pattern matches, no further matching/substitu-
tion is performed (first matching pattern wins). Use full paths
in substitutions. Default: ""
-x hide_files_patterns
A prefix pattern for the files to hide. Files that match the pat-
tern will not show up in directory listing and return 404 Not
Found if requested. Default: ""
```
## Common Problems
--------------- ---------------
- PHP doesn't work - getting empty page, or 'File not found' error. The
reason for that is wrong paths to the interpreter. Remember that with PHP,
correct interpreter is `php-cgi.exe` (`php-cgi` on UNIX). Solution: specify
full path to the PHP interpreter, e.g.
`mongoose -cgi_interpreter /full/path/to/php-cgi`
Embedding ## Embedding
--------- Embedding Mongoose is easy. Somewhere in the application code, `mg_start()`
function must be called. That starts the web server in a separate thread.
When it is not needed anymore, `mg_stop()` must be called. Application code
can pass configuration options to `mg_start()`, and also specify callback
functions that Mongoose should call at certain events.
[hello.c](http://a.c) provides a minimalistic example.
Other Resources Common pattern is to implement `begin_request` callback, and serve static files
--------------- from memory, and/or construct dynamic replies on the fly. Here is
my [embed.c](https://gist.github.com/valenok/4714740) gist
that shows how to easily any data can be embedded
directly into the executable. If such data needs to be encrypted, then
encrypted database or encryption dongles would be a better choice.
## Other Resources
- Presentation made by Arnout Vandecappelle at FOSDEM 2011 on 2011-02-06 - Presentation made by Arnout Vandecappelle at FOSDEM 2011 on 2011-02-06
in Brussels, Belgium, called in Brussels, Belgium, called
"Creating secure web based user interfaces for Embedded Devices" "Creating secure web based user interfaces for Embedded Devices"
+14 -12
View File
@@ -2,17 +2,19 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>CFBundleExecutable</key> <key>CFBundleExecutable</key> <string>Mongoose</string>
<string>mongoose</string> <key>CFBundlePackageType</key> <string>APPL</string>
<key>CFBundlePackageType</key> <key>CFBundleTypeRole</key> <string>None</string>
<string>APPL</string> <key>CFBundleIconFiles</key> <array>
<key>CFBundleTypeRole</key> <string>mongoose_16x16.png</string>
<string>None</string> <string>mongoose_22x22.png</string>
<key>CFBundleIconFiles</key> <string>mongoose_32x32.png</string>
<array> <string>mongoose_64x64.png</string>
<string>mongoose_16x16.png</string> </array>
</array> <key>LSUIElement</key> <true/>
<key>LSUIElement</key> <key>RunAtLoad</key> <true/>
<true/> <key>Label</key> <string>com.kolkin.mongoose</string>
<key>ProgramArguments</key> <array> </array>
<key>KeepAlive</key> <true/>
</dict> </dict>
</plist> </plist>
+1885
View File
File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -1 +1 @@
200 ICON DISCARDABLE "systray.ico" 100 ICON DISCARDABLE "systray.ico"
+137414
View File
File diff suppressed because it is too large. Load diff
+7160
View File
File diff suppressed because it is too large. Load diff
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 17 KiB

+1 -1
View File
@@ -25,4 +25,4 @@ windows:
$(CL) /DUSE_WEBSOCKET websocket.c ../mongoose.c $(CLFLAGS) $(CL) /DUSE_WEBSOCKET websocket.c ../mongoose.c $(CLFLAGS)
clean: clean:
rm -rf hello hello.exe upload upload.exe post post.exe websocket websocket.exe chat chat.exe *.dSYM *.obj rm -rf hello upload post websocket chat *.exe *.dSYM *.obj
+19 -25
View File
@@ -325,34 +325,25 @@ static void redirect_to_ssl(struct mg_connection *conn,
} }
} }
static void *event_handler(enum mg_event event, static int begin_request_handler(struct mg_connection *conn) {
struct mg_connection *conn) {
const struct mg_request_info *request_info = mg_get_request_info(conn); const struct mg_request_info *request_info = mg_get_request_info(conn);
void *processed = "yes"; int processed = 1;
if (event == MG_NEW_REQUEST) { if (!request_info->is_ssl) {
if (!request_info->is_ssl) { redirect_to_ssl(conn, request_info);
redirect_to_ssl(conn, request_info); } else if (!is_authorized(conn, request_info)) {
} else if (!is_authorized(conn, request_info)) { redirect_to_login(conn, request_info);
redirect_to_login(conn, request_info); } else if (strcmp(request_info->uri, authorize_url) == 0) {
} else if (strcmp(request_info->uri, authorize_url) == 0) { authorize(conn, request_info);
authorize(conn, request_info); } else if (strcmp(request_info->uri, "/ajax/get_messages") == 0) {
} else if (strcmp(request_info->uri, "/ajax/get_messages") == 0) { ajax_get_messages(conn, request_info);
ajax_get_messages(conn, request_info); } else if (strcmp(request_info->uri, "/ajax/send_message") == 0) {
} else if (strcmp(request_info->uri, "/ajax/send_message") == 0) { ajax_send_message(conn, request_info);
ajax_send_message(conn, request_info);
} else {
// No suitable handler found, mark as not processed. Mongoose will
// try to serve the request.
processed = NULL;
}
} else if (event == MG_EVENT_LOG) {
printf("%s\n", (const char *) mg_get_request_info(conn)->ev_data);
processed = NULL;
} else { } else {
processed = NULL; // No suitable handler found, mark as not processed. Mongoose will
// try to serve the request.
processed = 0;
} }
return processed; return processed;
} }
@@ -365,6 +356,7 @@ static const char *options[] = {
}; };
int main(void) { int main(void) {
struct mg_callbacks callbacks;
struct mg_context *ctx; struct mg_context *ctx;
// Initialize random number generator. It will be used later on for // Initialize random number generator. It will be used later on for
@@ -372,7 +364,9 @@ int main(void) {
srand((unsigned) time(0)); srand((unsigned) time(0));
// Setup and start Mongoose // Setup and start Mongoose
if ((ctx = mg_start(&event_handler, NULL, options)) == NULL) { memset(&callbacks, 0, sizeof(callbacks));
callbacks.begin_request = begin_request_handler;
if ((ctx = mg_start(&callbacks, NULL, options)) == NULL) {
printf("%s\n", "Cannot start chat server, fatal exit"); printf("%s\n", "Cannot start chat server, fatal exit");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
+35 -21
View File
@@ -2,35 +2,49 @@
#include <string.h> #include <string.h>
#include "mongoose.h" #include "mongoose.h"
static void *callback(enum mg_event event, // This function will be called by mongoose on every new request.
struct mg_connection *conn) { static int begin_request_handler(struct mg_connection *conn) {
const struct mg_request_info *request_info = mg_get_request_info(conn); const struct mg_request_info *request_info = mg_get_request_info(conn);
char content[100];
if (event == MG_NEW_REQUEST) { // Prepare the message we're going to send
char content[1024]; int content_length = snprintf(content, sizeof(content),
int content_length = snprintf(content, sizeof(content), "Hello from mongoose! Remote port: %d",
"Hello from mongoose! Remote port: %d", request_info->remote_port);
request_info->remote_port);
mg_printf(conn, // Send HTTP reply to the client
"HTTP/1.1 200 OK\r\n" mg_printf(conn,
"Content-Type: text/plain\r\n" "HTTP/1.1 200 OK\r\n"
"Content-Length: %d\r\n" // Always set Content-Length "Content-Type: text/plain\r\n"
"\r\n" "Content-Length: %d\r\n" // Always set Content-Length
"%s", "\r\n"
content_length, content); "%s",
// Mark as processed content_length, content);
return "";
} else { // Returning non-zero tells mongoose that our function has replied to
return NULL; // the client, and mongoose should not send client any more data.
} return 1;
} }
int main(void) { int main(void) {
struct mg_context *ctx; struct mg_context *ctx;
struct mg_callbacks callbacks;
// List of options. Last element must be NULL.
const char *options[] = {"listening_ports", "8080", NULL}; const char *options[] = {"listening_ports", "8080", NULL};
ctx = mg_start(&callback, NULL, options); // Prepare callbacks structure. We have only one callback, the rest are NULL.
getchar(); // Wait until user hits "enter" memset(&callbacks, 0, sizeof(callbacks));
callbacks.begin_request = begin_request_handler;
// Start the web server.
ctx = mg_start(&callbacks, NULL, options);
// Wait until user hits "enter". Server is running in separate thread.
// Navigating to http://localhost:8080 will invoke begin_request_handler().
getchar();
// Stop the server.
mg_stop(ctx); mg_stop(ctx);
return 0; return 0;
+27 -32
View File
@@ -10,50 +10,45 @@ static const char *html_form =
"<input type=\"submit\" />" "<input type=\"submit\" />"
"</form></body></html>"; "</form></body></html>";
static void *callback(enum mg_event event, static int begin_request_handler(struct mg_connection *conn) {
struct mg_connection *conn) {
const struct mg_request_info *ri = mg_get_request_info(conn); const struct mg_request_info *ri = mg_get_request_info(conn);
char post_data[1024], input1[sizeof(post_data)], input2[sizeof(post_data)];
int post_data_len;
if (event == MG_NEW_REQUEST) { if (!strcmp(ri->uri, "/handle_post_request")) {
if (!strcmp(ri->uri, "/handle_post_request")) { // User has submitted a form, show submitted data and a variable value
// User has submitted a form, show submitted data and a variable value post_data_len = mg_read(conn, post_data, sizeof(post_data));
char post_data[1024],
input1[sizeof(post_data)], input2[sizeof(post_data)];
int post_data_len;
// Read POST data // Parse form data. input1 and input2 are guaranteed to be NUL-terminated
post_data_len = mg_read(conn, post_data, sizeof(post_data)); mg_get_var(post_data, post_data_len, "input_1", input1, sizeof(input1));
mg_get_var(post_data, post_data_len, "input_2", input2, sizeof(input2));
// Parse form data. input1 and input2 are guaranteed to be NUL-terminated // Send reply to the client, showing submitted form values.
mg_get_var(post_data, post_data_len, "input_1", input1, sizeof(input1)); mg_printf(conn, "HTTP/1.0 200 OK\r\n"
mg_get_var(post_data, post_data_len, "input_2", input2, sizeof(input2)); "Content-Type: text/plain\r\n\r\n"
"Submitted data: [%.*s]\n"
mg_printf(conn, "HTTP/1.0 200 OK\r\n" "Submitted data length: %d bytes\n"
"Content-Type: text/plain\r\n\r\n" "input_1: [%s]\n"
"Submitted data: [%.*s]\n" "input_2: [%s]\n",
"Submitted data length: %d bytes\n" post_data_len, post_data, post_data_len, input1, input2);
"input_1: [%s]\n"
"input_2: [%s]\n",
post_data_len, post_data, post_data_len, input1, input2);
} else {
// Show HTML form.
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
"Content-Length: %d\r\n"
"Content-Type: text/html\r\n\r\n%s",
(int) strlen(html_form), html_form);
}
// Mark as processed
return "";
} else { } else {
return NULL; // Show HTML form.
mg_printf(conn, "HTTP/1.0 200 OK\r\n"
"Content-Length: %d\r\n"
"Content-Type: text/html\r\n\r\n%s",
(int) strlen(html_form), html_form);
} }
return 1; // Mark request as processed
} }
int main(void) { int main(void) {
struct mg_context *ctx; struct mg_context *ctx;
const char *options[] = {"listening_ports", "8080", NULL}; const char *options[] = {"listening_ports", "8080", NULL};
struct mg_callbacks callbacks;
ctx = mg_start(&callback, NULL, options); memset(&callbacks, 0, sizeof(callbacks));
callbacks.begin_request = begin_request_handler;
ctx = mg_start(&callbacks, NULL, options);
getchar(); // Wait until user hits "enter" getchar(); // Wait until user hits "enter"
mg_stop(ctx); mg_stop(ctx);
+28 -26
View File
@@ -17,42 +17,44 @@ typedef __int64 int64_t;
#include "mongoose.h" #include "mongoose.h"
static void *callback(enum mg_event event, struct mg_connection *conn) { static int begin_request_handler(struct mg_connection *conn) {
if (event == MG_NEW_REQUEST) { if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) {
if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) { mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n");
mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n"); mg_upload(conn, "/tmp");
mg_upload(conn, "/tmp"); } else {
} else { // Show HTML form. Make sure it has enctype="multipart/form-data" attr.
// Show HTML form. Make sure it has enctype="multipart/form-data" attr. static const char *html_form =
static const char *html_form = "<html><body>Upload example."
"<html><body>Upload example." "<form method=\"POST\" action=\"/handle_post_request\" "
"<form method=\"POST\" action=\"/handle_post_request\" " " enctype=\"multipart/form-data\">"
" enctype=\"multipart/form-data\">" "<input type=\"file\" name=\"file\" /> <br/>"
"<input type=\"file\" name=\"file\" /> <br/>" "<input type=\"submit\" value=\"Upload\" />"
"<input type=\"submit\" value=\"Upload\" />" "</form></body></html>";
"</form></body></html>";
mg_printf(conn, "HTTP/1.0 200 OK\r\n" mg_printf(conn, "HTTP/1.0 200 OK\r\n"
"Content-Length: %d\r\n" "Content-Length: %d\r\n"
"Content-Type: text/html\r\n\r\n%s", "Content-Type: text/html\r\n\r\n%s",
(int) strlen(html_form), html_form); (int) strlen(html_form), html_form);
}
// Mark as processed
return "";
} else if (event == MG_UPLOAD) {
mg_printf(conn, "Saved [%s]", mg_get_request_info(conn)->ev_data);
} }
return NULL; // Mark request as processed
return 1;
}
static void upload_handler(struct mg_connection *conn, const char *path) {
mg_printf(conn, "Saved [%s]", path);
} }
int main(void) { int main(void) {
struct mg_context *ctx; struct mg_context *ctx;
const char *options[] = {"listening_ports", "8080", NULL}; const char *options[] = {"listening_ports", "8080", NULL};
struct mg_callbacks callbacks;
ctx = mg_start(&callback, NULL, options); memset(&callbacks, 0, sizeof(callbacks));
callbacks.begin_request = begin_request_handler;
callbacks.upload = upload_handler;
ctx = mg_start(&callbacks, NULL, options);
getchar(); // Wait until user hits "enter" getchar(); // Wait until user hits "enter"
pause();
mg_stop(ctx); mg_stop(ctx);
return 0; return 0;
+49 -48
View File
@@ -5,69 +5,70 @@
#include <string.h> #include <string.h>
#include "mongoose.h" #include "mongoose.h"
static void *callback(enum mg_event event, struct mg_connection *conn) { static void websocket_ready_handler(struct mg_connection *conn) {
if (event == MG_WEBSOCKET_READY) { unsigned char buf[40];
unsigned char buf[40]; buf[0] = 0x81;
buf[0] = 0x81; buf[1] = snprintf((char *) buf + 2, sizeof(buf) - 2, "%s", "server ready");
buf[1] = snprintf((char *) buf + 2, sizeof(buf) - 2, "%s", "server ready"); mg_write(conn, buf, 2 + buf[1]);
mg_write(conn, buf, 2 + buf[1]); }
return ""; // MG_WEBSOCKET_READY return value is ignored
} else if (event == MG_WEBSOCKET_MESSAGE) {
unsigned char buf[200], reply[200];
int n, i, mask_len, xor, msg_len, len;
// Read message from the client. static int websocket_data_handler(struct mg_connection *conn) {
// Accept only small (<126 bytes) messages. unsigned char buf[200], reply[200];
len = 0; int n, i, mask_len, xor, msg_len, len;
msg_len = mask_len = 0;
for (;;) { // Read message from the client.
if ((n = mg_read(conn, buf + len, sizeof(buf) - len)) <= 0) { // Accept only small (<126 bytes) messages.
return ""; // Read error, close websocket len = 0;
msg_len = mask_len = 0;
for (;;) {
if ((n = mg_read(conn, buf + len, sizeof(buf) - len)) <= 0) {
return 0; // Read error, close websocket
}
len += n;
if (len >= 2) {
msg_len = buf[1] & 127;
mask_len = (buf[1] & 128) ? 4 : 0;
if (msg_len > 125) {
return 0; // Message is too long, close websocket
} }
len += n; // If we've buffered the whole message, exit the loop
if (len >= 2) { if (len >= 2 + mask_len + msg_len) {
msg_len = buf[1] & 127; break;
mask_len = (buf[1] & 128) ? 4 : 0;
if (msg_len > 125) {
return ""; // Message is too long, close websocket
}
// If we've buffered the whole message, exit the loop
if (len >= 2 + mask_len + msg_len) {
break;
}
} }
} }
// Prepare frame
reply[0] = 0x81; // text, FIN set
reply[1] = msg_len;
// Copy message from request to reply, applying the mask if required.
for (i = 0; i < msg_len; i++) {
xor = mask_len == 0 ? 0 : buf[2 + (i % 4)];
reply[i + 2] = buf[i + 2 + mask_len] ^ xor;
}
// Echo the message back to the client
mg_write(conn, reply, 2 + msg_len);
// Return non-NULL means stoping websocket conversation.
// Close the conversation if client has sent us "exit" string.
return memcmp(reply + 2, "exit", 4) == 0 ? "" : NULL;
} else {
return NULL;
} }
// Prepare frame
reply[0] = 0x81; // text, FIN set
reply[1] = msg_len;
// Copy message from request to reply, applying the mask if required.
for (i = 0; i < msg_len; i++) {
xor = mask_len == 0 ? 0 : buf[2 + (i % 4)];
reply[i + 2] = buf[i + 2 + mask_len] ^ xor;
}
// Echo the message back to the client
mg_write(conn, reply, 2 + msg_len);
// Returnint zero means stoping websocket conversation.
// Close the conversation if client has sent us "exit" string.
return memcmp(reply + 2, "exit", 4);
} }
int main(void) { int main(void) {
struct mg_context *ctx; struct mg_context *ctx;
struct mg_callbacks callbacks;
const char *options[] = { const char *options[] = {
"listening_ports", "8080", "listening_ports", "8080",
"document_root", "websocket_html_root", "document_root", "websocket_html_root",
NULL NULL
}; };
ctx = mg_start(&callback, NULL, options); memset(&callbacks, 0, sizeof(callbacks));
callbacks.websocket_ready = websocket_ready_handler;
callbacks.websocket_data = websocket_data_handler;
ctx = mg_start(&callbacks, NULL, options);
getchar(); // Wait until user hits "enter" getchar(); // Wait until user hits "enter"
mg_stop(ctx); mg_stop(ctx);
+376 -67
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2004-2011 Sergey Lyubka // Copyright (c) 2004-2013 Sergey Lyubka
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -40,6 +40,7 @@
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#include <winsvc.h> #include <winsvc.h>
#include <shlobj.h>
#define PATH_MAX MAX_PATH #define PATH_MAX MAX_PATH
#define S_ISDIR(x) ((x) & _S_IFDIR) #define S_ISDIR(x) ((x) & _S_IFDIR)
#define DIRSEP '\\' #define DIRSEP '\\'
@@ -104,12 +105,56 @@ static void show_usage_and_exit(void) {
fprintf(stderr, " -%s %s (default: \"%s\")\n", fprintf(stderr, " -%s %s (default: \"%s\")\n",
names[i], names[i + 1], names[i + 2] == NULL ? "" : names[i + 2]); names[i], names[i + 1], names[i + 2] == NULL ? "" : names[i + 2]);
} }
fprintf(stderr, "\nSee http://code.google.com/p/mongoose/wiki/MongooseManual"
" for more details.\n");
fprintf(stderr, "Example:\n mongoose -s cert.pem -p 80,443s -d no\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
#if defined(_WIN32) || defined(USE_COCOA)
static const char *config_file_top_comment =
"# Mongoose web server configuration file.\n"
"# For detailed description of every option, visit\n"
"# https://github.com/valenok/mongoose/blob/master/UserManual.md\n"
"# Lines starting with '#' and empty lines are ignored.\n"
"# To make a change, remove leading '#', modify option's value,\n"
"# save this file and then restart Mongoose.\n\n";
static const char *get_url_to_first_open_port(const struct mg_context *ctx) {
static char url[100];
const char *open_ports = mg_get_option(ctx, "listening_ports");
int a, b, c, d, port, n;
if (sscanf(open_ports, "%d.%d.%d.%d:%d%n", &a, &b, &c, &d, &port, &n) == 5) {
snprintf(url, sizeof(url), "%s://%d.%d.%d.%d:%d",
open_ports[n] == 's' ? "https" : "http", a, b, c, d, port);
} else if (sscanf(open_ports, "%d%n", &port, &n) == 1) {
snprintf(url, sizeof(url), "%s://localhost:%d",
open_ports[n] == 's' ? "https" : "http", port);
} else {
snprintf(url, sizeof(url), "%s", "http://localhost:8080");
}
return url;
}
static void create_config_file(const char *path) {
const char **names, *value;
FILE *fp;
int i;
// Create config file if it is not present yet
if ((fp = fopen(path, "r")) != NULL) {
fclose(fp);
} else if ((fp = fopen(path, "a+")) != NULL) {
fprintf(fp, "%s", config_file_top_comment);
names = mg_get_valid_option_names();
for (i = 0; names[i] != NULL; i += 3) {
value = mg_get_option(ctx, names[i]);
fprintf(fp, "# %s %s\n", names[i + 1], *value ? value : "<value>");
}
fclose(fp);
}
}
#endif
static void verify_document_root(const char *root) { static void verify_document_root(const char *root) {
const char *p, *path; const char *p, *path;
char buf[PATH_MAX]; char buf[PATH_MAX];
@@ -188,29 +233,37 @@ static void process_command_line_arguments(char *argv[], char **options) {
// Loop over the lines in config file // Loop over the lines in config file
while (fgets(line, sizeof(line), fp) != NULL) { while (fgets(line, sizeof(line), fp) != NULL) {
line_no++; line_no++;
// Ignore empty lines and comments // Ignore empty lines and comments
for (i = 0; isspace(* (unsigned char *) &line[i]); ) i++; for (i = 0; isspace(* (unsigned char *) &line[i]); ) i++;
if (line[i] == '#' || line[i] == '\0') if (line[i] == '#' || line[i] == '\0') {
continue; continue;
}
if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) { if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) {
die("%s: line %d is invalid", config_file, (int) line_no); printf("%s: line %d is invalid, ignoring it:\n %s",
config_file, (int) line_no, line);
} else {
set_option(options, opt, val);
} }
set_option(options, opt, val);
} }
(void) fclose(fp); (void) fclose(fp);
} }
// Handle command line flags. They override config file and default settings. // If we're under MacOS and started by launchd, then the second
for (i = cmd_line_opts_start; argv[i] != NULL; i += 2) { // argument is process serial number, -psn_.....
if (argv[i][0] != '-' || argv[i + 1] == NULL) { // In this case, don't process arguments at all.
show_usage_and_exit(); if (argv[1] == NULL || memcmp(argv[1], "-psn_", 5) != 0) {
// Handle command line flags.
// They override config file and default settings.
for (i = cmd_line_opts_start; argv[i] != NULL; i += 2) {
if (argv[i][0] != '-' || argv[i + 1] == NULL) {
show_usage_and_exit();
}
set_option(options, &argv[i][1], argv[i + 1]);
} }
set_option(options, &argv[i][1], argv[i + 1]);
} }
} }
@@ -219,17 +272,14 @@ static void init_server_name(void) {
mg_version()); mg_version());
} }
static void *mongoose_callback(enum mg_event ev, struct mg_connection *conn) { static int log_message(const struct mg_connection *conn, const char *message) {
if (ev == MG_EVENT_LOG) { (void) conn;
printf("%s\n", (const char *) mg_get_request_info(conn)->ev_data); printf("%s\n", message);
} return 0;
// Returning NULL marks request as not handled, signalling mongoose to
// proceed with handling it.
return NULL;
} }
static void start_mongoose(int argc, char *argv[]) { static void start_mongoose(int argc, char *argv[]) {
struct mg_callbacks callbacks;
char *options[MAX_OPTIONS]; char *options[MAX_OPTIONS];
int i; int i;
@@ -255,7 +305,9 @@ static void start_mongoose(int argc, char *argv[]) {
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);
/* Start Mongoose */ /* Start Mongoose */
ctx = mg_start(&mongoose_callback, NULL, (const char **) options); memset(&callbacks, 0, sizeof(callbacks));
callbacks.log_message = &log_message;
ctx = mg_start(&callbacks, NULL, (const char **) options);
for (i = 0; options[i] != NULL; i++) { for (i = 0; options[i] != NULL; i++) {
free(options[i]); free(options[i]);
} }
@@ -266,9 +318,25 @@ static void start_mongoose(int argc, char *argv[]) {
} }
#ifdef _WIN32 #ifdef _WIN32
enum {
ID_ICON = 100, ID_QUIT, ID_SETTINGS, ID_SEPARATOR, ID_INSTALL_SERVICE,
ID_REMOVE_SERVICE, ID_STATIC, ID_GROUP, ID_SAVE, ID_RESET_DEFAULTS,
ID_STATUS, ID_CONNECT,
// All dynamically created text boxes for options have IDs starting from
// ID_CONTROLS, incremented by one.
ID_CONTROLS = 200,
// Text boxes for files have "..." buttons to open file browser. These
// buttons have IDs that are ID_FILE_BUTTONS_DELTA higher than associated
// text box ID.
ID_FILE_BUTTONS_DELTA = 1000
};
static HICON hIcon;
static SERVICE_STATUS ss; static SERVICE_STATUS ss;
static SERVICE_STATUS_HANDLE hStatus; static SERVICE_STATUS_HANDLE hStatus;
static const char *service_magic_argument = "--"; static const char *service_magic_argument = "--";
static NOTIFYICONDATA TrayIcon;
static void WINAPI ControlHandler(DWORD code) { static void WINAPI ControlHandler(DWORD code) {
if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) { if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) {
@@ -296,41 +364,6 @@ static void WINAPI ServiceMain(void) {
SetServiceStatus(hStatus, &ss); SetServiceStatus(hStatus, &ss);
} }
#define ID_TRAYICON 100
#define ID_QUIT 101
#define ID_EDIT_CONFIG 102
#define ID_SEPARATOR 103
#define ID_INSTALL_SERVICE 104
#define ID_REMOVE_SERVICE 105
#define ID_ICON 200
static NOTIFYICONDATA TrayIcon;
static void edit_config_file(void) {
const char **names, *value;
FILE *fp;
int i;
char cmd[200];
// Create config file if it is not present yet
if ((fp = fopen(config_file, "r")) != NULL) {
fclose(fp);
} else if ((fp = fopen(config_file, "a+")) != NULL) {
fprintf(fp,
"# Mongoose web server configuration file.\n"
"# Lines starting with '#' and empty lines are ignored.\n"
"# For detailed description of every option, visit\n"
"# http://code.google.com/p/mongoose/wiki/MongooseManual\n\n");
names = mg_get_valid_option_names();
for (i = 0; names[i] != NULL; i += 3) {
value = mg_get_option(ctx, names[i]);
fprintf(fp, "# %s %s\n", names[i + 1], *value ? value : "<value>");
}
fclose(fp);
}
snprintf(cmd, sizeof(cmd), "notepad.exe %s", config_file);
WinExec(cmd, SW_SHOW);
}
static void show_error(void) { static void show_error(void) {
char buf[256]; char buf[256];
@@ -341,6 +374,272 @@ static void show_error(void) {
MessageBox(NULL, buf, "Error", MB_OK); MessageBox(NULL, buf, "Error", MB_OK);
} }
static void *align(void *ptr, DWORD alig) {
ULONG ul = (ULONG) ptr;
ul += alig;
ul &= ~alig;
return ((void *) ul);
}
static int is_boolean_option(const char *option_name) {
return !strcmp(option_name, "enable_directory_listing") ||
!strcmp(option_name, "enable_keep_alive");
}
static int is_filename_option(const char *option_name) {
return !strcmp(option_name, "cgi_interpreter") ||
!strcmp(option_name, "global_auth_file") ||
!strcmp(option_name, "put_delete_auth_file") ||
!strcmp(option_name, "access_log_file") ||
!strcmp(option_name, "error_log_file") ||
!strcmp(option_name, "ssl_certificate");
}
static int is_directory_option(const char *option_name) {
return !strcmp(option_name, "document_root");
}
static int is_numeric_options(const char *option_name) {
return !strcmp(option_name, "num_threads");
}
static void save_config(HWND hDlg, FILE *fp) {
char value[2000];
const char **options, *name, *default_value;
int i, id;
fprintf(fp, "%s", config_file_top_comment);
options = mg_get_valid_option_names();
for (i = 0; options[i] != NULL; i += 3) {
name = options[i + 1];
id = ID_CONTROLS + i / 3;
if (is_boolean_option(name)) {
snprintf(value, sizeof(value), "%s",
IsDlgButtonChecked(hDlg, id) ? "yes" : "no");
} else {
GetDlgItemText(hDlg, id, value, sizeof(value));
}
default_value = options[i + 2] == NULL ? "" : options[i + 2];
// If value is the same as default, skip it
if (strcmp(value, default_value) != 0) {
fprintf(fp, "%s %s\n", name, value);
}
}
}
static BOOL CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP) {
FILE *fp;
int i;
const char *name, *value, **options = mg_get_valid_option_names();
switch (msg) {
case WM_CLOSE:
DestroyWindow(hDlg);
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_SAVE:
EnableWindow(GetDlgItem(hDlg, ID_SAVE), FALSE);
if ((fp = fopen(config_file, "w+")) != NULL) {
save_config(hDlg, fp);
fclose(fp);
mg_stop(ctx);
start_mongoose(__argc, __argv);
}
EnableWindow(GetDlgItem(hDlg, ID_SAVE), TRUE);
break;
case ID_RESET_DEFAULTS:
for (i = 0; options[i] != NULL; i += 3) {
name = options[i + 1];
value = options[i + 2] == NULL ? "" : options[i + 2];
if (is_boolean_option(name)) {
CheckDlgButton(hDlg, ID_CONTROLS + i / 3, !strcmp(value, "yes") ?
BST_CHECKED : BST_UNCHECKED);
} else {
SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i / 3), value);
}
}
break;
}
for (i = 0; options[i] != NULL; i += 3) {
name = options[i + 1];
if ((is_filename_option(name) || is_directory_option(name)) &&
LOWORD(wParam) == ID_CONTROLS + i / 3 + ID_FILE_BUTTONS_DELTA) {
OPENFILENAME of;
BROWSEINFO bi;
char path[PATH_MAX] = "";
memset(&of, 0, sizeof(of));
of.lStructSize = sizeof(of);
of.hwndOwner = (HWND) hDlg;
of.lpstrFile = path;
of.nMaxFile = sizeof(path);
of.lpstrInitialDir = mg_get_option(ctx, "document_root");
of.Flags = OFN_CREATEPROMPT | OFN_NOCHANGEDIR;
memset(&bi, 0, sizeof(bi));
bi.hwndOwner = (HWND) hDlg;
bi.lpszTitle = "Choose WWW root directory:";
bi.ulFlags = BIF_RETURNONLYFSDIRS;
if (is_directory_option(name)) {
SHGetPathFromIDList(SHBrowseForFolder(&bi), path);
} else {
GetOpenFileName(&of);
}
if (path[0] != '\0') {
SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i / 3), path);
}
}
}
break;
case WM_INITDIALOG:
SendMessage(hDlg, WM_SETICON,(WPARAM) ICON_SMALL, (LPARAM) hIcon);
SendMessage(hDlg, WM_SETICON,(WPARAM) ICON_BIG, (LPARAM) hIcon);
SetWindowText(hDlg, "Mongoose settings");
SetFocus(GetDlgItem(hDlg, ID_SAVE));
for (i = 0; options[i] != NULL; i += 3) {
name = options[i + 1];
value = mg_get_option(ctx, name);
if (is_boolean_option(name)) {
CheckDlgButton(hDlg, ID_CONTROLS + i / 3, !strcmp(value, "yes") ?
BST_CHECKED : BST_UNCHECKED);
} else {
SetDlgItemText(hDlg, ID_CONTROLS + i / 3, value == NULL ? "" : value);
}
}
break;
default:
break;
}
return FALSE;
}
static void add_control(unsigned char **mem, DLGTEMPLATE *dia, WORD type,
DWORD id, DWORD style, WORD x, WORD y,
WORD cx, WORD cy, const char *caption) {
DLGITEMTEMPLATE *tp;
LPWORD p;
dia->cdit++;
*mem = align(*mem, 3);
tp = (DLGITEMTEMPLATE *) *mem;
tp->id = (WORD)id;
tp->style = style;
tp->dwExtendedStyle = 0;
tp->x = x;
tp->y = y;
tp->cx = cx;
tp->cy = cy;
p = align(*mem + sizeof(*tp), 1);
*p++ = 0xffff;
*p++ = type;
while (*caption != '\0') {
*p++ = (WCHAR) *caption++;
}
*p++ = 0;
p = align(p, 1);
*p++ = 0;
*mem = (unsigned char *) p;
}
static void show_settings_dialog() {
#define HEIGHT 15
#define WIDTH 400
#define LABEL_WIDTH 80
unsigned char mem[4096], *p;
const char **option_names, *long_option_name;
DWORD style;
DLGTEMPLATE *dia = (DLGTEMPLATE *) mem;
WORD i, cl, x, y, width, nelems = 0;
static int guard;
static struct {
DLGTEMPLATE template; // 18 bytes
WORD menu, class;
wchar_t caption[1];
WORD fontsiz;
wchar_t fontface[7];
} dialog_header = {{WS_CAPTION | WS_POPUP | WS_SYSMENU | WS_VISIBLE |
DS_SETFONT | WS_DLGFRAME, WS_EX_TOOLWINDOW, 0, 200, 200, WIDTH, 0},
0, 0, L"", 8, L"Tahoma"};
if (guard == 0) {
guard++;
} else {
return;
}
(void) memset(mem, 0, sizeof(mem));
(void) memcpy(mem, &dialog_header, sizeof(dialog_header));
p = mem + sizeof(dialog_header);
option_names = mg_get_valid_option_names();
for (i = 0; option_names[i] != NULL; i += 3) {
long_option_name = option_names[i + 1];
style = WS_CHILD | WS_VISIBLE | WS_TABSTOP;
x = 10 + (WIDTH / 2) * (nelems % 2);
y = (nelems/2 + 1) * HEIGHT + 5;
width = WIDTH / 2 - 20 - LABEL_WIDTH;
if (is_numeric_options(long_option_name)) {
style |= ES_NUMBER;
cl = 0x81;
style |= WS_BORDER | ES_AUTOHSCROLL;
} else if (is_boolean_option(long_option_name)) {
cl = 0x80;
style |= BS_AUTOCHECKBOX;
} else if (is_filename_option(long_option_name) ||
is_directory_option(long_option_name)) {
style |= WS_BORDER | ES_AUTOHSCROLL;
width -= 20;
cl = 0x81;
add_control(&p, dia, 0x80,
ID_CONTROLS + (i / 3) + ID_FILE_BUTTONS_DELTA,
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
(WORD) (x + width + LABEL_WIDTH + 5),
y, 15, 12, "...");
} else {
cl = 0x81;
style |= WS_BORDER | ES_AUTOHSCROLL;
}
add_control(&p, dia, 0x82, ID_STATIC, WS_VISIBLE | WS_CHILD,
x, y, LABEL_WIDTH, HEIGHT, long_option_name);
add_control(&p, dia, cl, ID_CONTROLS + (i / 3), style,
(WORD) (x + LABEL_WIDTH), y, width, 12, "");
nelems++;
}
y = (WORD) (((nelems + 1) / 2 + 1) * HEIGHT + 5);
add_control(&p, dia, 0x80, ID_GROUP, WS_CHILD | WS_VISIBLE |
BS_GROUPBOX, 5, 5, WIDTH - 10, y, " Settings ");
y += 10;
add_control(&p, dia, 0x80, ID_SAVE,
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
WIDTH - 70, y, 65, 12, "Save Settings");
add_control(&p, dia, 0x80, ID_RESET_DEFAULTS,
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
WIDTH - 140, y, 65, 12, "Reset to defaults");
add_control(&p, dia, 0x82, ID_STATIC,
WS_CHILD | WS_VISIBLE | WS_DISABLED,
5, y, 180, 12, server_name);
dia->cy = ((nelems + 1) / 2 + 1) * HEIGHT + 30;
DialogBoxIndirectParam(NULL, dia, NULL, DlgProc, (LPARAM) NULL);
guard--;
}
static int manage_service(int action) { static int manage_service(int action) {
static const char *service_name = "Mongoose"; static const char *service_name = "Mongoose";
SC_HANDLE hSCM = NULL, hService = NULL; SC_HANDLE hSCM = NULL, hService = NULL;
@@ -409,14 +708,19 @@ static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
mg_stop(ctx); mg_stop(ctx);
Shell_NotifyIcon(NIM_DELETE, &TrayIcon); Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
PostQuitMessage(0); PostQuitMessage(0);
break; return 0;
case ID_EDIT_CONFIG: case ID_SETTINGS:
edit_config_file(); show_settings_dialog();
break; break;
case ID_INSTALL_SERVICE: case ID_INSTALL_SERVICE:
case ID_REMOVE_SERVICE: case ID_REMOVE_SERVICE:
manage_service(LOWORD(wParam)); manage_service(LOWORD(wParam));
break; break;
case ID_CONNECT:
printf("[%s]\n", get_url_to_first_open_port(ctx));
ShellExecute(NULL, "open", get_url_to_first_open_port(ctx),
NULL, NULL, SW_SHOW);
break;
} }
break; break;
case WM_USER: case WM_USER:
@@ -436,7 +740,9 @@ static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
AppendMenu(hMenu, MF_STRING | (!service_installed ? MF_GRAYED : 0), AppendMenu(hMenu, MF_STRING | (!service_installed ? MF_GRAYED : 0),
ID_REMOVE_SERVICE, "Deinstall service"); ID_REMOVE_SERVICE, "Deinstall service");
AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, ""); AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
AppendMenu(hMenu, MF_STRING, ID_EDIT_CONFIG, "Edit config file"); AppendMenu(hMenu, MF_STRING, ID_CONNECT, "Start browser");
AppendMenu(hMenu, MF_STRING, ID_SETTINGS, "Edit Settings");
AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
AppendMenu(hMenu, MF_STRING, ID_QUIT, "Exit"); AppendMenu(hMenu, MF_STRING, ID_QUIT, "Exit");
GetCursorPos(&pt); GetCursorPos(&pt);
SetForegroundWindow(hWnd); SetForegroundWindow(hWnd);
@@ -473,10 +779,11 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show) {
ShowWindow(hWnd, SW_HIDE); ShowWindow(hWnd, SW_HIDE);
TrayIcon.cbSize = sizeof(TrayIcon); TrayIcon.cbSize = sizeof(TrayIcon);
TrayIcon.uID = ID_TRAYICON; TrayIcon.uID = ID_ICON;
TrayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; TrayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
TrayIcon.hIcon = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ICON), TrayIcon.hIcon = hIcon = LoadImage(GetModuleHandle(NULL),
IMAGE_ICON, 16, 16, 0); MAKEINTRESOURCE(ID_ICON),
IMAGE_ICON, 16, 16, 0);
TrayIcon.hWnd = hWnd; TrayIcon.hWnd = hWnd;
snprintf(TrayIcon.szTip, sizeof(TrayIcon.szTip), "%s", server_name); snprintf(TrayIcon.szTip, sizeof(TrayIcon.szTip), "%s", server_name);
TrayIcon.uCallbackMessage = WM_USER; TrayIcon.uCallbackMessage = WM_USER;
@@ -502,11 +809,13 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show) {
- (void) openBrowser { - (void) openBrowser {
[[NSWorkspace sharedWorkspace] [[NSWorkspace sharedWorkspace]
openURL:[NSURL URLWithString: openURL:[NSURL URLWithString:
[NSString stringWithUTF8String:"http://www.yahoo.com"]]]; [NSString stringWithUTF8String:get_url_to_first_open_port(ctx)]]];
} }
- (void) editConfig { - (void) editConfig {
create_config_file(config_file);
[[NSWorkspace sharedWorkspace] [[NSWorkspace sharedWorkspace]
openFile:@"mongoose.conf" withApplication:@"TextEdit"]; openFile:[NSString stringWithUTF8String:config_file]
withApplication:@"TextEdit"];
} }
- (void)shutDown{ - (void)shutDown{
[NSApp terminate:nil]; [NSApp terminate:nil];
-184
View File
@@ -1,184 +0,0 @@
.\" Process this file with
.\" groff -man -Tascii mongoose.1
.\" $Id: mongoose.1,v 1.12 2008/11/29 15:32:42 drozd Exp $
.Dd Sep 23, 2012
.Dt mongoose 1
.Sh NAME
.Nm mongoose
.Nd lightweight web server
.Sh SYNOPSIS
.Nm
.Op Ar config_file
.Op Ar OPTIONS
.Nm
.Fl A Ar htpasswd_file domain_name user_name password
.Sh DESCRIPTION
.Nm
is small, fast and easy to use web server with CGI, SSL, MD5 authorization,
and basic SSI support.
.Pp
.Nm
does not detach from terminal, and uses current working directory
as the web root, unless
.Fl r
option is specified.
It is possible to specify multiple ports to listen on. For example, to make
mongoose listen on HTTP port 80 and HTTPS port 443, one should start it as:
.Nm
.Fl s Ar cert.pem Fl p Ar 80,443s
.Pp
Unlike other web servers,
.Nm
does not require CGI scripts be put in a special directory. CGI scripts can
be anywhere. CGI (and SSI) files are recognized by the file name pattern.
.Nm
uses shell-like glob patterns with the following syntax:
.Bl -tag -compact -width indent
.It **
Matches everything
.It *
Matches everything but slash character, '/'
.It ?
Matches any character
.It $
Matches the end of the string
.It |
Matches if pattern on the left side or the right side matches. Pattern on the
left side is matched first
.El
All other characters in the pattern match themselves.
.Pp
If no arguments are given,
.Nm
searches for a configuration file called "mongoose.conf" in the same directory
where mongoose binary is located. Alternatively, a file name could be
specified in the command line. Format of the configuration file is the same
as for the command line options except that each option must be specified
on a separate line, leading dashes for option names must be omitted.
Lines beginning with '#' and empty lines are ignored.
.Pp
.Sh OPTIONS
.Bl -tag -width indent
.It Fl A Ar htpasswd_file domain_name user_name password
Add/edit user's password in the passwords file. Deleting users can be done
with any text editor. Functionality is similar to Apache's
.Ic htdigest
utility.
.It Fl C Ar cgi_pattern
All files that fully match cgi_pattern are treated as CGI.
Default pattern allows CGI files be
anywhere. To restrict CGIs to certain directory, use e.g. "-C /cgi-bin/**.cgi".
Default: "**.cgi$|**.pl$|**.php$"
.It Fl E Ar cgi_environment
Extra environment variables to be passed to the CGI script in addition to
standard ones. The list must be comma-separated list of X=Y pairs, like this:
"VARIABLE1=VALUE1,VARIABLE2=VALUE2". Default: ""
.It Fl G Ar put_delete_passwords_file
PUT and DELETE passwords file. This must be specified if PUT or
DELETE methods are used. Default: ""
.It Fl I Ar cgi_interpreter
Use
.Ar cgi_interpreter
as a CGI interpreter for all CGI scripts regardless script extension.
Mongoose decides which interpreter to use by looking at
the first line of a CGI script. Default: "".
.It Fl P Ar protect_uri
Comma separated list of URI=PATH pairs, specifying that given URIs
must be protected with respected password files. Default: ""
.It Fl R Ar authentication_domain
Authorization realm. Default: "mydomain.com"
.It Fl S Ar ssi_pattern
All files that fully match ssi_pattern are treated as SSI.
Unknown SSI directives are silently ignored. Currently, two SSI directives
are supported, "include" and "exec". Default: "**.shtml$|**.shtm$"
.It Fl T Ar throttle
Limit download speed for clients.
.Ar throttle
is a comma-separated list of key=value pairs, where
key could be a '*' character (limit for all connections), a subnet in form
x.x.x.x/mask (limit for a given subnet, for example 10.0.0.0/8), or an
URI prefix pattern (limit for the set of URIs, for example /foo/**). The value
is a floating-point number of bytes per second, optionally followed by a
`k' or `m' character, meaning kilobytes and megabytes respectively. A limit
of 0 means unlimited rate. The last matching rule wins. For example,
"*=1k,10.0.0.0/8" means limit everybody to 1 kilobyte per second, but give
people from 10/8 subnet unlimited speed. Default: ""
.It Fl a Ar access_log_file
Access log file. Default: "", no logging is done.
.It Fl d Ar enable_directory_listing
Enable/disable directory listing. Default: "yes"
.It Fl e Ar error_log_file
Error log file. Default: "", no errors are logged.
.It Fl g Ar global_passwords_file
Location of a global passwords file. If set, per-directory .htpasswd files are
ignored, and all requests must be authorised against that file. Default: ""
.It Fl i Ar index_files
Comma-separated list of files to be treated as directory index files.
Default: "index.html,index.htm,index.cgi"
.It Fl l Ar access_control_list
Specify access control list (ACL). ACL is a comma separated list
of IP subnets, each subnet is prepended by '-' or '+' sign. Plus means allow,
minus means deny. If subnet mask is
omitted, like "-1.2.3.4", then it means single IP address. Mask may vary
from 0 to 32 inclusive. On each request, full list is traversed, and
last match wins. Default setting is to allow all. For example, to allow only
192.168/16 subnet to connect, run "mongoose -0.0.0.0/0,+192.168/16".
Default: ""
.It Fl m Ar extra_mime_types
Extra mime types to recognize, in form
"extension1=type1,extension2=type2,...". Extension must include dot.
Example: "mongoose -m .cpp=plain/text,.java=plain/text". Default: ""
.It Fl p Ar listening_ports
Comma-separated list of ports to listen on. If the port is SSL, a letter 's'
must be appeneded, for example, "-p 80,443s" will open port 80 and port 443,
and connections on port 443 will be SSL-ed. It is possible to specify an
IP address to bind to. In this case, an IP address and a colon must be
prepended to the port number. For example, to bind to a loopback interface
on port 80 and to all interfaces on HTTPS port 443, use
"mongoose -p 127.0.0.1:80,443s". Default: "8080"
.It Fl r Ar document_root
Location of the WWW root directory. Default: "."
.It Fl s Ar ssl_certificate
Location of SSL certificate file. Default: ""
.It Fl t Ar num_threads
Number of worker threads to start. Default: "10"
.It Fl u Ar run_as_user
Switch to given user's credentials after startup. Default: ""
.It Fl w Ar url_rewrite_patterns
Comma-separated list of URL rewrites in the form of
"pattern=substitution,..." If the "pattern" matches some prefix
of the requested URL, then matched prefix gets substituted with "substitution".
For example, "-w /config=/etc,**.doc|**.rtf=/path/to/cgi-bin/handle_doc.cgi"
will serve all URLs that start with "/config" from the "/etc" directory, and
call handle_doc.cgi script for .doc and .rtf file requests. If some pattern
matches, no further matching/substitution is performed
(first matching pattern wins). Use full paths in substitutions. Default: ""
.It Fl x Ar hide_files_patterns
A prefix pattern for the files to hide. Files that match the pattern will not
show up in directory listing and return 404 Not Found if requested. Default: ""
.El
.Pp
.Sh EMBEDDING
.Nm
was designed to be embeddable into C/C++ applications. Since the
source code is contained in single C file, it is fairly easy to embed it
and follow the updates. Please refer to http://code.google.com/p/mongoose
for details.
.Pp
.Sh EXAMPLES
.Bl -tag -width indent
.It Nm Fl r Ar /var/www Fl s Ar /etc/cert.pem Fl p Ar 8080,8043s
Start serving files from /var/www. Listen on port 8080 for HTTP, and 8043
for HTTPS connections. Use /etc/cert.pem as SSL certificate file.
.It Nm Fl l Ar -0.0.0.0/0,+10.0.0.0/8,+1.2.3.4
Deny connections from everywhere, allow only IP address 1.2.3.4 and
all IP addresses from 10.0.0.0/8 subnet to connect.
.It Nm Fl w Ar **=/usr/bin/script.cgi
Invoke /usr/bin/script.cgi for every incoming request, regardless of the URL.
.El
.Pp
.Sh COPYRIGHT
.Nm
is licensed under the terms of the MIT license.
.Sh AUTHOR
.An Sergey Lyubka Aq valenok@gmail.com .
+449 -456
View File
File diff suppressed because it is too large. Load diff
+45 -131
View File
@@ -42,121 +42,37 @@ struct mg_request_info {
long remote_ip; // Client's IP address long remote_ip; // Client's IP address
int remote_port; // Client's port int remote_port; // Client's port
int is_ssl; // 1 if SSL-ed, 0 if not int is_ssl; // 1 if SSL-ed, 0 if not
int num_headers; // Number of headers void *user_data; // User data pointer passed to mg_start()
int num_headers; // Number of HTTP headers
struct mg_header { struct mg_header {
const char *name; // HTTP header name const char *name; // HTTP header name
const char *value; // HTTP header value const char *value; // HTTP header value
} http_headers[64]; // Maximum 64 headers } http_headers[64]; // Maximum 64 headers
void *user_data; // User data pointer passed to the mg_start()
void *ev_data; // Event-specific data pointer
}; };
// Various events on which user-defined callback function is called by Mongoose. // This structure needs to be passed to mg_start(), to let mongoose know
enum mg_event { // which callbacks to invoke. For detailed description, see
// New HTTP request has arrived from the client. // https://github.com/valenok/mongoose/blob/master/UserManual.md
// If callback returns non-NULL, Mongoose stops handling current request. struct mg_callbacks {
// ev_data contains NULL. int (*begin_request)(struct mg_connection *);
MG_NEW_REQUEST, void (*end_request)(const struct mg_connection *, int reply_status_code);
int (*log_message)(const struct mg_connection *, const char *message);
// Mongoose has finished handling the request. int (*init_ssl)(void *ssl_context);
// Callback return value is ignored. int (*websocket_connect)(const struct mg_connection *);
// ev_data contains NULL. void (*websocket_ready)(struct mg_connection *);
MG_REQUEST_COMPLETE, int (*websocket_data)(struct mg_connection *);
const char * (*open_file)(const struct mg_connection *,
// HTTP error must be returned to the client. const char *path, size_t *data_len);
// If callback returns non-NULL, Mongoose stops handling error. void (*init_lua)(struct mg_connection *, void *lua_context);
// ev_data contains HTTP error code: void (*upload)(struct mg_connection *, const char *file_name);
// int http_reply_status_code = (long) request_info->ev_data;
MG_HTTP_ERROR,
// Mongoose logs a message.
// If callback returns non-NULL, Mongoose stops handling that event.
// ev_data contains a message to be logged:
// const char *log_message = request_info->ev_data;
MG_EVENT_LOG,
// SSL initialization, sent before certificate setup.
// If callback returns non-NULL, Mongoose does not set up certificates.
// ev_data contains server's OpenSSL context:
// SSL_CTX *ssl_context = request_info->ev_data;
MG_INIT_SSL,
// Sent on HTTP connect, before websocket handshake.
// If user callback returns NULL, then mongoose proceeds
// with handshake, otherwise it closes the connection.
// ev_data contains NULL.
MG_WEBSOCKET_CONNECT,
// Handshake has been successfully completed.
// Callback's return value is ignored.
// ev_data contains NULL.
MG_WEBSOCKET_READY,
// Incoming message from the client, data could be read with mg_read().
// If user callback returns non-NULL, mongoose closes the websocket.
// ev_data contains NULL.
MG_WEBSOCKET_MESSAGE,
// Client has closed the connection.
// Callback's return value is ignored.
// ev_data contains NULL.
MG_WEBSOCKET_CLOSE,
// Mongoose tries to open file.
// If callback returns non-NULL, Mongoose will not try to open it, but
// will use the returned value as a pointer to the file data. This allows
// for example to serve files from memory.
// ev_data contains file path, including document root path.
// Upon return, ev_data should return file size, which should be a long int.
//
// const char *file_name = request_info->ev_data;
// if (strcmp(file_name, "foo.txt") == 0) {
// request_info->ev_data = (void *) (long) 4;
// return "data";
// }
// return NULL;
//
// Note that this even is sent multiple times during one request. Each
// time mongoose tries to open or stat the file, this event is sent, e.g.
// for opening .htpasswd file, stat-ting requested file, opening requested
// file, etc.
MG_OPEN_FILE,
// Mongoose initializes Lua server page. Sent only if Lua support is enabled.
// Callback's return value is ignored.
// ev_data contains lua_State pointer.
MG_INIT_LUA,
// Mongoose has uploaded file to a temporary directory.
// Callback's return value is ignored.
// ev_data contains NUL-terminated file name.
MG_UPLOAD,
}; };
// Prototype for the user-defined function. Mongoose calls this function
// on every MG_* event.
//
// Parameters:
// event: which event has been triggered.
// conn: opaque connection handler. Could be used to read, write data to the
// client, etc. See functions below that have "mg_connection *" arg.
//
// Return:
// If handler returns non-NULL, that means that handler has processed the
// request by sending appropriate HTTP reply to the client. Mongoose treats
// the request as served.
// If handler returns NULL, that means that handler has not processed
// the request. Handler must not send any data to the client in this case.
// Mongoose proceeds with request handling as if nothing happened.
typedef void *(*mg_callback_t)(enum mg_event event, struct mg_connection *conn);
// Start web server. // Start web server.
// //
// Parameters: // Parameters:
// callback: user defined event handling function or NULL. // callbacks: mg_callbacks structure with user-defined callbacks.
// options: NULL terminated list of option_name, option_value pairs that // options: NULL terminated list of option_name, option_value pairs that
// specify Mongoose configuration parameters. // specify Mongoose configuration parameters.
// //
@@ -178,8 +94,9 @@ typedef void *(*mg_callback_t)(enum mg_event event, struct mg_connection *conn);
// //
// Return: // Return:
// web server context, or NULL on error. // web server context, or NULL on error.
struct mg_context *mg_start(mg_callback_t callback, void *user_data, struct mg_context *mg_start(const struct mg_callbacks *callbacks,
const char **options); void *user_data,
const char **configuration_options);
// Stop the web server. // Stop the web server.
@@ -235,12 +152,6 @@ struct mg_request_info *mg_get_request_info(struct mg_connection *);
int mg_write(struct mg_connection *, const void *buf, size_t len); int mg_write(struct mg_connection *, const void *buf, size_t len);
// Send data to the browser using printf() semantics.
//
// Works exactly like mg_write(), but allows to do message formatting.
// Below are the macros for enabling compiler-specific checks for
// printf-like arguments.
#undef PRINTF_FORMAT_STRING #undef PRINTF_FORMAT_STRING
#if _MSC_VER >= 1400 #if _MSC_VER >= 1400
#include <sal.h> #include <sal.h>
@@ -259,6 +170,11 @@ int mg_write(struct mg_connection *, const void *buf, size_t len);
#define PRINTF_ARGS(x, y) #define PRINTF_ARGS(x, y)
#endif #endif
// Send data to the browser using printf() semantics.
//
// Works exactly like mg_write(), but allows to do message formatting.
// Below are the macros for enabling compiler-specific checks for
// printf-like arguments.
int mg_printf(struct mg_connection *, int mg_printf(struct mg_connection *,
PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3); PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3);
@@ -315,32 +231,30 @@ int mg_get_cookie(const struct mg_connection *,
const char *cookie_name, char *buf, size_t buf_len); const char *cookie_name, char *buf, size_t buf_len);
// Connect to the remote web server. // Download data from the remote web server.
// host: host name to connect to, e.g. "foo.com", or "10.12.40.1".
// port: port number, e.g. 80.
// use_ssl: wether to use SSL connection.
// error_buffer, error_buffer_size: error message placeholder.
// request_fmt,...: HTTP request.
// Return: // Return:
// On success, valid pointer to the new connection // On success, valid pointer to the new connection, suitable for mg_read().
// On error, NULL // On error, NULL. error_buffer contains error message.
struct mg_connection *mg_connect(struct mg_context *ctx, // Example:
const char *host, int port, int use_ssl); // char ebuf[100];
// struct mg_connection *conn;
// conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf),
// "%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n");
struct mg_connection *mg_download(const char *host, int port, int use_ssl,
char *error_buffer, size_t error_buffer_size,
PRINTF_FORMAT_STRING(const char *request_fmt),
...) PRINTF_ARGS(6, 7);
// Close the connection opened by mg_connect(). // Close the connection opened by mg_download().
void mg_close_connection(struct mg_connection *conn); void mg_close_connection(struct mg_connection *conn);
// Download given URL to a given file.
// url: URL to download
// path: file name where to save the data
// request_info: pointer to a structure that will hold parsed reply headers
// buf, bul_len: a buffer for the reply headers
// Return:
// On error, NULL
// On success, opened file stream to the downloaded contents. The stream
// is positioned to the end of the file. It is the user's responsibility
// to fclose() the opened file stream.
FILE *mg_fetch(struct mg_context *ctx, const char *url, const char *path,
char *buf, size_t buf_len, struct mg_request_info *request_info);
// File upload functionality. Each uploaded file gets saved into a temporary // File upload functionality. Each uploaded file gets saved into a temporary
// file and MG_UPLOAD event is sent. // file and MG_UPLOAD event is sent.
// Return number of uploaded files. // Return number of uploaded files.
+45 -33
View File
@@ -1,40 +1,52 @@
<html> <?
<p>Prime numbers from 0 to 100, calculated by Lua:</p> -- Lua server pages have full control over the output, including HTTP
<? -- headers they send to the client. Send HTTP headers:
print('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n')
?><html><body>
function is_prime(n) <p>This is an example Lua server page served by
if n <= 0 then return false end <a href="http://code.google.com/p/mongoose">Mongoose web server</a>.
if n <= 2 then return true end Mongoose has Lua, Sqlite, and other functionality built in the binary.
if (n % 2 == 0) then return false end This example page stores the request in the Sqlite database, and shows
for i = 3, n / 2, 2 do all requests done previously.</p>
if (n % i == 0) then return false end
end
return true
end
for i = 1, 100 do <pre>
if is_prime(i) then print('<span>' .. i .. '</span>&nbsp;') end <?
end -- Open database
local db = sqlite3.open('requests.db')
?> -- Setup a trace callback, to show SQL statements we'll be executing.
-- db:trace(function(data, sql) print('Executing: ' .. sql .. '\n') end, nil)
<p>Reading POST data from Lua (click submit):</p> -- Create a table if it is not created already
<form method="POST"><input type="text" name="t1"/><input type="submit"></form> db:exec([[
CREATE TABLE IF NOT EXISTS requests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp NOT NULL,
method NOT NULL,
uri NOT NULL,
user_agent
);
]])
<pre> -- Add entry about this request
POST data: [<? post_data = read() print(post_data) ?>] local stmt = db:prepare(
request method: [<? print(request_info.request_method) ?>] 'INSERT INTO requests VALUES(NULL, datetime("now"), ?, ?, ?);');
IP/port: [<? print(request_info.remote_ip, ':', request_info.remote_port) ?>] stmt:bind_values(request_info.request_method, request_info.uri,
URI: [<? print(request_info.uri) ?>] request_info.http_headers['User-Agent'])
HTTP version [<? print(request_info.http_version) ?>] stmt:step()
HEADERS: stmt:finalize()
<?
for name, value in pairs(request_info.http_headers) do
print(name, ':', value, '\n')
end
?>
</pre> -- Show all previous records
print('Previous requests:\n')
stmt = db:prepare('SELECT * FROM requests ORDER BY id DESC;')
while stmt:step() == sqlite3.ROW do
local v = stmt:get_values()
print(v[1] .. ' ' .. v[2] .. ' ' .. v[3] .. ' '
.. v[4] .. ' ' .. v[5] .. '\n')
end
-- Close database
</html> db:close()
?>
</pre></body></html>
+11 -88
View File
@@ -146,11 +146,6 @@ if ($^O =~ /darwin|bsd|linux/) {
} }
} }
if (scalar(@ARGV) > 0 and $ARGV[0] eq 'embedded') {
do_embedded_test();
exit 0;
}
if (scalar(@ARGV) > 0 and $ARGV[0] eq 'unit') { if (scalar(@ARGV) > 0 and $ARGV[0] eq 'unit') {
do_unit_test(); do_unit_test();
exit 0; exit 0;
@@ -158,20 +153,21 @@ if (scalar(@ARGV) > 0 and $ARGV[0] eq 'unit') {
# Make sure we load config file if no options are given. # Make sure we load config file if no options are given.
# Command line options override config files settings # Command line options override config files settings
write_file($config, "access_log_file access.log\nlistening_ports 12345\n"); write_file($config, "access_log_file access.log\n" .
spawn("$exe -p $port"); "listening_ports 127.0.0.1:12345\n");
spawn("$exe -p 127.0.0.1:$port");
o("GET /test/hello.txt HTTP/1.0\n\n", 'HTTP/1.1 200 OK', 'Loading config file'); o("GET /test/hello.txt HTTP/1.0\n\n", 'HTTP/1.1 200 OK', 'Loading config file');
unlink $config; unlink $config;
kill_spawned_child(); kill_spawned_child();
# Spawn the server on port $port # Spawn the server on port $port
my $cmd = "$exe ". my $cmd = "$exe ".
"-listening_ports $port ". "-listening_ports 127.0.0.1:$port ".
"-access_log_file access.log ". "-access_log_file access.log ".
"-error_log_file debug.log ". "-error_log_file debug.log ".
"-cgi_environment CGI_FOO=foo,CGI_BAR=bar,CGI_BAZ=baz " . "-cgi_environment CGI_FOO=foo,CGI_BAR=bar,CGI_BAZ=baz " .
"-extra_mime_types .bar=foo/bar,.tar.gz=blah,.baz=foo " . "-extra_mime_types .bar=foo/bar,.tar.gz=blah,.baz=foo " .
'-put_delete_passwords_file test/passfile ' . '-put_delete_auth_file test/passfile ' .
'-access_control_list -0.0.0.0/0,+127.0.0.1 ' . '-access_control_list -0.0.0.0/0,+127.0.0.1 ' .
"-document_root $root ". "-document_root $root ".
"-hide_files_patterns **exploit.pl ". "-hide_files_patterns **exploit.pl ".
@@ -219,11 +215,10 @@ write_file("$root/a+.txt", '');
o("GET /a+.txt HTTP/1.0\n\n", 'HTTP/1.1 200 OK', 'URL-decoding, + in URI'); o("GET /a+.txt HTTP/1.0\n\n", 'HTTP/1.1 200 OK', 'URL-decoding, + in URI');
# Test HTTP version parsing # Test HTTP version parsing
o("GET / HTTPX/1.0\r\n\r\n", '400 Bad Request', 'Bad HTTP Version', 0); o("GET / HTTPX/1.0\r\n\r\n", '^HTTP/1.1 500', 'Bad HTTP Version', 0);
o("GET / HTTP/x.1\r\n\r\n", '505 HTTP', 'Bad HTTP maj Version'); o("GET / HTTP/x.1\r\n\r\n", '^HTTP/1.1 505', 'Bad HTTP maj Version', 0);
o("GET / HTTP/1.1z\r\n\r\n", '505 HTTP', 'Bad HTTP min Version'); o("GET / HTTP/1.1z\r\n\r\n", '^HTTP/1.1 505', 'Bad HTTP min Version', 0);
o("GET / HTTP/02.0\r\n\r\n", '505 HTTP version not supported', o("GET / HTTP/02.0\r\n\r\n", '^HTTP/1.1 505', 'HTTP Version >1.1', 0);
'HTTP Version >1.1');
# File with leading single dot # File with leading single dot
o("GET /.leading.dot.txt HTTP/1.0\n\n", 'abc123', 'Leading dot 1'); o("GET /.leading.dot.txt HTTP/1.0\n\n", 'abc123', 'Leading dot 1');
@@ -426,7 +421,6 @@ unless (scalar(@ARGV) > 0 and $ARGV[0] eq "basic_tests") {
do_PUT_test(); do_PUT_test();
kill_spawned_child(); kill_spawned_child();
do_unit_test(); do_unit_test();
do_embedded_test();
} }
sub do_PUT_test { sub do_PUT_test {
@@ -456,79 +450,8 @@ sub do_PUT_test {
} }
sub do_unit_test { sub do_unit_test {
my $cmd = "cc -g -W -Wall -o $unit_test_exe $root/unit_test.c -I. ". my $target = on_windows() ? 'w' : 'u';
"-pthread -DNO_SSL "; system("make $target") == 0 or fail("Unit test failed!");
if (on_windows()) {
$cmd = "cl $root/embed.c mongoose.c /I. /nologo /DNO_SSL ".
"/DLISTENING_PORT=\\\"$port\\\" /link /out:$embed_exe.exe ws2_32.lib ";
}
print $cmd, "\n";
system($cmd) == 0 or fail("Cannot compile unit test");
system($unit_test_exe) == 0 or fail("Unit test failed!");
}
sub do_embedded_test {
my $cmd = "cc -W -Wall -o $embed_exe $root/embed.c mongoose.c -I. ".
"-pthread -DNO_SSL -DLISTENING_PORT=\\\"$port\\\"";
if (on_windows()) {
$cmd = "cl $root/embed.c mongoose.c /I. /nologo /DNO_SSL ".
"/DLISTENING_PORT=\\\"$port\\\" /link /out:$embed_exe.exe ws2_32.lib ";
}
print $cmd, "\n";
system($cmd) == 0 or fail("Cannot compile embedded unit test");
spawn("./$embed_exe");
o("GET /test_get_header HTTP/1.0\nHost: blah\n\n",
'Value: \[blah\]', 'mg_get_header', 0);
o("GET /test_get_var?a=b&my_var=foo&c=d HTTP/1.0\n\n",
'Value: \[foo\]', 'mg_get_var 1', 0);
o("GET /test_get_var?my_var=foo&c=d HTTP/1.0\n\n",
'Value: \[foo\]', 'mg_get_var 2', 0);
o("GET /test_get_var?a=b&my_var=foo HTTP/1.0\n\n",
'Value: \[foo\]', 'mg_get_var 3', 0);
o("POST /test_get_var HTTP/1.0\nContent-Length: 10\n\n".
"my_var=foo", 'Value: \[foo\]', 'mg_get_var 4', 0);
o("POST /test_get_var HTTP/1.0\nContent-Length: 18\n\n".
"a=b&my_var=foo&c=d", 'Value: \[foo\]', 'mg_get_var 5', 0);
o("POST /test_get_var HTTP/1.0\nContent-Length: 14\n\n".
"a=b&my_var=foo", 'Value: \[foo\]', 'mg_get_var 6', 0);
o("GET /test_get_var?a=one%2btwo&my_var=foo& HTTP/1.0\n\n",
'Value: \[foo\]', 'mg_get_var 7', 0);
o("GET /test_get_var?my_var=one%2btwo&b=two%2b HTTP/1.0\n\n",
'Value: \[one\+two\]', 'mg_get_var 8', 0);
# + in form data MUST be decoded to space
o("POST /test_get_var HTTP/1.0\nContent-Length: 10\n\n".
"my_var=b+c", 'Value: \[b c\]', 'mg_get_var 9', 0);
# Test that big POSTed vars are not truncated
my $my_var = 'x' x 64000;
o("POST /test_get_var HTTP/1.0\nContent-Length: 64007\n\n".
"my_var=$my_var", 'Value size: \[64000\]', 'mg_get_var 10', 0);
# Other methods should also work
o("PUT /test_get_var HTTP/1.0\nContent-Length: 10\n\n".
"my_var=foo", 'Value: \[foo\]', 'mg_get_var 11', 0);
o("POST /test_get_request_info?xx=yy HTTP/1.0\nFoo: bar\n".
"Content-Length: 3\n\na=b",
'Method: \[POST\].URI: \[/test_get_request_info\].'.
'HTTP version: \[1.0\].HTTP header \[Foo\]: \[bar\].'.
'HTTP header \[Content-Length\]: \[3\].'.
'Query string: \[xx=yy\].'.
'Remote IP: \[\d+\].Remote port: \[\d+\].'.
'Remote user: \[\]'
, 'request_info', 0);
o("GET /not_exist HTTP/1.0\n\n", 'Error: \[404\]', '404 handler', 0);
o("bad request\n\n", 'Error: \[400\]', '* error handler', 0);
# o("GET /foo/secret HTTP/1.0\n\n",
# '401 Unauthorized', 'mg_protect_uri', 0);
# o("GET /foo/secret HTTP/1.0\nAuthorization: Digest username=bill\n\n",
# '401 Unauthorized', 'mg_protect_uri (bill)', 0);
# o("GET /foo/secret HTTP/1.0\nAuthorization: Digest username=joe\n\n",
# '200 OK', 'mg_protect_uri (joe)', 0);
kill_spawned_child();
} }
print "SUCCESS! All tests passed.\n"; print "SUCCESS! All tests passed.\n";
+316 -115
View File
@@ -1,4 +1,27 @@
// Copyright (c) 2004-2013 Sergey Lyubka
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Unit test for the mongoose web server. Tests embedded API.
#define USE_WEBSOCKET #define USE_WEBSOCKET
#define USE_LUA
#include "mongoose.c" #include "mongoose.c"
#define FATAL(str, line) do { \ #define FATAL(str, line) do { \
@@ -7,24 +30,43 @@
} while (0) } while (0)
#define ASSERT(expr) do { if (!(expr)) FATAL(#expr, __LINE__); } while (0) #define ASSERT(expr) do { if (!(expr)) FATAL(#expr, __LINE__); } while (0)
#define UNUSED_PORT "33796" #define HTTP_PORT "56789"
#define HTTPS_PORT "56790"
#define HTTP_PORT2 "56791"
#define LISTENING_ADDR \
"127.0.0.1:" HTTP_PORT "r" \
",127.0.0.1:" HTTPS_PORT "s" \
",127.0.0.1:" HTTP_PORT2
static void test_parse_http_request() { #ifndef _WIN32
#define __cdecl
#endif
static void test_parse_http_message() {
struct mg_request_info ri; struct mg_request_info ri;
char req1[] = "GET / HTTP/1.1\r\n\r\n"; char req1[] = "GET / HTTP/1.1\r\n\r\n";
char req2[] = "BLAH / HTTP/1.1\r\n\r\n"; char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
char req3[] = "GET / HTTP/1.1\r\nBah\r\n"; char req3[] = "GET / HTTP/1.1\r\nBah\r\n";
char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n"; char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n";
char req5[] = "GET / HTTP/1.1\r\n\r\n";
char req6[] = "G";
char req7[] = " blah ";
char req8[] = " HTTP/1.1 200 OK \n\n";
ASSERT(parse_http_request(req1, sizeof(req1), &ri) == sizeof(req1) - 1); ASSERT(parse_http_message(req1, sizeof(req1), &ri) == sizeof(req1) - 1);
ASSERT(strcmp(ri.http_version, "1.1") == 0); ASSERT(strcmp(ri.http_version, "1.1") == 0);
ASSERT(ri.num_headers == 0); ASSERT(ri.num_headers == 0);
ASSERT(parse_http_request(req2, sizeof(req2), &ri) == -1); ASSERT(parse_http_message(req2, sizeof(req2), &ri) == -1);
ASSERT(parse_http_request(req3, sizeof(req3), &ri) == -1); ASSERT(parse_http_message(req3, sizeof(req3), &ri) == 0);
ASSERT(parse_http_message(req6, sizeof(req6), &ri) == 0);
ASSERT(parse_http_message(req7, sizeof(req7), &ri) == 0);
ASSERT(parse_http_message("", 0, &ri) == 0);
ASSERT(parse_http_message(req8, sizeof(req8), &ri) == sizeof(req8) - 1);
// TODO(lsm): Fix this. Header value may span multiple lines. // TODO(lsm): Fix this. Header value may span multiple lines.
ASSERT(parse_http_request(req4, sizeof(req4), &ri) == sizeof(req4) - 1); ASSERT(parse_http_message(req4, sizeof(req4), &ri) == sizeof(req4) - 1);
ASSERT(strcmp(ri.http_version, "1.1") == 0);
ASSERT(ri.num_headers == 3); ASSERT(ri.num_headers == 3);
ASSERT(strcmp(ri.http_headers[0].name, "A") == 0); ASSERT(strcmp(ri.http_headers[0].name, "A") == 0);
ASSERT(strcmp(ri.http_headers[0].value, "foo bar") == 0); ASSERT(strcmp(ri.http_headers[0].value, "foo bar") == 0);
@@ -33,7 +75,9 @@ static void test_parse_http_request() {
ASSERT(strcmp(ri.http_headers[2].name, "baz\r\n\r") == 0); ASSERT(strcmp(ri.http_headers[2].name, "baz\r\n\r") == 0);
ASSERT(strcmp(ri.http_headers[2].value, "") == 0); ASSERT(strcmp(ri.http_headers[2].value, "") == 0);
// TODO(lsm): add more tests. ASSERT(parse_http_message(req5, sizeof(req5), &ri) == sizeof(req5) - 1);
ASSERT(strcmp(ri.request_method, "GET") == 0);
ASSERT(strcmp(ri.http_version, "1.1") == 0);
} }
static void test_should_keep_alive(void) { static void test_should_keep_alive(void) {
@@ -46,7 +90,8 @@ static void test_should_keep_alive(void) {
memset(&conn, 0, sizeof(conn)); memset(&conn, 0, sizeof(conn));
conn.ctx = &ctx; conn.ctx = &ctx;
parse_http_request(req1, sizeof(req1), &conn.request_info); ASSERT(parse_http_message(req1, sizeof(req1), &conn.request_info) ==
sizeof(req1) - 1);
ctx.config[ENABLE_KEEP_ALIVE] = "no"; ctx.config[ENABLE_KEEP_ALIVE] = "no";
ASSERT(should_keep_alive(&conn) == 0); ASSERT(should_keep_alive(&conn) == 0);
@@ -58,13 +103,13 @@ static void test_should_keep_alive(void) {
ASSERT(should_keep_alive(&conn) == 0); ASSERT(should_keep_alive(&conn) == 0);
conn.must_close = 0; conn.must_close = 0;
parse_http_request(req2, sizeof(req2), &conn.request_info); parse_http_message(req2, sizeof(req2), &conn.request_info);
ASSERT(should_keep_alive(&conn) == 0); ASSERT(should_keep_alive(&conn) == 0);
parse_http_request(req3, sizeof(req3), &conn.request_info); parse_http_message(req3, sizeof(req3), &conn.request_info);
ASSERT(should_keep_alive(&conn) == 0); ASSERT(should_keep_alive(&conn) == 0);
parse_http_request(req4, sizeof(req4), &conn.request_info); parse_http_message(req4, sizeof(req4), &conn.request_info);
ASSERT(should_keep_alive(&conn) == 1); ASSERT(should_keep_alive(&conn) == 1);
conn.status_code = 401; conn.status_code = 401;
@@ -121,106 +166,204 @@ static void test_remove_double_dots() {
size_t i; size_t i;
for (i = 0; i < ARRAY_SIZE(data); i++) { for (i = 0; i < ARRAY_SIZE(data); i++) {
printf("[%s] -> [%s]\n", data[i].before, data[i].after);
remove_double_dots_and_double_slashes(data[i].before); remove_double_dots_and_double_slashes(data[i].before);
ASSERT(strcmp(data[i].before, data[i].after) == 0); ASSERT(strcmp(data[i].before, data[i].after) == 0);
} }
} }
static char *read_file(const char *path, int *size) {
FILE *fp;
struct stat st;
char *data = NULL;
if ((fp = fopen(path, "rb")) != NULL && !fstat(fileno(fp), &st)) {
*size = (int) st.st_size;
ASSERT((data = malloc(*size)) != NULL);
ASSERT(fread(data, 1, *size, fp) == (size_t) *size);
fclose(fp);
}
return data;
}
static const char *fetch_data = "hello world!\n"; static const char *fetch_data = "hello world!\n";
static const char *inmemory_file_data = "hi there"; static const char *inmemory_file_data = "hi there";
static void *event_handler(enum mg_event event, struct mg_connection *conn) { static const char *upload_filename = "upload_test.txt";
const struct mg_request_info *request_info = mg_get_request_info(conn); static const char *upload_ok_message = "upload successful";
if (event == MG_NEW_REQUEST && !strcmp(request_info->uri, "/data")) { static const char *open_file_cb(const struct mg_connection *conn,
const char *path, size_t *size) {
(void) conn;
if (!strcmp(path, "./blah")) {
*size = strlen(inmemory_file_data);
return inmemory_file_data;
}
return NULL;
}
static void upload_cb(struct mg_connection *conn, const char *path) {
char *p1, *p2;
int len1, len2;
ASSERT(!strcmp(path, "./upload_test.txt"));
ASSERT((p1 = read_file("mongoose.c", &len1)) != NULL);
ASSERT((p2 = read_file(path, &len2)) != NULL);
ASSERT(len1 == len2);
ASSERT(memcmp(p1, p2, len1) == 0);
free(p1), free(p2);
remove(upload_filename);
mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n\r\n%s",
(int) strlen(upload_ok_message), upload_ok_message);
}
static int begin_request_handler_cb(struct mg_connection *conn) {
const struct mg_request_info *ri = mg_get_request_info(conn);
if (!strcmp(ri->uri, "/data")) {
mg_printf(conn, "HTTP/1.1 200 OK\r\n" mg_printf(conn, "HTTP/1.1 200 OK\r\n"
"Content-Length: %d\r\n" "Content-Length: %d\r\n"
"Content-Type: text/plain\r\n\r\n" "Content-Type: text/plain\r\n\r\n"
"%s", (int) strlen(fetch_data), fetch_data); "%s", (int) strlen(fetch_data), fetch_data);
return ""; return 1;
} else if (event == MG_OPEN_FILE) {
const char *path = request_info->ev_data;
printf("%s: [%s]\n", __func__, path);
if (strcmp(path, "./blah") == 0) {
mg_get_request_info(conn)->ev_data =
(void *) (int) strlen(inmemory_file_data);
return (void *) inmemory_file_data;
}
} else if (event == MG_EVENT_LOG) {
printf("%s\n", (const char *) mg_get_request_info(conn)->ev_data);
} }
return NULL; if (!strcmp(ri->uri, "/upload")) {
ASSERT(mg_upload(conn, ".") == 1);
}
return 0;
} }
static void test_mg_fetch(void) { static int log_message_cb(const struct mg_connection *conn, const char *msg) {
static const char *options[] = { (void) conn;
"document_root", ".", printf("%s\n", msg);
"listening_ports", UNUSED_PORT, return 0;
NULL, }
};
char buf[2000], buf2[2000]; static const struct mg_callbacks CALLBACKS = {
int n, length; &begin_request_handler_cb, NULL, &log_message_cb, NULL, NULL, NULL, NULL,
&open_file_cb, NULL, &upload_cb
};
static const char *OPTIONS[] = {
"document_root", ".",
"listening_ports", LISTENING_ADDR,
"ssl_certificate", "build/ssl_cert.pem",
NULL,
};
static char *read_conn(struct mg_connection *conn, int *size) {
char buf[100], *data = NULL;
int len;
*size = 0;
while ((len = mg_read(conn, buf, sizeof(buf))) > 0) {
*size += len;
ASSERT((data = realloc(data, *size)) != NULL);
memcpy(data + *size - len, buf, len);
}
return data;
}
static void test_mg_download(void) {
char *p1, *p2, ebuf[100];
int len1, len2, port = atoi(HTTPS_PORT);
struct mg_connection *conn;
struct mg_context *ctx; struct mg_context *ctx;
struct mg_request_info ri;
const char *tmp_file = "temporary_file_name_for_unit_test.txt";
struct file file;
FILE *fp;
ASSERT((ctx = mg_start(event_handler, NULL, options)) != NULL); ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
// Failed fetch, pass invalid URL ASSERT(mg_download(NULL, port, 0, ebuf, sizeof(ebuf), "%s", "") == NULL);
ASSERT(mg_fetch(ctx, "localhost", tmp_file, buf, sizeof(buf), &ri) == NULL); ASSERT(mg_download("localhost", 0, 0, ebuf, sizeof(ebuf), "%s", "") == NULL);
ASSERT(mg_fetch(ctx, "localhost:" UNUSED_PORT, tmp_file, ASSERT(mg_download("localhost", port, 1, ebuf, sizeof(ebuf),
buf, sizeof(buf), &ri) == NULL); "%s", "") == NULL);
ASSERT(mg_fetch(ctx, "http://$$$.$$$", tmp_file,
buf, sizeof(buf), &ri) == NULL);
// Failed fetch, pass invalid file name // Fetch nonexistent file, should see 404
ASSERT(mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/data", ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
"/this/file/must/not/exist/ever", "GET /gimbec HTTP/1.0\r\n\r\n")) != NULL);
buf, sizeof(buf), &ri) == NULL); ASSERT(strcmp(conn->request_info.uri, "404") == 0);
mg_close_connection(conn);
// Successful fetch ASSERT((conn = mg_download("google.com", 443, 1, ebuf, sizeof(ebuf), "%s",
ASSERT((fp = mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/data", "GET / HTTP/1.0\r\n\r\n")) != NULL);
tmp_file, buf, sizeof(buf), &ri)) != NULL); mg_close_connection(conn);
ASSERT(ri.num_headers == 2);
ASSERT(!strcmp(ri.request_method, "HTTP/1.1"));
ASSERT(!strcmp(ri.uri, "200"));
ASSERT(!strcmp(ri.http_version, "OK"));
ASSERT((length = ftell(fp)) == (int) strlen(fetch_data));
fseek(fp, 0, SEEK_SET);
ASSERT(fread(buf2, 1, length, fp) == (size_t) length);
ASSERT(memcmp(buf2, fetch_data, length) == 0);
fclose(fp);
// Fetch big file, mongoose.c // Fetch mongoose.c, should succeed
ASSERT((fp = mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/mongoose.c", ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
tmp_file, buf, sizeof(buf), &ri)) != NULL); "GET /mongoose.c HTTP/1.0\r\n\r\n")) != NULL);
ASSERT(mg_stat(fc(ctx), "mongoose.c", &file)); ASSERT(!strcmp(conn->request_info.uri, "200"));
ASSERT(file.size == ftell(fp)); ASSERT((p1 = read_conn(conn, &len1)) != NULL);
ASSERT(!strcmp(ri.request_method, "HTTP/1.1")); ASSERT((p2 = read_file("mongoose.c", &len2)) != NULL);
ASSERT(len1 == len2);
ASSERT(memcmp(p1, p2, len1) == 0);
free(p1), free(p2);
mg_close_connection(conn);
// Fetch nonexistent file, /blah // Fetch in-memory file, should succeed.
ASSERT((fp = mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/boo", ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
tmp_file, buf, sizeof(buf), &ri)) != NULL); "GET /blah HTTP/1.1\r\n\r\n")) != NULL);
ASSERT(!mg_strcasecmp(ri.uri, "404")); ASSERT((p1 = read_conn(conn, &len1)) != NULL);
fclose(fp); ASSERT(len1 == (int) strlen(inmemory_file_data));
ASSERT(memcmp(p1, inmemory_file_data, len1) == 0);
free(p1);
mg_close_connection(conn);
// Fetch existing inmemory file // Test SSL redirect, IP address
ASSERT((fp = mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/blah", ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
tmp_file, buf, sizeof(buf), &ri)) != NULL); ebuf, sizeof(ebuf), "%s",
ASSERT(!mg_strcasecmp(ri.uri, "200")); "GET /foo HTTP/1.1\r\n\r\n")) != NULL);
n = ftell(fp); ASSERT(strcmp(conn->request_info.uri, "302") == 0);
fseek(fp, 0, SEEK_SET); ASSERT(strcmp(mg_get_header(conn, "Location"),
printf("%s %d %d [%.*s]\n", __func__, n, (int) feof(fp), n, buf2); "https://127.0.0.1:" HTTPS_PORT "/foo") == 0);
n = fread(buf2, 1, n, fp); mg_close_connection(conn);
printf("%s %d %d [%.*s]\n", __func__, n, (int) feof(fp), n, buf2);
ASSERT((size_t) ftell(fp) == (size_t) strlen(inmemory_file_data));
ASSERT(!memcmp(inmemory_file_data, buf2, ftell(fp)));
fclose(fp);
remove(tmp_file); // Test SSL redirect, Host:
ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
ebuf, sizeof(ebuf), "%s",
"GET /foo HTTP/1.1\r\nHost: a.b:77\n\n")) != NULL);
ASSERT(strcmp(conn->request_info.uri, "302") == 0);
ASSERT(strcmp(mg_get_header(conn, "Location"),
"https://a.b:" HTTPS_PORT "/foo") == 0);
mg_close_connection(conn);
mg_stop(ctx);
}
static int alloc_printf(char **buf, size_t size, char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
return alloc_vprintf(buf, size, fmt, ap);
}
static void test_mg_upload(void) {
static const char *boundary = "OOO___MY_BOUNDARY___OOO";
struct mg_context *ctx;
struct mg_connection *conn;
char ebuf[100], buf[20], *file_data, *post_data = NULL;
int file_len, post_data_len;
ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
ASSERT((file_data = read_file("mongoose.c", &file_len)) != NULL);
post_data_len = alloc_printf(&post_data, 0,
"--%s\r\n"
"Content-Disposition: form-data; "
"name=\"file\"; "
"filename=\"%s\"\r\n\r\n"
"%.*s\r\n"
"--%s\r\n",
boundary, upload_filename,
file_len, file_data, boundary);
ASSERT(post_data_len > 0);
ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
ebuf, sizeof(ebuf),
"POST /upload HTTP/1.1\r\n"
"Content-Length: %d\r\n"
"Content-Type: multipart/form-data; "
"boundary=%s\r\n\r\n"
"%.*s", post_data_len, boundary,
post_data_len, post_data)) != NULL);
free(file_data), free(post_data);
ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message));
ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0);
mg_close_connection(conn);
mg_stop(ctx); mg_stop(ctx);
} }
@@ -232,7 +375,6 @@ static void test_base64_encode(void) {
for (i = 0; in[i] != NULL; i++) { for (i = 0; in[i] != NULL; i++) {
base64_encode((unsigned char *) in[i], strlen(in[i]), buf); base64_encode((unsigned char *) in[i], strlen(in[i]), buf);
printf("[%s] [%s]\n", out[i], buf);
ASSERT(!strcmp(buf, out[i])); ASSERT(!strcmp(buf, out[i]));
} }
} }
@@ -297,10 +439,9 @@ static void check_lua_expr(lua_State *L, const char *expr, const char *value) {
char buf[100]; char buf[100];
snprintf(buf, sizeof(buf), "%s = %s", var_name, expr); snprintf(buf, sizeof(buf), "%s = %s", var_name, expr);
luaL_dostring(L, buf); (void) luaL_dostring(L, buf);
lua_getglobal(L, var_name); lua_getglobal(L, var_name);
v = lua_tostring(L, -1); v = lua_tostring(L, -1);
printf("%s: %s: [%s] [%s]\n", __func__, expr, v == NULL ? "null" : v, value);
ASSERT((value == NULL && v == NULL) || ASSERT((value == NULL && v == NULL) ||
(value != NULL && v != NULL && !strcmp(value, v))); (value != NULL && v != NULL && !strcmp(value, v)));
} }
@@ -312,13 +453,12 @@ static void test_lua(void) {
char http_request[] = "POST /foo/bar HTTP/1.1\r\n" char http_request[] = "POST /foo/bar HTTP/1.1\r\n"
"Content-Length: 12\r\n" "Content-Length: 12\r\n"
"Connection: close\r\n\r\nhello world!"; "Connection: close\r\n\r\nhello world!";
const char *page = "<? print('hi') ?>";
lua_State *L = luaL_newstate(); lua_State *L = luaL_newstate();
conn.ctx = &ctx; conn.ctx = &ctx;
conn.buf = http_request; conn.buf = http_request;
conn.buf_size = conn.data_len = strlen(http_request); conn.buf_size = conn.data_len = strlen(http_request);
conn.request_len = parse_http_request(conn.buf, conn.data_len, conn.request_len = parse_http_message(conn.buf, conn.data_len,
&conn.request_info); &conn.request_info);
conn.content_len = conn.data_len - conn.request_len; conn.content_len = conn.data_len - conn.request_len;
@@ -332,33 +472,16 @@ static void test_lua(void) {
check_lua_expr(L, "request_info.remote_ip", "0"); check_lua_expr(L, "request_info.remote_ip", "0");
check_lua_expr(L, "request_info.http_headers['Content-Length']", "12"); check_lua_expr(L, "request_info.http_headers['Content-Length']", "12");
check_lua_expr(L, "request_info.http_headers['Connection']", "close"); check_lua_expr(L, "request_info.http_headers['Connection']", "close");
luaL_dostring(L, "post = read()"); (void) luaL_dostring(L, "post = read()");
check_lua_expr(L, "# post", "12"); check_lua_expr(L, "# post", "12");
check_lua_expr(L, "post", "hello world!"); check_lua_expr(L, "post", "hello world!");
lua_close(L); lua_close(L);
} }
#endif #endif
static void *user_data_tester(enum mg_event event, struct mg_connection *conn) {
struct mg_request_info *ri = mg_get_request_info(conn);
ASSERT(ri->user_data == (void *) 123);
ASSERT(event == MG_NEW_REQUEST);
return NULL;
}
static void test_user_data(void) {
static const char *options[] = {"listening_ports", UNUSED_PORT, NULL};
struct mg_context *ctx;
ASSERT((ctx = mg_start(user_data_tester, (void *) 123, options)) != NULL);
ASSERT(ctx->user_data == (void *) 123);
call_user(fc(ctx), MG_NEW_REQUEST);
}
static void test_mg_stat(void) { static void test_mg_stat(void) {
static struct mg_context ctx; static struct mg_context ctx;
struct file file; struct file file = STRUCT_FILE_INITIALIZER;
memset(&file, 'A', sizeof(file));
ASSERT(!mg_stat(fc(&ctx), " does not exist ", &file)); ASSERT(!mg_stat(fc(&ctx), " does not exist ", &file));
} }
@@ -380,21 +503,99 @@ static void test_skip_quoted(void) {
#endif #endif
} }
int main(void) { static void test_alloc_vprintf(void) {
char buf[MG_BUF_LEN], *p = buf;
ASSERT(alloc_printf(&p, sizeof(buf), "%s", "hi") == 2);
ASSERT(p == buf);
ASSERT(alloc_printf(&p, sizeof(buf), "%s", "") == 0);
ASSERT(alloc_printf(&p, sizeof(buf), "") == 0);
// Pass small buffer, make sure alloc_printf allocates
ASSERT(alloc_printf(&p, 1, "%s", "hello") == 5);
ASSERT(p != buf);
free(p);
}
static void test_request_replies(void) {
char ebuf[100];
int i, port = atoi(HTTPS_PORT);
struct mg_connection *conn;
struct mg_context *ctx;
static struct { const char *request, *reply_regex; } tests[] = {
{
"GET test/hello.txt HTTP/1.0\r\nRange: bytes=3-5\r\n\r\n",
"^HTTP/1.1 206 Partial Content"
},
{NULL, NULL},
};
ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
for (i = 0; tests[i].request != NULL; i++) {
ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
tests[i].request)) != NULL);
mg_close_connection(conn);
}
mg_stop(ctx);
}
static int api_callback(struct mg_connection *conn) {
struct mg_request_info *ri = mg_get_request_info(conn);
char post_data[100] = "";
ASSERT(ri->user_data == (void *) 123);
ASSERT(ri->num_headers == 2);
ASSERT(strcmp(mg_get_header(conn, "host"), "blah.com") == 0);
ASSERT(mg_read(conn, post_data, sizeof(post_data)) == 3);
ASSERT(memcmp(post_data, "b=1", 3) == 0);
ASSERT(ri->query_string != NULL);
ASSERT(ri->remote_ip > 0);
ASSERT(ri->remote_port > 0);
ASSERT(strcmp(ri->http_version, "1.0") == 0);
mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\n");
return 1;
}
static void test_api_calls(void) {
char ebuf[100];
struct mg_callbacks callbacks;
struct mg_connection *conn;
struct mg_context *ctx;
static const char *request = "POST /?a=%20&b=&c=xx HTTP/1.0\r\n"
"Host: blah.com\n" // More spaces before
"content-length: 3\r\n" // Lower case header name
"\r\nb=123456"; // Content size > content-length, test for mg_read()
memset(&callbacks, 0, sizeof(callbacks));
callbacks.begin_request = api_callback;
ASSERT((ctx = mg_start(&callbacks, (void *) 123, OPTIONS)) != NULL);
ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
ebuf, sizeof(ebuf), "%s", request)) != NULL);
mg_close_connection(conn);
mg_stop(ctx);
}
int __cdecl main(void) {
test_alloc_vprintf();
test_base64_encode(); test_base64_encode();
test_match_prefix(); test_match_prefix();
test_remove_double_dots(); test_remove_double_dots();
test_should_keep_alive(); test_should_keep_alive();
test_parse_http_request(); test_parse_http_message();
test_mg_fetch(); test_mg_download();
test_mg_get_var(); test_mg_get_var();
test_set_throttle(); test_set_throttle();
test_next_option(); test_next_option();
test_user_data();
test_mg_stat(); test_mg_stat();
test_skip_quoted();
test_mg_upload();
test_request_replies();
test_api_calls();
#ifdef USE_LUA #ifdef USE_LUA
test_lua(); test_lua();
#endif #endif
test_skip_quoted();
printf("%s\n", "PASSED");
return 0; return 0;
} }