Skip to content

Thamidu-Nadun/tinyWebC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Web Server From Scratch

This is a lightweight HTTP web server built in C from the ground up. If you want to learn how web servers work under the hood this is the resource that you're looking for!

✨ Features

Currently this server support multiple features such as,

  • 📁 Serve Static Files — Effortlessly serve HTML, CSS, JavaScript, images, and more
  • 🎯 MIME Type Support — Automatic content-type detection for proper file rendering
  • ⚙️ Custom Port Configuration — Launch your server on any port you prefer
  • 💻 Lightweight — Minimal dependencies, pure C implementation
  • 🔧 Easy to Use — Simple command-line interface with intuitive options

🚀 Quick Start

Prerequisites

  • GCC compiler
  • Linux/Unix environment (uses POSIX.1)

Building

gcc -o web_server main.c

Running

./web_server -p 3000

Then open your browser and visit:

http://localhost:3000

📂 Project Structure

web_server/
├── main.c              # Core server implementation
├── public/             # Static files directory
│   ├── index.html      # Home page
│   ├── style.css       # Styling
│   └── script.js       # Frontend logic
└── utils/
    └── filehelper.c    # Utility functions for file handling & MIME types

🔍 How It Works

  1. Server Setup — Binds to a TCP socket and listens for incoming connections
  2. Request Parsing — Reads HTTP requests from clients
  3. File Serving — Locates requested files in the public/ directory
  4. MIME Detection — Automatically detects file type and sets correct content-type headers
  5. Response Sending — Sends HTTP responses with proper headers and file content

📝 Key Features Explained

Auto MIME Type Detection

The server intelligently detects file types:

  • .html, .htmtext/html
  • .csstext/css
  • .jsapplication/javascript
  • .png, .jpg, .gif → Image types
  • And many more...

Custom Port Support

Start your server on any port with the -p flag:

./web_server -p 5000

🛠️ Building Blocks

  • Socket Programming — TCP/IP communication
  • HTTP Protocol — Request/response handling
  • File I/O — Reading and serving file content
  • String Manipulation — Parsing HTTP requests and building responses
  • Memory Management — Efficient allocation and cleanup

🤝 Contributing

I'll add these features later but you are feel free to extend this project! Some ideas such as:

  • Add support for multiple concurrent clients (threading)
  • Implement POST request handling
  • Add basic routing
  • Support for CGI scripts
  • Performance optimizations

📜 License

This project is provided as-is for educational purposes.


Made with passion for learning low-level networking!

About

A simple web server in C, It can server files, mime type and other general web server functionalities

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors