netutil.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "strings"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
  22. )
  23. var (
  24. plog = capnslog.NewPackageLogger("github.com/coreos/etcd/pkg", "netutil")
  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. plog.Errorf("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. plog.Errorf("could not resolve host %s", u.Host)
  48. return err
  49. }
  50. plog.Infof("resolving %s to %s", u.Host, tcpAddr.String())
  51. us[i].Host = tcpAddr.String()
  52. }
  53. }
  54. return nil
  55. }
  56. // BasicAuth returns the username and password provided in the request's
  57. // Authorization header, if the request uses HTTP Basic Authentication.
  58. // See RFC 2617, Section 2.
  59. // Based on the BasicAuth method from the Golang standard lib.
  60. // TODO: use the standard lib BasicAuth method when we move to Go 1.4.
  61. func BasicAuth(r *http.Request) (username, password string, ok bool) {
  62. auth := r.Header.Get("Authorization")
  63. if auth == "" {
  64. return
  65. }
  66. return parseBasicAuth(auth)
  67. }
  68. // parseBasicAuth parses an HTTP Basic Authentication string.
  69. // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
  70. // Taken from the Golang standard lib.
  71. // TODO: use the standard lib BasicAuth method when we move to Go 1.4.
  72. func parseBasicAuth(auth string) (username, password string, ok bool) {
  73. if !strings.HasPrefix(auth, "Basic ") {
  74. return
  75. }
  76. c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(auth, "Basic "))
  77. if err != nil {
  78. return
  79. }
  80. cs := string(c)
  81. s := strings.IndexByte(cs, ':')
  82. if s < 0 {
  83. return
  84. }
  85. return cs[:s], cs[s+1:], true
  86. }