extract application/x-www-form-urlencoded handling to codec
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
		
							
								
								
									
										45
									
								
								handler.go
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								handler.go
									
									
									
									
									
								
							| @@ -88,10 +88,12 @@ 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 { | ||||||
|  | 		if ph, ok := h.contentTypeHandlers[ct[:idx]]; ok { | ||||||
| 			ph(w, r) | 			ph(w, r) | ||||||
| 			return | 			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,12 +193,10 @@ 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) | ||||||
| 	if err = rflutil.Merge(argv.Interface(), matches, rflutil.SliceAppend(true), rflutil.Tags([]string{"protobuf", "json"})); err != nil { | 	if err = rflutil.Merge(argv.Interface(), matches, rflutil.SliceAppend(true), rflutil.Tags([]string{"protobuf", "json"})); err != nil { | ||||||
| @@ -228,14 +204,11 @@ 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" { |  | ||||||
| 		b, err = cf.Marshal(argv.Interface()) |  | ||||||
| 	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 | ||||||
| 	} | 	} | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	hr := &rpcRequest{ | 	hr := &rpcRequest{ | ||||||
| 		codec:       cf, | 		codec:       cf, | ||||||
| @@ -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: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user