fixup headers

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-03-11 23:30:33 +03:00
parent a483e08ffa
commit 4c3d26c39b
3 changed files with 35 additions and 33 deletions

View File

@ -109,16 +109,22 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error)
md = metadata.New(len(r.Header) + 8) md = metadata.New(len(r.Header) + 8)
} }
for k, v := range r.Header { for k, v := range r.Header {
md[k] = strings.Join(v, ", ") md[k] = v
} }
md["RemoteAddr"] = r.RemoteAddr md["RemoteAddr"] = []string{r.RemoteAddr}
md["Method"] = r.Method md["Method"] = []string{r.Method}
md["URL"] = r.URL.String() md["URL"] = []string{r.URL.String()}
md["Proto"] = r.Proto md["Proto"] = []string{r.Proto}
md["ContentLength"] = fmt.Sprintf("%d", r.ContentLength) md["Content-Length"] = []string{fmt.Sprintf("%d", r.ContentLength)}
md["TransferEncoding"] = strings.Join(r.TransferEncoding, ",") md["Transfer-Encoding"] = r.TransferEncoding
md["Host"] = r.Host md["Host"] = []string{r.Host}
md["RequestURI"] = r.RequestURI md["RequestURI"] = []string{r.RequestURI}
if r.TLS != nil {
md["TLS"] = []string{"true"}
md["TLS-ALPN"] = []string{r.TLS.NegotiatedProtocol}
md["TLS-ServerName"] = []string{r.TLS.ServerName}
}
ctx = metadata.NewIncomingContext(ctx, md) ctx = metadata.NewIncomingContext(ctx, md)
path := r.URL.Path path := r.URL.Path
@ -257,10 +263,9 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error)
} }
if nmd, ok := metadata.FromOutgoingContext(fctx); ok { if nmd, ok := metadata.FromOutgoingContext(fctx); ok {
for k, v := range nmd { for k, v := range nmd {
md.Set(k, v) md[k] = v
} }
} }
metadata.SetOutgoingContext(ctx, md)
return err return err
} }
@ -285,14 +290,12 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error)
w.Header().Set(metadata.HeaderContentType, ct) w.Header().Set(metadata.HeaderContentType, ct)
if md, ok := metadata.FromOutgoingContext(ctx); ok { if md, ok := metadata.FromOutgoingContext(ctx); ok {
for k, v := range md { for k, v := range md {
w.Header().Set(k, v) w.Header()[k] = v
} }
} }
if md := getRspHeader(ctx); md != nil { if md := getRspHeader(ctx); md != nil {
for k, v := range md { for k, v := range md {
for _, vv := range v { w.Header()[k] = v
w.Header().Add(k, vv)
}
} }
} }
if nct := w.Header().Get(metadata.HeaderContentType); nct != ct { if nct := w.Header().Get(metadata.HeaderContentType); nct != ct {
@ -346,23 +349,23 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
md = metadata.New(len(r.Header) + 8) md = metadata.New(len(r.Header) + 8)
} }
for k, v := range r.Header { for k, v := range r.Header {
md[k] = strings.Join(v, ", ") md[k] = v
} }
md["RemoteAddr"] = r.RemoteAddr md["RemoteAddr"] = []string{r.RemoteAddr}
if r.TLS != nil { if r.TLS != nil {
md["Scheme"] = "https" md["Scheme"] = []string{"https"}
} else { } else {
md["Scheme"] = "http" md["Scheme"] = []string{"http"}
} }
md["Method"] = r.Method md["Method"] = []string{r.Method}
md["URL"] = r.URL.String() md["URL"] = []string{r.URL.String()}
md["Proto"] = r.Proto md["Proto"] = []string{r.Proto}
md["ContentLength"] = fmt.Sprintf("%d", r.ContentLength) md["Content-Length"] = []string{fmt.Sprintf("%d", r.ContentLength)}
if len(r.TransferEncoding) > 0 { if len(r.TransferEncoding) > 0 {
md["TransferEncoding"] = strings.Join(r.TransferEncoding, ",") md["Transfer-Encoding"] = r.TransferEncoding
} }
md["Host"] = r.Host md["Host"] = []string{r.Host}
md["RequestURI"] = r.RequestURI md["RequestURI"] = []string{r.RequestURI}
ctx = metadata.NewIncomingContext(ctx, md) ctx = metadata.NewIncomingContext(ctx, md)
path := r.URL.Path path := r.URL.Path
@ -520,10 +523,9 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
if nmd, ok := metadata.FromOutgoingContext(fctx); ok { if nmd, ok := metadata.FromOutgoingContext(fctx); ok {
for k, v := range nmd { for k, v := range nmd {
md.Set(k, v) md[k] = v
} }
} }
metadata.SetOutgoingContext(ctx, md)
return err return err
} }
@ -548,7 +550,7 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(metadata.HeaderContentType, ct) w.Header().Set(metadata.HeaderContentType, ct)
if md, ok := metadata.FromOutgoingContext(ctx); ok { if md, ok := metadata.FromOutgoingContext(ctx); ok {
for k, v := range md { for k, v := range md {
w.Header().Set(k, v) w.Header()[k] = v
} }
} }
if md := getRspHeader(ctx); md != nil { if md := getRspHeader(ctx); md != nil {

View File

@ -217,8 +217,8 @@ func (h *Server) newHTTPHandler(handler interface{}, opts ...options.Option) *ht
pth := &patHandler{mtype: mtype, name: name, rcvr: rcvr} pth := &patHandler{mtype: mtype, name: name, rcvr: rcvr}
hdlr.name = name hdlr.name = name
if err := hdlr.handlers.Insert([]string{md["Method"]}, md["Path"], pth); err != nil { if err := hdlr.handlers.Insert(md["Method"], md["Path"][0], pth); err != nil {
h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("cant add handler for %s %s", md["Method"], md["Path"])) h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("cant add handler for %s %s", md["Method"][0], md["Path"][0]))
} }
if h.registerRPC { if h.registerRPC {
@ -308,7 +308,7 @@ func (h *Server) Register() error {
if err != nil { if err != nil {
return err return err
} }
service.Nodes[0].Metadata["protocol"] = "http" service.Nodes[0].Metadata.Set("protocol", "http")
service.Endpoints = eps service.Endpoints = eps
h.RLock() h.RLock()

View File

@ -31,7 +31,7 @@ func FillRequest(ctx context.Context, req interface{}, opts ...FillRequestOption
} }
} }
cookies := strings.Split(md["Cookie"], ";") cookies := md["Cookie"]
cmd := make(map[string]string, len(cookies)) cmd := make(map[string]string, len(cookies))
for _, cookie := range cookies { for _, cookie := range cookies {
kv := strings.Split(cookie, "=") kv := strings.Split(cookie, "=")