From 03767d1a778f2bf594765e9ce5e5d46ff4d97ea6 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Tue, 27 Jun 2017 14:24:43 +0200 Subject: [PATCH] Add support for HTTP 1.0 and less --- proto/proto.go | 5 +++-- proto/proto_test.go | 14 ++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/proto/proto.go b/proto/proto.go index aa4f129..1ef2449 100644 --- a/proto/proto.go +++ b/proto/proto.go @@ -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] diff --git a/proto/proto_test.go b/proto/proto_test.go index 58e6062..4ff2dc5 100644 --- a/proto/proto_test.go +++ b/proto/proto_test.go @@ -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]) } } @@ -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]) } } @@ -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) {