From c82671351f0c8d1a9670b8331a1f9f40e86f4b4f Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Sat, 19 Feb 2022 09:26:33 -0800 Subject: [PATCH] jhttp: add ParseBasic function This exports the default query parsing logic for a Getter when no more specific function is given in the options. This function can be used to get the default parsing behaviour when wiring a getter to a bridge. --- jhttp/getter.go | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/jhttp/getter.go b/jhttp/getter.go index 6ed51c4..20ede74 100644 --- a/jhttp/getter.go +++ b/jhttp/getter.go @@ -30,16 +30,14 @@ import ( // Method not found 404 (Not found) // (other errors) 500 (Internal server error) // -// By default, the URL path identifies the JSON-RPC method, and the URL query -// parameters are converted into a JSON object for the parameters. Leading and -// trailing slashes are stripped from the path, and query values are sent as -// JSON strings. -// -// For example, this URL: +// By default, a Getter uses ParseBasic to convert the HTTP request. The URL +// path identifies the JSON-RPC method, and the URL query parameters are +// converted into a JSON object for the parameters. Query values are sent as +// JSON strings. For example, this URL: // // http://site.org:2112/some/method?param1=xyzzy¶m2=apple // -// would produce the method name "some/method" and this parameter object: +// produces the method name "some/method" and this parameter object: // // {"param1":"xyzzy", "param2":"apple"} // @@ -100,18 +98,7 @@ func (g Getter) parseHTTPRequest(req *http.Request) (string, interface{}, error) if g.parseReq != nil { return g.parseReq(req) } - if err := req.ParseForm(); err != nil { - return "", nil, err - } - method := strings.Trim(req.URL.Path, "/") - if method == "" { - return "", nil, errors.New("empty method name") - } - params := make(map[string]string) - for key := range req.Form { - params[key] = req.Form.Get(key) - } - return method, params, nil + return ParseBasic(req) } // GetterOptions are optional settings for a Getter. A nil pointer is ready for @@ -166,6 +153,27 @@ func writeJSON(w http.ResponseWriter, code int, obj interface{}) { w.Write(bits) } +// ParseBasic parses a request URL and query parameters to obtain a method name +// and parameters. The URL path identifies the method name, with leading and +// trailing slashes removed. Query values are packed into a map[string]string. +// +// This is the default query parser used by a Getter if none is specified in +// its GetterOptions. +func ParseBasic(req *http.Request) (string, interface{}, error) { + if err := req.ParseForm(); err != nil { + return "", nil, err + } + method := strings.Trim(req.URL.Path, "/") + if method == "" { + return "", nil, errors.New("empty method name") + } + params := make(map[string]string) + for key := range req.Form { + params[key] = req.Form.Get(key) + } + return method, params, nil +} + // ParseQuery parses a request URL and constructs a parameter map from the // query values encoded in the URL and/or request body. //