Ignore Cloudflare Priority header
With these changes, If the web request contains the new Priority header (RFC 9218), The server will ignore it and continue searching for other headers or query parameters.pull/851/head
parent
45ca20dec9
commit
30a913c05c
|
@ -9,6 +9,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
|
/*"regexp"*/
|
||||||
)
|
)
|
||||||
|
|
||||||
var mimeDecoder mime.WordDecoder
|
var mimeDecoder mime.WordDecoder
|
||||||
|
@ -129,7 +130,25 @@ func fromContext[T any](r *http.Request, key contextKey) (T, error) {
|
||||||
func maybeDecodeHeader(header string) string {
|
func maybeDecodeHeader(header string) string {
|
||||||
decoded, err := mimeDecoder.DecodeHeader(header)
|
decoded, err := mimeDecoder.DecodeHeader(header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return header
|
return cloudflarePriorityIgnore(header)
|
||||||
}
|
}
|
||||||
return decoded
|
return cloudflarePriorityIgnore(decoded)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore new HTTP Priority header (see https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-priority)
|
||||||
|
// Cloudflare adds this to requests when forwarding to the backend (ntfy), so we just ignore it.
|
||||||
|
// If the Priority header is set to "u=*, i" or "u=*" (by cloudflare), the header will be ignored.
|
||||||
|
// And continue searching for another header (x-priority, prio, p) or in the Query parameters.
|
||||||
|
func cloudflarePriorityIgnore(value string) string {
|
||||||
|
if strings.HasPrefix(value, "u=") {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// The same but with regex
|
||||||
|
/* pattern := `^u=\d+\s*,\s*i|u=\d+$`
|
||||||
|
regex := regexp.MustCompile(pattern)
|
||||||
|
if regex.MatchString(value) {
|
||||||
|
return ""
|
||||||
|
} */
|
||||||
|
return value
|
||||||
}
|
}
|
||||||
|
|
|
@ -161,11 +161,6 @@ func ParsePriority(priority string) (int, error) {
|
||||||
case "5", "max", "urgent":
|
case "5", "max", "urgent":
|
||||||
return 5, nil
|
return 5, nil
|
||||||
default:
|
default:
|
||||||
// Ignore new HTTP Priority header (see https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-priority)
|
|
||||||
// Cloudflare adds this to requests when forwarding to the backend (ntfy), so we just ignore it.
|
|
||||||
if strings.HasPrefix(p, "u=") {
|
|
||||||
return 3, nil
|
|
||||||
}
|
|
||||||
return 0, errInvalidPriority
|
return 0, errInvalidPriority
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue