Skip to content

A simple multi-client HTTP server built in C++ using POSIX sockets and `select()`, supporting static file serving, JSON responses, POST handling, and request logging.

Notifications You must be signed in to change notification settings

s1upee/http-server1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Raw HTTP Server in C++ (macOS, POSIX Sockets, select())

📌 Overview

This project is a basic HTTP server implemented in C++, using POSIX sockets and the select() system call to handle multiple clients concurrently. It supports GET and POST requests, serves static files, and logs requests for debugging.

📅 Developed during Winter Break, December 2024 📅
🔍 Purpose: Educational project to learn about networking and server-side development in C++.

🔧 Features

  • Multi-client handling using select()
  • Static file serving (index.html, styles.css, etc.)
  • Dynamic responses (/time, /echo)
  • Basic request logging (logs/access.log)
  • Graceful shutdown (SIGINT handling)

🛠️ Project Structure

/http-server
│── src/
│   ├── main.cpp  (Entry point, starts the server)
│   ├── server.cpp  (Socket handling and select() loop)
│   ├── request.cpp  (HTTP request parsing)
│   ├── response.cpp  (Constructing HTTP responses)
│── public/  (Static files directory)
│   ├── index.html
│   ├── styles.css
│── logs/
│   ├── access.log
│── Makefile  (For simple compilation)
│── README.md  (This file)

🚀 Installation & Running

1️⃣ Clone the Repository

git clone <repository-url>
cd http-server

2️⃣ Compile the Server

make

3️⃣ Run the Server

make run

Expected output:

🚀 Server running on http://127.0.0.1:8080

4️⃣ Test the Endpoints

Static File Request

curl http://localhost:8080/index.html

Dynamic JSON Response

curl http://localhost:8080/time

Expected Output:

{"time": "yourtime"}

POST Request to /echo

curl -X POST http://localhost:8080/echo -d "Hello, yourname!"

Expected Output:

Hello, yourname!

📜 Logging

All requests are logged in logs/access.log in this format:

[timestamp] IP:PORT METHOD PATH STATUS_CODE

Example:

[2025-03-07 15:00:00] 127.0.0.1:54321 GET /index.html 200

❌ Stopping the Server

Press Ctrl + C to stop the server gracefully.

📩 Contact

If you have any questions, feel free to reach out! 😊
🔗 GitHub: s1upee
🔗 Email: lisakrasiuk@gmail.com

About

A simple multi-client HTTP server built in C++ using POSIX sockets and `select()`, supporting static file serving, JSON responses, POST handling, and request logging.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors