minor content-type fixes

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-04-16 17:14:27 +03:00
parent b2b4d6a5a3
commit 145a0f4aa6
2 changed files with 18 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import (
"net/http"
"reflect"
"strings"
"sync"
"github.com/unistack-org/micro/v3/codec"
"github.com/unistack-org/micro/v3/errors"
@ -40,9 +41,17 @@ type httpHandler struct {
eps []*register.Endpoint
hd interface{}
handlers map[string][]patHandler
sync.RWMutex
}
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 {
return cf, nil
}
@ -98,9 +107,9 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
switch ct {
case "application/x-www-form-urlencoded":
cf, err = h.newCodec(strings.Split(DefaultContentType, ";")[0])
cf, err = h.newCodec(DefaultContentType)
default:
cf, err = h.newCodec(strings.Split(ct, ";")[0])
cf, err = h.newCodec(ct)
}
if err != nil {

View File

@ -41,6 +41,13 @@ type httpServer struct {
}
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 {
return cf, nil
}