caddy/middleware/path.go

45 lines
1.1 KiB
Go
Raw Normal View History

2015-01-30 13:08:40 +08:00
package middleware
import (
"os"
"strings"
)
const caseSensitivePathEnv = "CASE_SENSITIVE_PATH"
func init() {
initCaseSettings()
}
// CaseSensitivePath determines if paths should be case sensitive.
// This is configurable via CASE_SENSITIVE_PATH environment variable.
// It defaults to false.
var CaseSensitivePath = true
// initCaseSettings loads case sensitivity config from environment variable.
//
// This could have been in init, but init cannot be called from tests.
func initCaseSettings() {
switch os.Getenv(caseSensitivePathEnv) {
case "0", "false":
CaseSensitivePath = false
default:
CaseSensitivePath = true
}
}
2015-01-30 13:08:40 +08:00
// Path represents a URI path, maybe with pattern characters.
type Path string
2015-05-25 10:52:34 +08:00
// Matches checks to see if other matches p.
//
2015-01-30 13:08:40 +08:00
// Path matching will probably not always be a direct
// comparison; this method assures that paths can be
2015-04-18 23:54:35 +08:00
// easily and consistently matched.
2015-01-30 13:08:40 +08:00
func (p Path) Matches(other string) bool {
if CaseSensitivePath {
return strings.HasPrefix(string(p), other)
}
return strings.HasPrefix(strings.ToLower(string(p)), strings.ToLower(other))
2015-01-30 13:08:40 +08:00
}