Skip to content

Commit 3b41b01

Browse files
committed
fix: remove only one whitespace from data prefix #1044 (#1075)
1 parent 1a30b66 commit 3b41b01

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

sse.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,12 @@ func trimHeader(size int, data []byte) []byte {
745745
return data
746746
}
747747
data = data[size:]
748-
data = bytes.TrimSpace(data)
749-
data = bytes.TrimSuffix(data, []byte("\n"))
748+
if len(data) > 0 && data[0] == ' ' {
749+
data = data[1:]
750+
}
751+
if len(data) > 0 && data[len(data)-1] == '\n' {
752+
data = data[:len(data)-1]
753+
}
750754
return data
751755
}
752756

sse_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,25 @@ func TestEventSourceNoRetryRequired(t *testing.T) {
323323
assertEqual(t, true, strings.Contains(err.Error(), "400 Bad Request"))
324324
}
325325

326+
func TestGH1044TrimHeader(t *testing.T) {
327+
t.Run("data is nil", func(t *testing.T) {
328+
result := trimHeader(0, nil)
329+
assertNil(t, result)
330+
})
331+
332+
t.Run("data has double whitespace", func(t *testing.T) {
333+
data := []byte("data: double whitespace message")
334+
result := trimHeader(5, data)
335+
assertEqual(t, true, result[0] == ' ')
336+
})
337+
338+
t.Run("data has newline", func(t *testing.T) {
339+
data := []byte("data: newline message\n")
340+
result := trimHeader(5, data)
341+
assertEqual(t, true, result[len(result)-1] != '\n')
342+
})
343+
}
344+
326345
func TestEventSourceHTTPError(t *testing.T) {
327346
es := createEventSource(t, "", func(any) {}, nil)
328347
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)