From 09f47b25bf8ebca697b265460f8706c44c1aa1ac Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Thu, 20 Jul 2017 21:02:07 +0300 Subject: [PATCH] Fix panic for non compilant http requests --- proto/proto.go | 16 ++++++++++++++-- proto/proto_test.go | 12 ++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/proto/proto.go b/proto/proto.go index 0bd83dc..31b8507 100644 --- a/proto/proto.go +++ b/proto/proto.go @@ -346,8 +346,20 @@ func Path(payload []byte) []byte { eol := bytes.IndexByte(payload[start:], '\r') end := bytes.IndexByte(payload[start:], ' ') - if eol > 0 && eol < end { - return payload[start : start + eol] + if eol > 0 { + if end == -1 || eol < end { + return payload[start : start + eol] + } + } else { // support for legacy clients + eol = bytes.IndexByte(payload[start:], '\n') + + if eol > 0 && (end == - 1 || eol < end) { + return payload[start : start + eol] + } + } + + if end < 0 { + return payload[start: len(payload)] } return payload[start : start+end] diff --git a/proto/proto_test.go b/proto/proto_test.go index 4ff2dc5..210a9c1 100644 --- a/proto/proto_test.go +++ b/proto/proto_test.go @@ -243,6 +243,18 @@ func TestPath(t *testing.T) { if path = Path(payload); !bytes.Equal(path, []byte("/get")) { t.Error("Should find path", string(path)) } + + payload = []byte("GET /get\n") + + if path = Path(payload); !bytes.Equal(path, []byte("/get")) { + t.Error("Should find path", string(path)) + } + + payload = []byte("GET /get") + + if path = Path(payload); !bytes.Equal(path, []byte("/get")) { + t.Error("Should find path", string(path)) + } } func TestSetPath(t *testing.T) {