An open API service indexing awesome lists of open source software.

https://github.com/barcellos-pedro/java-http-server

Simple Web Server with zero dependencies. From TCP to HTTP.
https://github.com/barcellos-pedro/java-http-server

http java server study tcp

Last synced: about 1 month ago
JSON representation

Simple Web Server with zero dependencies. From TCP to HTTP.

Awesome Lists containing this project

README

          

# Simple Java HTTP Server

This project implements a minimal, educational HTTP server in Java.
The goal is to understand low-level request handling, routing, controllers, and response building without relying on large frameworks.

It includes:

- Socket-based HTTP server
- Basic router and mapping system
- Controllers implementing a `RequestHandler` interface
- Request parsing (method, path, headers, body)
- Response builder with support for multiple formats
- A complete class diagram for architecture visibility

---

## πŸš€ Overview

The server listens on a configurable port and processes each incoming socket by:

1. Parsing the HTTP request into a `Request` object
2. Passing it to the `Router`
3. Resolving the appropriate `RequestHandler` via `Routes`
4. Building the HTTP response with `Response` helpers
5. Sending the result back through the socket

The structure is intentionally simple so you can extend it β€” adding controllers, middleware, or even template rendering.

---

## πŸ“¦ Project Structure

```text
java-echo-server/
β”œβ”€β”€ README.md
β”œβ”€β”€ src
β”‚Β Β  └── main
β”‚Β Β  └── java
β”‚Β Β  └── com
β”‚Β Β  └── server
β”‚Β Β  β”œβ”€β”€ controller
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ EchoController.java
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ GreetingController.java
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ NotFoundController.java
β”‚Β Β  β”‚Β Β  └── RequestHandler.java
β”‚Β Β  β”œβ”€β”€ http
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ HttpMethod.java
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ HttpServer.java
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ Request.java
β”‚Β Β  β”‚Β Β  └── Response.java
β”‚Β Β  β”œβ”€β”€ Main.java
β”‚Β Β  β”œβ”€β”€ router
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ Router.java
β”‚Β Β  β”‚Β Β  └── Routes.java
β”‚Β Β  └── utils
β”‚Β Β  └── ResponseTemplate.java
└── tests
β”œβ”€β”€ load_get_greeting.sh
β”œβ”€β”€ load_post_message.sh
└── run_all_load_tests.sh
```

## 🧩 Class Diagram

Below is a full Mermaid UML diagram representing the architecture:

```mermaid
sequenceDiagram
autonumber

participant Client
participant HttpServer
participant Router
participant Routes
participant Controller
participant Response

Client ->> HttpServer: HTTP request
HttpServer ->> Router: handle(socket)
Router ->> Routes: getOrNotFound(request)
Routes -->> Router: handler
Router ->> Controller: handle(request)
Controller -->> Router: body
Router ->> Response: build response
Response -->> Router: http string
Router -->> Client: http response
```

## 🧱Architecture Overview

```mermaid
flowchart LR

Client["Client(curl / browser)"]

subgraph Server["Java Echo Server"]
HttpServer["HttpServer(Socket listener)"]
Router["Router(Request dispatcher)"]
Routes["Routes(Path β†’ Handler mapping)"]

subgraph Controllers
Greeting["GreetingController"]
Echo["EchoController"]
NotFound["NotFoundController"]
end

subgraph HTTP
Request["Request(Parsed HTTP)"]
Response["Response(HTTP builder)"]
Templates["ResponseTemplate"]
end
end

Client --> HttpServer
HttpServer --> Router
Router --> Request
Router --> Routes
Routes --> Greeting
Routes --> Echo
Routes --> NotFound
Router --> Response
Response --> Templates
Response --> Client
```