A custom HTTP server implementation written in C++98 for the 42 project. basically we built our own web server from scratch that can handle multiple connections, serve files, run CGI scripts and more.
- serve static files (HTML, CSS, JS, images, etc)
- handle GET, POST, and DELETE methods
- execute CGI scripts (PHP, Python, whatever)
- upload files from clients
- directory listing (like when you browse a folder)
- multiple virtual hosts on different ports
- custom error pages
- HTTP redirections
- configurable request body size limits
- keep-alive connections with timeout
everything runs non-blocking with epoll so the server doesn't freeze when handling multiple clients.
- C++ compiler that supports C++98
- Unix-like system
- Make
- optional: php-cgi, python or other CGI stuff if you want dynamic content
pretty straightforward:
git clone https://github.com/01-kali/Webserv.git
cd Webserv
#change "~/Webserv" inside ./tests/website.conf with the actual path
makeif you want to clean up:
make clean # removes object files
make fclean # removes everything including the executable
make re # rebuild from scratchwe use config files similar to nginx. heres an example:
# start the server
./webserv tests/website.conf
# test it with curl
curl http://localhost:8080/
# upload a file
echo test > example.txt
curl -X POST -F "file=@example.txt" http://localhost:8080/uploadjust open your browser and go to:
http://localhost:8080
- put your script in the CGI directory
- make sure its executable
- access it:
http://localhost:8080/cgi-bin/script.php
when a CGI script needs to run, we:
- fork a child process
- setup pipes for stdin/stdout communication
- exec the interpreter (php-cgi, python, etc)
- pass the request body through stdin
- read the response from stdout
- parse CGI headers and send everything to client
the whole thing is non-blocking with a 60 second timeout.
# static files
curl http://localhost:8080/index.html
# directory listing
curl http://localhost:8080/images/
# POST request
curl -X POST -d "data=test" http://localhost:8080/upload
# CGI script
curl http://localhost:8080/cgi-bin/info.phpuse siege or apache bench:
# apache bench - 1000 requests, 10 concurrent
ab -n 1000 -c 10 http://localhost:8080/
# siege
siege -c 50 -r 100 http://127.0.0.1:8080/- only supports HTTP/1.0
- no HTTPS
- CGI scripts timeout after 60 seconds
- max URI length: 1024 chars
- max header line: 1024 chars
- 400 - bad request (malformed HTTP)
- 403 - forbidden (permission denied)
- 404 - not found
- 405 - method not allowed
- 408 - request timeout
- 413 - payload too large
- 414 - URI too long
- 500 - internal server error
- 501 - not implemented
- 503 - service unavailable
iboutadg yait-lhi zelkalai