Compare commits
2 Commits
v0.2.0
...
a1f6966200
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a1f6966200 | ||
|
|
fa4c67b686 |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
# macOS Finder metadata
|
||||
.DS_Store
|
||||
45
middleware/headers.go
Normal file
45
middleware/headers.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// HeaderBool reads portal-proxy boolean headers.
|
||||
// Contract: "1", "true", "yes" and "on" are true; everything else is false.
|
||||
func HeaderBool(r *http.Request, name string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(r.Header.Get(name))) {
|
||||
case "1", "true", "yes", "on":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// HeaderCSV returns a trimmed non-empty CSV list from a portal-proxy header.
|
||||
func HeaderCSV(r *http.Request, name string) []string {
|
||||
raw := strings.TrimSpace(r.Header.Get(name))
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
parts := strings.Split(raw, ",")
|
||||
out := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
if v := strings.TrimSpace(part); v != "" {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// HeaderScope returns a lower-cased scope constrained to allowed values.
|
||||
// The fallback is returned when the header is empty or unknown.
|
||||
func HeaderScope(r *http.Request, name, fallback string, allowed ...string) string {
|
||||
value := strings.ToLower(strings.TrimSpace(r.Header.Get(name)))
|
||||
for _, option := range allowed {
|
||||
if value == strings.ToLower(strings.TrimSpace(option)) {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
Reference in New Issue
Block a user