Skip to content

pullrequesta/http-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

http-go

http-go is my own implementation of HTTP/1.1 server directly on top of Go’s standard TCP layer. This library covers all essential parts of HTTP/1.1 protocol, including parsing the request line and headers, according to RFC 9110 and RFC 9112. It handles the message body, either Content-Length or transfer-chunked encoding behaviour. Furthermore, it implements the structured handling system to customize the respective response error type. The library also supports proxy requests.

Warning

This is not a comprehensive library, e.g. it is not able to parse multi-line headers.

Project Structure

http-go/
 ├── cmd/httpserver/
 │    └── main.go         # Entry point for running the HTTP server
 │── internal/ 
 │    ├── server/         # Listens, connection loop, handler dispatch
 │    ├── constants.go    # Contains the definition of consants used for the internal package
 │    ├── headers.go      # Creates the generic header map for parsing the headers
 │    ├── http_message.go # Creates an interface for the HTTP message, reads the HTTP message
 │    ├── parser.go       # Parses the HTTP message based on the state of the parser
 │    ├── request.go      # Parses the request
 │    ├── response.go     # Parses the response, writes the response and performes chunked encoding
 └── README.md

✨ Features

Supported Transport Potocols

  • TCP
  • UDP

📚 RFC References

  • RFC 7231 – An active and widely referenced RFC.
  • RFC 9112 – Easier to read than RFC 7231, relies on understanding from RFC 9110.
  • RFC 9110 – Covers HTTP "semantics."
  • RFC 2616 – Deprecated by RFC 7231.

🔧 HTTP Stack Components

  • Custom request parser using a state machine
  • Support for request line, headers, CRLF parsing, and message bodies
  • Custom response writer, including:
    • Status line
    • Headers
    • Body
    • Chunked body
  • Chunked Transfer-Encoding writer:
    • Writes chunk size as a hexa decimal number
    • Writes data in the form of chunks
    • Writes terminating line
  • Proxy Handler

🚀 Getting Started

  1. Install Go
Ensure Go ≥ 1.20 is installed.
  1. Run the HTTP server
go run cmd/httpserver/main.go
  1. Make a request by using curl command
curl -v localhost:42069
  1. Raw HTTP over netcat (nc)
echo -e "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n" | nc localhost 42069
  1. Make a proxy request
curl -v localhost:42069/httpbin/stream/10

📤 Example HTTP Response

A typical HTTP response looks like:

HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 13
 
Hello World!

A chunked HTTP response looks like:

HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked

7
Welcome
1c
to Mozilla Developer Network
0

🎓 Learning Source: Boot.dev HTTP Server Course

This project was built while following the Boot.dev "Build an HTTP Server in Go" course: BOOT.DEV

Note

Also this doc is not generated by AI, i like using emojis. 😉

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages