A high-performance, concurrent HTTP/1.1 server implementation in Go, built from the ground up following RFC 2616 specifications. This project demonstrates deep understanding of network programming, TCP/IP protocols, and Go's concurrency patterns.
- HTTP/1.1 Protocol Implementation: Adheres to core aspects of the HTTP/1.1 specification (RFC 2616).
- Concurrent Request Handling: Leverages Go's concurrency model (goroutines) to efficiently handle multiple client connections simultaneously.
- Custom Router: Implements a basic but effective path-based routing system to direct requests to appropriate handlers.
- RESTful Endpoints: Provides several standard endpoints:
GET /
: Basic health check.GET /echo/{message}
: Echoes back the provided message in the response body.GET /user-agent
: Returns the client'sUser-Agent
header value.GET /files/{filename}
: Serves static files from a specified directory.POST /files/{filename}
: Allows clients to upload files to the server's specified directory.
- Static File Serving & Upload: Capable of serving existing files and accepting file uploads with appropriate
Content-Type
handling. - Header Parsing: Correctly parses HTTP request headers, including
User-Agent
,Content-Length
,Content-Type
, andConnection
. - HTTP Compression: Supports
gzip
compression based on theAccept-Encoding
request header. - Robust Error Handling: Returns appropriate HTTP status codes (e.g., 200 OK, 201 Created, 404 Not Found, 500 Internal Server Error) and informative error messages.
- Language: Go (Golang)
- Testing: Go testing package
- Network: Native Go TCP/IP stack
- Protocols: HTTP/1.1
- Core Libraries: Go Standard Library (
net
,bufio
,os
,fmt
,strings
,sync
,compress/gzip
)
The project follows clean architecture principles with clear separation of concerns:
├── app/
│ ├── main.go # Entry point
│ └── main_test.go # Integration tests
├── internal/
│ ├── handlers/ # Request handlers
│ ├── protocol/ # HTTP protocol implementation
Follow these steps to get the server running on your local machine.
Prerequisites:
- Go (Version 1.18+ Recommended)
- Git (for cloning the repository)
Steps:
-
Clone the repository:
git clone https://github.com/md-talim/codecrafters-http-server-go.git # Or: git clone [Your Fork URL]
-
Navigate to the project directory:
cd codecrafters-http-server-go
-
Build the application:
go build -o http-server-go ./app/main.go
This will create an executable file named
http-server-go
(orhttp-server-go.exe
on Windows) in the current directory. -
Environment Variables:
- No specific environment variables are required for basic operation. Configuration is primarily handled via command-line arguments (see Configuration).
-
Run the application:
./http-server-go [--directory /path/to/serve/files]
- Replace
/path/to/serve/files
with the actual directory you want the server to use for file operations (GET/POST/files/...
). If omitted, file operations might default to the current working directory or be disabled depending on implementation details. - The server will start listening on
0.0.0.0:4221
.
- Replace
Once the server is running, you can interact with it using an HTTP client like curl
or a web browser.
-
Health Check:
curl -v http://localhost:4221/
Expected Response:
HTTP/1.1 200 OK
-
Echo Service:
curl -v http://localhost:4221/echo/hello-world
Expected Response:
HTTP/1.1 200 OK
withhello-world
in the body. -
User-Agent:
curl -v --header "User-Agent: foobar/1.2.3" http://localhost:4221/user-agent
Expected Response:
HTTP/1.1 200 OK
withfoobar/1.2.3
in the body. -
Get File (Requires
--directory
flag during startup):# Assuming a file named 'example.txt' exists in the specified directory curl -v http://localhost:4221/files/example.txt
Expected Response:
HTTP/1.1 200 OK
with the content ofexample.txt
. -
Upload File (Requires
--directory
flag during startup):curl -v --data "This is the file content." http://localhost:4221/files/newfile.txt
Expected Response:
HTTP/1.1 201 Created
-
Not Found:
curl -v http://localhost:4221/nonexistent-path
Expected Response:
HTTP/1.1 404 Not Found
Run the test suite:
go test ./app
This project was built as part of the "Build Your Own HTTP Server" challenge from CodeCrafters. Key learning resources:
- CodeCrafters for the project challenge and structure
- The Go team for excellent networking primitives and documentation