A lightweight, concurrent HTTP/1.1 server implementation written in Go. This project interacts directly with the TCP layer, handling raw byte streams to parse HTTP requests and generate compliant responses without relying on high-level net/http handler abstractions. It is designed to serve static files, echo request data, and handle concurrent client connections via Go routines.
The server listens on port 4221 and implements the following endpoints and behaviors:
/: Returns a standard 200 OK status./echo/{string}: Returns the path parameter as the response body.- Supports Gzip compression if the
Accept-Encoding: gzipheader is present.
- Supports Gzip compression if the
/user-agent: Reads and returns the value of theUser-Agentheader from the client request./files/{filename}:GET: Reads the specified file from the server's configured directory and returns it withapplication/octet-stream.POST: Creates a new file (or overwrites an existing one) with the request body data.
- Persistent Connections: Supports HTTP Keep-Alive by processing multiple requests on a single connection loop until a
Connection: closeheader is detected.
The codebase demonstrates several low-level networking and system programming techniques:
- Raw TCP Socket Management: Utilizes the Go
netpackage to listen on TCP sockets and accept incoming connections, bypassing the standard HTTP multiplexer. - Manual Protocol Parsing: Implements a custom parser using
bufioto read the HTTP Request Line and Headers directly from the wire. This includes handling CRLF delimiters and parsingContent-Lengthto delimit body payloads. - Concurrency: Spawns lightweight Go routines for every incoming connection, ensuring the main listener remains unblocked.
- Compression Negotiation: Manually checks
Accept-Encodingheaders and utilizes agzip.Writerbuffer to compress response bodies when requested by the client. - CLI Flag Parsing: Uses the
flagpackage to accept directory configurations at runtime (e.g.,--directory /tmp).
This project relies on the Go standard library:
- net: Provides the portable interface for network I/O, including TCP/IP listeners.
- bufio: Implements buffered I/O, essential for reading variable-length HTTP lines efficiently.
- compress/gzip: Implements reading and writing of gzip format compressed files.
- os: Provides a platform-independent interface to operating system functionality, used here for file reading/writing.
- bytes: Implements functions for the manipulation of byte slices, used for buffer management during compression.
.
└── app/