1
0
mirror of https://github.com/go-kratos/kratos.git synced 2024-12-26 20:54:38 +02:00

adds Set-Cookie method to the HTTP transport. (#3353)

This commit is contained in:
Tony Chen 2024-06-27 18:40:09 +08:00 committed by GitHub
parent 46362d1a36
commit 3198e0b83b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 0 deletions

View File

@ -296,6 +296,7 @@ func (s *Server) filter() mux.MiddlewareFunc {
reqHeader: headerCarrier(req.Header),
replyHeader: headerCarrier(w.Header()),
request: req,
response: w,
}
if s.endpoint != nil {
tr.endpoint = s.endpoint.String()

View File

@ -23,6 +23,7 @@ type Transport struct {
reqHeader headerCarrier
replyHeader headerCarrier
request *http.Request
response http.ResponseWriter
pathTemplate string
}
@ -70,6 +71,17 @@ func SetOperation(ctx context.Context, op string) {
}
}
// SetCookie adds a Set-Cookie header to the provided [ResponseWriter]'s headers.
// The provided cookie must have a valid Name. Invalid cookies may be
// silently dropped.
func SetCookie(ctx context.Context, cookie *http.Cookie) {
if tr, ok := transport.FromServerContext(ctx); ok {
if tr, ok := tr.(*Transport); ok {
http.SetCookie(tr.response, cookie)
}
}
}
// RequestFromServerContext returns request from context.
func RequestFromServerContext(ctx context.Context) (*http.Request, bool) {
if tr, ok := transport.FromServerContext(ctx); ok {