mirror of
https://github.com/dutchcoders/transfer.sh.git
synced 2024-11-24 05:00:18 +01:00
50 lines
969 B
Go
50 lines
969 B
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// Kind of match to apply to the header check.
|
|
type HeaderMatchType int
|
|
|
|
const (
|
|
HmEquals HeaderMatchType = iota
|
|
HmStartsWith
|
|
HmEndsWith
|
|
HmContains
|
|
)
|
|
|
|
// Check if the specified header matches the test string, applying the header match type
|
|
// specified.
|
|
func HeaderMatch(hdr http.Header, nm string, matchType HeaderMatchType, test string) bool {
|
|
// First get the header value
|
|
val := hdr[http.CanonicalHeaderKey(nm)]
|
|
if len(val) == 0 {
|
|
return false
|
|
}
|
|
// Prepare the match test
|
|
test = strings.ToLower(test)
|
|
for _, v := range val {
|
|
v = strings.Trim(strings.ToLower(v), " \n\t")
|
|
switch matchType {
|
|
case HmEquals:
|
|
if v == test {
|
|
return true
|
|
}
|
|
case HmStartsWith:
|
|
if strings.HasPrefix(v, test) {
|
|
return true
|
|
}
|
|
case HmEndsWith:
|
|
if strings.HasSuffix(v, test) {
|
|
return true
|
|
}
|
|
case HmContains:
|
|
if strings.Contains(v, test) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|