Skip to content

Commit 4a3daee

Browse files
jmallocneild
authored andcommitted
net/http/httputil: allow MIME parameters when detecting SSE in ReverseProxy
This change allows httputil.ReverseProxy to detect SSE (server-sent events) content when the response's Content-Type header includes MIME parameters, such as "text/event-stream;charset=utf-8". Prior to this change the value of the Content-Type header was compared directly to the literal "text/event-stream". This caused a false-negative which failed to set the FlushInterval correctly when MIME parameters were present. Change-Id: If8bb43efb78787b6519d7fe7599ca018a0da0023 GitHub-Last-Rev: 224518c GitHub-Pull-Request: #48427 Reviewed-on: https://go-review.googlesource.com/c/go/+/350509 Trust: Alexander Rakoczy <[email protected]> Trust: Damien Neil <[email protected]> Run-TryBot: Alexander Rakoczy <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent b5904f3 commit 4a3daee

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/net/http/httputil/reverseproxy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"io"
1313
"log"
14+
"mime"
1415
"net"
1516
"net/http"
1617
"net/http/internal/ascii"
@@ -412,7 +413,7 @@ func (p *ReverseProxy) flushInterval(res *http.Response) time.Duration {
412413

413414
// For Server-Sent Events responses, flush immediately.
414415
// The MIME type is defined in https://www.w3.org/TR/eventsource/#text-event-stream
415-
if resCT == "text/event-stream" {
416+
if baseCT, _, _ := mime.ParseMediaType(resCT); baseCT == "text/event-stream" {
416417
return -1 // negative means immediately
417418
}
418419

src/net/http/httputil/reverseproxy_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,26 @@ func TestSelectFlushInterval(t *testing.T) {
11941194
p: &ReverseProxy{FlushInterval: 0},
11951195
want: -1,
11961196
},
1197+
{
1198+
name: "server-sent events with media-type parameters overrides non-zero",
1199+
res: &http.Response{
1200+
Header: http.Header{
1201+
"Content-Type": {"text/event-stream;charset=utf-8"},
1202+
},
1203+
},
1204+
p: &ReverseProxy{FlushInterval: 123},
1205+
want: -1,
1206+
},
1207+
{
1208+
name: "server-sent events with media-type parameters overrides zero",
1209+
res: &http.Response{
1210+
Header: http.Header{
1211+
"Content-Type": {"text/event-stream;charset=utf-8"},
1212+
},
1213+
},
1214+
p: &ReverseProxy{FlushInterval: 0},
1215+
want: -1,
1216+
},
11971217
{
11981218
name: "Content-Length: -1, overrides non-zero",
11991219
res: &http.Response{

0 commit comments

Comments
 (0)