util.go 783 B

1234567891011121314151617181920212223242526272829
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package connections
  7. import (
  8. "net"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. )
  13. func fixupPort(uri *url.URL, defaultPort int) *url.URL {
  14. copyURI := *uri
  15. host, port, err := net.SplitHostPort(uri.Host)
  16. if err != nil && strings.Contains(err.Error(), "missing port") {
  17. // addr is on the form "1.2.3.4"
  18. copyURI.Host = net.JoinHostPort(uri.Host, strconv.Itoa(defaultPort))
  19. } else if err == nil && port == "" {
  20. // addr is on the form "1.2.3.4:"
  21. copyURI.Host = net.JoinHostPort(host, strconv.Itoa(defaultPort))
  22. }
  23. return &copyURI
  24. }