diff options
Diffstat (limited to 'vendor/github.com/PuerkitoBio/purell/purell.go')
-rw-r--r-- | vendor/github.com/PuerkitoBio/purell/purell.go | 70 |
1 files changed, 22 insertions, 48 deletions
diff --git a/vendor/github.com/PuerkitoBio/purell/purell.go b/vendor/github.com/PuerkitoBio/purell/purell.go index 6d0fc19..98010c0 100644 --- a/vendor/github.com/PuerkitoBio/purell/purell.go +++ b/vendor/github.com/PuerkitoBio/purell/purell.go @@ -12,11 +12,6 @@ import ( "sort" "strconv" "strings" - - "github.com/PuerkitoBio/urlesc" - "golang.org/x/net/idna" - "golang.org/x/text/unicode/norm" - "golang.org/x/text/width" ) // A set of normalization flags determines how a URL will @@ -25,7 +20,7 @@ type NormalizationFlags uint const ( // Safe normalizations - FlagLowercaseScheme NormalizationFlags = 1 << iota // HTTP://host -> http://host, applied by default in Go1.1 + FlagLowercaseScheme NormalizationFlags = 1 << iota // HTTP://host -> http://host FlagLowercaseHost // http://HOST -> http://host FlagUppercaseEscapes // http://host/t%ef -> http://host/t%EF FlagDecodeUnnecessaryEscapes // http://host/t%41 -> http://host/tA @@ -140,36 +135,23 @@ var flags = map[NormalizationFlags]func(*url.URL){ // MustNormalizeURLString returns the normalized string, and panics if an error occurs. // It takes an URL string as input, as well as the normalization flags. func MustNormalizeURLString(u string, f NormalizationFlags) string { - result, e := NormalizeURLString(u, f) - if e != nil { + if parsed, e := url.Parse(u); e != nil { panic(e) + } else { + return NormalizeURL(parsed, f) } - return result + panic("Unreachable code.") } // NormalizeURLString returns the normalized string, or an error if it can't be parsed into an URL object. // It takes an URL string as input, as well as the normalization flags. func NormalizeURLString(u string, f NormalizationFlags) (string, error) { - parsed, err := url.Parse(u) - if err != nil { - return "", err - } - - if f&FlagLowercaseHost == FlagLowercaseHost { - parsed.Host = strings.ToLower(parsed.Host) + if parsed, e := url.Parse(u); e != nil { + return "", e + } else { + return NormalizeURL(parsed, f), nil } - - // The idna package doesn't fully conform to RFC 5895 - // (https://tools.ietf.org/html/rfc5895), so we do it here. - // Taken from Go 1.8 cycle source, courtesy of bradfitz. - // TODO: Remove when (if?) idna package conforms to RFC 5895. - parsed.Host = width.Fold.String(parsed.Host) - parsed.Host = norm.NFC.String(parsed.Host) - if parsed.Host, err = idna.ToASCII(parsed.Host); err != nil { - return "", err - } - - return NormalizeURL(parsed, f), nil + panic("Unreachable code.") } // NormalizeURL returns the normalized string. @@ -180,7 +162,7 @@ func NormalizeURL(u *url.URL, f NormalizationFlags) string { flags[k](u) } } - return urlesc.Escape(u) + return u.String() } func lowercaseScheme(u *url.URL) { @@ -208,26 +190,18 @@ func removeDefaultPort(u *url.URL) { } func removeTrailingSlash(u *url.URL) { - if l := len(u.Path); l > 0 { - if strings.HasSuffix(u.Path, "/") { - u.Path = u.Path[:l-1] - } - } else if l = len(u.Host); l > 0 { - if strings.HasSuffix(u.Host, "/") { - u.Host = u.Host[:l-1] - } + if l := len(u.Path); l > 0 && strings.HasSuffix(u.Path, "/") { + u.Path = u.Path[:l-1] + } else if l = len(u.Host); l > 0 && strings.HasSuffix(u.Host, "/") { + u.Host = u.Host[:l-1] } } func addTrailingSlash(u *url.URL) { - if l := len(u.Path); l > 0 { - if !strings.HasSuffix(u.Path, "/") { - u.Path += "/" - } - } else if l = len(u.Host); l > 0 { - if !strings.HasSuffix(u.Host, "/") { - u.Host += "/" - } + if l := len(u.Path); l > 0 && !strings.HasSuffix(u.Path, "/") { + u.Path += "/" + } else if l = len(u.Host); l > 0 && !strings.HasSuffix(u.Host, "/") { + u.Host += "/" } } @@ -249,7 +223,7 @@ func removeDotSegments(u *url.URL) { } // Special case if host does not end with / and new path does not begin with / u.Path = strings.Join(dotFree, "/") - if u.Host != "" && !strings.HasSuffix(u.Host, "/") && !strings.HasPrefix(u.Path, "/") { + if !strings.HasSuffix(u.Host, "/") && !strings.HasPrefix(u.Path, "/") { u.Path = "/" + u.Path } // Special case if the last segment was a dot, make sure the path ends with a slash @@ -299,7 +273,7 @@ func sortQuery(u *url.URL) { if len(q) > 0 { arKeys := make([]string, len(q)) i := 0 - for k := range q { + for k, _ := range q { arKeys[i] = k i++ } @@ -311,7 +285,7 @@ func sortQuery(u *url.URL) { if buf.Len() > 0 { buf.WriteRune('&') } - buf.WriteString(fmt.Sprintf("%s=%s", k, urlesc.QueryEscape(v))) + buf.WriteString(fmt.Sprintf("%s=%s", k, url.QueryEscape(v))) } } |