Skip to content

sahitya-chandra/http-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

HTTP server from scratch

A simple static file web server built from scratch in C++ using low-level socket programming.
This project demonstrates how web servers work under the hood by manually handling TCP connections and parsing HTTP requests.


🌐 Features

  • βœ… Serves static files from the public/ directory
  • βœ… Parses raw HTTP GET requests
  • βœ… Dynamically detects and serves correct MIME types
  • βœ… Custom 404 page support (public/404.html)
  • βœ… Handles multiple clients using fork()
  • βœ… Written with only standard C/C++ libraries (no external dependencies)

πŸ“ File Structure

http-server-from-scratch/
β”œβ”€β”€ server.cpp # Core server logic
β”œβ”€β”€ public/ # Static files served by the server
β”‚ β”œβ”€β”€ index.html
β”‚ β”œβ”€β”€ style.css
β”‚ └── 404.html
β”œβ”€β”€ Makefile # Build instructions
└── README.md # This file

βš™οΈ Build Instructions

βœ… Requirements

  • Linux (e.g., Ubuntu)
  • g++ compiler

πŸ› οΈ Build & Run

make
./server

Then open your browser or run:

curl http://localhost:8080/

How It Works:

  • Server binds to port 8080
  • Listens for TCP connections
  • Forks a child process to handle each incoming client
  • Reads the HTTP request line
  • Extracts the file path and serves it if available
  • Sets appropriate Content-Type based on file extension
  • Returns a 404 page if the file isn’t found

Future Enhancements

  • Support HTTP POST and other methods
  • Add threading instead of fork() for concurrency
  • Log requests and status codes
  • Add command-line options for port or root directory
  • HTTP/1.1 features (Keep-Alive, headers)

Built as a learning project to understand low-level networking and HTTP. Feel free to fork, modify, or suggest improvements!

About

HTTP SERVER 101

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages