mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-07 02:02:18 +00:00
4c47756aed
* remove validation package * structs cleanup * merge with apimiddleware removal * more validation and Bls capitalization * builder test fix * use strconv for uint->str conversions * use DecodeHexWithLength * use exact param names * rename http package to httputil * change conversions to fmt.Sprintf * handle query paramsd and route variables * spans and receiver name * split structs, move bytes helper * missing ok check * fix reference to indexed failure * errors fixup * add godoc to helper * fix BLS casing and chainhead ref * review * fix import in tests * gzl
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package httputil
|
|
|
|
import (
|
|
"net/http"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/api"
|
|
)
|
|
|
|
// match a number with optional decimals
|
|
var priorityRegex = regexp.MustCompile(`q=(\d+(?:\.\d+)?)`)
|
|
|
|
// SszRequested takes a http request and checks to see if it should be requesting a ssz response.
|
|
func SszRequested(req *http.Request) bool {
|
|
accept := req.Header.Values("Accept")
|
|
if len(accept) == 0 {
|
|
return false
|
|
}
|
|
types := strings.Split(accept[0], ",")
|
|
currentType, currentPriority := "", 0.0
|
|
for _, t := range types {
|
|
values := strings.Split(t, ";")
|
|
name := values[0]
|
|
if name != api.JsonMediaType && name != api.OctetStreamMediaType {
|
|
continue
|
|
}
|
|
// no params specified
|
|
if len(values) == 1 {
|
|
priority := 1.0
|
|
if priority > currentPriority {
|
|
currentType, currentPriority = name, priority
|
|
}
|
|
continue
|
|
}
|
|
params := values[1]
|
|
|
|
match := priorityRegex.FindAllStringSubmatch(params, 1)
|
|
if len(match) != 1 {
|
|
continue
|
|
}
|
|
priority, err := strconv.ParseFloat(match[0][1], 32)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if priority > currentPriority {
|
|
currentType, currentPriority = name, priority
|
|
}
|
|
}
|
|
|
|
return currentType == api.OctetStreamMediaType
|
|
}
|