This is a basic HTTP server written in Go. It listens on a specified port and handles various HTTP requests, including serving files, echoing content, and returning the User-Agent string.
- Echo Endpoint: Returns the content appended to
/echo/
in the URL. - User-Agent Endpoint: Returns the User-Agent string from the request.
- File Serving: Serves files from a specified directory. Supports
GET
andPOST
methods. - Gzip Compression: Supports gzip compression based on the
Accept-Encoding
header.
To start the server, run the following command:
go run main.go [directory]
- Directory (optional): The directory from which to serve files. If not specified, the current directory will be used.
- URL:
/echo/{content}
- Method:
GET
- Description: Returns the
{content}
appended to the URL.
- URL:
/user-agent
- Method:
GET
- Description: Returns the User-Agent string from the request.
- URL:
/files/{filename}
- Method:
- GET: Serves the specified file.
- POST: Saves the content in the request body to the specified file.
- Description: Serves or saves files in the specified directory.
curl http://localhost:4221/echo/hello
hello
curl http://localhost:4221/user-agent
curl/7.68.0
curl http://localhost:4221/files/example.txt
curl -X POST -H "Content-Type: text/plain" -d "This is a test" http://localhost:4221/files/example.txt
Here's a brief overview of the code structure:
- main.go: The main entry point of the server.
- main(): Starts the server and listens on the specified port.
- handleConnection(conn net.Conn): Handles incoming connections.
- parseRequest(conn net.Conn): Parses the HTTP request.
- getStatus(statusCode int, statusText string): Returns the HTTP status line.
- getDirectoryFromArgs(): Returns the directory from which to serve files, defaulting to the current directory if not specified.
- Returns 400 Bad Request for malformed requests.
- Returns 404 Not Found for unknown paths or missing files.
- Returns 405 Method Not Allowed for unsupported methods.
- Returns 500 Internal Server Error for server-side errors during file operations.
The server supports gzip compression if the Accept-Encoding
header includes gzip
. Compressed responses include the Content-Encoding: gzip
header.
This project is licensed under the MIT License.