GHTTP is an educational HTTP server built entirely from scratch in Go using raw TCP sockets (net.Listen). It was created for learning purposes to understand the inner workings of the HTTP protocol, connection management, and routing.
- Raw TCP Sockets: No
net/httpused! Everything is parsed and written directly from/to TCP connections. - Radix Tree Routing: Efficient routing with support for dynamic path parameters (e.g.,
/users/:id). - HTTP Keep-Alive: Reuses TCP connections for sequential requests to maximize throughput.
- Chunked Transfer Streaming: Automatically uses
Transfer-Encoding: chunkedfor true streaming when noContent-Lengthis provided. - Query Parameter Parsing: Extracts URL query strings (e.g.,
?q=search) seamlessly. - Middleware Support: Includes custom
LoggerandRecovery(Panic catching) middleware. - Security & Timeouts: Implements socket-level read/write deadlines and provides
context.Contextfor request cancellation.
main.go- Entry point. Sets up the router, registers middlewares and routes, and starts the server.server.go- Handles the underlying TCP listener and spawns goroutines for incoming connections.handler.go- The core HTTP parser and response writer. Handles Keep-Alive loops, chunked encoding, and HTTP formatting.router.go- A custom Radix Tree (Trie) implementation for performant, parameterized route matching.middleware.go- Request interceptors for logging and panic recovery (returning 500 status codes).routes.go- Example endpoint definitions.types.go- Core struct definitions (Request,Response,Handler).
-
Clone the repository and navigate to the project directory:
cd ghttp -
Run the server:
go run . -
Test it out using
curlor your browser:Basic Request
curl -v http://localhost:8080/
Dynamic Path Parameters
curl -v http://localhost:8080/users/123
Query Parameters
curl -v "http://localhost:8080/users/123?q=search"Panic Recovery Test
curl -v http://localhost:8080/panic
This project demonstrates how Go's powerful standard library features like bufio.Reader, io.Reader, and net.Conn can be used to construct a fully functional web server without relying on the heavily optimized net/http package.
Created for learning and exploration.