1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00

server: quick path for http join query

This commit is contained in:
wdvxdr 2023-01-31 21:18:15 +08:00
parent 0be18fb221
commit 4ed04443c5

View File

@ -104,31 +104,35 @@ func init() {
var joinQuery = regexp.MustCompile(`\[(.+?),(.+?)]\.0`) var joinQuery = regexp.MustCompile(`\[(.+?),(.+?)]\.0`)
func (h *httpCtx) get(s string, join bool) gjson.Result { func mayJSONParam(p string) bool {
if strings.HasPrefix(p, "{") || strings.HasPrefix(p, "[") {
return gjson.Valid(p)
}
return false
}
func (h *httpCtx) get(pattern string, join bool) gjson.Result {
// support gjson advanced syntax: // support gjson advanced syntax:
// h.Get("[a,b].0") see usage in http_test.go // h.Get("[a,b].0") see usage in http_test.go. See issue #1241, #1325.
if join && joinQuery.MatchString(s) { if join && strings.HasPrefix(pattern, "[") && joinQuery.MatchString(pattern) {
matched := joinQuery.FindStringSubmatch(s) matched := joinQuery.FindStringSubmatch(pattern)
if r := h.get(matched[1], false); r.Exists() { if r := h.get(matched[1], false); r.Exists() {
return r return r
} }
return h.get(matched[2], false) return h.get(matched[2], false)
} }
validJSONParam := func(p string) bool {
return (strings.HasPrefix(p, "{") || strings.HasPrefix(p, "[")) && gjson.Valid(p)
}
if h.postForm != nil { if h.postForm != nil {
if form := h.postForm.Get(s); form != "" { if form := h.postForm.Get(pattern); form != "" {
if validJSONParam(form) { if mayJSONParam(form) {
return gjson.Result{Type: gjson.JSON, Raw: form} return gjson.Result{Type: gjson.JSON, Raw: form}
} }
return gjson.Result{Type: gjson.String, Str: form} return gjson.Result{Type: gjson.String, Str: form}
} }
} }
if h.query != nil { if h.query != nil {
if query := h.query.Get(s); query != "" { if query := h.query.Get(pattern); query != "" {
if validJSONParam(query) { if mayJSONParam(query) {
return gjson.Result{Type: gjson.JSON, Raw: query} return gjson.Result{Type: gjson.JSON, Raw: query}
} }
return gjson.Result{Type: gjson.String, Str: query} return gjson.Result{Type: gjson.String, Str: query}