A set of simple CGI-enabled web servers, with minimal support: HTTP and CGI, that's it.
See the webservers project for static-content-only web servers.
See the notify-portable project for a demo application with an embedded CGI-enabled web server.
Purpose
Try to get a few web servers, found on the net and written in C, to work the way I want:
- should be written in C
- should be minimal
- should have support for HTTP
- should have support for CGI (C is mandatory, Perl is nice-to-have)
- should NOT support any other functionality, such as:
- SSI
- HTTPS
- websockets
- directory listing
- authentication
- redirection
- threads
- etc
To check if a web server is portable between OS:es, both POSIX sockets and Winsock sockets should be supported.
To fast-check: If HTTP-CGI server compiles both with MSYS2 (POSIX) and with Mingw64 (Winsock),
chances are (big) that the web server works for several other OS:es as well.
CGI-enabled web server: MinGW64: Civetweb
https://github.com/civetweb/civetweb
MinGW64:
wget https://github.com/civetweb/civetweb/archive/master.zip -O civetweb-master.zip
unzip civetweb-master.zip
cd civetweb-master
mingw32-make CC=gcc
###mingw32-make
./civetweb.exe
###cp ../cgi-bin/hellohtml.exe .
###cp ../cgi-bin/hellohtml.exe hellohtml.cgi
###./civetweb.exe -listening_ports 1236 -cgi_pattern '**.exe$|**.cgi'
./civetweb -listening_ports 1234 -document_root ../cgi-bin -cgi_pattern '**.exe$|**.cgi$|**.pl$'
http://127.0.0.1:1236/hellohtml.exe
http://127.0.0.1:1236/hellohtml.cgi
- Pros: Works for MinGW64, notifications, integrates nice with Windows systray, CGI works for C applications with both .exe and .cgi extensions
Cons: CGI works for either C programs or Perl scripts, but not both of them simultaneously:
Error 500: Internal Server Error Error: Cannot spawn CGI process [C:\fossil\nested\notify-portable\civetweb-master/hellohtml-perl.cgi]: No such file or directory
Workaround for Perl scripts (NOTE: makes C programs stop working as CGI): cgi_interpreter
https://github.com/cztomczak/phpdesktop/issues/68
./civetweb.exe -listening_ports 1236 -cgi_pattern '**.exe$|**.cgi|**.pl' -cgi_interpreter '\\usr\\bin\\perl.exe' <-- ERROR
./civetweb.exe -listening_ports 1236 -cgi_pattern '**.exe$|**.cgi|**.pl' -cgi_interpreter '\\c\\msys64\\usr\\bin\\perl.exe' <-- ERROR
./civetweb.exe -listening_ports 1236 -cgi_pattern '**.exe$|**.cgi|**.pl' -cgi_interpreter 'C:\\msys64\\usr\\bin\\perl.exe' <-- OK (for Perl scripts, but not for C)
CGI-enabled web server: MSYS2: Merecat
Merecat was the original code for Civetweb (see above).
Merecat works with MSYS2:
wget https://github.com/troglobit/merecat/archive/master.zip -O merecat-master.zip
unzip merecat-master.zip
cd merecat-master
./autogen.sh
mkdir -p build/usr build/var build/etc
./configure --prefix=$PWD/build/usr --localstatedir=$PWD/build/var --sysconfdir=$PWD/build/etc --disable-dirlisting --without-config --without-ssl --without-symlinks --without-zlib
make
make install
build/usr/sbin/merecat.exe -p 8888 -c "**.cgi|**.exe|**.pl|**/cgi-bin/*"
cp ../cgi-bin/hellohtml.exe .
http://127.0.0.1:8888/hellohtml.exe
- Pros: Works out of the box for MSYS2, works for both C and Perl CGI simultaneously.
- Cons: Does not work for MinGW64, uses autoconf/automake
Other CGI-enabled web servers
CGI-enabled web server: MinGW64: Mongoose
https://github.com/cesanta/mongoose
https://cesanta.com/docs/overview/build-options.html
https://cesanta.com/docs/http/cgi.html
https://cesanta.com/docs/http/server-example.html
https://github.com/cesanta/mongoose/tree/master/examples/simplest_web_server
CGI problems with mongoose compiled with MinGW64:
wget https://github.com/cesanta/mongoose/archive/master.zip -O mongoose-master.zip
unzip mongoose-master.zip
patch mongoose-master.patch # Fix casts and call to thread_enabled function
cd mongoose-master/examples/simplest_web_server
s_http_server_opts.enable_directory_listing = "no";
Makefile:
----------------------------------------
PROG = simplest_web_server
# MODULE_CFLAGS=-DMG_DISABLE_DAV_AUTH -DMG_ENABLE_FAKE_DAVLOCK
# MODULE_CFLAGS=-DMG_DISABLE_DAV_AUTH -DMG_ENABLE_FAKE_DAVLOCK -DMG_DISABLE_THREADS
#MODULE_CFLAGS=-DMG_DISABLE_DAV_AUTH -DMG_ENABLE_FAKE_DAVLOCK -DMG_ENABLE_THREADS=0
MODULE_CFLAGS=-DMG_DISABLE_DAV_AUTH -DMG_ENABLE_THREADS=0
MODULE_CFLAGS = \
-DMG_DISABLE_DAV_AUTH \
-DMG_ENABLE_FAKE_DAVLOCK \
-DMG_ENABLE_SSL=0 \
-DMG_ENABLE_IPV6=0 \
-DMG_ENABLE_MQTT=0 \
-DMG_ENABLE_MQTT_BROKER=0 \
-DMG_ENABLE_DNS_SERVER=0 \
-DMG_ENABLE_COAP=0 \
-DMG_ENABLE_HTTP=1 \
-DMG_ENABLE_HTTP_CGI=1 \
-DMG_ENABLE_HTTP_SSI=0 \
-DMG_ENABLE_HTTP_SSI_EXEC=0 \
-DMG_ENABLE_HTTP_WEBDAV=0 \
-DMG_ENABLE_HTTP_WEBSOCKET=0 \
-DMG_ENABLE_BROADCAST=0 \
-DMG_ENABLE_GETADDRINFO=0 \
-DMG_ENABLE_THREADS=0 \
-DMG_DISABLE_HTTP_DIGEST_AUTH=1 \
-DCS_DISABLE_SHA1=1 \
-DCS_DISABLE_MD5=1 \
-DMG_DISABLE_HTTP_KEEP_ALIVE=1
include ../examples.mk
----------------------------------------
./simplest_web_server.exe
cp ../../../cgi-bin/hellohtml.exe .
http://127.0.0.1:8000/hellohtml.exe
- Pros: Works (after patch) for MinGW64
- Cons: Big source code tree, does not work for MSYS2:
Starting web server on port 8000, Failed to create listener
thttpd
Tiny web server.
http://www.acme.com/software/thttpd/
- Pros: Tiny
- Cons: Does not compile, neither on MSYS2 nor MinGW64
MSYS2/MinGW64:
wget http://www.acme.com/software/thttpd/thttpd-2.29.tar.gz
tar xvfz thttpd-2.29.tar.gz
cd thttpd-2.29
./configure --host=win32 --prefix=$PWD/build
make <--- ERROR
make install
shttpd
Tiny web server.
https://docs.huihoo.com/shttpd/
https://sourceforge.net/projects/shttpd/files/shttpd/
tar xvfz shttpd-1.42.tar.gz
cd shttpd-1.42
cd src
make unix DNO_SSL=1 DNO_AUTH=1 DNO_SSI=1 DNO_THREADS=1
./shttpd.exe -ports 8889 -cgi_ext "exe,cgi,pl,php"
cp ../../cgi-bin/hellohtml.exe .
http://127.0.0.1:8889/hellohtml.exe
- Pros: Small, compiles on MSYS, easy to reduce code using flags to
make
- Cons: Does not compile on MinGW64. Compiles on MSYS2, but .exe files returns error 500.
tcgi
https://github.com/ndevilla/tcgi
Depends on a small hash library: https://github.com/ndevilla/dict
git clone https://github.com/ndevilla/tcgi.git
git clone https://github.com/ndevilla/dict.git
cp dict/dict* tcgi/
althttpd
drh's standalone web server
https://sqlite.org/docsrc/doc/trunk/misc/althttpd.md
https://www.sqlite.org/docsrc/file/misc/althttpd.c
In MSYS2 shell:
cd althttpd
gcc -Os -o althttpd althttpd.c # Optimize for size
gcc -g -o althttpd althttpd.c # Debug
make clean all # Debug, using Makefile
./althttpd -root ../cgi-bin -port 9999
- Pros: Single-file. Small. Compiles and runs both C and Perl CGI on MSYS2 out-of-the-box.
- Cons: Does not compile on MinGW64.
mwwwd: CGI-enabled web server for both MinGW64/Win32 and MSYS2/POSIX: Merge Civetweb and althttpd code into one single server mwwwd
- Civetweb Pros: Works for MinGW64, notifications, integrates nice with Windows systray.
Althttpd Pros: Works for MSYS2, CGI works for both C and Perl applications with .exe/.cgi/.pl.
Civetweb Cons: Works for either .exe/.cgi or .pl, but not simultaneously.
Althttpd Pros: Does not integrate with Windows systray. Dows not compile on MinGW64.
Roadmap:
Probably the easiest way is to use althttpd
as a base for both MinGW64/Win32 and MSYS2/POSIX.
The parts of althttpd
which does not compile, should be replaced by Civetweb
code.
- https://docs.microsoft.com/en-us/windows/win32/winsock/porting-socket-applications-to-winsock
- https://tangentsoft.net/wskfaq/articles/bsd-compatibility.html
- http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2241&lngWId=3
TODO: Check these projects
- https://github.com/arman-bd/khudro Khudro is a very light weight web-server built with C. MS Windows
- https://www.binarytides.com/winsock-socket-programming-tutorial/ Winsock tutorial - Socket programming in C on windows
- http://www.rohitab.com/discuss/topic/41980-how-do-i-make-post-request-from-winsock/ How do i make post request from WinSock? (Portable solution at end of page)
- https://github.com/chichunchen/winsock-http-cgi Bundle simple http cgi server with nonblock winsock client
- https://github.com/jingedawang/Tiny-WebServer Tiny-WebServer (with CGI, not winsock)
- https://github.com/ia/connect tiny cross-platform socket API library
- http://jkorpela.fi/forms/cgic.html Getting Started with CGI Programming in C - Process a simple form
- https://www.eskimo.com/~scs/cclass/handouts/cgi.html
- https://www.drdobbs.com/cgi-programming-in-c/184403248
- http://www6.uniovi.es/cscene/topics/web/cs2-12.xml.html
- https://github.com/WileyBui/CGI-HTTPWebServer
- https://github.com/klange/cgiserver