Simple HTTP/1.1 Web Server
Description
This project is a lightweight web server implemented in C, using raw TCP sockets and the POSIX API. It serves static files from a specified resource directory and handles multiple clients concurrently in a non-blocking fashion.
Features
- Implements the HTTP/1.1 protocol (basic subset).
- Handles GET requests for static files.
- Uses non-blocking I/O with select.
- Manages multiple client sessions via a custom session array.
- Basic request parsing with a finite state machine (FSM).
- Logs events using syslog.
- Sends appropriate HTTP responses, including 400 Bad Request and 404 Not Found.
Technologies Used
- C (ISO C90/C99)
- POSIX sockets API (socket, bind, accept, etc.)
- Non-blocking I/O with select
- TCP/IP Networking
- Syslog logging
Structure
struct server
: represents the server and holds the listening socket and
session array. struct session
: represents a connected client and holds state
and buffers. Custom FSM (enum fsm_states
) for request parsing and response
generation. Defines for buffer sizes and constants allow easy tuning.
Usage
Compile with:
git clone https://git.scratko.xyz/http-server
cd http-server
gcc -Wall -O2 http_server.c -o http_server
Run:
./http_server [port] [host] [resource_directory]
Example:
./http_server 8082 test.scratko.xyz:8082 /var/www/test
Notes
Only basic HTTP/1.1 functionality is supported (no keep-alive, POST, etc.). Designed for educational and experimental purposes. Ensure the resource directory and files have proper read permissions.
The http-server directory contains a test_files
subdirectory with the files:
400.html
, 404.html
, and index.html
. These are placeholder pages that can
be placed in the resource directory specified when launching the server
([resource_directory]
). If the server starts correctly, you should see
index.html
in your browser (as shown in the screenshot).
If the browser sends an incorrect GET request, or if you specify an incorrect host when launching the server — for example:
server 8082 test.scrato.xyz:8082 /var/www/test
Note the intentional typo in the hostname (scrato instead of scratko). Then,
when the browser accesses test.scratko.xyz:8082, it will receive the 400.html
page (Bad Request).
Another case: if you try to access a nonexistent section or file of the site, for example:
test.scratko.xyz:8082/test
you will receive the 404.html
page (Page Not Found).