https://github.com/humankernel/http-server
HTTP Server from Scratch
https://github.com/humankernel/http-server
Last synced: 16 days ago
JSON representation
HTTP Server from Scratch
- Host: GitHub
- URL: https://github.com/humankernel/http-server
- Owner: humankernel
- Created: 2025-04-29T01:24:13.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-11-03T23:46:47.000Z (8 months ago)
- Last Synced: 2025-11-04T01:19:24.250Z (8 months ago)
- Language: Go
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# βοΈ Simple HTTP Server from Scratch (Go)
A minimal yet fully functional **HTTP/1.1 server built from scratch in Go**, without using `net/http` for handling requests or responses.
This project was inspired by the [Codecrafters HTTP Server Challenge](https://app.codecrafters.io/courses/http-server) and rebuilt from the ground up to deepen understanding of low-level networking and the HTTP protocol.
## π Features
β
**Manual TCP Handling** β Uses Goβs `net` package directly (`net.Listen`, `net.Conn`) to accept and manage client connections.
β
**HTTP/1.1 Parsing** β Parses request lines, headers, and bodies manually via buffered I/O.
β
**Persistent Connections** β Supports `Connection: keep-alive` and `Connection: close`.
β
**Custom Routing** β Handles multiple routes without using frameworks:
* `/` β Simple health check.
* `/echo/` β Responds with the given text.
* `/user-agent` β Returns the clientβs `User-Agent` header.
* `/files/` β
β’ `GET`: Serves static files from a provided directory.
β’ `POST`: Writes file contents to disk.
β
**Gzip Compression** β Compresses responses when the client includes `Accept-Encoding: gzip`.
β
**File I/O Handling** β Secure path resolution and support for reading/writing binary files.
β
**Proper Status Codes** β Returns `200 OK`, `201 Created`, `404 Not Found`, `500 Internal Server Error`, etc.
β
**Concurrent Connections** β Handles multiple clients via goroutines.
β
**Verbose Logging** β Logs request lifecycle and connection details.
## π§© Getting Started
### 1. Clone the repo
```bash
git clone https://github.com/humankernel/http-server.git
cd http-server
```
### 2. Run the server
You must provide a file directory path for `/files` route:
```bash
go run app/main.go ./tmp
```
### 3. Try some requests
```bash
# Echo
curl -v http://localhost:4221/echo/hello
# User-Agent
curl -v http://localhost:4221/user-agent
# Upload a file
curl -v -X POST --data-binary @example.txt http://localhost:4221/files/example.txt
# Download a file
curl -v http://localhost:4221/files/example.txt
```
### 4. Observe raw traffic (optional)
```bash
sudo tcpdump -i any -n host localhost
```
---
### π Example Response
```
> GET /echo/hi HTTP/1.1
> Host: localhost:4221
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Content-Length: 2
hi
```
## π§ Project Structure
```
http-server/
βββ app/
β βββ main.go # Core server implementation
βββ tmp/ # Example directory for /files route
βββ README.md
```
## π‘ Future Improvements
* Add proper routing abstraction (similar to `http.ServeMux`)
* Implement chunked transfer encoding and streaming responses
* Improve security and header handling