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
28 changes: 18 additions & 10 deletions internal/headers/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package headers
import (
"bytes"
"fmt"
"log/slog"
"sort"
"strconv"
"strings"
)

Expand Down Expand Up @@ -40,6 +40,12 @@ type Headers struct {
headers map[string]string
}

func NewHeaders() *Headers {
return &Headers{
headers: map[string]string{},
}
}

func (r *Headers) ForEach(cb func(n, v string)) {
keys := make([]string, 0, len(r.headers))
for k := range r.headers {
Expand All @@ -52,10 +58,18 @@ func (r *Headers) ForEach(cb func(n, v string)) {
}
}

func NewHeaders() *Headers {
return &Headers{
headers: map[string]string{},
func (h *Headers) GetInt(name string, defaultValue int) int {
value, exists := h.Get(name)
if !exists {
return defaultValue
}

v, err := strconv.Atoi(value)
if err != nil {
return defaultValue
}

return v
}

func (h *Headers) Set(name, value string) {
Expand All @@ -73,13 +87,10 @@ func (h *Headers) Get(name string) (string, bool) {
return "", false
}

// slog.Info("getHeader", "name", name, "value", v)

return v, ok
}

func (h *Headers) parseHeader(fieldLine []byte) (string, string, error) {
slog.Info("parseHeader", "fieldLine", string(fieldLine))
parts := bytes.SplitN(fieldLine, []byte(":"), 2)
if len(parts) != 2 {
return "", "", ERR_BAD_HEADER
Expand All @@ -91,7 +102,6 @@ func (h *Headers) parseHeader(fieldLine []byte) (string, string, error) {
return "", "", ERR_BAD_HEADER
}

// slog.Info("header", "name", string(fieldName), "value", string(fieldValue))
return string(fieldName), string(fieldValue), nil
}

Expand All @@ -105,8 +115,6 @@ func (h *Headers) Parse(data []byte) (int, bool, error) {
break
}

slog.Info("parse header", "read", read)

// Empty header
if idx == 0 {
done = true
Expand Down
44 changes: 17 additions & 27 deletions internal/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"fmt"
"io"
"log/slog"
"strconv"

"github.com/merge/handly/internal/headers"
)
Expand Down Expand Up @@ -33,35 +31,23 @@ type RequestLine struct {
type Request struct {
RequestLine RequestLine
Headers *headers.Headers
Body string
body string
bodyBuffer []byte
bodyPos int
state parsetState
}

func newRequest() *Request {
return &Request{
Headers: headers.NewHeaders(),
state: StateInit,
Body: "",
}
}

func getInt(h *headers.Headers, name string, defaultValue int) int {
value, exists := h.Get(name)
if !exists {
return defaultValue
}

v, err := strconv.Atoi(value)
if err != nil {
return defaultValue
}

return v
}

func (r *Request) hasBody() bool {
length := getInt(r.Headers, "content-length", 0)
length := r.Headers.GetInt("content-length", 0)
return length > 0

}

func (r *Request) Parse(data []byte) (int, error) {
Expand Down Expand Up @@ -105,28 +91,34 @@ outer:
}

read += n
slog.Info("StateHeader", "read", read)

if done {
if r.hasBody() {
length := r.Headers.GetInt("content-length", 0)

r.bodyBuffer = make([]byte, length)
r.bodyPos = 0
r.state = StateBody
} else {
r.state = StateDone
}
}

case StateBody:
length := getInt(r.Headers, "content-length", 0)
length := r.Headers.GetInt("content-length", 0)
if length == 0 {
r.state = StateDone
break outer
}

slog.Info("StateBody", "length-leb(r.body)", length-len(r.Body), "length currentData", len(currentData), "currentData", currentData, "read", read)
remaining := min(length-len(r.Body), len(currentData))
r.Body += string(currentData[:remaining])
remaining := min(length-r.bodyPos, len(currentData))
copy(r.bodyBuffer[r.bodyPos:], currentData[:remaining])

r.bodyPos += remaining
read += remaining

if len(r.Body) == length {
if r.bodyPos == length {
r.body = string(r.bodyBuffer) // Single conversion at end
r.state = StateDone
}
case StateDone:
Expand All @@ -147,7 +139,6 @@ func parseRequestLine(data []byte) (*RequestLine, int, error) {
return nil, 0, nil
}

slog.Info("parseRequestLine", "data", string(data))
startOfLine := data[:idx]
read := idx + len(SEPARATOR)

Expand Down Expand Up @@ -181,7 +172,6 @@ func RequestFromReader(r io.Reader) (*Request, error) {

bufLen += n
readN, err := request.Parse(buf[:bufLen])
slog.Info("RequestFromHeader", "readN", readN, "bufLen", bufLen)
if err != nil {
return nil, err
}
Expand Down
26 changes: 13 additions & 13 deletions internal/request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ func TestParseBody(t *testing.T) {
require.NoError(t, err)

require.NotNil(t, r)
assert.Equal(t, "hello world!\n", string(r.Body))

// // Test: Body shorter than reported content length
// reader = &ChunkReader{
// data: "POST /submit HTTP/1.1\r\n" +
// "Host: localhost:3000\r\n" +
// "Content-Length: 20\r\n" +
// "\r\n" +
// "partial content",
// numOfBytesPerRead: 3,
// }
// r, err = RequestFromReader(reader)
// require.Error(t, err)
assert.Equal(t, "hello world!\n", string(r.body))

// Test: Body shorter than reported content length
reader = &ChunkReader{
data: "POST /submit HTTP/1.1\r\n" +
"Host: localhost:3000\r\n" +
"Content-Length: 20\r\n" +
"\r\n" +
"partial content",
numOfBytesPerRead: 3,
}
r, err = RequestFromReader(reader)
require.Error(t, err)
}