netutil.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "log"
  18. "net"
  19. "net/http"
  20. "net/url"
  21. "reflect"
  22. "strings"
  23. )
  24. var (
  25. // indirection for testing
  26. resolveTCPAddr = net.ResolveTCPAddr
  27. )
  28. // ResolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr.
  29. // ResolveTCPAddrs resolves all DNS hostnames in-place for the given set of
  30. // url.URLs.
  31. func ResolveTCPAddrs(urls ...[]url.URL) error {
  32. for _, us := range urls {
  33. for i, u := range us {
  34. host, _, err := net.SplitHostPort(u.Host)
  35. if err != nil {
  36. log.Printf("netutil: Could not parse url %s during tcp resolving.", u.Host)
  37. return err
  38. }
  39. if host == "localhost" {
  40. continue
  41. }
  42. if net.ParseIP(host) != nil {
  43. continue
  44. }
  45. tcpAddr, err := resolveTCPAddr("tcp", u.Host)
  46. if err != nil {
  47. log.Printf("netutil: Could not resolve host: %s", u.Host)
  48. return err
  49. }
  50. log.Printf("netutil: Resolving %s to %s", u.Host, tcpAddr.String())
  51. us[i].Host = tcpAddr.String()
  52. }
  53. }
  54. return nil
  55. }
  56. // URLsEqual checks equality of url.URLS between two arrays.
  57. // This check pass even if an URL is in hostname and opposite is in IP address.
  58. func URLsEqual(a []url.URL, b []url.URL) bool {
  59. if len(a) != len(b) {
  60. return false
  61. }
  62. for i, urlA := range a {
  63. urlB := b[i]
  64. if !reflect.DeepEqual(urlA, urlB) {
  65. urls := []url.URL{urlA, urlB}
  66. ResolveTCPAddrs(urls)
  67. if !reflect.DeepEqual(urls[0], urls[1]) {
  68. return false
  69. }
  70. }
  71. }
  72. return true
  73. }
  74. func URLStringsEqual(a []string, b []string) bool {
  75. if len(a) != len(b) {
  76. return false
  77. }
  78. urlsA := make([]url.URL, len(a))
  79. for _, str := range a {
  80. u, err := url.Parse(str)
  81. if err != nil {
  82. return false
  83. }
  84. urlsA = append(urlsA, *u)
  85. }
  86. urlsB := make([]url.URL, len(b))
  87. for _, str := range b {
  88. u, err := url.Parse(str)
  89. if err != nil {
  90. return false
  91. }
  92. urlsB = append(urlsB, *u)
  93. }
  94. return URLsEqual(urlsA, urlsB)
  95. }
  96. // BasicAuth returns the username and password provided in the request's
  97. // Authorization header, if the request uses HTTP Basic Authentication.
  98. // See RFC 2617, Section 2.
  99. // Based on the BasicAuth method from the Golang standard lib.
  100. // TODO: use the standard lib BasicAuth method when we move to Go 1.4.
  101. func BasicAuth(r *http.Request) (username, password string, ok bool) {
  102. auth := r.Header.Get("Authorization")
  103. if auth == "" {
  104. return
  105. }
  106. return parseBasicAuth(auth)
  107. }
  108. // parseBasicAuth parses an HTTP Basic Authentication string.
  109. // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
  110. // Taken from the Golang standard lib.
  111. // TODO: use the standard lib BasicAuth method when we move to Go 1.4.
  112. func parseBasicAuth(auth string) (username, password string, ok bool) {
  113. if !strings.HasPrefix(auth, "Basic ") {
  114. return
  115. }
  116. c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(auth, "Basic "))
  117. if err != nil {
  118. return
  119. }
  120. cs := string(c)
  121. s := strings.IndexByte(cs, ':')
  122. if s < 0 {
  123. return
  124. }
  125. return cs[:s], cs[s+1:], true
  126. }