Skip to content

Commit

Permalink
jttp: clean up quoted value handling
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Feb 12, 2022
1 parent 5c2b32a commit f25d8cc
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions jhttp/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,15 @@ func ParseQuery(req *http.Request) (string, interface{}, error) {
}

func parseJSONString(s string) (string, bool, error) {
if len(s) >= 2 {
if s[0] == '"' && s[len(s)-1] == '"' {
var dec string
err := json.Unmarshal([]byte(s), &dec)
if err != nil {
return "", false, err
}
return dec, true, nil
} else if s[0] == '"' || s[len(s)-1] == '"' {
return "", false, errors.New("missing string quote")
if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' {
var dec string
err := json.Unmarshal([]byte(s), &dec)
if err != nil {
return "", false, err
}
return dec, true, nil
} else if s != "" && (s[0] == '"' || s[len(s)-1] == '"') {
return "", false, errors.New("missing string quote")
}
return "", false, nil
}
Expand Down Expand Up @@ -275,14 +273,12 @@ func parseConstant(s string) (interface{}, bool) {
}

func parseQuoted64(s string) ([]byte, bool, error) {
if len(s) >= 2 {
if s[0] == '\'' && s[len(s)-1] == '\'' {
trim := strings.TrimRight(s[1:len(s)-1], "=") // discard base64 padding
dec, err := base64.RawStdEncoding.DecodeString(trim)
return dec, err == nil, err
} else if s[0] == '\'' || s[len(s)-1] == '\'' {
return nil, false, errors.New("missing bytes quote")
}
if len(s) >= 2 && s[0] == '\'' && s[len(s)-1] == '\'' {
trim := strings.TrimRight(s[1:len(s)-1], "=") // discard base64 padding
dec, err := base64.RawStdEncoding.DecodeString(trim)
return dec, err == nil, err
} else if s != "" && (s[0] == '\'' || s[len(s)-1] == '\'') {
return nil, false, errors.New("missing bytes quote")
}
return nil, false, nil
}

0 comments on commit f25d8cc

Please sign in to comment.