Welcome to the Codecrafters HTTP Server project! This repository contains a simple HTTP server implemented in Java as part of the Codecrafters challenge. This project demonstrates understanding of network programming, HTTP protocol, concurrent programming, and Java's NIO capabilities. This project is an excellent demonstration of my understanding of core Java concepts and my ability to build applications from scratch.
- HTTP/1.1 Protocol Implementation: Built following RFC 2616 specifications
- Concurrent Request Handling: Multi-threaded design to handle multiple client connections
- RESTful Endpoints:
GET /
: Basic health check endpointGET /echo/{message}
: Echo service that returns the messageGET /user-agent
: Returns client's User-AgentGET /files/{filename}
: Serves static filesPOST /files/{filename}
: Uploads files to server
- Static File Serving: Ability to serve and receive files with proper content types
- Custom Router Implementation: Flexible routing system with path-based handlers
- Error Handling: Proper HTTP status codes and error responses
- Java 23
- Maven for build automation
- Pure Java standard library (no external dependencies)
- TCP/IP Networking (java.net)
- Java NIO for file operations
- Multi-threading for concurrent connections
The server follows a modular design with clear separation of concerns:
- Main: Server bootstrapping and configuration
- Router: Request routing and handler management
- HttpRequest: HTTP request parsing and representation
- HttpResponse: Response building and formatting
- RequestHandler: Interface for endpoint handlers
- FilesHandler: File operations handler
src/main/java/
├── Main.java # Server entry point and configuration
├── Router.java # Request routing logic
├── HttpRequest.java # Request parsing and model
├── HttpResponse.java # Response building and model
├── RequestHandler.java # Handler interface
└── FilesHandler.java # File operations implementation
-
Network Programming
- Socket programming in Java
- TCP/IP communication
- HTTP protocol implementation
-
Concurrent Programming
- Multi-threaded server design
- Thread management
- Resource synchronization
-
Software Design
- Interface-based design
- Separation of concerns
- Modular architecture
-
File System Operations
- File I/O using Java NIO
- Content type handling
- File upload/download operations
- Clone the repository
git clone https://github.com/Md-Talim/codecrafters-http-server-java.git
cd codecrafters-http-server-java
- Build the project
mvn clean package
- Run the server
./your_program.sh [port] [directory]
Test the server using curl commands:
# Test echo endpoint
curl -v http://localhost:4221/echo/hello
# Test file upload
curl -v --data-binary "@file.txt" http://localhost:4221/files/file.txt
# Test file download
curl -v http://localhost:4221/files/file.txt
-
Concurrent Connection Handling
- Implemented thread-per-connection model
- Used try-with-resources for proper resource cleanup
-
HTTP Protocol Compliance
- Careful implementation of request parsing
- Proper header handling
- Status code management
-
File System Security
- Path validation
- Content-Type determination
- Error handling for file operations