Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,12 @@ func trimHeader(size int, data []byte) []byte {
return data
}
data = data[size:]
data = bytes.TrimSpace(data)
data = bytes.TrimSuffix(data, []byte("\n"))
if len(data) > 0 && data[0] == ' ' {
data = data[1:]
}
if len(data) > 0 && data[len(data)-1] == '\n' {
data = data[:len(data)-1]
}
return data
}

Expand Down
19 changes: 19 additions & 0 deletions sse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,25 @@ func TestEventSourceNoRetryRequired(t *testing.T) {
assertEqual(t, true, strings.Contains(err.Error(), "400 Bad Request"))
}

func TestGH1044TrimHeader(t *testing.T) {
t.Run("data is nil", func(t *testing.T) {
result := trimHeader(0, nil)
assertNil(t, result)
})

t.Run("data has double whitespace", func(t *testing.T) {
data := []byte("data: double whitespace message")
result := trimHeader(5, data)
assertEqual(t, true, result[0] == ' ')
})

t.Run("data has newline", func(t *testing.T) {
data := []byte("data: newline message\n")
result := trimHeader(5, data)
assertEqual(t, true, result[len(result)-1] != '\n')
})
}

func TestEventSourceHTTPError(t *testing.T) {
es := createEventSource(t, "", func(any) {}, nil)
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) {
Expand Down