diff --git a/.gitignore b/.gitignore index 21ac4c1..3503fea 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,5 @@ *.class *.test - +.idea gor diff --git a/Makefile b/Makefile index 6c79473..45fa054 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ drace: docker run -v `pwd`:$(SOURCE_PATH) -t -i --env GORACE="halt_on_error=1" gor go test ./... $(ARGS) -v -race -timeout 15s dtest: - docker run -v `pwd`:$(SOURCE_PATH) -t -i gor go test ./ -timeout 60s $(ARGS) -v + docker run -v `pwd`:$(SOURCE_PATH) -t -i gor go test ./... -timeout 60s $(ARGS) -v dcover: docker run -v `pwd`:$(SOURCE_PATH) -t -i --env GORACE="halt_on_error=1" gor go test $(ARGS) -race -v -timeout 15s -coverprofile=coverage.out diff --git a/proto/proto.go b/proto/proto.go index 82a2f32..7e9609e 100644 --- a/proto/proto.go +++ b/proto/proto.go @@ -55,7 +55,13 @@ func header(payload []byte, name []byte) (value []byte, headerStart, valueStart, if payload[valueStart] == ' ' { // Ignore empty space after ':' valueStart++ } - headerEnd = valueStart + bytes.IndexByte(payload[valueStart:], '\r') + + headerEnd = valueStart + bytes.IndexByte(payload[valueStart:], '\n') + + if payload[headerEnd - 1] == '\r' { + headerEnd -= 1 + } + value = payload[valueStart:headerEnd] return diff --git a/proto/proto_test.go b/proto/proto_test.go index 80adbe2..1126c13 100644 --- a/proto/proto_test.go +++ b/proto/proto_test.go @@ -9,18 +9,36 @@ func TestHeader(t *testing.T) { var payload, val []byte var headerStart int + // Value with space at start payload = []byte("POST /post HTTP/1.1\r\nContent-Length: 7\r\nHost: www.w3.org\r\n\r\na=1&b=2") if val = Header(payload, []byte("Content-Length")); !bytes.Equal(val, []byte("7")) { t.Error("Should find header value") } + // Value without space at start payload = []byte("POST /post HTTP/1.1\r\nContent-Length:7\r\nHost: www.w3.org\r\n\r\na=1&b=2") if val = Header(payload, []byte("Content-Length")); !bytes.Equal(val, []byte("7")) { t.Error("Should find header value without space after :") } + // Value is empty + payload = []byte("GET /p HTTP/1.1\r\nCookie:\r\nHost: www.w3.org\r\n\r\n") + + if val = Header(payload, []byte("Cookie")); len(val) > 0 { + t.Error("Should return empty value") + } + + // Wrong delimeter + payload = []byte("GET /p HTTP/1.1\r\nCookie: 123\nHost: www.w3.org\r\n\r\n") + + if val = Header(payload, []byte("Cookie")); !bytes.Equal(val, []byte("123")) { + t.Error("Should handle wrong header delimeter") + } + + + // Header not found if _, headerStart, _, _ = header(payload, []byte("Not-Found")); headerStart != -1 { t.Error("Should not found header") }