Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

C++ HTTP Server

A lightweight HTTP server implemented in C++ supporting GET and POST requests, file handling, echo functionality, and gzip compression. Built for learning and experimentation with socket programming and HTTP protocols.


Setup (PORT 4221)

g++ -std=c++17 -pthread -lz -o server server.cpp ./server [--directory /path/to/base]


Features

1. HTTP Request Handling

  • Supports GET and POST methods.
  • Parses HTTP request headers and body.
  • Supports standard HTTP headers including Content-Length, Accept-Encoding, and Connection.

2. Routing

  • / : Returns a simple 200 OK response.
  • /echo/<message> : Returns the message part of the path as the response body.
  • /user-agent : Returns the value of the User-Agent header.
  • /files/<filename> :
    • GET: Reads and returns file content from the server directory.
    • POST: Writes request body to a file in the server directory.

3. File Handling

  • Supports reading and writing files in the server’s base directory.
  • Uses binary-safe I/O for file content.
  • Returns 404 Not Found if a file is missing.

4. Gzip Compression

  • Automatically compresses response body if client includes Accept-Encoding: gzip.
  • Adds Content-Encoding: gzip header for compressed responses.
  • Uses zlib for GZIP compression.

5. Connection Management

  • Handles multiple clients concurrently using threads.
  • Supports Connection: close header to properly close client connections.
  • Safely handles client disconnects and socket errors.

6. Custom Base Directory

  • Default base directory is the current directory (.).
  • Can be overridden using command line argument:
./server --directory /path/to/base

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages