reverseproxy.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. // HTTP reverse proxy handler
  5. package httputil
  6. import (
  7. "context"
  8. "fmt"
  9. "io"
  10. "log"
  11. "mime"
  12. "net"
  13. "net/http"
  14. "net/http/internal/ascii"
  15. "net/textproto"
  16. "net/url"
  17. "strings"
  18. "sync"
  19. "time"
  20. "golang.org/x/net/http/httpguts"
  21. )
  22. // ReverseProxy is an HTTP Handler that takes an incoming request and
  23. // sends it to another server, proxying the response back to the
  24. // client.
  25. //
  26. // ReverseProxy by default sets the client IP as the value of the
  27. // X-Forwarded-For header.
  28. //
  29. // If an X-Forwarded-For header already exists, the client IP is
  30. // appended to the existing values. As a special case, if the header
  31. // exists in the Request.Header map but has a nil value (such as when
  32. // set by the Director func), the X-Forwarded-For header is
  33. // not modified.
  34. //
  35. // To prevent IP spoofing, be sure to delete any pre-existing
  36. // X-Forwarded-For header coming from the client or
  37. // an untrusted proxy.
  38. type ReverseProxy struct {
  39. // Director must be a function which modifies
  40. // the request into a new request to be sent
  41. // using Transport. Its response is then copied
  42. // back to the original client unmodified.
  43. // Director must not access the provided Request
  44. // after returning.
  45. Director func(*http.Request)
  46. // The transport used to perform proxy requests.
  47. // If nil, http.DefaultTransport is used.
  48. Transport http.RoundTripper
  49. // FlushInterval specifies the flush interval
  50. // to flush to the client while copying the
  51. // response body.
  52. // If zero, no periodic flushing is done.
  53. // A negative value means to flush immediately
  54. // after each write to the client.
  55. // The FlushInterval is ignored when ReverseProxy
  56. // recognizes a response as a streaming response, or
  57. // if its ContentLength is -1; for such responses, writes
  58. // are flushed to the client immediately.
  59. FlushInterval time.Duration
  60. // ErrorLog specifies an optional logger for errors
  61. // that occur when attempting to proxy the request.
  62. // If nil, logging is done via the log package's standard logger.
  63. ErrorLog *log.Logger
  64. // BufferPool optionally specifies a buffer pool to
  65. // get byte slices for use by io.CopyBuffer when
  66. // copying HTTP response bodies.
  67. BufferPool BufferPool
  68. // ModifyResponse is an optional function that modifies the
  69. // Response from the backend. It is called if the backend
  70. // returns a response at all, with any HTTP status code.
  71. // If the backend is unreachable, the optional ErrorHandler is
  72. // called without any call to ModifyResponse.
  73. //
  74. // If ModifyResponse returns an error, ErrorHandler is called
  75. // with its error value. If ErrorHandler is nil, its default
  76. // implementation is used.
  77. ModifyResponse func(*http.Response) error
  78. // ErrorHandler is an optional function that handles errors
  79. // reaching the backend or errors from ModifyResponse.
  80. //
  81. // If nil, the default is to log the provided error and return
  82. // a 502 Status Bad Gateway response.
  83. ErrorHandler func(http.ResponseWriter, *http.Request, error)
  84. }
  85. // A BufferPool is an interface for getting and returning temporary
  86. // byte slices for use by io.CopyBuffer.
  87. type BufferPool interface {
  88. Get() []byte
  89. Put([]byte)
  90. }
  91. func singleJoiningSlash(a, b string) string {
  92. aslash := strings.HasSuffix(a, "/")
  93. bslash := strings.HasPrefix(b, "/")
  94. switch {
  95. case aslash && bslash:
  96. return a + b[1:]
  97. case !aslash && !bslash:
  98. return a + "/" + b
  99. }
  100. return a + b
  101. }
  102. func joinURLPath(a, b *url.URL) (path, rawpath string) {
  103. if a.RawPath == "" && b.RawPath == "" {
  104. return singleJoiningSlash(a.Path, b.Path), ""
  105. }
  106. // Same as singleJoiningSlash, but uses EscapedPath to determine
  107. // whether a slash should be added
  108. apath := a.EscapedPath()
  109. bpath := b.EscapedPath()
  110. aslash := strings.HasSuffix(apath, "/")
  111. bslash := strings.HasPrefix(bpath, "/")
  112. switch {
  113. case aslash && bslash:
  114. return a.Path + b.Path[1:], apath + bpath[1:]
  115. case !aslash && !bslash:
  116. return a.Path + "/" + b.Path, apath + "/" + bpath
  117. }
  118. return a.Path + b.Path, apath + bpath
  119. }
  120. // NewSingleHostReverseProxy returns a new ReverseProxy that routes
  121. // URLs to the scheme, host, and base path provided in target. If the
  122. // target's path is "/base" and the incoming request was for "/dir",
  123. // the target request will be for /base/dir.
  124. // NewSingleHostReverseProxy does not rewrite the Host header.
  125. // To rewrite Host headers, use ReverseProxy directly with a custom
  126. // Director policy.
  127. func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
  128. targetQuery := target.RawQuery
  129. director := func(req *http.Request) {
  130. req.URL.Scheme = target.Scheme
  131. req.URL.Host = target.Host
  132. req.URL.Path, req.URL.RawPath = joinURLPath(target, req.URL)
  133. if targetQuery == "" || req.URL.RawQuery == "" {
  134. req.URL.RawQuery = targetQuery + req.URL.RawQuery
  135. } else {
  136. req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
  137. }
  138. if _, ok := req.Header["User-Agent"]; !ok {
  139. // explicitly disable User-Agent so it's not set to default value
  140. req.Header.Set("User-Agent", "")
  141. }
  142. }
  143. return &ReverseProxy{Director: director}
  144. }
  145. func copyHeader(dst, src http.Header) {
  146. for k, vv := range src {
  147. for _, v := range vv {
  148. dst.Add(k, v)
  149. }
  150. }
  151. }
  152. // Hop-by-hop headers. These are removed when sent to the backend.
  153. // As of RFC 7230, hop-by-hop headers are required to appear in the
  154. // Connection header field. These are the headers defined by the
  155. // obsoleted RFC 2616 (section 13.5.1) and are used for backward
  156. // compatibility.
  157. var hopHeaders = []string{
  158. "Connection",
  159. "Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
  160. "Keep-Alive",
  161. "Proxy-Authenticate",
  162. "Proxy-Authorization",
  163. "Te", // canonicalized version of "TE"
  164. "Trailer", // not Trailers per URL above; https://www.rfc-editor.org/errata_search.php?eid=4522
  165. "Transfer-Encoding",
  166. "Upgrade",
  167. }
  168. func (p *ReverseProxy) defaultErrorHandler(rw http.ResponseWriter, req *http.Request, err error) {
  169. p.logf("http: proxy error: %v", err)
  170. rw.WriteHeader(http.StatusBadGateway)
  171. }
  172. func (p *ReverseProxy) getErrorHandler() func(http.ResponseWriter, *http.Request, error) {
  173. if p.ErrorHandler != nil {
  174. return p.ErrorHandler
  175. }
  176. return p.defaultErrorHandler
  177. }
  178. // modifyResponse conditionally runs the optional ModifyResponse hook
  179. // and reports whether the request should proceed.
  180. func (p *ReverseProxy) modifyResponse(rw http.ResponseWriter, res *http.Response, req *http.Request) bool {
  181. if p.ModifyResponse == nil {
  182. return true
  183. }
  184. if err := p.ModifyResponse(res); err != nil {
  185. res.Body.Close()
  186. p.getErrorHandler()(rw, req, err)
  187. return false
  188. }
  189. return true
  190. }
  191. func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
  192. transport := p.Transport
  193. if transport == nil {
  194. transport = http.DefaultTransport
  195. }
  196. ctx := req.Context()
  197. if cn, ok := rw.(http.CloseNotifier); ok {
  198. var cancel context.CancelFunc
  199. ctx, cancel = context.WithCancel(ctx)
  200. defer cancel()
  201. notifyChan := cn.CloseNotify()
  202. go func() {
  203. select {
  204. case <-notifyChan:
  205. cancel()
  206. case <-ctx.Done():
  207. }
  208. }()
  209. }
  210. outreq := req.Clone(ctx)
  211. if req.ContentLength == 0 {
  212. outreq.Body = nil // Issue 16036: nil Body for http.Transport retries
  213. }
  214. if outreq.Body != nil {
  215. // Reading from the request body after returning from a handler is not
  216. // allowed, and the RoundTrip goroutine that reads the Body can outlive
  217. // this handler. This can lead to a crash if the handler panics (see
  218. // Issue 46866). Although calling Close doesn't guarantee there isn't
  219. // any Read in flight after the handle returns, in practice it's safe to
  220. // read after closing it.
  221. defer outreq.Body.Close()
  222. }
  223. if outreq.Header == nil {
  224. outreq.Header = make(http.Header) // Issue 33142: historical behavior was to always allocate
  225. }
  226. p.Director(outreq)
  227. outreq.Close = false
  228. reqUpType := upgradeType(outreq.Header)
  229. if !ascii.IsPrint(reqUpType) {
  230. p.getErrorHandler()(rw, req, fmt.Errorf("client tried to switch to invalid protocol %q", reqUpType))
  231. return
  232. }
  233. removeConnectionHeaders(outreq.Header)
  234. // Remove hop-by-hop headers to the backend. Especially
  235. // important is "Connection" because we want a persistent
  236. // connection, regardless of what the client sent to us.
  237. for _, h := range hopHeaders {
  238. outreq.Header.Del(h)
  239. }
  240. // Issue 21096: tell backend applications that care about trailer support
  241. // that we support trailers. (We do, but we don't go out of our way to
  242. // advertise that unless the incoming client request thought it was worth
  243. // mentioning.) Note that we look at req.Header, not outreq.Header, since
  244. // the latter has passed through removeConnectionHeaders.
  245. if httpguts.HeaderValuesContainsToken(req.Header["Te"], "trailers") {
  246. outreq.Header.Set("Te", "trailers")
  247. }
  248. // After stripping all the hop-by-hop connection headers above, add back any
  249. // necessary for protocol upgrades, such as for websockets.
  250. if reqUpType != "" {
  251. outreq.Header.Set("Connection", "Upgrade")
  252. outreq.Header.Set("Upgrade", reqUpType)
  253. }
  254. if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
  255. // If we aren't the first proxy retain prior
  256. // X-Forwarded-For information as a comma+space
  257. // separated list and fold multiple headers into one.
  258. prior, ok := outreq.Header["X-Forwarded-For"]
  259. omit := ok && prior == nil // Issue 38079: nil now means don't populate the header
  260. if len(prior) > 0 {
  261. clientIP = strings.Join(prior, ", ") + ", " + clientIP
  262. }
  263. if !omit {
  264. outreq.Header.Set("X-Forwarded-For", clientIP)
  265. }
  266. }
  267. res, err := transport.RoundTrip(outreq)
  268. if err != nil {
  269. p.getErrorHandler()(rw, outreq, err)
  270. return
  271. }
  272. // Deal with 101 Switching Protocols responses: (WebSocket, h2c, etc)
  273. if res.StatusCode == http.StatusSwitchingProtocols {
  274. if !p.modifyResponse(rw, res, outreq) {
  275. return
  276. }
  277. p.handleUpgradeResponse(rw, outreq, res)
  278. return
  279. }
  280. removeConnectionHeaders(res.Header)
  281. for _, h := range hopHeaders {
  282. res.Header.Del(h)
  283. }
  284. if !p.modifyResponse(rw, res, outreq) {
  285. return
  286. }
  287. copyHeader(rw.Header(), res.Header)
  288. // The "Trailer" header isn't included in the Transport's response,
  289. // at least for *http.Transport. Build it up from Trailer.
  290. announcedTrailers := len(res.Trailer)
  291. if announcedTrailers > 0 {
  292. trailerKeys := make([]string, 0, len(res.Trailer))
  293. for k := range res.Trailer {
  294. trailerKeys = append(trailerKeys, k)
  295. }
  296. rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
  297. }
  298. rw.WriteHeader(res.StatusCode)
  299. err = p.copyResponse(rw, res.Body, p.flushInterval(res))
  300. if err != nil {
  301. defer res.Body.Close()
  302. // Since we're streaming the response, if we run into an error all we can do
  303. // is abort the request. Issue 23643: ReverseProxy should use ErrAbortHandler
  304. // on read error while copying body.
  305. if !shouldPanicOnCopyError(req) {
  306. p.logf("suppressing panic for copyResponse error in test; copy error: %v", err)
  307. return
  308. }
  309. panic(http.ErrAbortHandler)
  310. }
  311. res.Body.Close() // close now, instead of defer, to populate res.Trailer
  312. if len(res.Trailer) > 0 {
  313. // Force chunking if we saw a response trailer.
  314. // This prevents net/http from calculating the length for short
  315. // bodies and adding a Content-Length.
  316. if fl, ok := rw.(http.Flusher); ok {
  317. fl.Flush()
  318. }
  319. }
  320. if len(res.Trailer) == announcedTrailers {
  321. copyHeader(rw.Header(), res.Trailer)
  322. return
  323. }
  324. for k, vv := range res.Trailer {
  325. k = http.TrailerPrefix + k
  326. for _, v := range vv {
  327. rw.Header().Add(k, v)
  328. }
  329. }
  330. }
  331. var inOurTests bool // whether we're in our own tests
  332. // shouldPanicOnCopyError reports whether the reverse proxy should
  333. // panic with http.ErrAbortHandler. This is the right thing to do by
  334. // default, but Go 1.10 and earlier did not, so existing unit tests
  335. // weren't expecting panics. Only panic in our own tests, or when
  336. // running under the HTTP server.
  337. func shouldPanicOnCopyError(req *http.Request) bool {
  338. if inOurTests {
  339. // Our tests know to handle this panic.
  340. return true
  341. }
  342. if req.Context().Value(http.ServerContextKey) != nil {
  343. // We seem to be running under an HTTP server, so
  344. // it'll recover the panic.
  345. return true
  346. }
  347. // Otherwise act like Go 1.10 and earlier to not break
  348. // existing tests.
  349. return false
  350. }
  351. // removeConnectionHeaders removes hop-by-hop headers listed in the "Connection" header of h.
  352. // See RFC 7230, section 6.1
  353. func removeConnectionHeaders(h http.Header) {
  354. for _, f := range h["Connection"] {
  355. for _, sf := range strings.Split(f, ",") {
  356. if sf = textproto.TrimString(sf); sf != "" {
  357. h.Del(sf)
  358. }
  359. }
  360. }
  361. }
  362. // flushInterval returns the p.FlushInterval value, conditionally
  363. // overriding its value for a specific request/response.
  364. func (p *ReverseProxy) flushInterval(res *http.Response) time.Duration {
  365. resCT := res.Header.Get("Content-Type")
  366. // For Server-Sent Events responses, flush immediately.
  367. // The MIME type is defined in https://www.w3.org/TR/eventsource/#text-event-stream
  368. if baseCT, _, _ := mime.ParseMediaType(resCT); baseCT == "text/event-stream" {
  369. return -1 // negative means immediately
  370. }
  371. // We might have the case of streaming for which Content-Length might be unset.
  372. if res.ContentLength == -1 {
  373. return -1
  374. }
  375. return p.FlushInterval
  376. }
  377. func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader, flushInterval time.Duration) error {
  378. if flushInterval != 0 {
  379. if wf, ok := dst.(writeFlusher); ok {
  380. mlw := &maxLatencyWriter{
  381. dst: wf,
  382. latency: flushInterval,
  383. }
  384. defer mlw.stop()
  385. // set up initial timer so headers get flushed even if body writes are delayed
  386. mlw.flushPending = true
  387. mlw.t = time.AfterFunc(flushInterval, mlw.delayedFlush)
  388. dst = mlw
  389. }
  390. }
  391. var buf []byte
  392. if p.BufferPool != nil {
  393. buf = p.BufferPool.Get()
  394. defer p.BufferPool.Put(buf)
  395. }
  396. _, err := p.copyBuffer(dst, src, buf)
  397. return err
  398. }
  399. // copyBuffer returns any write errors or non-EOF read errors, and the amount
  400. // of bytes written.
  401. func (p *ReverseProxy) copyBuffer(dst io.Writer, src io.Reader, buf []byte) (int64, error) {
  402. if len(buf) == 0 {
  403. buf = make([]byte, 32*1024)
  404. }
  405. var written int64
  406. for {
  407. nr, rerr := src.Read(buf)
  408. if rerr != nil && rerr != io.EOF && rerr != context.Canceled {
  409. p.logf("httputil: ReverseProxy read error during body copy: %v", rerr)
  410. }
  411. if nr > 0 {
  412. nw, werr := dst.Write(buf[:nr])
  413. if nw > 0 {
  414. written += int64(nw)
  415. }
  416. if werr != nil {
  417. return written, werr
  418. }
  419. if nr != nw {
  420. return written, io.ErrShortWrite
  421. }
  422. }
  423. if rerr != nil {
  424. if rerr == io.EOF {
  425. rerr = nil
  426. }
  427. return written, rerr
  428. }
  429. }
  430. }
  431. func (p *ReverseProxy) logf(format string, args ...any) {
  432. if p.ErrorLog != nil {
  433. p.ErrorLog.Printf(format, args...)
  434. } else {
  435. log.Printf(format, args...)
  436. }
  437. }
  438. type writeFlusher interface {
  439. io.Writer
  440. http.Flusher
  441. }
  442. type maxLatencyWriter struct {
  443. dst writeFlusher
  444. latency time.Duration // non-zero; negative means to flush immediately
  445. mu sync.Mutex // protects t, flushPending, and dst.Flush
  446. t *time.Timer
  447. flushPending bool
  448. }
  449. func (m *maxLatencyWriter) Write(p []byte) (n int, err error) {
  450. m.mu.Lock()
  451. defer m.mu.Unlock()
  452. n, err = m.dst.Write(p)
  453. if m.latency < 0 {
  454. m.dst.Flush()
  455. return
  456. }
  457. if m.flushPending {
  458. return
  459. }
  460. if m.t == nil {
  461. m.t = time.AfterFunc(m.latency, m.delayedFlush)
  462. } else {
  463. m.t.Reset(m.latency)
  464. }
  465. m.flushPending = true
  466. return
  467. }
  468. func (m *maxLatencyWriter) delayedFlush() {
  469. m.mu.Lock()
  470. defer m.mu.Unlock()
  471. if !m.flushPending { // if stop was called but AfterFunc already started this goroutine
  472. return
  473. }
  474. m.dst.Flush()
  475. m.flushPending = false
  476. }
  477. func (m *maxLatencyWriter) stop() {
  478. m.mu.Lock()
  479. defer m.mu.Unlock()
  480. m.flushPending = false
  481. if m.t != nil {
  482. m.t.Stop()
  483. }
  484. }
  485. func upgradeType(h http.Header) string {
  486. if !httpguts.HeaderValuesContainsToken(h["Connection"], "Upgrade") {
  487. return ""
  488. }
  489. return h.Get("Upgrade")
  490. }
  491. func (p *ReverseProxy) handleUpgradeResponse(rw http.ResponseWriter, req *http.Request, res *http.Response) {
  492. reqUpType := upgradeType(req.Header)
  493. resUpType := upgradeType(res.Header)
  494. if !ascii.IsPrint(resUpType) { // We know reqUpType is ASCII, it's checked by the caller.
  495. p.getErrorHandler()(rw, req, fmt.Errorf("backend tried to switch to invalid protocol %q", resUpType))
  496. }
  497. if !ascii.EqualFold(reqUpType, resUpType) {
  498. p.getErrorHandler()(rw, req, fmt.Errorf("backend tried to switch protocol %q when %q was requested", resUpType, reqUpType))
  499. return
  500. }
  501. hj, ok := rw.(http.Hijacker)
  502. if !ok {
  503. p.getErrorHandler()(rw, req, fmt.Errorf("can't switch protocols using non-Hijacker ResponseWriter type %T", rw))
  504. return
  505. }
  506. backConn, ok := res.Body.(io.ReadWriteCloser)
  507. if !ok {
  508. p.getErrorHandler()(rw, req, fmt.Errorf("internal error: 101 switching protocols response with non-writable body"))
  509. return
  510. }
  511. backConnCloseCh := make(chan bool)
  512. go func() {
  513. // Ensure that the cancellation of a request closes the backend.
  514. // See issue https://golang.org/issue/35559.
  515. select {
  516. case <-req.Context().Done():
  517. case <-backConnCloseCh:
  518. }
  519. backConn.Close()
  520. }()
  521. defer close(backConnCloseCh)
  522. conn, brw, err := hj.Hijack()
  523. if err != nil {
  524. p.getErrorHandler()(rw, req, fmt.Errorf("Hijack failed on protocol switch: %v", err))
  525. return
  526. }
  527. defer conn.Close()
  528. copyHeader(rw.Header(), res.Header)
  529. res.Header = rw.Header()
  530. res.Body = nil // so res.Write only writes the headers; we have res.Body in backConn above
  531. if err := res.Write(brw); err != nil {
  532. p.getErrorHandler()(rw, req, fmt.Errorf("response write: %v", err))
  533. return
  534. }
  535. if err := brw.Flush(); err != nil {
  536. p.getErrorHandler()(rw, req, fmt.Errorf("response flush: %v", err))
  537. return
  538. }
  539. errc := make(chan error, 1)
  540. spc := switchProtocolCopier{user: conn, backend: backConn}
  541. go spc.copyToBackend(errc)
  542. go spc.copyFromBackend(errc)
  543. <-errc
  544. return
  545. }
  546. // switchProtocolCopier exists so goroutines proxying data back and
  547. // forth have nice names in stacks.
  548. type switchProtocolCopier struct {
  549. user, backend io.ReadWriter
  550. }
  551. func (c switchProtocolCopier) copyFromBackend(errc chan<- error) {
  552. _, err := io.Copy(c.user, c.backend)
  553. errc <- err
  554. }
  555. func (c switchProtocolCopier) copyToBackend(errc chan<- error) {
  556. _, err := io.Copy(c.backend, c.user)
  557. errc <- err
  558. }