Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(net/ghttp): skip common response body in common response handler for streaming content types #3762

Merged
merged 4 commits into from
Sep 12, 2024
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
20 changes: 20 additions & 0 deletions net/ghttp/ghttp_middleware_handler_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package ghttp

import (
"mime"
"net/http"

"github.com/gogf/gf/v2/errors/gcode"
Expand All @@ -20,6 +21,17 @@ type DefaultHandlerResponse struct {
Data interface{} `json:"data" dc:"Result data for certain request according API definition"`
}

const (
contentTypeEventStream = "text/event-stream"
contentTypeOctetStream = "application/octet-stream"
contentTypeMixedReplace = "multipart/x-mixed-replace"
)

var (
// streamContentType is the content types for stream response.
streamContentType = []string{contentTypeEventStream, contentTypeOctetStream, contentTypeMixedReplace}
)

// MiddlewareHandlerResponse is the default middleware handling handler response object and its error.
func MiddlewareHandlerResponse(r *Request) {
r.Middleware.Next()
Expand All @@ -29,6 +41,14 @@ func MiddlewareHandlerResponse(r *Request) {
return
}

// It does not output common response content if it is stream response.
mediaType, _, _ := mime.ParseMediaType(r.Response.Header().Get("Content-Type"))
for _, ct := range streamContentType {
if mediaType == ct {
return
}
}

wwwfeng marked this conversation as resolved.
Show resolved Hide resolved
var (
msg string
err = r.GetError()
Expand Down
43 changes: 43 additions & 0 deletions net/ghttp/ghttp_z_unit_feature_middleware_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,49 @@ func Test_MiddlewareHandlerGzipResponse(t *testing.T) {
})
}

func Test_MiddlewareHandlerStreamResponse(t *testing.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)

group.GET("/stream/event", func(r *ghttp.Request) {
r.Response.Header().Set("Content-Type", "text/event-stream")
})

group.GET("/stream/octet", func(r *ghttp.Request) {
r.Response.Header().Set("Content-Type", "application/octet-stream")
})

group.GET("/stream/mixed", func(r *ghttp.Request) {
r.Response.Header().Set("Content-Type", "multipart/x-mixed-replace")
})
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)

gtest.C(t, func(t *gtest.T) {
client := g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))

rsp, err := client.Get(ctx, "/stream/event")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusOK)
t.Assert(rsp.ReadAllString(), "")

rsp, err = client.Get(ctx, "/stream/octet")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusOK)
t.Assert(rsp.ReadAllString(), "")

rsp, err = client.Get(ctx, "/stream/mixed")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusOK)
t.Assert(rsp.ReadAllString(), "")
})
}

type testTracerProvider struct{}

var _ trace.TracerProvider = &testTracerProvider{}
Expand Down
Loading