rewrite x-www-form-urlencoded

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-04-10 01:24:28 +03:00
parent 475452a781
commit 7e35316cf2

View File

@ -143,22 +143,25 @@ 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 // get fields from url values
switch ct { if len(r.URL.RawQuery) > 0 {
case "application/x-www-form-urlencoded": umd, err := rflutil.URLMap(r.URL.RawQuery)
if err != nil {
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
return
}
for k, v := range umd {
matches[k] = v
}
}
if ct == "application/x-www-form-urlencoded" {
buf, err := io.ReadAll(r.Body) buf, err := io.ReadAll(r.Body)
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
} }
query = string(buf) umd, err := rflutil.URLMap(string(buf))
default:
query = r.URL.RawQuery
}
// get fields from url values
if len(query) > 0 {
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
@ -198,7 +201,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
matches = rflutil.FlattenMap(matches) matches = rflutil.FlattenMap(matches)
if err = rflutil.MergeMap(argv.Interface(), matches); err != nil { if err = rflutil.Merge(argv.Interface(), matches, rflutil.SliceAppend(true), rflutil.Tags([]string{"protobuf", "json"})); err != nil {
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest) h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
return return
} }