Skip to content

Commit

Permalink
Add support for HTTP 1.0 and less
Browse files Browse the repository at this point in the history
  • Loading branch information
buger committed Jun 27, 2017
1 parent 2680ce9 commit 03767d1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,11 @@ func Body(payload []byte) []byte {
// Path takes payload and retuns request path: Split(firstLine, ' ')[1]
func Path(payload []byte) []byte {
start := bytes.IndexByte(payload, ' ') + 1
eol := bytes.IndexByte(payload[start:], '\r')
end := bytes.IndexByte(payload[start:], ' ')

if len(payload) < start + end {
return []byte{}
if eol < end {
return payload[start : start + eol]
}

return payload[start : start+end]
Expand Down
14 changes: 10 additions & 4 deletions proto/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func TestHeader(t *testing.T) {
}

func TestMIMEHeadersEndPos(t *testing.T) {
head := []byte("POST /post HTTP/1.1\r\nContent-Length: 7\r\nHost: www.w3.org")
head := []byte("POST /post HTTP/1.1\r\nContent-Length: 7\r\nHost: www.w3.org\r\n\r\n")
payload := []byte("POST /post HTTP/1.1\r\nContent-Length: 7\r\nHost: www.w3.org\r\n\r\na=1&b=2")

end := MIMEHeadersEndPos(payload)

if !bytes.Equal(payload[:end], head) {
t.Error("Wrong headers end position:", end)
t.Error("Wrong headers end position:", end, head, payload[:end])
}
}

Expand All @@ -80,10 +80,10 @@ func TestMIMEHeadersStartPos(t *testing.T) {
payload := []byte("POST /post HTTP/1.1\r\nContent-Length: 7\r\nHost: www.w3.org\r\n\r\na=1&b=2")

start := MIMEHeadersStartPos(payload)
end := MIMEHeadersEndPos(payload)
end := MIMEHeadersEndPos(payload) - 4

if !bytes.Equal(payload[start:end], headers) {
t.Error("Wrong headers end position:", start, end)
t.Error("Wrong headers end position:", start, end, payload[start:end])
}
}

Expand Down Expand Up @@ -237,6 +237,12 @@ func TestPath(t *testing.T) {
if path = Path(payload); !bytes.Equal(path, []byte("/post")) {
t.Error("Should find path", string(path))
}

payload = []byte("GET /get\r\n\r\nHost: www.w3.org\r\n\r\n")

if path = Path(payload); !bytes.Equal(path, []byte("/get")) {
t.Error("Should find path", string(path))
}
}

func TestSetPath(t *testing.T) {
Expand Down

0 comments on commit 03767d1

Please sign in to comment.