HTTP is the protocol that powers the web. This is an HTTP server that's capable of handling simple GET/POST requests, serving files, and handling multiple concurrent connections.
Along the way, I learnt about:
- TCP connections
- HTTP headers
- HTTP verbs (GET, POST)
- Handling multiple connections
- Serving files from a directory
- Gzip compression
- Handle GET and POST requests: The server can handle basic GET and POST requests.
- Serve files: The server can serve files from a specified directory.
- Concurrent connections: The server can handle multiple connections simultaneously using goroutines.
- Gzip compression: The server supports gzip compression for responses when requested by the client.
- Go (version 1.16 or later)
-
Clone the repository:
git clone https://github.com/mivige/ServerHTTP.git cd ServerHTTP -
Build the project:
go build -o server.go
-
Run the server with the
--directoryflag to specify the directory to serve files from:./server.go --directory /path/to/your/directory
-
The server will start listening on port
4221.
To get a simple response:
curl -v http://localhost:4221/To echo a message:
curl -v http://localhost:4221/echo/your-messageTo get the user-agent:
curl -v http://localhost:4221/user-agentTo get a file:
curl -v http://localhost:4221/files/your-filenameTo create a file:
curl -v --data "file content" -H "Content-Type: application/octet-stream" http://localhost:4221/files/your-filenameTo request gzip compression:
curl -v -H "Accept-Encoding: gzip" http://localhost:4221/echo/your-messageThe main function sets up the server, parses command-line arguments, and starts listening for connections.
The handleConnection function handles individual connections, parses requests, and constructs appropriate responses.
The handleFileCreation function handles file creation for POST requests.
The handleFileRequest function handles file requests for GET requests.
The compressGzip function compresses data using gzip.
Contributions are welcome! Please feel free to submit a Pull Request.
This project was made following the CodeCrafters challenge: Build your own HTTP server.