forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-openapi/jsonreference/internal/normalize_url.go

114 lines
1.7 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
package internal
import (
"net/url"
"regexp"
"strings"
)
const (
defaultHTTPPort = ":80"
2024-05-14 13:07:09 +00:00
defaultHTTPSPort = ":443"
)
// Regular expressions used by the normalizations
2024-05-14 13:07:09 +00:00
var rxPort = regexp.MustCompile(`(:\d+)/?$`)
2024-05-14 13:07:09 +00:00
var rxDupSlashes = regexp.MustCompile(`/{2,}`)
// NormalizeURL will normalize the specified URL
2024-05-14 13:07:09 +00:00
// This was added to replace a previous call to the no longer maintained purell library:
2024-05-14 13:07:09 +00:00
// The call that was used looked like the following:
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// url.Parse(purell.NormalizeURL(parsed, purell.FlagsSafe|purell.FlagRemoveDuplicateSlashes))
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// To explain all that was included in the call above, purell.FlagsSafe was really just the following:
2024-05-14 13:07:09 +00:00
// - FlagLowercaseScheme
2024-05-14 13:07:09 +00:00
// - FlagLowercaseHost
2024-05-14 13:07:09 +00:00
// - FlagRemoveDefaultPort
2024-05-14 13:07:09 +00:00
// - FlagRemoveDuplicateSlashes (and this was mixed in with the |)
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// This also normalizes the URL into its urlencoded form by removing RawPath and RawFragment.
2024-05-14 13:07:09 +00:00
func NormalizeURL(u *url.URL) {
2024-05-14 13:07:09 +00:00
lowercaseScheme(u)
2024-05-14 13:07:09 +00:00
lowercaseHost(u)
2024-05-14 13:07:09 +00:00
removeDefaultPort(u)
2024-05-14 13:07:09 +00:00
removeDuplicateSlashes(u)
u.RawPath = ""
2024-05-14 13:07:09 +00:00
u.RawFragment = ""
2024-05-14 13:07:09 +00:00
}
func lowercaseScheme(u *url.URL) {
2024-05-14 13:07:09 +00:00
if len(u.Scheme) > 0 {
2024-05-14 13:07:09 +00:00
u.Scheme = strings.ToLower(u.Scheme)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
func lowercaseHost(u *url.URL) {
2024-05-14 13:07:09 +00:00
if len(u.Host) > 0 {
2024-05-14 13:07:09 +00:00
u.Host = strings.ToLower(u.Host)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
func removeDefaultPort(u *url.URL) {
2024-05-14 13:07:09 +00:00
if len(u.Host) > 0 {
2024-05-14 13:07:09 +00:00
scheme := strings.ToLower(u.Scheme)
2024-05-14 13:07:09 +00:00
u.Host = rxPort.ReplaceAllStringFunc(u.Host, func(val string) string {
2024-05-14 13:07:09 +00:00
if (scheme == "http" && val == defaultHTTPPort) || (scheme == "https" && val == defaultHTTPSPort) {
2024-05-14 13:07:09 +00:00
return ""
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return val
2024-05-14 13:07:09 +00:00
})
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
func removeDuplicateSlashes(u *url.URL) {
2024-05-14 13:07:09 +00:00
if len(u.Path) > 0 {
2024-05-14 13:07:09 +00:00
u.Path = rxDupSlashes.ReplaceAllString(u.Path, "/")
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}