refactor gjson parsing code

This commit is contained in:
Hunter Kehoe 2024-03-19 20:21:45 -06:00
parent b2eb5b94bd
commit 867cf28080

View File

@ -110,6 +110,7 @@ var (
fileRegex = regexp.MustCompile(`^/file/([-_A-Za-z0-9]{1,64})(?:\.[A-Za-z0-9]{1,16})?$`) fileRegex = regexp.MustCompile(`^/file/([-_A-Za-z0-9]{1,64})(?:\.[A-Za-z0-9]{1,16})?$`)
urlRegex = regexp.MustCompile(`^https?://`) urlRegex = regexp.MustCompile(`^https?://`)
phoneNumberRegex = regexp.MustCompile(`^\+\d{1,100}$`) phoneNumberRegex = regexp.MustCompile(`^\+\d{1,100}$`)
templateVarRegex = regexp.MustCompile(`\${([^}]+)}`)
//go:embed site //go:embed site
webFs embed.FS webFs embed.FS
@ -1076,36 +1077,28 @@ func (s *Server) handleBodyAsMessageAutoDetect(m *message, body *util.PeekedRead
return nil return nil
} }
func replaceGJSONTemplate(template string, source string) string {
matches := templateVarRegex.FindAllStringSubmatch(template, -1)
for _, v := range matches {
query := v[1]
if result := gjson.Get(source, query); result.Exists() {
template = strings.ReplaceAll(template, fmt.Sprintf("${%s}", query), result.String())
}
}
return template
}
func (s *Server) handleBodyAsTextMessage(m *message, body *util.PeekedReadCloser, template bool) error { func (s *Server) handleBodyAsTextMessage(m *message, body *util.PeekedReadCloser, template bool) error {
if !utf8.Valid(body.PeekedBytes) { if !utf8.Valid(body.PeekedBytes) {
return errHTTPBadRequestMessageNotUTF8.With(m) return errHTTPBadRequestMessageNotUTF8.With(m)
} }
if len(body.PeekedBytes) > 0 { // Empty body should not override message (publish via GET!) if len(body.PeekedBytes) > 0 { // Empty body should not override message (publish via GET!)
peakedBody := strings.TrimSpace(string(body.PeekedBytes)) // Truncates the message to the peek limit if required peekedBody := strings.TrimSpace(string(body.PeekedBytes)) // Truncates the message to the peek limit if required
if template && gjson.Valid(peakedBody) { if template && gjson.Valid(peekedBody) {
// Replace JSON paths in message m.Message = replaceGJSONTemplate(m.Message, peekedBody)
r := regexp.MustCompile(`\${([^}]+)}`) m.Title = replaceGJSONTemplate(m.Title, peekedBody)
matches := r.FindAllStringSubmatch(m.Message, -1)
for _, v := range matches {
query := v[1]
result := gjson.Get(peakedBody, query)
if result.Exists() {
m.Message = strings.ReplaceAll(m.Message, fmt.Sprintf("${%s}", query), result.String())
}
}
// Replace JSON paths in title
r = regexp.MustCompile(`\${([^}]+)}`)
matches = r.FindAllStringSubmatch(m.Title, -1)
for _, v := range matches {
query := v[1]
result := gjson.Get(peakedBody, query)
if result.Exists() {
m.Title = strings.ReplaceAll(m.Title, fmt.Sprintf("${%s}", query), result.String())
}
}
} else { } else {
m.Message = peakedBody m.Message = peekedBody
} }
} }
if m.Attachment != nil && m.Attachment.Name != "" && m.Message == "" { if m.Attachment != nil && m.Attachment.Name != "" && m.Message == "" {