A simple HTTP server built from scratch using TCP primitives. This project demonstrates how to build a basic HTTP server step by step in an iterative way.
src/main/java/org/example/server/- Server implementationHttpServer.java- Main server class that listens for connectionsRequestHandler.java- Interface for handling HTTP requestsHttpRequest.java- Class representing an HTTP requestHttpResponse.java- Class representing an HTTP responseSimpleRequestHandler.java- A simple implementation of RequestHandler
src/main/java/org/example/App.java- Main application classsrc/test/java/org/example/server/HttpServerTest.java- Tests for the server
This project is set up for a solid development loop with the following features:
mvn clean compilemvn testmvn exec:javaOr with a custom port:
mvn exec:java -Dexec.args="8081"mvn packageThis will create an executable JAR file in the target directory that includes all dependencies.
java -jar target/basic-http-server-1.0-SNAPSHOT.jarOr with a custom port:
java -jar target/basic-http-server-1.0-SNAPSHOT.jar 8081- Make changes to the code
- Run tests to verify your changes:
mvn test - Run the application to see it in action:
mvn exec:java - Access the server in your browser at http://localhost:8080
- Repeat
Here are some ideas for extending the server:
- Add support for different HTTP methods (PUT, DELETE, etc.)
- Implement routing to handle different paths
- Add support for serving static files
- Implement HTTP status codes for different scenarios
- Add support for cookies and sessions
- Implement middleware for request processing
- Add support for JSON responses
- Implement a simple template engine for HTML responses
The application uses SLF4J with Logback for logging. The log configuration is in src/main/resources/logback.xml.
- Console logs show basic information
- File logs are stored in the
logsdirectory - The default log level is INFO, with DEBUG level for the application code
Feel free to fork this project and submit pull requests with improvements or bug fixes.