Skip to content

Zurard/HTTP-server

Repository files navigation

Multi-threaded C-HTTP Server

A high-performance HTTP/1.1 server built from the ground up in C. This project handles concurrent connections using threads and features a custom-built compression engine that implements LZ77 and Huffman Coding to mimic GZIP.


Quick Start

1. Compile the Server

You will need gcc and the pthread library (standard on Linux/macOS):

gcc -o server main.c -lpthread

2. Run the Server

Specify the directory where you want to store or serve files:

./server --directory .

The server will start listening on port 4221.


Manual Testing (Telnet)

You can test the raw HTTP interaction by using telnet. This allows you to see exactly how the server parses headers and sends responses.

Perform a GET Request

Connect to the server:

telnet localhost 4221

Then paste the following and press Enter twice:

GET /echo/hello_world HTTP/1.1
Host: localhost:4221

Perform a POST Request (File Upload)

Connect to the server:

telnet localhost 4221

Then paste the following and press Enter:

POST /files/note.txt HTTP/1.1
Host: localhost:4221
Content-Length: 13

Hello from C!

API Endpoints

Endpoint Method Result
/ GET Returns a simple 200 OK.
/echo/{str} GET Returns {str} in the response body.
/user-agent GET Returns the client's User-Agent string.
/files/{name} GET Downloads the specified file from the directory.
/files/{name} POST Saves the request body as a new file.

Core Features

Multi-threading

The server uses pthread_create for every incoming connection. This ensures that a large file download from one user does not block smaller or faster requests from other clients.

Custom Compression Pipeline

If a request includes the Accept-Encoding: gzip header, the server passes the response data through two compression stages:

  1. LZ77 A sliding window algorithm that replaces repeated substrings with distance/length pairs.

  2. Huffman Coding A frequency-based encoding system using a Min-Heap to build a prefix tree for bit-level compression.

Persistent Connections

The server respects the Connection: close header. If it is not present, the server attempts to keep the socket open for subsequent requests until a 15-second timeout is reached.


Requirements

  • GCC compiler
  • POSIX-compliant OS (Linux, Unix, or macOS)
  • pthread library

Created as a deep-dive into systems programming and network protocols.

About

Trying to create a HTTP server from scratch in C

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors