netutil.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "strings"
  22. )
  23. var (
  24. // indirection for testing
  25. resolveTCPAddr = net.ResolveTCPAddr
  26. )
  27. // ResolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr.
  28. // ResolveTCPAddrs resolves all DNS hostnames in-place for the given set of
  29. // url.URLs.
  30. func ResolveTCPAddrs(urls ...[]url.URL) error {
  31. for _, us := range urls {
  32. for i, u := range us {
  33. host, _, err := net.SplitHostPort(u.Host)
  34. if err != nil {
  35. log.Printf("netutil: Could not parse url %s during tcp resolving.", u.Host)
  36. return err
  37. }
  38. if host == "localhost" {
  39. continue
  40. }
  41. if net.ParseIP(host) != nil {
  42. continue
  43. }
  44. tcpAddr, err := resolveTCPAddr("tcp", u.Host)
  45. if err != nil {
  46. log.Printf("netutil: Could not resolve host: %s", u.Host)
  47. return err
  48. }
  49. log.Printf("netutil: Resolving %s to %s", u.Host, tcpAddr.String())
  50. us[i].Host = tcpAddr.String()
  51. }
  52. }
  53. return nil
  54. }
  55. // BasicAuth returns the username and password provided in the request's
  56. // Authorization header, if the request uses HTTP Basic Authentication.
  57. // See RFC 2617, Section 2.
  58. // Based on the BasicAuth method from the Golang standard lib.
  59. // TODO: use the standard lib BasicAuth method when we move to Go 1.4.
  60. func BasicAuth(r *http.Request) (username, password string, ok bool) {
  61. auth := r.Header.Get("Authorization")
  62. if auth == "" {
  63. return
  64. }
  65. return parseBasicAuth(auth)
  66. }
  67. // parseBasicAuth parses an HTTP Basic Authentication string.
  68. // "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
  69. // Taken from the Golang standard lib.
  70. // TODO: use the standard lib BasicAuth method when we move to Go 1.4.
  71. func parseBasicAuth(auth string) (username, password string, ok bool) {
  72. if !strings.HasPrefix(auth, "Basic ") {
  73. return
  74. }
  75. c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(auth, "Basic "))
  76. if err != nil {
  77. return
  78. }
  79. cs := string(c)
  80. s := strings.IndexByte(cs, ':')
  81. if s < 0 {
  82. return
  83. }
  84. return cs[:s], cs[s+1:], true
  85. }