Fix panic for non compilant http requests

This commit is contained in:
Leonid Bugaev
2017-07-20 21:02:07 +03:00
parent 652e589e2b
commit 09f47b25bf
2 changed files with 26 additions and 2 deletions
+14 -2
View File
@@ -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]
+12
View File
@@ -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) {