extract application/x-www-form-urlencoded handling to codec

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-04-17 13:54:32 +03:00
parent 145a0f4aa6
commit 84167d359e

View File

@ -88,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)
@ -103,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(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
@ -177,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.
@ -215,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)
@ -228,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{
@ -271,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: