Compare commits

...

4 Commits

Author SHA1 Message Date
84167d359e extract application/x-www-form-urlencoded handling to codec
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-04-17 13:54:32 +03:00
145a0f4aa6 minor content-type fixes
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-04-16 17:14:27 +03:00
Renovate Bot
b2b4d6a5a3 Update golang.org/x/net commit hash to e915ea6 2021-04-16 01:28:59 +00:00
Renovate Bot
f17afa7950 Update golang.org/x/net commit hash to 0645797 2021-04-14 21:16:29 +00:00
4 changed files with 40 additions and 43 deletions

2
go.mod
View File

@@ -4,7 +4,7 @@ go 1.16
require ( require (
github.com/unistack-org/micro/v3 v3.3.13 github.com/unistack-org/micro/v3 v3.3.13
golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1 golang.org/x/net v0.0.0-20210415231046-e915ea6b2b7d
) )
//replace github.com/unistack-org/micro/v3 => ../../micro //replace github.com/unistack-org/micro/v3 => ../../micro

4
go.sum
View File

@@ -9,8 +9,8 @@ github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7
github.com/unistack-org/micro/v3 v3.3.13 h1:y4bDDkbwnjgOckrhFkC6D/o42tr75X33UbrB+Ko0M68= github.com/unistack-org/micro/v3 v3.3.13 h1:y4bDDkbwnjgOckrhFkC6D/o42tr75X33UbrB+Ko0M68=
github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y= github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1 h1:4qWs8cYYH6PoEFy4dfhDFgoMGkwAcETd+MmPdCPMzUc= golang.org/x/net v0.0.0-20210415231046-e915ea6b2b7d h1:BgJvlyh+UqCUaPlscHJ+PN8GcpfrFdr7NHjd1JL0+Gs=
golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= golang.org/x/net v0.0.0-20210415231046-e915ea6b2b7d/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

View File

@@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"reflect" "reflect"
"strings" "strings"
"sync"
"github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/codec"
"github.com/unistack-org/micro/v3/errors" "github.com/unistack-org/micro/v3/errors"
@@ -40,9 +41,17 @@ type httpHandler struct {
eps []*register.Endpoint eps []*register.Endpoint
hd interface{} hd interface{}
handlers map[string][]patHandler handlers map[string][]patHandler
sync.RWMutex
} }
func (h *httpHandler) newCodec(ct string) (codec.Codec, error) { func (h *httpHandler) newCodec(ct string) (codec.Codec, error) {
h.RLock()
defer h.RUnlock()
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
ct = ct[:idx]
}
if cf, ok := h.sopts.Codecs[ct]; ok { if cf, ok := h.sopts.Codecs[ct]; ok {
return cf, nil return cf, nil
} }
@@ -79,9 +88,11 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ct = htype ct = htype
} }
if ph, ok := h.contentTypeHandlers[strings.Split(ct, ";")[0]]; ok { if idx := strings.Index(ct, ":"); idx > 0 {
ph(w, r) if ph, ok := h.contentTypeHandlers[ct[:idx]]; ok {
return ph(w, r)
return
}
} }
ctx := metadata.NewContext(r.Context(), nil) ctx := metadata.NewContext(r.Context(), nil)
@@ -94,15 +105,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
var cf codec.Codec cf, err := h.newCodec(ct)
var err error
switch ct {
case "application/x-www-form-urlencoded":
cf, err = h.newCodec(strings.Split(DefaultContentType, ";")[0])
default:
cf, err = h.newCodec(strings.Split(ct, ";")[0])
}
if err != nil { if err != nil {
h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest) h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest)
return return
@@ -168,22 +171,6 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
if ct == "application/x-www-form-urlencoded" {
buf, err := io.ReadAll(r.Body)
if err != nil {
h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest)
return
}
umd, err := rflutil.URLMap(string(buf))
if err != nil {
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
return
}
for k, v := range umd {
matches[k] = v
}
}
var argv, replyv reflect.Value var argv, replyv reflect.Value
// Decode the argument value. // Decode the argument value.
@@ -206,11 +193,9 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//function := hldr.rcvr //function := hldr.rcvr
var returnValues []reflect.Value var returnValues []reflect.Value
if ct != "application/x-www-form-urlencoded" { if err = cf.ReadBody(r.Body, argv.Interface()); err != nil && err != io.EOF {
if err = cf.ReadBody(r.Body, argv.Interface()); err != nil && err != io.EOF { h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError)
h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError) return
return
}
} }
matches = rflutil.FlattenMap(matches) matches = rflutil.FlattenMap(matches)
@@ -219,13 +204,10 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
var b []byte b, err := cf.Marshal(argv.Interface())
if ct != "application/x-www-form-urlencoded" { if err != nil {
b, err = cf.Marshal(argv.Interface()) h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
if err != nil { return
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
return
}
} }
hr := &rpcRequest{ hr := &rpcRequest{
@@ -262,6 +244,14 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fn = handler.sopts.HdlrWrappers[i-1](fn) fn = handler.sopts.HdlrWrappers[i-1](fn)
} }
if ct == "application/x-www-form-urlencoded" {
cf, err = h.newCodec(DefaultContentType)
if err != nil {
h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError)
return
}
}
if appErr := fn(ctx, hr, replyv.Interface()); appErr != nil { if appErr := fn(ctx, hr, replyv.Interface()); appErr != nil {
switch verr := appErr.(type) { switch verr := appErr.(type) {
case *errors.Error: case *errors.Error:

View File

@@ -41,6 +41,13 @@ type httpServer struct {
} }
func (h *httpServer) newCodec(ct string) (codec.Codec, error) { func (h *httpServer) newCodec(ct string) (codec.Codec, error) {
h.RLock()
defer h.RUnlock()
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
ct = ct[:idx]
}
if cf, ok := h.opts.Codecs[ct]; ok { if cf, ok := h.opts.Codecs[ct]; ok {
return cf, nil return cf, nil
} }