2021-10-22 22:40:03 +02:00
|
|
|
package http
|
2017-06-28 12:42:28 +02:00
|
|
|
|
|
|
|
import (
|
2022-01-13 15:59:57 +01:00
|
|
|
"errors"
|
2017-06-28 12:42:28 +02:00
|
|
|
"fmt"
|
2022-01-13 15:59:57 +01:00
|
|
|
"strings"
|
2017-06-28 12:42:28 +02:00
|
|
|
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2022-01-13 15:59:57 +01:00
|
|
|
var (
|
|
|
|
// ErrQueryTooShort will be returned when the query
|
|
|
|
// is less than 2 characters.
|
|
|
|
ErrQueryTooShort = errors.New("query too short")
|
|
|
|
|
|
|
|
// ErrQueryIncomplete will be returned when the
|
|
|
|
// prefix query lacks a : or .
|
|
|
|
ErrQueryIncomplete = errors.New(
|
|
|
|
"prefix query must contain at least on '.' or ':'")
|
|
|
|
)
|
|
|
|
|
2017-06-28 12:42:28 +02:00
|
|
|
// Helper: Validate source Id
|
2021-03-22 16:50:08 +01:00
|
|
|
func validateSourceID(id string) (string, error) {
|
2019-01-28 09:19:32 +01:00
|
|
|
if len(id) > 42 {
|
2021-11-15 21:43:22 +01:00
|
|
|
return "unknown", fmt.Errorf("source ID too long with length: %d", len(id))
|
2017-06-28 12:42:28 +02:00
|
|
|
}
|
2018-12-09 17:31:02 +01:00
|
|
|
return id, nil
|
2017-06-28 12:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper: Validate query string
|
|
|
|
func validateQueryString(req *http.Request, key string) (string, error) {
|
|
|
|
query := req.URL.Query()
|
|
|
|
values, ok := query[key]
|
|
|
|
if !ok {
|
2021-03-22 16:50:08 +01:00
|
|
|
return "", fmt.Errorf("query param %s is missing", key)
|
2017-06-28 12:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(values) != 1 {
|
2021-03-22 16:50:08 +01:00
|
|
|
return "", fmt.Errorf("query param %s is ambigous", key)
|
2017-06-28 12:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
value := values[0]
|
|
|
|
if value == "" {
|
2021-03-22 16:50:08 +01:00
|
|
|
return "", fmt.Errorf("query param %s may not be empty", key)
|
2017-06-28 12:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2022-01-13 15:59:57 +01:00
|
|
|
// Helper: Validate prefix query. It should contain
|
|
|
|
// at least one dot or :
|
2017-06-28 12:42:28 +02:00
|
|
|
func validatePrefixQuery(value string) (string, error) {
|
|
|
|
// We should at least provide 2 chars
|
|
|
|
if len(value) < 2 {
|
2022-01-13 15:59:57 +01:00
|
|
|
return "", ErrQueryTooShort
|
|
|
|
}
|
|
|
|
if !strings.Contains(value, ":") && !strings.Contains(value, ".") {
|
|
|
|
return "", ErrQueryIncomplete
|
2017-06-28 12:42:28 +02:00
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|