Skip to content

HTTP server in C++98 - implemented sockets, request parsing, and multi-client handling. Demonstrates knowledge of networking, concurrency, and scalable backend design.

CodingOnBush/webserv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📚 What we learned in this project

  • Network Programming & Socket Management: Implemented low-level socket programming with system calls, managed client connections, and developed event-driven I/O using poll() for efficient concurrent handling
  • HTTP Protocol Implementation: Built a complete HTTP server from scratch, including request parsing, response generation, and proper handling of HTTP methods (GET, POST, DELETE) with standard compliance
  • Concurrent Connection Handling: Designed multi-client support using non-blocking I/O, efficient connection management, and timeout handling for scalable performance
  • Configuration Management & Parsing: Developed a custom configuration file parser similar to NGINX, supporting complex directives and nested server/location blocks with validation
  • CGI Integration & Process Management: Implemented dynamic content generation through CGI script execution with proper environment variable handling and process lifecycle management
  • Memory Management & Error Handling: Applied robust error handling with custom error pages, resource cleanup, and strict C++98 standard compliance for stability and performance

Webserv

Screenshot 2024-04-13 222920

Overview

This project is part of 42 school common core cursus. Webserv is a custom HTTP server inspired by NGINX and designed to handle HTTP requests and responses, including support for CGI scripts, file uploads, and most common HTTP methods. The project aims to provide a fully functional web server with configurable settings and error handling.

Features

  • HTTP Methods: Supports GET, POST and DELETE methods. Sending other types of request will result in 405 Method Not Allowed Error.
  • File Uploads: Handles file uploads to specified directories.
  • CGI Support: Executes CGI scripts for dynamic content.
  • Configuration: Customizable server settings via configuration files.
  • Error Handling: Default error pages for various HTTP status codes. Custom error pages can be defined as well.
  • Multiple Ports: Listens on multiple ports as specified in the configuration.

Directory Structure

  • bin/: Compiled object files
  • cgi-bin/: CGI scripts
  • config/: Configuration files
  • include/: Header files
  • src/: Source files
  • www/: Web content

Configuration

The server configuration is specified in .conf files located in the config directory. The default configuration file is default.conf.

Directives

Directive Syntax Example(s) Default Context Description
Listen listen listen localhost:8080;, listen 127.0.0.1:8081;, listen 8082; localhost:8080 server Sets host address and port for IP on which the server will accept requests. Both address and port, or only address or only port can be specified. An address may also be a hostname.
Server name server_name server_name example.com; - server Sets names of a virtual server. The first name becomes the primary server name.
Root root root ./www; ./www server, location Sets the root directory for requests.
Default error pages error_page error_page 404 /404.html; error_page 403 /403.html; - server, location Defines the URI to redirect to in case of a specified error code.
Client body size limit client_max_body_size client_max_body_size 1k; client_max_body_size 1m; server, location Sets the maximum allowed size of the client request body.
Allowed HTTP methods allowed_methods allowed_methods GET|POST|DELETE; - server, location Defines a list of accepted HTTP methods for the route.
Directory listing autoindex autoindex on; off server, location Turns on or off directory listing.
HTTP redirection return return 301 http://example.com; - server, location Stops processing and redirects to a specified source, returning a specified code to the client.
Index file index index new_index.html; index.html server, location Sets a default file to answer if the request is a directory.
Alias alias alias /var/www/html; - location Defines a directory or a file from where the file should be searched (a replacement for the specified location).
CGI cgi cgi .py .php; - server, location Specifies file extension(s) based on which CGI script can be executed for a given location.
Uploaded files location upload_location upload_location ./www/upload; ./www/upload location Makes the route able to accept uploaded files and configures where they should be saved.

Example Configuration file

server {
	listen 8080;
	server_name localhost;
	client_max_body_size 1k;

    location / {
	allowed_methods GET|POST|DELETE;
	root ./www;
	autoindex on;
    }

    location /upload {
	allowed_methods POST|DELETE;
	upload_location ./www/upload;
    }
}

Building the Project

To build the project, use the provided Makefile. Run the following command in the project root directory:

make

This will compile the source files and generate the webserv executable.

Running the Server

To run the server with the default configuration:

./webserv

To specify a custom configuration file:

./webserv path/to/config.conf

Handling Requests

GET Request

Handles static files and directory listings (if autoindex is enabled).

POST Request

Handles form submissions and file uploads.

DELETE Request

Deletes specified files from the server.

Error Handling

Custom error pages can be specified in the configuration file. Default error pages are provided for common HTTP status codes.

CGI Support

CGI scripts can be executed for dynamic content. The server will handle the execution of the script and return the output as the response.

Authors

Useful resources

Additional tips

For better understanding of the project, first of all, we recommend installing and configuring NGINX. It will help you to understand how a web server works and how to configure it.
As you explore resources with code examples, we encourage you to implement these examples yourself. Take the time to thoroughly understand each step by referring to the official documentation and man pages.

About

HTTP server in C++98 - implemented sockets, request parsing, and multi-client handling. Demonstrates knowledge of networking, concurrency, and scalable backend design.

Resources

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •