This project has been created as part of the 42 curriculum by ssoeno, takitaga, aenshin.
This project implements an HTTP server in C++98. It receives requests from clients (such as browsers) and returns appropriate responses. It supports methods such as GET, POST, and DELETE, serves static files, executes CGI scripts, and returns proper HTTP status codes and headers.
The server uses an event-driven architecture based on epoll to efficiently handle multiple connections concurrently. epoll monitors multiple file descriptors and notifies the server when they are ready for I/O operations, allowing non-blocking and scalable request handling.
-
Single event loop handling:
- client connections
- request parsing
- response sending
-
Non-blocking I/O:
- I/O operations are performed only when
epollsignals readiness - Read/write operations continue until
EAGAINorEWOULDBLOCK, then control returns to the event loop
- I/O operations are performed only when
-
Main loop design:
epollis placed in the main loop to continuously monitor all file descriptors for both read and write events without blocking
- CGI is handled using
fork()andexecve()
All I/O operations (read, recv, write, send) properly check return values:
> 0: success0: connection closed-1: error (handled appropriately, e.g., closing the connection)
Control flow is driven primarily by syscall return values. When an operation fails, the implementation may also consult errno to distinguish error conditions (for example, retryable non-blocking cases versus other failures) and to map filesystem or syscall errors to appropriate HTTP status codes.
- epoll_wait monitors all file descriptors
- On read event:
- parse HTTP request incrementally
- On write event:
- send response buffer
- CGI:
- fork + execve
- communicate via pipes
- C++ Compiler: g++ (>= 9.0) or clang++ (>= 10.0)
- Build System: GNU Make
- Standard Libraries: C++98 compatible standard library
- Operating System: Linux (epoll support required)
- Architecture: x86_64 or compatible
sudo apt update
sudo apt install build-essential g++ make clang-format clang-tidy python3 python3-pip
# if needed, sudo apt install -y netcat-openbsdmake
./webserv demo/conf/webserv_eval.confmake test- Source files are located in the
srcsdirectory, and corresponding test files should be placed under thetestsdirectory. - Name all test files with the
.test.cppextension.
- RFC9110, 9112, 3875
- nginx documentation https://nginx.org/en/docs/
- How AI was used
- to review pull requests
- to clarify RFC interpretation
- for documentation improvement
Install clang-format using your package manager such as brew or apt.
make formatmake tidyIf you want to fix lint errors automatically, use following command.
make tidy-fixThe server supports:
- multiple ports
- route-based configuration
- allowed methods per route
- file upload directory
- CGI execution by extension
- autoindex on/off
Open different terminal and run following curl commands to test the server in a dev container:
# Confirm it listens
lsof -nP -iTCP -sTCP:LISTEN | grep webserv
# or
netstat -an | grep LISTEN | grep <PORT>
# Basic checks
# listens on two ports, e.g. 8080 and 8081:
curl -v http://127.0.0.1:8080/
curl -v http://127.0.0.1:8081/
# Port conflict behavior (coherent and non-crashing)
# Terminal A
./webserv demo/conf/webserv_eval_browser.conf
# Terminal B: start another instance using same interface:port
./webserv demo/conf/webserv_eval_browser.conf
# Expect: clean error (bind failure), no crash, clear message.
# Must not crash / never hang indefinitely”
# Unknown method: expect 501(not implemented)
printf "PUT / HTTP/1.1\r\nHost: localhost\r\n\r\n" | nc -v 127.0.0.1 <PORT>
# Slow client / partial request should not hang
# send headers slowly
{ printf "GET / HTTP/1.1\r\nHost: localhost\r\n"; sleep 5; printf "\r\n"; } | nc -v 127.0.0.1 <PORT>
# GET / POST / DELETE work
# GET a file
curl -v http://127.0.0.1:<PORT>/index.html
# GET a directory (index vs autoindex)
curl -v http://127.0.0.1:<PORT>/
# Autoindex listing
curl -v http://127.0.0.1:8080/dirlist
# chunked transfer encoding
(
printf "POST /upload/test.txt HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHello\r\n"
sleep 1
printf "6\r\n World\r\n0\r\n\r\n"
) | nc -v 127.0.0.1 8080
# POST upload (file)
curl -X POST --data-binary @cat.jpg http://localhost:8080/upload/cat.jpg
# DELETE a resource
curl -X DELETE -v http://localhost:8080/upload/cat.jpg
# Expect: 200 + file removed.Test a valid static file:
curl -i http://127.0.0.1:8080/static/a.txtExpected result:
200 OK- File content is returned
Test file upload via POST:
curl -i -X POST http://127.0.0.1:8080/upload/test.txt \
-H "Content-Type: text/plain" \
--data "hello world"Expected result:
201 CreatedLocationheader is set (if implemented)- File is created on the server
Test redirection:
curl -i http://127.0.0.1:8080/redirectExpected result:
301 Moved Permanentlyor302 FoundLocationheader is present Follow redirect:
curl -L http://127.0.0.1:8080/redirectSend an invalid HTTP request:
printf "GET / HTTP/1.1\r\n\r\n" | nc 127.0.0.1 8080Expected result:
400 Bad Request- Server does not crash
Test a directory with no index file and autoindex disabled:
curl -i http://127.0.0.1:8080/static/Expected result:
403 Forbidden
Test a non-existent path:
curl -i http://127.0.0.1:8080/this_does_not_existExpected result:
404 Not Found- Default or custom error page is returned
Test a disallowed method:
curl -i -X DELETE http://127.0.0.1:8080/Expected result:
405 Method Not AllowedAllowheader should be present
Send a POST request without Content-Length:
printf "POST /upload HTTP/1.1\r\nHost: localhost\r\n\r\n" | nc 127.0.0.1 8080411 Length Requiredwould normally be appropriate, but the tester expects POST requests without body headers to be treated as having a content length of 0. Therefore, we handle them accordingly.
Send a request body exceeding client_max_body_size:
python3 - <<'PY' | curl -i -X POST http://127.0.0.1:8080/upload \
-H "Content-Type: text/plain" \
--data-binary @-
print("A" * 2000000)
PYExpected result:
413 Payload Too Large
Test missing trailing slash:
curl -i http://127.0.0.1:8080/dirlistExpected result:
301 Moved Permanently- Redirects to
/dirlist/
curl -v http://127.0.0.1:8080/redirectExpected result:
- 301/302 + Location header Follow redirect:
- curl -v -L http://127.0.0.1:8080/redirect
curl localhost:8080/cgi/hello.py curl localhost:8080/cgi/hello_post.py -d "test data" curl localhost:8080/cgi/endless.py -d "test data"
sudo apt install siege
echo "Hello World" > demo/static_sites/demo_site/empty.html
siege -b -c50 -d1 -r10 http://127.0.0.1:8080/empty.html
siege -b -c50 -d1 -t30S http://127.0.0.1:8080/empty.html
watch -n 1 "ps -o pid,rss,vsz,command -p $(pgrep webserv)"
- multipart/form-data is not supported
- absolute-form requests are not supported
- percent-decoding is not implemented
printf "GET http://127.0.0.1:8080/cgi/hello.py HTTP/1.1\r\nHost: 127.0.0.1:8080\r\n\r\n"
| nc -v 127.0.0.1 8080 -> Bad request