reverse.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 proxy
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "io/ioutil"
  20. "log"
  21. "net"
  22. "net/http"
  23. "net/url"
  24. "strings"
  25. "github.com/coreos/etcd/etcdserver/etcdhttp/httptypes"
  26. )
  27. // Hop-by-hop headers. These are removed when sent to the backend.
  28. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
  29. // This list of headers borrowed from stdlib httputil.ReverseProxy
  30. var singleHopHeaders = []string{
  31. "Connection",
  32. "Keep-Alive",
  33. "Proxy-Authenticate",
  34. "Proxy-Authorization",
  35. "Te", // canonicalized version of "TE"
  36. "Trailers",
  37. "Transfer-Encoding",
  38. "Upgrade",
  39. }
  40. func removeSingleHopHeaders(hdrs *http.Header) {
  41. for _, h := range singleHopHeaders {
  42. hdrs.Del(h)
  43. }
  44. }
  45. type reverseProxy struct {
  46. director *director
  47. transport http.RoundTripper
  48. }
  49. func (p *reverseProxy) ServeHTTP(rw http.ResponseWriter, clientreq *http.Request) {
  50. proxyreq := new(http.Request)
  51. *proxyreq = *clientreq
  52. var (
  53. proxybody []byte
  54. err error
  55. )
  56. if clientreq.Body != nil {
  57. proxybody, err = ioutil.ReadAll(clientreq.Body)
  58. if err != nil {
  59. msg := fmt.Sprintf("proxy: failed to read request body: %v", err)
  60. e := httptypes.NewHTTPError(http.StatusInternalServerError, msg)
  61. e.WriteTo(rw)
  62. return
  63. }
  64. }
  65. // deep-copy the headers, as these will be modified below
  66. proxyreq.Header = make(http.Header)
  67. copyHeader(proxyreq.Header, clientreq.Header)
  68. normalizeRequest(proxyreq)
  69. removeSingleHopHeaders(&proxyreq.Header)
  70. maybeSetForwardedFor(proxyreq)
  71. endpoints := p.director.endpoints()
  72. if len(endpoints) == 0 {
  73. msg := "proxy: zero endpoints currently available"
  74. // TODO: limit the rate of the error logging.
  75. log.Printf(msg)
  76. e := httptypes.NewHTTPError(http.StatusServiceUnavailable, msg)
  77. e.WriteTo(rw)
  78. return
  79. }
  80. completeCh := make(chan bool, 1)
  81. closeNotifier, ok := rw.(http.CloseNotifier)
  82. if ok {
  83. go func() {
  84. select {
  85. case <-closeNotifier.CloseNotify():
  86. tp, ok := p.transport.(*http.Transport)
  87. if ok {
  88. tp.CancelRequest(proxyreq)
  89. }
  90. case <-completeCh:
  91. }
  92. }()
  93. defer func() {
  94. completeCh <- true
  95. }()
  96. }
  97. var res *http.Response
  98. for _, ep := range endpoints {
  99. if proxybody != nil {
  100. proxyreq.Body = ioutil.NopCloser(bytes.NewBuffer(proxybody))
  101. }
  102. redirectRequest(proxyreq, ep.URL)
  103. res, err = p.transport.RoundTrip(proxyreq)
  104. if err != nil {
  105. log.Printf("proxy: failed to direct request to %s: %v", ep.URL.String(), err)
  106. ep.Failed()
  107. continue
  108. }
  109. break
  110. }
  111. if res == nil {
  112. // TODO: limit the rate of the error logging.
  113. msg := fmt.Sprintf("proxy: unable to get response from %d endpoint(s)", len(endpoints))
  114. log.Printf(msg)
  115. e := httptypes.NewHTTPError(http.StatusBadGateway, msg)
  116. e.WriteTo(rw)
  117. return
  118. }
  119. defer res.Body.Close()
  120. removeSingleHopHeaders(&res.Header)
  121. copyHeader(rw.Header(), res.Header)
  122. rw.WriteHeader(res.StatusCode)
  123. io.Copy(rw, res.Body)
  124. }
  125. func copyHeader(dst, src http.Header) {
  126. for k, vv := range src {
  127. for _, v := range vv {
  128. dst.Add(k, v)
  129. }
  130. }
  131. }
  132. func redirectRequest(req *http.Request, loc url.URL) {
  133. req.URL.Scheme = loc.Scheme
  134. req.URL.Host = loc.Host
  135. }
  136. func normalizeRequest(req *http.Request) {
  137. req.Proto = "HTTP/1.1"
  138. req.ProtoMajor = 1
  139. req.ProtoMinor = 1
  140. req.Close = false
  141. }
  142. func maybeSetForwardedFor(req *http.Request) {
  143. clientIP, _, err := net.SplitHostPort(req.RemoteAddr)
  144. if err != nil {
  145. return
  146. }
  147. // If we aren't the first proxy retain prior
  148. // X-Forwarded-For information as a comma+space
  149. // separated list and fold multiple headers into one.
  150. if prior, ok := req.Header["X-Forwarded-For"]; ok {
  151. clientIP = strings.Join(prior, ", ") + ", " + clientIP
  152. }
  153. req.Header.Set("X-Forwarded-For", clientIP)
  154. }