Skip to content
shineyrbl-bytePublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

TitanHTTP

A lightweight, multithreaded HTTP/1.1 web server built from scratch in C++ using POSIX sockets.

Overview

TitanHTTP is a custom HTTP server implemented in C++ to explore how web servers work at the TCP and HTTP layers.

The server accepts TCP connections, parses incoming HTTP requests, validates them, serves static files, constructs HTTP responses, and sends them reliably to clients.

It uses a fixed-size thread pool to handle multiple client connections concurrently.

Quick Start

Clone

git clone https://github.com/shineyrbl-byte/TitanHTTP.git cd TitanHTTP

Compile

TitanHTTP can be compiled using Clang: clang++ src/main.cpp ThreadPool.cpp HTTPRequest.cpp HTTPResponse.cpp Logger.cpp -o server

Run

./server The server starts listening on port 8080.

Open in a Browser

Visit: http://localhost:8080 You can also test the server using tools such as curl and netcat.

Requirements

  • C++17 or later
  • Clang or another C++17-compatible compiler
  • POSIX-compatible operating system

Features

  • TCP socket-based HTTP server using POSIX sockets

  • HTTP/1.1 request parsing

  • HTTP request header parsing

  • GET request handling

  • Static file serving

  • MIME type detection

  • Content-Length response header

  • Content-Type response header

  • Connection: close response handling

  • Reliable transmission using a sendAll() loop

  • Request size limit to prevent excessively large requests

  • HTTP error responses:

    • 400 Bad Request
    • 404 Not Found
    • 405 Method Not Allowed
    • 505 HTTP Version Not Supported
  • Fixed-size thread pool for concurrent clients

  • Thread-safe request logging

  • Support for HTML, CSS, JavaScript, PNG, JPEG, GIF, SVG and text files

Architecture

                    Client
                       |
                       | TCP connection
                       v
                +-------------+
                |   Server    |
                |   accept()  |
                +-------------+
                       |
                       v
                +-------------+
                | Thread Pool |
                +-------------+
                       |
              +--------+--------+
              |        |        |
              v        v        v
           Worker   Worker   Worker
              |
              v
        HTTP Request
              |
              v
       Request Parser
              |
              v
         Validation
              |
              v
        Static File
           Lookup
              |
        +-----+-----+
        |           |
       404          File
                    |
                    v
             HTTP Response
                    |
                    v
                 sendAll()
                    |
                    v
                  Client

Request Processing

A typical request follows this flow:

Client
  |
  v
TCP connection
  |
  v
recv()
  |
  v
HTTP request parser
  |
  +--> Validate request
  |
  +--> Check Host header
  |
  +--> Check HTTP method
  |
  +--> Check HTTP version
  |
  v
Map URL to file
  |
  v
Read static file
  |
  v
Build HTTP response
  |
  v
sendAll()
  |
  v
Close connection

Thread Pool

TitanHTTP uses a fixed number of worker threads rather than creating a new thread for every connection.

Incoming client sockets are added to a shared queue. Worker threads wait for connections and process them when available.

                 Client connections
                         |
                         v
                  Shared Queue
                  /     |     \
                 /      |      \
                v       v       v
            Worker 1 Worker 2 Worker 3
                \       |       /
                 \      |      /
                  HTTP request

This allows multiple clients to be processed concurrently while avoiding the overhead of continuously creating and destroying threads.

The server was also tested with a deliberately stalled TCP client using netcat while browser requests were being processed. Other worker threads continued serving requests, demonstrating the benefit of the thread-pool architecture.

Project Structure

TitanHTTP/
├── src/
│   └── main.cpp
│
├── public/
│   ├── index.html
│   ├── about.html
│   ├── style.css
│   └── script.js
│
├── ThreadPool.h
├── ThreadPool.cpp
├── HTTPRequest.h
├── HTTPRequest.cpp
├── HTTPResponse.h
├── HTTPResponse.cpp
├── Logger.h
├── Logger.cpp
├── .gitignore
└── README.md

Components

main.cpp

  • Creates and configures the TCP server socket
  • Accepts client connections
  • Receives HTTP requests
  • Coordinates request processing

ThreadPool

  • Maintains worker threads
  • Stores incoming client connections in a queue
  • Synchronizes workers using mutexes and condition variables

HTTPRequest

  • Parses the HTTP request line
  • Extracts method, path and HTTP version
  • Parses request headers

HTTPResponse

  • Builds valid HTTP responses
  • Calculates Content-Length
  • Sets appropriate Content-Type
  • Adds connection handling headers

Logger

  • Records incoming HTTP requests
  • Uses synchronization to prevent concurrent log output from interfering with each other

Building

TitanHTTP can be compiled using Clang on macOS:

clang++ src/main.cpp ThreadPool.cpp HTTPRequest.cpp HTTPResponse.cpp Logger.cpp -o server

Running

Start the server with:

./server

The server listens on:

http://localhost:8080

You can then open the URL in a web browser.

Example Requests

GET / HTTP/1.1
Host: localhost:8080
GET /about HTTP/1.1
Host: localhost:8080

Static assets such as CSS and JavaScript files can also be served from the public directory.

Error Handling

TitanHTTP handles several invalid or unsupported requests.

Situation Response
Malformed HTTP request 400 Bad Request
Missing Host header 400 Bad Request
Requested file does not exist 404 Not Found
Unsupported request method 405 Method Not Allowed
Unsupported HTTP version 505 HTTP Version Not Supported
Request exceeds 8192 bytes Connection closed

Testing

The server was tested using browser requests, curl, and netcat.

Tests included:

  • Normal GET requests
  • Static HTML, CSS and JavaScript files
  • Missing files
  • Unsupported HTTP methods
  • Malformed HTTP requests
  • Oversized requests
  • Large HTTP responses
  • Multiple simultaneous clients
  • A deliberately stalled TCP client
  • Thread-pool concurrency

Technologies

  • C++
  • POSIX sockets
  • TCP/IP
  • HTTP/1.1
  • C++ threads
  • Mutexes
  • Condition variables
  • Clang
  • macOS

Future Improvements

Possible future improvements include:

  • Persistent HTTP connections
  • Additional HTTP methods
  • More complete HTTP header handling
  • Socket timeouts
  • Improved security validation
  • Configurable server ports
  • More comprehensive automated tests
  • Graceful server shutdown
  • Request and response metrics

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages