netutil.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package netutil
  15. import (
  16. "encoding/base64"
  17. "net"
  18. "net/http"
  19. "net/url"
  20. "reflect"
  21. "strings"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
  23. )
  24. var (
  25. plog = capnslog.NewPackageLogger("github.com/coreos/etcd/pkg", "netutil")
  26. // indirection for testing
  27. resolveTCPAddr = net.ResolveTCPAddr
  28. )
  29. // ResolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr.
  30. // ResolveTCPAddrs resolves all DNS hostnames in-place for the given set of
  31. // url.URLs.
  32. func ResolveTCPAddrs(urls ...[]url.URL) error {
  33. for _, us := range urls {
  34. for i, u := range us {
  35. host, _, err := net.SplitHostPort(u.Host)
  36. if err != nil {
  37. plog.Errorf("could not parse url %s during tcp resolving", u.Host)
  38. return err
  39. }
  40. if host == "localhost" {
  41. continue
  42. }
  43. if net.ParseIP(host) != nil {
  44. continue
  45. }
  46. tcpAddr, err := resolveTCPAddr("tcp", u.Host)
  47. if err != nil {
  48. plog.Errorf("could not resolve host %s", u.Host)
  49. return err
  50. }
  51. plog.Infof("resolving %s to %s", u.Host, tcpAddr.String())
  52. us[i].Host = tcpAddr.String()
  53. }
  54. }
  55. return nil
  56. }
  57. // URLsEqual checks equality of url.URLS between two arrays.
  58. // This check pass even if an URL is in hostname and opposite is in IP address.
  59. func URLsEqual(a []url.URL, b []url.URL) bool {
  60. if len(a) != len(b) {
  61. return false
  62. }
  63. for i, urlA := range a {
  64. urlB := b[i]
  65. if !reflect.DeepEqual(urlA, urlB) {
  66. urls := []url.URL{urlA, urlB}
  67. ResolveTCPAddrs(urls)
  68. if !reflect.DeepEqual(urls[0], urls[1]) {
  69. return false
  70. }
  71. }
  72. }
  73. return true
  74. }
  75. func URLStringsEqual(a []string, b []string) bool {
  76. if len(a) != len(b) {
  77. return false
  78. }
  79. urlsA := make([]url.URL, len(a))
  80. for _, str := range a {
  81. u, err := url.Parse(str)
  82. if err != nil {
  83. return false
  84. }
  85. urlsA = append(urlsA, *u)
  86. }
  87. urlsB := make([]url.URL, len(b))
  88. for _, str := range b {
  89. u, err := url.Parse(str)
  90. if err != nil {
  91. return false
  92. }
  93. urlsB = append(urlsB, *u)
  94. }
  95. return URLsEqual(urlsA, urlsB)
  96. }
  97. // BasicAuth returns the username and password provided in the request's
  98. // Authorization header, if the request uses HTTP Basic Authentication.
  99. // See RFC 2617, Section 2.
  100. // Based on the BasicAuth method from the Golang standard lib.
  101. // TODO: use the standard lib BasicAuth method when we move to Go 1.4.
  102. func BasicAuth(r *http.Request) (username, password string, ok bool) {
  103. auth := r.Header.Get("Authorization")
  104. if auth == "" {
  105. return
  106. }
  107. return parseBasicAuth(auth)
  108. }
  109. // parseBasicAuth parses an HTTP Basic Authentication string.
  110. // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
  111. // Taken from the Golang standard lib.
  112. // TODO: use the standard lib BasicAuth method when we move to Go 1.4.
  113. func parseBasicAuth(auth string) (username, password string, ok bool) {
  114. if !strings.HasPrefix(auth, "Basic ") {
  115. return
  116. }
  117. c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(auth, "Basic "))
  118. if err != nil {
  119. return
  120. }
  121. cs := string(c)
  122. s := strings.IndexByte(cs, ':')
  123. if s < 0 {
  124. return
  125. }
  126. return cs[:s], cs[s+1:], true
  127. }