Skip to content

Bmogul/c-http

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

HTTP Server in C

A basic HTTP/1.1 server implementation written in C that handles multiple concurrent connections using poll-based I/O multiplexing.

Overview

This project implements a simple HTTP server from scratch in C, demonstrating low-level network programming and HTTP protocol handling. The server can handle basic GET and POST requests, serve files, echo messages, and parse HTTP headers.

Technologies & Concepts Learned

Network Programming

  • Berkeley Sockets API - Socket creation, binding, listening, and accepting connections
  • TCP/IP - Understanding the transport layer protocol
  • IPv4/IPv6 - Dual-stack socket addressing with sockaddr_storage
  • poll() - Event-driven I/O multiplexing for handling multiple concurrent connections
  • SO_REUSEADDR - Socket option to allow address reuse

HTTP Protocol

  • HTTP/1.1 - Request/response structure and status codes
  • HTTP Methods - Handling GET and POST requests
  • HTTP Headers - Parsing headers (User-Agent, Content-Length, Content-Type)
  • MIME Types - Setting appropriate content types (text/plain, application/octet-stream)

C Programming

  • String manipulation - strtok(), strstr(), sscanf(), snprintf()
  • Memory management - Dynamic allocation with malloc(), realloc(), strdup(), and free()
  • Signal handling - Graceful shutdown with SIGINT, SIGTERM, SIGHUP, SIGQUIT
  • File I/O - Reading and writing files with fopen(), fgets(), fprintf()
  • Command-line arguments - Parsing arguments for directory configuration

Features

  • Route handling:

    • GET / - Returns 200 OK
    • GET /echo/{message} - Echoes back the message
    • GET /user-agent - Returns the User-Agent header value
    • GET /files/{filename} - Serves files from a specified directory
    • POST /files/{filename} - Creates files in the specified directory
  • Concurrent connections - Uses poll() to handle multiple clients simultaneously

  • Dynamic scaling - Doubles the client capacity when max connections are reached

  • Graceful shutdown - Handles termination signals to close sockets properly

Usage

# Compile
gcc -o server server.c

# Run without file serving
./server

# Run with file directory
./server --directory /path/to/files

The server listens on port 4221 by default.

Learning Outcomes

This project provided hands-on experience with:

  • Low-level network programming and socket API
  • HTTP protocol implementation from scratch
  • Event-driven programming with poll()
  • Manual memory management in C
  • Parsing and handling text-based protocols
  • Building a real-world server application

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages