Lightweight HTTP Server in C. Serves static files, support MIME type detection, handles directory listing (with basic vim motions) and file previews with proper HTTP responses.
I wanted to know more about HTTP requests & servers, request-handling, and have some experience in C. Plus whenever I was using a local server, I was missing the vim motions to navigate around in the file tree.
So I decided to build one for a hopefully great learning experience and my personal usage.
It would have been a lot easier to do this in JavaScript, but I chose C to get low-level and get to have some experience with manipulating sockets, file-descriptors, HTTP Protocol with its headers and MIME types, and the C language in general.
- Supports Linux & most UNIX systems.
- Uses threads for concurrent processing of requests.
- MIME detection ensures proper previewing of file, done using
libmagic
. - Supported formats for preview: Text, Images, PDFs.
- Informs if a requested file is empty or unsupported for preview(can be downloaded in that case), with help of content-type header.
- Supports keep-alive connections with socket timeouts.
- Directory listing is done by using a static html file & javascript.
- Custom Error Page is served in case of any error, which changes dynamically based on the response status code.
- Clean Shutdown is done by handling interrupt and kill signals.
AS OF NOW, THIS SERVER ONLY SUPPORTS UNIX SYSTEMS.
-
For
Windows
, winsock API has to be implemented. Although,WSL
can be used in that case. -
For
Linux
:
GCC
is used as the compiler & libmagic
is required for MIME detection.
#Ubuntu/Debian
sudo apt update
sudo apt install build-essential gcc libmagic-dev
- Clone the Repository
git clone https://github.com/navrajkalsi/server-c
cd server-c
- Make
make
make install
By default, the binary is installed to /usr/local/bin & the static files required for the
server go in /usr/local/share/server-c/static.
You can pass in STATIC_DIR
, while compiling to change the directory in which the server looks for the static files,
and DESTDIR
while installing, to create a staging environment & manage files manually, like:
make STATIC_DIR=/NEW_STATIC_DIR #NEW_STATIC_DIR is where the program will look for the static files
make DESTDIR=/TEMP_DIR install #TEMP_DIR is where the program files will be installed now (binary & static files)
# Now the user is supposed to put the static files in NEW_STATIC_DIR
make clean
STATIC_DIR CAN ONLY BE CHANGED DURING COMPILATION, i.e., during make
, as it is used as a preprocessor macro.
If server-c
command is not found after installation, the directory in which the binary got installed is not on the PATH.
ADD THE MAKE INSTALLATION DIRECTORY TO THE PATH AND TRY AGAIN.
The following flags can be used to alter the behaviour of the program:
Flag | Flag Description |
---|---|
-a | Listen to connections on all interfaces of the machine |
-d | Debug Mode (Prints all functions calls to the console) |
-h | Print usage on command line |
-p | Port to listen on |
-r | Root of the directory to serve |
server-c
By default:
- Serves the current working directory.
- Uses port 1419.
- Listens to only localhost requests.
- Prints the client's address, request method & path on the console.
server-c -r /DIR_TO_SERVE -p 8080 -a
- Serves 'DIR_TO_SERVE' on port 8080 and listens to all requests from all IPs.
- Here, since we have passed -a flag, we can access files on your machine from different devices by visiting the IP address of your machine and targeting the appropriate port.
See CHANGELOG.md for changes made.