This project is a simple HTTP server implemented in Java. It handles HTTP requests, serves static files, and supports dynamic request handling through custom handlers.
src/com/unsubble/assets/: Contains static HTML files.404.html: Custom 404 error page.index.html: Home page.
src/com/unsubble/core/: Core server components.AbstractHandler.java: Base class for request handlers.ConnectionHandler.java: Handles client connections.Handler.java: Functional interface for request handlers.HttpRequestParser.java: Parses HTTP requests.HttpResponseBuilder.java: Builds HTTP responses.HttpServer.java: Main server class.Main.java: Entry point of the application.RequestHandler.java: Annotation for request handlers.Router.java: Manages routing of requests.
src/com/unsubble/handlers/: Custom request handlers.CommonHandler.java: Handles common requests.ConfigHandler.java: Handles configuration files.StaticFileHandler.java: Serves static files.
src/com/unsubble/models/: HTTP request and response models.HttpHeader.java: Represents an HTTP header.HttpMethod.java: Enum for HTTP methods.HttpRequest.java: Represents an HTTP request.HttpResponse.java: Represents an HTTP response.HttpStatus.java: Enum for HTTP status codes.
src/com/unsubble/utils/: Utility classes.ObjToString.java: Utility for converting objects to strings.ReflectionUtil.java: Utility for reflection operations.
- Java Development Kit (JDK) 11 or higher
- IntelliJ IDEA or any other Java IDE
- Clone the repository.
- Open the project in your IDE.
- Run the
Mainclass located insrc/com/unsubble/core/Main.java. - The server will start on port 4444 by default.
You can provide configuration files in XML format to customize the server. Place the configuration files in the project directory and specify their paths when creating the HttpServer instance.
To add custom request handlers:
- Create a new class that extends
AbstractHandler. - Annotate the class with
@RequestHandlerand specify the path and supported methods. - Implement the
handlemethod to process the request.
@RequestHandler(value = "/custom", supportedMethods = {HttpMethod.GET})
public class CustomHandler extends AbstractHandler {
@Override
public HttpResponse handle(HttpRequest request) {
return new HttpResponseBuilder()
.status(HttpStatus.OK)
.body("Custom handler response")
.build();
}
}This project is licensed under the MIT License. See the LICENSE file for details.