handle x-form-urlencoded content-type

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-03-31 00:49:55 +03:00
parent e31185b6dc
commit 48620e6297
2 changed files with 41 additions and 16 deletions

View File

@ -81,7 +81,15 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ct = htype ct = htype
} }
cf, err := h.newCodec(ct) var cf codec.Codec
var err error
switch ct {
case "application/x-www-form-urlencoded":
cf, err = h.newCodec(DefaultContentType)
default:
cf, err = h.newCodec(ct)
}
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
@ -101,6 +109,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
matches := make(map[string]interface{}) matches := make(map[string]interface{})
var match bool var match bool
var hldr patHandler var hldr patHandler
var handler *httpHandler var handler *httpHandler
@ -134,9 +143,22 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
md.Set(k, strings.Join(v, ", ")) md.Set(k, strings.Join(v, ", "))
} }
var query string
switch ct {
case "application/x-www-form-urlencoded":
buf, err := io.ReadAll(r.Body)
if err != nil {
h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest)
return
}
query = string(buf)
default:
query = r.URL.RawQuery
}
// get fields from url values // get fields from url values
if len(r.URL.RawQuery) > 0 { if len(query) > 0 {
umd, err := rflutil.URLMap(r.URL.RawQuery) umd, err := rflutil.URLMap(query)
if err != nil { if err != nil {
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest) h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
return return
@ -168,9 +190,11 @@ 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 err = cf.ReadBody(r.Body, argv.Interface()); err != nil && err != io.EOF { if ct != "application/x-www-form-urlencoded" {
h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError) if err = cf.ReadBody(r.Body, argv.Interface()); err != nil && err != io.EOF {
return h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError)
return
}
} }
matches = rflutil.FlattenMap(matches) matches = rflutil.FlattenMap(matches)
@ -179,10 +203,13 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
b, err := cf.Marshal(argv.Interface()) var b []byte
if err != nil { if ct != "application/x-www-form-urlencoded" {
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest) b, err = cf.Marshal(argv.Interface())
return if err != nil {
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
return
}
} }
hr := &rpcRequest{ hr := &rpcRequest{

10
http.go
View File

@ -63,9 +63,6 @@ func (h *httpServer) Init(opts ...server.Option) error {
if fn, ok := h.opts.Context.Value(errorHandlerKey{}).(func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int)); ok && fn != nil { if fn, ok := h.opts.Context.Value(errorHandlerKey{}).(func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int)); ok && fn != nil {
h.errorHandler = fn h.errorHandler = fn
} }
if h.errorHandler == nil {
h.errorHandler = DefaultErrorHandler
}
if h.handlers == nil { if h.handlers == nil {
h.handlers = make(map[string]server.Handler) h.handlers = make(map[string]server.Handler)
} }
@ -553,8 +550,9 @@ func (h *httpServer) Name() string {
func NewServer(opts ...server.Option) server.Server { func NewServer(opts ...server.Option) server.Server {
options := server.NewOptions(opts...) options := server.NewOptions(opts...)
return &httpServer{ return &httpServer{
opts: options, opts: options,
exit: make(chan chan error), exit: make(chan chan error),
subscribers: make(map[*httpSubscriber][]broker.Subscriber), subscribers: make(map[*httpSubscriber][]broker.Subscriber),
errorHandler: DefaultErrorHandler,
} }
} }