canonicalize.go 898 B

12345678910111213141516171819202122232425262728
  1. // Copyright 2012-2015 Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import "net/url"
  6. // canonicalize takes a list of URLs and returns its canonicalized form, i.e.
  7. // remove anything but scheme, userinfo, host, and port. It also removes the
  8. // slash at the end. It also skips invalid URLs or URLs that do not use
  9. // protocol http or https.
  10. //
  11. // Example:
  12. // http://127.0.0.1:9200/path?query=1 -> http://127.0.0.1:9200
  13. func canonicalize(rawurls ...string) []string {
  14. canonicalized := make([]string, 0)
  15. for _, rawurl := range rawurls {
  16. u, err := url.Parse(rawurl)
  17. if err == nil && (u.Scheme == "http" || u.Scheme == "https") {
  18. u.Fragment = ""
  19. u.Path = ""
  20. u.RawQuery = ""
  21. canonicalized = append(canonicalized, u.String())
  22. }
  23. }
  24. return canonicalized
  25. }