Skip to content

Lightweight HTTP Server in C. Serves static files, support MIME type detection, handles directory listing (with vim motions) and file previews with proper HTTP responses.

License

Notifications You must be signed in to change notification settings

navrajkalsi/server-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Server-C

Server Demo

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.

Motivation

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.

Worth-Mentioning Points

  • 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.

Quick Start

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:

Install Dependencies

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.

Usage

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

Default Usage

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.

Additional Usage Example

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.

Demo

Server Demo

Changelog

See CHANGELOG.md for changes made.

About

Lightweight HTTP Server in C. Serves static files, support MIME type detection, handles directory listing (with vim motions) and file previews with proper HTTP responses.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published