Skip to content

A lightweight HTTP/1.1 server built from scratch in Java 21

Notifications You must be signed in to change notification settings

DereckTav/Java-HTTP-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java HTTP Server

A lightweight, RFC-compliant HTTP/1.1 server built from scratch in Java. Designed as a standalone logging service that external applications can use to persist log entries remotely.


Features

Feature Description
Hand-rolled HTTP Parser Byte-by-byte parsing of request-line, headers, and body following HTTP/1.1 spec
Virtual Threads Leverages Java 21 virtual threads for efficient concurrent connection handling
Streaming Body Reader Memory-efficient 8-byte chunked body reading with consumer callbacks
JSON Configuration Fully configurable via server.json — ports, timeouts, allowed methods/headers
Custom Exception Typed HTTP exceptions mapped directly to proper status codes
File-based Logging Endpoint POST log entries organized by application name and log file

Tech Stack

  • Java 21 — Virtual threads, records, pattern matching
  • Maven — Build tooling
  • Jackson — JSON configuration parsing
  • SLF4J + Log4j2 — Structured logging
  • JUnit 5 — Testing framework

Architecture


HTTP Parser Deep Dive

The parser is the core of this project — a byte-level state machine that strictly follows HTTP/1.1 semantics.

Request-Line Parsing

GET /api/logs HTTP/1.1\r\n
│   │         │
│   │         └─► Version validation (only HTTP/1.1 supported)
│   └───────────► Request target extraction with length limits
└───────────────► Method validation against configured allowlist

Key Features:

  • Configurable method allowlist — Only parses methods you explicitly support
  • CRLF enforcement — Strictly validates \r\n sequences per spec

Body Handling

// Lazy streaming — body isn't read until handler requests it
request.getBodyReader().ifPresent(reader -> 
    reader.readTo(chunk -> processChunk(chunk))
);

File Organization:

server_logs/
├── my-app/
│   ├── errors.txt      ← Log-At: errors
│   └── access.txt      ← Log-At: access
└── other-app/
    └── debug.txt

Quick Start

Prerequisites

  • Java 21+
  • Maven 3.6+

Run the Server

# Clone and build
mvn clean compile

# Start server
mvn exec:java -Dexec.mainClass="org.http_servers.HttpServer"

# Server starts on http://localhost:8080

Test Endpoints

# Health check
curl http://localhost:8080/

# Post a log entry
curl -X POST http://localhost:8080/ \
  -H "Application: test-app" \
  -H "Log-At: events" \
  -H "Content-Type: text/plain" \
  -H "Content-Length: 11" \
  -d "Hello World"

Made to understand HTTP

About

A lightweight HTTP/1.1 server built from scratch in Java 21

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages