-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Unable to customize http.ResponseWriter because of wrong implementation of Kratos' http.wrapper.Result() #3174
Comments
🤖 It's great that you've identified a potential improvement in the This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
我看完源码后猜测可能是因为这个原因:
所以我推测,是因为必须在 Result 等方法中先设置响应码。而先设置响应码会导致响应头的修改无效。所以才有了 responseWriter。不过不论如何,这里肯定有问题就是了,起码 c.res 和 c.w 不应该同时存在。 |
After reading the source code, I guessed it might be because of this:
So I speculate that it is because the response code must be set first in methods such as Result. Setting the response code first will cause the modification of the response header to be invalid. That's why there is responseWriter. But no matter what, there must be a problem here. At least c.res and c.w should not exist at the same time. |
…ssing wrapped http.ResponseWriter. (go-kratos#3174)
@xbchaos Use can use the code below when kratos version >= v2.7.3 to get the underlying custom writer that you've set before: package main
import (
"context"
"fmt"
"io"
nt "net/http"
"net/http/httptest"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport/http"
)
type customWriter struct {
http.ResponseWriter
}
func main() {
s := http.NewServer(
http.Middleware(func(h middleware.Handler) middleware.Handler {
return func(ctx context.Context, req any) (any, error) {
hc := ctx.(http.Context)
hc.Reset(customWriter{hc.Response()}, hc.Request()) // reset response writer
return h(ctx, req)
}
}),
http.ResponseEncoder(func(w http.ResponseWriter, r *http.Request, v any) error {
// get underlying response writer
var ok bool
under := w
out:
for {
switch cur := under.(type) {
case customWriter:
under = cur
ok = true
break out
case interface{ Unwrap() http.ResponseWriter }:
under = cur.Unwrap()
default:
break out
}
}
return http.DefaultResponseEncoder(w, r, ok)
}),
)
// simulate what happened in xx_http.pb.go
s.Route("/").GET("/a", func(ctx http.Context) error {
h := ctx.Middleware(func(ctx context.Context, req any) (any, error) {
return nil, nil
})
out, _ := h(ctx, nil)
return ctx.Result(200, out)
})
w := httptest.NewRecorder()
r := httptest.NewRequest(nt.MethodGet, "/a", nil)
s.ServeHTTP(w, r)
res := w.Result()
defer res.Body.Close()
bs, _ := io.ReadAll(res.Body)
fmt.Println(string(bs)) // true
} |
What happened:
i implemented
ResponseWriter
ofnet/http
and use it in a custom Filter, but in my custom ResponseEncoder, the type assertion code below will fail:customResponseWriter := w.(*CustomResponseWriter)
finally, i found that, in Kratos'
http.wrapper.Result()
,wrapper.res
(which is right actually my custom ResponseWriter) is not used, instead,wrapper.w
is used:and what's more, we can see any other method of the wrapper, like
JSON()
orXML()
, they just usewrapper.res
:so, i'm wondering, the
Result()
method ofwrapper
should usewrapper.res
too, just like other methods.What you expected to happen:
http.wrapper.Result()
changed to:How to reproduce it (as minimally and precisely as possible):
Anything else we need to know?:
Environment:
kratos -v
): 2.1go version
): 1.20cat /etc/os-release
): macOSThe text was updated successfully, but these errors were encountered: