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
}
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 {
h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest)
return
@ -101,6 +109,7 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
matches := make(map[string]interface{})
var match bool
var hldr patHandler
var handler *httpHandler
@ -134,9 +143,22 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
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
if len(r.URL.RawQuery) > 0 {
umd, err := rflutil.URLMap(r.URL.RawQuery)
if len(query) > 0 {
umd, err := rflutil.URLMap(query)
if err != nil {
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
return
@ -168,9 +190,11 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//function := hldr.rcvr
var returnValues []reflect.Value
if err = cf.ReadBody(r.Body, argv.Interface()); err != nil && err != io.EOF {
h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError)
return
if ct != "application/x-www-form-urlencoded" {
if err = cf.ReadBody(r.Body, argv.Interface()); err != nil && err != io.EOF {
h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError)
return
}
}
matches = rflutil.FlattenMap(matches)
@ -179,10 +203,13 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
b, err := cf.Marshal(argv.Interface())
if err != nil {
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
return
var b []byte
if ct != "application/x-www-form-urlencoded" {
b, err = cf.Marshal(argv.Interface())
if err != nil {
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
return
}
}
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 {
h.errorHandler = fn
}
if h.errorHandler == nil {
h.errorHandler = DefaultErrorHandler
}
if h.handlers == nil {
h.handlers = make(map[string]server.Handler)
}
@ -553,8 +550,9 @@ func (h *httpServer) Name() string {
func NewServer(opts ...server.Option) server.Server {
options := server.NewOptions(opts...)
return &httpServer{
opts: options,
exit: make(chan chan error),
subscribers: make(map[*httpSubscriber][]broker.Subscriber),
opts: options,
exit: make(chan chan error),
subscribers: make(map[*httpSubscriber][]broker.Subscriber),
errorHandler: DefaultErrorHandler,
}
}