A lightweight, custom HTTP server implementation written in Java that provides basic HTTP/1.1 functionality with a simple routing system.
- HTTP/1.1 Protocol Support: Handles standard HTTP requests and responses
- Multi-threaded: Uses a thread pool to handle concurrent connections
- Custom HTTP Parser: Implements HTTP message parsing without external dependencies
- Routing System: Simple route-based request handling
- Error Handling: Comprehensive error responses for various HTTP status codes
The server is built with a modular architecture:
- HTTP Layer: Core HTTP protocol implementation (parsing, responses, status codes)
- Routing: Simple route matching and handler delegation
- Handlers: Request processing logic
- Server: Connection management and threading
- Java 8 or higher
- Compile the project:
javac -cp . com/example/httpserver/*.java com/example/httpserver/**/*.java- Run the server:
java com.example.httpserver.HttpServerApp- The server will start on port 4221
| Method | Path | Description |
|---|---|---|
| GET | /hello |
Returns "Hello, World!" message |
GET /hello HTTP/1.1
Host: localhost:4221HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 13
Hello, World!To add new routes, modify the Router constructor:
public Router() {
addRoute("/hello", HttpMethod.GET, new HelloWorldHandler());
addRoute("/custom", HttpMethod.GET, new CustomHandler());
}Create custom handlers by implementing the RequestHandler interface:
public class CustomHandler implements RequestHandler {
@Override
public HttpResponse handle(HttpRequest request) {
// Your custom logic here
return new HttpResponse.Builder(HttpStatus.SUCCESS_200_OK)
.withHeader("Content-Type", "application/json")
.withBody("{\"message\": \"Custom response\"}")
.build();
}
}com.example.httpserver/
├── HttpServerApp.java # Main application entry point
├── handlers/
│ ├── RequestHandler.java # Handler interface
│ └── HelloWorldHandler.java # Default hello world handler
├── http/
│ ├── HttpMethod.java # HTTP method enum
│ ├── HttpParser.java # HTTP message parser
│ ├── HttpParsingException.java
│ ├── HttpRequest.java # HTTP request model
│ ├── HttpResponse.java # HTTP response model
│ ├── HttpStatus.java # HTTP status codes
│ ├── HttpVersion.java # HTTP version enum
│ └── HttpWriter.java # HTTP response writer
├── routing/
│ └── Router.java # Request routing logic
└── server/
├── ConnectionHandler.java # Individual connection handler
└── Server.java # Main server class
The server provides comprehensive error handling:
- Parsing Errors: Invalid HTTP format returns 400 Bad Request
- Unsupported Methods: Returns 501 Not Implemented
- Route Not Found: Returns 404 Not Found
- Method Not Allowed: Returns 405 Method Not Allowed
- Internal Errors: Returns 500 Internal Server Error
The application uses SLF4J for logging. Configure your preferred logging implementation (Logback, Log4j, etc.) to see server logs.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is open source and available under the MIT License.