Skip to content

zatrange/webserver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Webserv

A lightweight, high-performance HTTP web server implemented in C++98, designed to handle multiple concurrent connections using an event-driven architecture with poll().

Features

  • HTTP Methods Support: GET, POST, DELETE
  • CGI Support: Execute Python (.py) and PHP (.php) scripts
  • Chunked Transfer Encoding: Efficient handling of large data transfers
  • File Uploads: Support for multipart/form-data uploads with configurable temporary paths
  • Autoindex: Automatic directory listing when enabled
  • Configuration File: Nginx-style configuration syntax
  • Multiple Servers: Support for multiple server blocks on different ports
  • Client Body Size Limits: Configurable maximum request body size
  • Timeout Handling: Connection timeout configuration
  • Cookies Support: Session management and cookie handling
  • Error Pages: Custom error responses

Requirements

  • C++98 compatible compiler (tested with g++)
  • POSIX compliant system (Linux/macOS)
  • Standard C++ libraries

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd webserv
  2. Build the project:

    make
  3. The executable webserv will be created in the current directory.

Usage

Run the web server with a configuration file:

./webserv [configuration_file]

If no configuration file is provided, it defaults to webserv.conf.

Example

./webserv webserv.conf

The server will start listening on the configured ports (default: 8080).

Configuration

The server uses a configuration file similar to Nginx. The default configuration file is webserv.conf.

Basic Server Block

server {
    root website;
    host localhost;
    index index.html;
    listen 8080;
    timeout 1;
    autoindex on;
    client_max_body_size 100m;

    location / {
        index index.html;
        allowed_methods GET POST DELETE;
        autoindex on;
        root ./website;
        cgi_pass .py;
        cgi_pass .php;
        client_body_temp_path ./website/uploads;
    }
}

Configuration Directives

  • root: Root directory for serving files
  • host: Server hostname
  • index: Default index file(s)
  • listen: Port to listen on
  • timeout: Connection timeout in seconds
  • autoindex: Enable/disable directory listing (on/off)
  • client_max_body_size: Maximum request body size
  • allowed_methods: Permitted HTTP methods for the location
  • cgi_pass: File extensions to pass to CGI
  • client_body_temp_path: Temporary path for file uploads

Directory Structure

.
├── main.cpp                 # Main entry point
├── Makefile                 # Build configuration
├── webserv.conf            # Default configuration file
├── includes/               # Header files
│   ├── Webserv.hpp
│   ├── Server.hpp
│   ├── Client.hpp
│   ├── Request.hpp
│   ├── Respond.hpp
│   └── ...
└── src/                    # Source files
    ├── RequestRespond/     # Request/response handling
    └── server/             # Server implementation

Testing

  1. Create a website directory with your HTML files
  2. Configure the server in webserv.conf
  3. Run the server: ./webserv
  4. Access via browser: http://localhost:8080

CGI Support

The server supports CGI for Python and PHP scripts. Configure CGI extensions in your location blocks:

location /cgi-bin/ {
    cgi_pass .py;
    cgi_pass .php;
}

File Uploads

File uploads are supported with configurable temporary paths:

client_body_temp_path ./website/uploads;

Error Handling

The server provides appropriate HTTP status codes and error messages for various scenarios including:

  • 403 Forbidden
  • 404 Not Found
  • 405 Method Not Allowed
  • 413 Request Entity Too Large
  • 500 Internal Server Error

Architecture

  • Event-Driven: Uses poll() for handling multiple connections efficiently
  • Modular Design: Separate modules for request parsing, response generation, and server management
  • Configuration Parser: Custom parser for the configuration file syntax
  • Memory Management: Careful memory handling to prevent leaks

About

webserver

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors