Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
run:
go run ./cmd/tcplistener/
go run ./cmd/tcplistener/ | tee tcplistener.log
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# RawHTTP
HTTP 1.1 server from scratch

## HTTP Message Structure

According to RFC 7230, HTTP messages follow this structure:

```
start-line CRLF
*( field-line CRLF )
*( field-line CRLF )
...
CRLF
[ message-body ]
```

Where:
- **start-line**: Request line (method, URI, version) or status line
- **field-line**: HTTP headers (key-value pairs) (The RFC uses the term)
- **CRLF**: Carriage return + line feed (`\r\n`)
- **message-body**: Optional request/response body
61 changes: 13 additions & 48 deletions cmd/tcplistener/main.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,11 @@
package main

import (
"RAWHTTP/internal/request"
"fmt"
"io"
"net"
"strings"
)

func getLinesChannel(f io.ReadCloser) <-chan string {
lines := make(chan string)

go func() {
defer f.Close()
defer close(lines)

currentLine := ""

for {
read := make([]byte, 8)
_, err := f.Read(read)

if err == io.EOF {
// Send the last line if it has content
if currentLine != "" {
lines <- currentLine
}
return
}

if err != nil {
fmt.Println("Some Error Happened While Putting in Slice")
return
}

parts := strings.Split(string(read), "\n")

// Process all parts except the last one, which may be incomplete
if len(parts)-1 > 0 {
currentLine += parts[0]
lines <- currentLine
currentLine = "" // Reset for next line
}

// The last part, which may be incomplete gets added to currentLine
currentLine += parts[len(parts)-1]
}
}()

return lines
}

func main() {
listener, err := net.Listen("tcp", ":42069")

Expand All @@ -66,10 +22,19 @@ func main() {
}

go func(c net.Conn) {
lines := getLinesChannel(conn)
req, err := request.RequestFromReader(conn)

if err != nil {
fmt.Println("error happened", err.Error())
}

for line := range lines {
fmt.Println(line)
fmt.Println("Request line:")
fmt.Println("Method: ", req.RequestLine.Method)
fmt.Println("Http Version: ", req.RequestLine.HttpVersion)
fmt.Println("Target: ", req.RequestLine.RequestTarget)
fmt.Println("Headers")
for key, val := range req.Headers {
fmt.Println(key, ": ", val)
}
}(conn)
}
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module RAWHTTP

go 1.25.3

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.11.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
88 changes: 88 additions & 0 deletions internal/headers/headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package headers

import (
"errors"
"strings"
"unicode"
)

const FieldLineSeperator = ":"
const crlf = "\r\n"

type Headers map[string]string

// there can be an unlimited amount of whitespace
// before and after the field-value (Header value). However, when parsing a field-name,
// there must be no spaces betwixt the colon and the field-name. In other words,
// these are valid:

// 'Host: localhost:42069'
// ' Host: localhost:42069 '

// But this is not:

// Host : localhost:42069

func (h Headers) Parse(data []byte) (n int, done bool, err error) {
endIdx := strings.Index(string(data), crlf)
if endIdx == -1 {
return 0, false, nil
}
// if encounter \r\n in front of line that means we consumed all the headers/fieldLines
if endIdx == 0 {
return endIdx + 2, true, nil
}

currentLine := string(data[:endIdx])
fieldParts := strings.SplitN(currentLine, FieldLineSeperator, 2)

if len(fieldParts) != 2 {
return 0, false, errors.New("field-line have wrong number of parts")
}

fieldName := strings.TrimLeft(fieldParts[0], " ")
if len(fieldName) != len(strings.TrimSpace(fieldName)) {
return 0, false, errors.New("error in field-name syntax, whitespace unexpected")
}
if !isValidFieldName(fieldName) {
return 0, false, errors.New("invalid characters in field-name")
}

fieldValue := strings.TrimSpace(strings.Trim(fieldParts[1], crlf))
// lowercase the fieldname while adding to the map
val, exists := h[strings.ToLower(fieldName)]
if exists {
h[strings.ToLower(fieldName)] = val + "," + fieldValue
} else {
h[strings.ToLower(fieldName)] = fieldValue
}

return endIdx + 2, false, nil
}

func isValidFieldName(fieldName string) bool {
allowedSpecials := map[rune]bool{
'!': true, '#': true, '$': true, '%': true, '&': true, '\'': true,
'*': true, '+': true, '-': true, '.': true, '^': true, '_': true,
'`': true, '|': true, '~': true,
}

if len(fieldName) == 0 {
return false
}

for _, ch := range fieldName {
switch {
case unicode.IsLetter(ch):
continue
case unicode.IsDigit(ch):
continue
case allowedSpecials[ch]:
continue
default:
return false
}
}

return true
}
57 changes: 57 additions & 0 deletions internal/headers/headers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package headers

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHeadersParser(t *testing.T) {
// Test: Valid single header
headers := make(Headers)
data := []byte("host: localhost:42069\r\n\r\n")
n, done, err := headers.Parse(data)
require.NoError(t, err)
require.NotNil(t, headers)
assert.Equal(t, "localhost:42069", headers["host"])
assert.Equal(t, 23, n)
assert.False(t, done)

// Test: Invalid spacing header
headers = make(Headers)
data = []byte(" Host : localhost:42069 \r\n\r\n")
n, done, err = headers.Parse(data)
require.Error(t, err)
assert.Equal(t, 0, n)
assert.False(t, done)

// Test: Invalid character in header
headers = make(Headers)
data = []byte(" H(st : localhost:42069 \r\n\r\n")
n, done, err = headers.Parse(data)
require.Error(t, err)
assert.Equal(t, 0, n)
assert.False(t, done)

// Test: Uppercase FieldName should add as a lowercase key
headers = make(Headers)
data = []byte("Host: localhost:42069\r\n\r\n")
n, done, err = headers.Parse(data)
require.NoError(t, err)
require.NotNil(t, headers)
assert.Equal(t, "localhost:42069", headers["host"])
assert.Equal(t, 23, n)
assert.False(t, done)

// Test: multipe same fieldname should have values comma separated
headers = make(Headers)
data = []byte("Host: localhost:42069\r\nHost: localhost:42070\r\n\r\n")
bytesConsumed0, _, _ := headers.Parse(data)
bytesConsumed1, done, err := headers.Parse(data[bytesConsumed0:])
require.NoError(t, err)
require.NotNil(t, headers)
assert.Equal(t, "localhost:42069,localhost:42070", headers["host"])
assert.Equal(t, bytesConsumed0+bytesConsumed1, len(data)-2)
assert.False(t, done)
}
Loading