netutil.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "sort"
  22. "strings"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
  24. "github.com/coreos/etcd/pkg/types"
  25. )
  26. var (
  27. plog = capnslog.NewPackageLogger("github.com/coreos/etcd/pkg", "netutil")
  28. // indirection for testing
  29. resolveTCPAddr = net.ResolveTCPAddr
  30. )
  31. // resolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr.
  32. // resolveTCPAddrs resolves all DNS hostnames in-place for the given set of
  33. // url.URLs.
  34. func resolveTCPAddrs(urls ...[]url.URL) error {
  35. for _, us := range urls {
  36. for i, u := range us {
  37. host, _, err := net.SplitHostPort(u.Host)
  38. if err != nil {
  39. plog.Errorf("could not parse url %s during tcp resolving", u.Host)
  40. return err
  41. }
  42. if host == "localhost" {
  43. continue
  44. }
  45. if net.ParseIP(host) != nil {
  46. continue
  47. }
  48. tcpAddr, err := resolveTCPAddr("tcp", u.Host)
  49. if err != nil {
  50. plog.Errorf("could not resolve host %s", u.Host)
  51. return err
  52. }
  53. plog.Infof("resolving %s to %s", u.Host, tcpAddr.String())
  54. us[i].Host = tcpAddr.String()
  55. }
  56. }
  57. return nil
  58. }
  59. // urlsEqual checks equality of url.URLS between two arrays.
  60. // This check pass even if an URL is in hostname and opposite is in IP address.
  61. func urlsEqual(a []url.URL, b []url.URL) bool {
  62. if len(a) != len(b) {
  63. return false
  64. }
  65. resolveTCPAddrs(a, b)
  66. sort.Sort(types.URLs(a))
  67. sort.Sort(types.URLs(b))
  68. for i := range a {
  69. if !reflect.DeepEqual(a[i], b[i]) {
  70. return false
  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, 0)
  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, 0)
  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. }