/42-webserv

Write a HTTP server in C++

Primary LanguageC++


42-webserv jremy's 42 webserv Score

๐Ÿ“ It's time to understand why URLs start with HTTP!

โš™๏ธ Developed with the software and tools below:

GNU%20Bash C++ JavaScript HTML5 PHP Docker Markdown

๐Ÿ“ Overview

The 42-webserv project is a C++ web server that provides a robust and efficient solution for handling client requests. It includes features such as server and location configuration, support for HTTP methods (such as GET, POST, and DELETE), handling of cookies and file uploads, and CGI support. Its value proposition lies in its ability to handle large volumes of client requests while providing the necessary flexibility to customize server configuration according to specific needs.


๐Ÿ’ซ Features

  • Read a configuration file.
  • Listen on multiple ports simultaneously.
  • Multiple servers can be configured on the same port.
  • Parse and serve an HTTP request.
  • Interact with a web browser.
  • The GET, POST, and DELETE methods are implemented.
  • Implement CGI in PHP and SHELL.
  • Properly handle errors.

๐Ÿ“‚ Project Structure

repo
โ”œโ”€โ”€ Makefile
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ cgi
โ”‚   โ””โ”€โ”€ bash_cgi.sh
โ”œโ”€โ”€ includes
โ”‚   โ”œโ”€โ”€ Client.hpp
โ”‚   โ”œโ”€โ”€ Config.hpp
โ”‚   โ”œโ”€โ”€ EventListener.hpp
โ”‚   โ”œโ”€โ”€ Location.hpp
โ”‚   โ”œโ”€โ”€ Multipart.hpp
โ”‚   โ”œโ”€โ”€ Request.hpp
โ”‚   โ”œโ”€โ”€ Response.hpp
โ”‚   โ”œโ”€โ”€ Server.hpp
โ”‚   โ”œโ”€โ”€ Webserv.hpp
โ”‚   โ””โ”€โ”€ _utils.hpp
โ”œโ”€โ”€ srcs
    โ”œโ”€โ”€ Client.cpp
    โ”œโ”€โ”€ Config.cpp
    โ”œโ”€โ”€ EventListener.cpp
    โ”œโ”€โ”€ Location.cpp
    โ”œโ”€โ”€ Multipart.cpp
    โ”œโ”€โ”€ Request.cpp
    โ”œโ”€โ”€ Response.cpp
    โ”œโ”€โ”€ Server.cpp
    โ”œโ”€โ”€ Webserv.cpp
    โ”œโ”€โ”€ _utils.cpp
    โ””โ”€โ”€ main.cpp



๐Ÿš€ Getting Started

โœ… Prerequisites

Before you begin, ensure that you have the following prerequisites installed:

  • Make
  • gcc

๐Ÿ–ฅ Installation

  1. Clone the 42-webserv repository:
git clone https://github.com/jremy42/42-webserv
  1. Change to the project directory:
cd 42-webserv
  1. compiling the project:
make
  1. launch webserv:
./webserv ./webserv [configuration file]

๐Ÿค– Using 42-webserv

  1. launch webserv:
./webserv ./webserv [configuration file]

configuration file :

server {
		root 			./www/site1/;
		listen			127.0.0.1:8080; #config port and ip 
		client_max_body_size	8m; 
		#error_page 		404 404.html;
		#error_page 		413 413.html;

		location /{
			root ./www/site1/;
			allowed_method GET DELETE POST;
			autoindex on;
			index index.html;
			cgi .php /usr/bin/php-cgi;
			cgi .sh ./cgi/bash_cgi.sh;
			upload ./www/site1/upload;
		}
}
  • root specifies the root directory for serving files.

  • listen defines the IP address and port number for the server to listen on.

  • client_max_body_size sets the maximum allowed size for client request bodies.

  • error_page specifies the error pages to be displayed for specific HTTP error codes.

  • location / defines the location block for the root directory. It specifies various directives within it:

    • allowed_method lists the HTTP methods allowed for this location. Only GET, DELETE, and POST are allowed.
    • autoindex enables the automatic generation of directory listings when no index file is found.
    • index specifies the default index file to serve if available.
    • cgi configures the handling of CGI scripts with their corresponding interpreter.
    • upload specifies the directory where uploaded files will be stored.

๐Ÿงช Running Tests

for testing parsing config :

./tester_webserv/test_config.sh

for testing request :

./tester_webserv/test_header.sh

for testing route configuration :

./tester_webserv/test_route.sh

๐Ÿ‘ Acquired knowledge

HTTP Protocol:

  • โœ… HTTP (Hypertext Transfer Protocol)

    • Application protocol for distributed, collaborative, and hypermedia information systems
    • Communication between client and server using HTTP
  • โœ… Web Server:

    • Storing, processing, and delivering web pages to clients
    • Responding to client requests with the content of requested resources
    • Serving HTML documents, including images, stylesheets, and scripts
  • โœ… Client-Server Communication:

    • Initiating communication with a server through HTTP requests
    • Server responding with the requested resource or an error message
    • Handling different HTTP methods like GET, POST, and DELETE
    • Support for cookies and session management
  • โœ… Server Configuration:

    • Using a configuration file to set up the server
    • Configuring server properties such as port and host
    • Defining server names, error pages, and other settings
    • Enabling/disabling directory listing and redirecting HTTP requests
  • โœ… File Handling:

    • Serving static websites and downloading files
    • Setting default files for directory requests
    • Executing CGI (Common Gateway Interface) scripts based on file extensions
    • Handling fragmented requests and CGI output
    • Multiple CGI management
  • โœ… Server Resilience:

    • Implementing non-blocking I/O using select(), poll(), epoll(), or equivalent
    • Ensuring the server does not block indefinitely
    • Stress testing the server for availability under high load

๐Ÿ˜Ž Team :

Fred, Jonathan