4 min read
HTTP Server From Scratch

progress-banner

Project Overview

Ever wondered how web servers handle the requests your browser sends? To deepen my understanding of fundamental web protocols and network programming, I took on the Codecrafters challenge: building a functional HTTP/1.1 server entirely from scratch using Java and low-level TCP primitives.

This project involved stepping away from high-level frameworks to directly implement core server functionalities, demonstrating a strong grasp of network communication, concurrency, and the HTTP protocol itself.

Role: Personal Project (Codecrafters Challenge)

Goal: Implement an HTTP/1.1 compliant server capable of handling concurrent connections, routing requests, serving files, and supporting features like compression, purely using standard Java libraries.

Key Features & My Contributions

I successfully implemented a range of essential HTTP server features:

  • Core HTTP/1.1 Handling: Built logic to parse incoming HTTP requests and construct valid HTTP responses according to RFC 2616 specifications.
  • Concurrent Connections: Designed and implemented a multi-threaded architecture to efficiently manage multiple simultaneous client connections without blocking.
  • Request Routing: Developed a flexible routing system to direct incoming requests based on their path (e.g., /, /echo/*, /files/*) to the appropriate handlers.
  • Static File Operations: Implemented functionality to serve static files from a specified directory (GET /files/{filename}) and handle file uploads (POST /files/{filename}), including correct Content-Type management.
  • Header Processing: Added logic to parse and utilize key HTTP headers like User-Agent, Content-Length, Content-Type, Connection, and Accept-Encoding.
  • Gzip Compression: Implemented content compression using gzip when requested by the client via the Accept-Encoding header, optimizing data transfer.
  • RESTful Endpoints: Created handlers for various endpoints, including echoing messages (/echo/{message}) and returning client information (/user-agent).
  • Robust Error Handling: Ensured the server responds with appropriate HTTP status codes (200, 201, 404, 500, etc.) for different scenarios.

Technical Approach & Architecture

  • Technology: Built entirely with Java (JDK 23), leveraging core libraries like java.net for TCP socket programming and Java’s built-in multi-threading capabilities. No external web server frameworks were used.
  • Build Tool: Utilized Maven for dependency management (though none were external) and build automation.
  • Design: Employed a modular design with clear separation of concerns (request parsing, response building, routing, request handling) to ensure maintainability and clarity. Key components included HttpRequest, HttpResponse, Router, and specific RequestHandler implementations.

Challenges & Solutions

Building a server from scratch presented interesting challenges:

  1. Concurrency Management: Ensuring the server could handle multiple clients simultaneously without data corruption or deadlocks required careful implementation of a thread-per-connection model and proper resource management (e.g., using try-with-resources for sockets).
  2. HTTP Protocol Adherence: Precisely parsing varying request formats and constructing specification-compliant responses, including correct header handling and status codes, demanded meticulous attention to the RFC 2616 details.
  3. Secure File Handling: Implementing file serving and uploads required validation of file paths to prevent directory traversal vulnerabilities and correctly determining Content-Type for served files.

Skills Demonstrated

  • Network Programming: TCP/IP Sockets, Client-Server Communication (Java java.net)
  • HTTP Protocol: Deep understanding and implementation of HTTP/1.1 (Request/Response Structure, Methods, Headers, Status Codes)
  • Concurrent Programming: Multi-threading, Thread Management, Synchronization Basics
  • Core Java: File I/O (NIO), Exception Handling, Object-Oriented Design
  • Software Design: Modular Architecture, Interface-based Design, Separation of Concerns
  • Build Tools: Maven