github.com/c0deoo1/golang1.5@v0.0.0-20220525150107-c87c805d4593/src/net/http/httputil/reverseproxy.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // HTTP reverse proxy handler 6 7 package httputil 8 9 import ( 10 "io" 11 "log" 12 "net" 13 "net/http" 14 "net/url" 15 "strings" 16 "sync" 17 "time" 18 ) 19 20 // onExitFlushLoop is a callback set by tests to detect the state of the 21 // flushLoop() goroutine. 22 var onExitFlushLoop func() 23 24 // ReverseProxy is an HTTP Handler that takes an incoming request and 25 // sends it to another server, proxying the response back to the 26 // client. 27 type ReverseProxy struct { 28 // Director must be a function which modifies 29 // the request into a new request to be sent 30 // using Transport. Its response is then copied 31 // back to the original client unmodified. 32 Director func(*http.Request) 33 34 // The transport used to perform proxy requests. 35 // If nil, http.DefaultTransport is used. 36 Transport http.RoundTripper 37 38 // FlushInterval specifies the flush interval 39 // to flush to the client while copying the 40 // response body. 41 // If zero, no periodic flushing is done. 42 FlushInterval time.Duration 43 44 // ErrorLog specifies an optional logger for errors 45 // that occur when attempting to proxy the request. 46 // If nil, logging goes to os.Stderr via the log package's 47 // standard logger. 48 ErrorLog *log.Logger 49 } 50 51 func singleJoiningSlash(a, b string) string { 52 aslash := strings.HasSuffix(a, "/") 53 bslash := strings.HasPrefix(b, "/") 54 switch { 55 case aslash && bslash: 56 return a + b[1:] 57 case !aslash && !bslash: 58 return a + "/" + b 59 } 60 return a + b 61 } 62 63 // NewSingleHostReverseProxy returns a new ReverseProxy that rewrites 64 // URLs to the scheme, host, and base path provided in target. If the 65 // target's path is "/base" and the incoming request was for "/dir", 66 // the target request will be for /base/dir. 67 func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy { 68 targetQuery := target.RawQuery 69 director := func(req *http.Request) { 70 req.URL.Scheme = target.Scheme 71 req.URL.Host = target.Host 72 req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path) 73 if targetQuery == "" || req.URL.RawQuery == "" { 74 req.URL.RawQuery = targetQuery + req.URL.RawQuery 75 } else { 76 req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery 77 } 78 } 79 return &ReverseProxy{Director: director} 80 } 81 82 func copyHeader(dst, src http.Header) { 83 for k, vv := range src { 84 for _, v := range vv { 85 dst.Add(k, v) 86 } 87 } 88 } 89 90 // Hop-by-hop headers. These are removed when sent to the backend. 91 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html 92 var hopHeaders = []string{ 93 "Connection", 94 "Keep-Alive", 95 "Proxy-Authenticate", 96 "Proxy-Authorization", 97 "Te", // canonicalized version of "TE" 98 "Trailers", 99 "Transfer-Encoding", 100 "Upgrade", 101 } 102 103 type requestCanceler interface { 104 CancelRequest(*http.Request) 105 } 106 107 type runOnFirstRead struct { 108 io.Reader 109 110 fn func() // Run before first Read, then set to nil 111 } 112 113 func (c *runOnFirstRead) Read(bs []byte) (int, error) { 114 if c.fn != nil { 115 c.fn() 116 c.fn = nil 117 } 118 return c.Reader.Read(bs) 119 } 120 121 func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { 122 transport := p.Transport 123 if transport == nil { 124 transport = http.DefaultTransport 125 } 126 127 outreq := new(http.Request) 128 *outreq = *req // includes shallow copies of maps, but okay 129 130 if closeNotifier, ok := rw.(http.CloseNotifier); ok { 131 if requestCanceler, ok := transport.(requestCanceler); ok { 132 reqDone := make(chan struct{}) 133 defer close(reqDone) 134 135 clientGone := closeNotifier.CloseNotify() 136 137 outreq.Body = struct { 138 io.Reader 139 io.Closer 140 }{ 141 Reader: &runOnFirstRead{ 142 Reader: outreq.Body, 143 fn: func() { 144 go func() { 145 select { 146 case <-clientGone: 147 requestCanceler.CancelRequest(outreq) 148 case <-reqDone: 149 } 150 }() 151 }, 152 }, 153 Closer: outreq.Body, 154 } 155 } 156 } 157 158 p.Director(outreq) 159 outreq.Proto = "HTTP/1.1" 160 outreq.ProtoMajor = 1 161 outreq.ProtoMinor = 1 162 outreq.Close = false 163 164 // Remove hop-by-hop headers to the backend. Especially 165 // important is "Connection" because we want a persistent 166 // connection, regardless of what the client sent to us. This 167 // is modifying the same underlying map from req (shallow 168 // copied above) so we only copy it if necessary. 169 copiedHeaders := false 170 for _, h := range hopHeaders { 171 if outreq.Header.Get(h) != "" { 172 if !copiedHeaders { 173 outreq.Header = make(http.Header) 174 copyHeader(outreq.Header, req.Header) 175 copiedHeaders = true 176 } 177 outreq.Header.Del(h) 178 } 179 } 180 181 if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil { 182 // If we aren't the first proxy retain prior 183 // X-Forwarded-For information as a comma+space 184 // separated list and fold multiple headers into one. 185 if prior, ok := outreq.Header["X-Forwarded-For"]; ok { 186 clientIP = strings.Join(prior, ", ") + ", " + clientIP 187 } 188 outreq.Header.Set("X-Forwarded-For", clientIP) 189 } 190 191 res, err := transport.RoundTrip(outreq) 192 if err != nil { 193 p.logf("http: proxy error: %v", err) 194 rw.WriteHeader(http.StatusInternalServerError) 195 return 196 } 197 198 for _, h := range hopHeaders { 199 res.Header.Del(h) 200 } 201 202 copyHeader(rw.Header(), res.Header) 203 204 // The "Trailer" header isn't included in the Transport's response, 205 // at least for *http.Transport. Build it up from Trailer. 206 if len(res.Trailer) > 0 { 207 var trailerKeys []string 208 for k := range res.Trailer { 209 trailerKeys = append(trailerKeys, k) 210 } 211 rw.Header().Add("Trailer", strings.Join(trailerKeys, ", ")) 212 } 213 214 rw.WriteHeader(res.StatusCode) 215 if len(res.Trailer) > 0 { 216 // Force chunking if we saw a response trailer. 217 // This prevents net/http from calculating the length for short 218 // bodies and adding a Content-Length. 219 if fl, ok := rw.(http.Flusher); ok { 220 fl.Flush() 221 } 222 } 223 p.copyResponse(rw, res.Body) 224 res.Body.Close() // close now, instead of defer, to populate res.Trailer 225 copyHeader(rw.Header(), res.Trailer) 226 } 227 228 func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) { 229 if p.FlushInterval != 0 { 230 if wf, ok := dst.(writeFlusher); ok { 231 mlw := &maxLatencyWriter{ 232 dst: wf, 233 latency: p.FlushInterval, 234 done: make(chan bool), 235 } 236 go mlw.flushLoop() 237 defer mlw.stop() 238 dst = mlw 239 } 240 } 241 242 io.Copy(dst, src) 243 } 244 245 func (p *ReverseProxy) logf(format string, args ...interface{}) { 246 if p.ErrorLog != nil { 247 p.ErrorLog.Printf(format, args...) 248 } else { 249 log.Printf(format, args...) 250 } 251 } 252 253 type writeFlusher interface { 254 io.Writer 255 http.Flusher 256 } 257 258 type maxLatencyWriter struct { 259 dst writeFlusher 260 latency time.Duration 261 262 lk sync.Mutex // protects Write + Flush 263 done chan bool 264 } 265 266 func (m *maxLatencyWriter) Write(p []byte) (int, error) { 267 m.lk.Lock() 268 defer m.lk.Unlock() 269 return m.dst.Write(p) 270 } 271 272 func (m *maxLatencyWriter) flushLoop() { 273 t := time.NewTicker(m.latency) 274 defer t.Stop() 275 for { 276 select { 277 case <-m.done: 278 if onExitFlushLoop != nil { 279 onExitFlushLoop() 280 } 281 return 282 case <-t.C: 283 m.lk.Lock() 284 m.dst.Flush() 285 m.lk.Unlock() 286 } 287 } 288 } 289 290 func (m *maxLatencyWriter) stop() { m.done <- true }