Skip to content

Mark-Vu/simple-go-http-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Go Simple HTTP Server

This project implements a minimal HTTP/1.1 server in Go without using net/http.
It handles raw TCP connections, manually parses HTTP requests, and supports file serving, echo endpoints, and persistent connections.

Features

  • Raw TCP-based HTTP/1.1 implementation
  • Persistent connections (keep-alive by default)
  • Concurrent handling via goroutines
  • Endpoints:
    • GET / — simple OK response
    • GET /echo/{msg} — returns plain text
    • GET /user-agent — returns client's User-Agent header
    • GET /files/{filename} — serves files from a directory
    • POST /files/{filename} — writes request body to a file
  • Automatic Content-Length, Content-Type, and Connection headers
  • Graceful handling of Connection: close

Usage

Start the server:

go run main.go --directory ./files/

The server listens on:

0.0.0.0:4221

Example Requests Echo:

curl http://localhost:4221/echo/hello
User-Agent:

curl http://localhost:4221/user-agent -H "User-Agent: test-agent"

Serve a File:

echo -n "Hello" > files/foo.txt
curl http://localhost:4221/files/foo.txt

Upload a File (POST):

curl -X POST --data "12345" http://localhost:4221/files/newfile

How it works

  1. Uses net.Listen and conn.Read to accept raw TCP requests
  2. Splits HTTP request into request line, headers, and body
  3. Processes multiple requests per connection (HTTP/1.1) 4.Routes requests using manual string parsing
  4. Sends fully manual HTTP responses:
Status Line
Headers
Body

This project is for learning low-level HTTP mechanics and building a deeper understanding of how servers work under the hood

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages