Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

webserv - HTTP Server in C++98

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.

Features

what it can do

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

Requirements

  • C++ compiler that supports C++98
  • Unix-like system
  • Make
  • optional: php-cgi, python or other CGI stuff if you want dynamic content

installation

pretty straightforward:

git clone https://github.com/01-kali/Webserv.git
cd Webserv
#change "~/Webserv" inside ./tests/website.conf with the actual path 
make

if you want to clean up:

make clean    # removes object files
make fclean   # removes everything including the executable
make re       # rebuild from scratch

Configuration

we use config files similar to nginx. heres an example:

usage

basic stuff

# 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/upload

using a browser

just open your browser and go to:

http://localhost:8080

testing CGI

  1. put your script in the CGI directory
  2. make sure its executable
  3. access it:
http://localhost:8080/cgi-bin/script.php

CGI execution

when a CGI script needs to run, we:

  1. fork a child process
  2. setup pipes for stdin/stdout communication
  3. exec the interpreter (php-cgi, python, etc)
  4. pass the request body through stdin
  5. read the response from stdout
  6. parse CGI headers and send everything to client

the whole thing is non-blocking with a 60 second timeout.

testing

manual tests

# 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.php

stress testing

use 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/

known issues / limitations

  • only supports HTTP/1.0
  • no HTTPS
  • CGI scripts timeout after 60 seconds
  • max URI length: 1024 chars
  • max header line: 1024 chars

error codes we handle

  • 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

authors

iboutadg yait-lhi zelkalai

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages