From 12e26d0018437d2cd06573c83d927bf83184d46a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=93=D0=BE=D1=80?= =?UTF-8?q?=D0=B1=D1=83=D0=BD=D0=BE=D0=B2?= Date: Tue, 26 Mar 2024 14:53:14 +0300 Subject: [PATCH] hotfix (#186) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Pull Request template Please, go through these steps before clicking submit on this PR. 1. Give a descriptive title to your PR. 2. Provide a description of your changes. 3. Make sure you have some relevant tests. 4. Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if applicable). **PLEASE REMOVE THIS TEMPLATE BEFORE SUBMITTING** Co-authored-by: Gorbunov Kirill Andreevich Reviewed-on: https://git.unistack.org/unistack-org/micro-server-http/pulls/186 Co-authored-by: Кирилл Горбунов Co-committed-by: Кирилл Горбунов --- handler.go | 50 ++++++++++++++++++------------------- handler/meter/meter.go | 21 +++++++++++----- handler/meter/meter_test.go | 49 ++++++++++++++++++++++++++++++++++++ http.go | 4 +-- util.go | 10 +++----- 5 files changed, 95 insertions(+), 39 deletions(-) create mode 100644 handler/meter/meter_test.go diff --git a/handler.go b/handler.go index 23cb470..803a1c7 100644 --- a/handler.go +++ b/handler.go @@ -109,20 +109,20 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error) md = metadata.New(len(r.Header) + 8) } for k, v := range r.Header { - md[k] = v + md[k] = v[0] } - md["RemoteAddr"] = []string{r.RemoteAddr} - md["Method"] = []string{r.Method} - md["URL"] = []string{r.URL.String()} - md["Proto"] = []string{r.Proto} - md["Content-Length"] = []string{fmt.Sprintf("%d", r.ContentLength)} - md["Transfer-Encoding"] = r.TransferEncoding - md["Host"] = []string{r.Host} - md["RequestURI"] = []string{r.RequestURI} + md["RemoteAddr"] = r.RemoteAddr + md["Method"] = r.Method + md["URL"] = r.URL.String() + md["Proto"] = r.Proto + md["Content-Length"] = fmt.Sprintf("%d", r.ContentLength) + md["Transfer-Encoding"] = r.TransferEncoding[0] + md["Host"] = r.Host + md["RequestURI"] = r.RequestURI if r.TLS != nil { - md["TLS"] = []string{"true"} - md["TLS-ALPN"] = []string{r.TLS.NegotiatedProtocol} - md["TLS-ServerName"] = []string{r.TLS.ServerName} + md["TLS"] = "true" + md["TLS-ALPN"] = r.TLS.NegotiatedProtocol + md["TLS-ServerName"] = r.TLS.ServerName } ctx = metadata.NewIncomingContext(ctx, md) @@ -290,7 +290,7 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error) w.Header().Set(metadata.HeaderContentType, ct) if md, ok := metadata.FromOutgoingContext(ctx); ok { for k, v := range md { - w.Header()[k] = v + w.Header()[k] = []string{v} } } if md := getRspHeader(ctx); md != nil { @@ -349,23 +349,23 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { md = metadata.New(len(r.Header) + 8) } for k, v := range r.Header { - md[k] = v + md[k] = v[0] } - md["RemoteAddr"] = []string{r.RemoteAddr} + md["RemoteAddr"] = r.RemoteAddr if r.TLS != nil { - md["Scheme"] = []string{"https"} + md["Scheme"] = "https" } else { - md["Scheme"] = []string{"http"} + md["Scheme"] = "http" } - md["Method"] = []string{r.Method} - md["URL"] = []string{r.URL.String()} - md["Proto"] = []string{r.Proto} - md["Content-Length"] = []string{fmt.Sprintf("%d", r.ContentLength)} + md["Method"] = r.Method + md["URL"] = r.URL.String() + md["Proto"] = r.Proto + md["Content-Length"] = fmt.Sprintf("%d", r.ContentLength) if len(r.TransferEncoding) > 0 { - md["Transfer-Encoding"] = r.TransferEncoding + md["Transfer-Encoding"] = r.TransferEncoding[0] } - md["Host"] = []string{r.Host} - md["RequestURI"] = []string{r.RequestURI} + md["Host"] = r.Host + md["RequestURI"] = r.RequestURI ctx = metadata.NewIncomingContext(ctx, md) path := r.URL.Path @@ -550,7 +550,7 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set(metadata.HeaderContentType, ct) if md, ok := metadata.FromOutgoingContext(ctx); ok { for k, v := range md { - w.Header()[k] = v + w.Header()[k] = []string{v} } } if md := getRspHeader(ctx); md != nil { diff --git a/handler/meter/meter.go b/handler/meter/meter.go index 4157cd9..30461d9 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -42,9 +42,10 @@ type Handler struct { type Option func(*Options) type Options struct { - Meter meter.Meter - Name string - MeterOptions []options.Option + Meter meter.Meter + Name string + MeterOptions []options.Option + DisableCompress bool } func Meter(m meter.Meter) Option { @@ -59,6 +60,12 @@ func Name(name string) Option { } } +func DisableCompress(g bool) Option { + return func(o *Options) { + o.DisableCompress = g + } +} + func MeterOptions(opts ...options.Option) Option { return func(o *Options) { o.MeterOptions = append(o.MeterOptions, opts...) @@ -66,7 +73,7 @@ func MeterOptions(opts ...options.Option) Option { } func NewOptions(opts ...Option) Options { - options := Options{Meter: meter.DefaultMeter} + options := Options{Meter: meter.DefaultMeter, DisableCompress: false} for _, o := range opts { o(&options) } @@ -90,8 +97,9 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb. w := io.Writer(buf) - if md, ok := metadata.FromContext(ctx); gzipAccepted(md) && ok { - md.Set(contentEncodingHeader, "gzip") + if md, ok := metadata.FromIncomingContext(ctx); gzipAccepted(md) && ok && !h.opts.DisableCompress { + omd, _ := metadata.FromOutgoingContext(ctx) + omd.Set(contentEncodingHeader, "gzip") gz := gzipPool.Get().(*gzip.Writer) defer gzipPool.Put(gz) @@ -99,6 +107,7 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb. defer gz.Close() w = gz + gz.Flush() } if err := h.opts.Meter.Write(w, h.opts.MeterOptions...); err != nil { diff --git a/handler/meter/meter_test.go b/handler/meter/meter_test.go new file mode 100644 index 0000000..a3b7f51 --- /dev/null +++ b/handler/meter/meter_test.go @@ -0,0 +1,49 @@ +package meter + +import ( + "context" + "testing" + + codecpb "go.unistack.org/micro-proto/v4/codec" +) + +func TestHandler_Metrics(t *testing.T) { + type fields struct { + opts Options + } + type args struct { + ctx context.Context + req *codecpb.Frame + rsp *codecpb.Frame + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + "Test #1", + fields{ + opts: NewOptions(), + }, + args{ + context.Background(), + &codecpb.Frame{Data: []byte("gzip")}, + &codecpb.Frame{}, + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &Handler{ + opts: tt.fields.opts, + } + if err := h.Metrics(tt.args.ctx, tt.args.req, tt.args.rsp); (err != nil) != tt.wantErr { + t.Errorf("Metrics() error = %v, wantErr %v", err, tt.wantErr) + } + t.Logf("RSP: %v", tt.args.rsp.Data) + }) + } +} diff --git a/http.go b/http.go index d8f41e9..9dfdffd 100644 --- a/http.go +++ b/http.go @@ -217,7 +217,7 @@ func (h *Server) newHTTPHandler(handler interface{}, opts ...options.Option) *ht pth := &patHandler{mtype: mtype, name: name, rcvr: rcvr} hdlr.name = name - if err := hdlr.handlers.Insert(md["Method"], md["Path"][0], pth); err != nil { + if err := hdlr.handlers.Insert([]string{md["Method"]}, md["Path"], pth); err != nil { h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("cant add handler for %s %s", md["Method"][0], md["Path"][0])) } @@ -468,7 +468,7 @@ func (h *Server) Start() error { go func() { if cerr := hs.Serve(ts); cerr != nil && !errors.Is(cerr, net.ErrClosed) { - h.opts.Logger.Error(h.opts.Context, cerr) + h.opts.Logger.Error(h.opts.Context, cerr.Error()) } }() diff --git a/util.go b/util.go index d856146..d07d3ad 100644 --- a/util.go +++ b/util.go @@ -33,13 +33,11 @@ func FillRequest(ctx context.Context, req interface{}, opts ...FillRequestOption cookies := md["Cookie"] cmd := make(map[string]string, len(cookies)) - for _, cookie := range cookies { - kv := strings.Split(cookie, "=") - if len(kv) != 2 { - continue - } - cmd[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1]) + kv := strings.Split(cookies, "=") + if len(kv) != 2 { + return nil } + cmd[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1]) for idx := 0; idx < len(options.cookies)/2; idx += 2 { k := http.CanonicalHeaderKey(options.cookies[idx]) v, ok := cmd[k]