12 htmltemplate "html/template"
29 "github.com/mjl-/mox/config"
30 "github.com/mjl-/mox/dns"
31 "github.com/mjl-/mox/mlog"
32 "github.com/mjl-/mox/mox-"
35func recvid(r *http.Request) string {
36 cid := mox.CidFromCtx(r.Context())
40 return " (id " + mox.ReceivedID(cid) + ")"
43// WebHandle serves an HTTP request by going through the list of WebHandlers,
44// check if there is a domain+path match, and running the handler if so.
45// WebHandle runs after the built-in handlers for mta-sts, autoconfig, etc.
46// If no handler matched, false is returned.
47// WebHandle sets w.Name to that of the matching handler.
48func WebHandle(w *loggingWriter, r *http.Request, host dns.IPDomain) (handled bool) {
49 conf := mox.Conf.DynamicConfig()
50 redirects := conf.WebDNSDomainRedirects
51 handlers := conf.WebHandlers
53 for from, to := range redirects {
54 if host.Domain != from {
60 w.Handler = "(domainredirect)"
61 http.Redirect(w, r, u.String(), http.StatusPermanentRedirect)
65 for _, h := range handlers {
66 if host.Domain != h.DNSDomain {
69 loc := h.Path.FindStringIndex(r.URL.Path)
75 path := r.URL.Path[s:e]
77 if r.TLS == nil && !h.DontRedirectPlainHTTP {
80 u.Host = h.DNSDomain.Name()
82 w.Compress = h.Compress
83 http.Redirect(w, r, u.String(), http.StatusPermanentRedirect)
87 // We don't want the loggingWriter to override the static handler's decisions to compress.
88 w.Compress = h.Compress
89 if h.WebStatic != nil && HandleStatic(h.WebStatic, h.Compress, w, r) {
93 if h.WebRedirect != nil && HandleRedirect(h.WebRedirect, w, r) {
97 if h.WebForward != nil && HandleForward(h.WebForward, w, r, path) {
101 if h.WebInternal != nil && HandleInternal(h.WebInternal, w, r) {
110var lsTemplate = htmltemplate.Must(htmltemplate.New("ls").Parse(`<!doctype html>
113 <meta charset="utf-8" />
114 <meta name="viewport" content="width=device-width, initial-scale=1" />
117body, html { padding: 1em; font-size: 16px; }
118* { font-size: inherit; font-family: ubuntu, lato, sans-serif; margin: 0; padding: 0; box-sizing: border-box; }
119h1 { margin-bottom: 1ex; font-size: 1.2rem; }
120table td, table th { padding: .2em .5em; }
121table > tbody > tr:nth-child(odd) { background-color: #f8f8f8; }
122[title] { text-decoration: underline; text-decoration-style: dotted; }
131 <th>Modified (UTC)</th>
137 <tr><td colspan="3">No files.</td></tr>
141 <td title="{{ .Size }} bytes" style="text-align: right">{{ .SizeReadable }}{{ if .SizePad }}<span style="visibility:hidden">. </span>{{ end }}</td>
142 <td>{{ .Modified }}</td>
143 <td><a style="display: block" href="{{ .Name }}">{{ .Name }}</a></td>
152// HandleStatic serves static files. If a directory is requested and the URL
153// path doesn't end with a slash, a response with a redirect to the URL path with trailing
154// slash is written. If a directory is requested and an index.html exists, that
155// file is returned. Otherwise, for directories with ListFiles configured, a
156// directory listing is returned.
157func HandleStatic(h *config.WebStatic, compress bool, w http.ResponseWriter, r *http.Request) (handled bool) {
158 log := func() mlog.Log {
159 return pkglog.WithContext(r.Context())
161 if r.Method != "GET" && r.Method != "HEAD" {
162 if h.ContinueNotFound {
163 // Give another handler that is presumbly configured, for the same path, a chance.
164 // E.g. an app that may generate this file for future requests to pick up.
167 http.Error(w, "405 - method not allowed", http.StatusMethodNotAllowed)
172 if h.StripPrefix != "" {
173 if !strings.HasPrefix(r.URL.Path, h.StripPrefix) {
174 if h.ContinueNotFound {
175 // We haven't handled this request, try a next WebHandler in the list.
181 fspath = filepath.Join(h.Root, strings.TrimPrefix(r.URL.Path, h.StripPrefix))
183 fspath = filepath.Join(h.Root, r.URL.Path)
185 // fspath will not have a trailing slash anymore, we'll correct for it
186 // later when the path turns out to be file instead of a directory.
188 serveFile := func(name string, fi fs.FileInfo, content *os.File) {
189 // ServeContent only sets a content-type if not already present in the response headers.
191 for k, v := range h.ResponseHeaders {
194 // We transparently compress here, but still use ServeContent, because it handles
195 // conditional requests, range requests. It's a bit of a hack, but on first write
196 // to staticgzcacheReplacer where we are compressing, we write the full compressed
197 // file instead, and return an error to ServeContent so it stops. We still have all
198 // the useful behaviour (status code and headers) from ServeContent.
200 if compress && acceptsGzip(r) && compressibleContent(content) {
201 xw = &staticgzcacheReplacer{w, r, content.Name(), content, fi.ModTime(), fi.Size(), 0, false}
203 w.(*loggingWriter).Compress = false
205 http.ServeContent(xw, r, name, fi.ModTime(), content)
208 f, err := os.Open(fspath)
210 if os.IsNotExist(err) || errors.Is(err, syscall.ENOTDIR) {
211 if h.ContinueNotFound {
212 // We haven't handled this request, try a next WebHandler in the list.
217 } else if errors.Is(err, syscall.ENAMETOOLONG) {
220 } else if os.IsPermission(err) {
221 // If we tried opening a directory, we may not have permission to read it, but
222 // still access files inside it (execute bit), such as index.html. So try to serve it.
223 index, err := os.Open(filepath.Join(fspath, "index.html"))
225 http.Error(w, "403 - permission denied", http.StatusForbidden)
230 log().Check(err, "closing index file for serving")
233 ifi, err = index.Stat()
235 log().Errorx("stat index.html in directory we cannot list", err, slog.Any("url", r.URL), slog.String("fspath", fspath))
236 http.Error(w, "500 - internal server error"+recvid(r), http.StatusInternalServerError)
239 w.Header().Set("Content-Type", "text/html; charset=utf-8")
240 serveFile("index.html", ifi, index)
243 log().Errorx("open file for static file serving", err, slog.Any("url", r.URL), slog.String("fspath", fspath))
244 http.Error(w, "500 - internal server error"+recvid(r), http.StatusInternalServerError)
248 if err := f.Close(); err != nil {
249 log().Check(err, "closing file for static file serving")
255 log().Errorx("stat file for static file serving", err, slog.Any("url", r.URL), slog.String("fspath", fspath))
256 http.Error(w, "500 - internal server error"+recvid(r), http.StatusInternalServerError)
259 // Redirect if the local path is a directory.
260 if fi.IsDir() && !strings.HasSuffix(r.URL.Path, "/") {
261 http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
263 } else if !fi.IsDir() && strings.HasSuffix(r.URL.Path, "/") {
264 if h.ContinueNotFound {
272 index, err := os.Open(filepath.Join(fspath, "index.html"))
273 if err != nil && os.IsPermission(err) {
274 http.Error(w, "403 - permission denied", http.StatusForbidden)
276 } else if err != nil && os.IsNotExist(err) && !h.ListFiles {
277 if h.ContinueNotFound {
280 http.Error(w, "403 - permission denied", http.StatusForbidden)
282 } else if err == nil {
284 if err := index.Close(); err != nil {
285 log().Check(err, "closing index file for serving")
290 ifi, err = index.Stat()
292 w.Header().Set("Content-Type", "text/html; charset=utf-8")
293 serveFile("index.html", ifi, index)
297 if !os.IsNotExist(err) {
298 log().Errorx("stat for static file serving", err, slog.Any("url", r.URL), slog.String("fspath", fspath))
299 http.Error(w, "500 - internal server error"+recvid(r), http.StatusInternalServerError)
307 SizePad bool // Whether the size needs padding because it has no decimal point.
311 if r.URL.Path != "/" {
312 files = append(files, File{"..", 0, "", false, ""})
315 l, err := f.Readdir(1000)
316 for _, e := range l {
317 mb := float64(e.Size()) / (1024 * 1024)
322 size = fmt.Sprintf("%d", int64(mb))
325 size = fmt.Sprintf("%.2f", mb)
328 const dateTime = "2006-01-02 15:04:05" // time.DateTime, but only since go1.20.
329 modified := e.ModTime().UTC().Format(dateTime)
330 f := File{e.Name(), e.Size(), size, sizepad, modified}
334 files = append(files, f)
338 } else if err != nil {
339 log().Errorx("reading directory for file listing", err, slog.Any("url", r.URL), slog.String("fspath", fspath))
340 http.Error(w, "500 - internal server error"+recvid(r), http.StatusInternalServerError)
344 sort.Slice(files, func(i, j int) bool {
345 return files[i].Name < files[j].Name
348 hdr.Set("Content-Type", "text/html; charset=utf-8")
349 for k, v := range h.ResponseHeaders {
350 if !strings.EqualFold(k, "content-type") {
354 err = lsTemplate.Execute(w, map[string]any{"Files": files})
356 log().Check(err, "executing directory listing template")
361 serveFile(fspath, fi, f)
365// HandleRedirect writes a response with an HTTP redirect.
366func HandleRedirect(h *config.WebRedirect, w http.ResponseWriter, r *http.Request) (handled bool) {
368 if h.OrigPath == nil {
369 // No path rewrite necessary.
371 } else if !h.OrigPath.MatchString(r.URL.Path) {
375 dstpath = h.OrigPath.ReplaceAllString(r.URL.Path, h.ReplacePath)
383 u.Scheme = h.URL.Scheme
385 u.ForceQuery = h.URL.ForceQuery
386 u.RawQuery = h.URL.RawQuery
387 u.Fragment = h.URL.Fragment
388 if r.URL.RawQuery != "" {
389 if u.RawQuery != "" {
392 u.RawQuery += r.URL.RawQuery
396 code := http.StatusPermanentRedirect
397 if h.StatusCode != 0 {
401 // If we would be redirecting to the same scheme,host,path, we would get here again
402 // causing a redirect loop. Instead, this causes this redirect to not match,
403 // allowing to try the next WebHandler. This can be used to redirect all plain http
404 // requests to https.
409 if reqscheme == u.Scheme && r.Host == u.Host && r.URL.Path == u.Path {
413 http.Redirect(w, r, u.String(), code)
417// HandleInternal passes the request to an internal service.
418func HandleInternal(h *config.WebInternal, w http.ResponseWriter, r *http.Request) (handled bool) {
419 h.Handler.ServeHTTP(w, r)
423// HandleForward handles a request by forwarding it to another webserver and
424// passing the response on. I.e. a reverse proxy. It handles websocket
425// connections by monitoring the websocket handshake and then just passing along the
427func HandleForward(h *config.WebForward, w http.ResponseWriter, r *http.Request, path string) (handled bool) {
428 log := func() mlog.Log {
429 return pkglog.WithContext(r.Context())
436 u.Path = r.URL.Path[len(path):]
437 if !strings.HasPrefix(u.Path, "/") {
438 u.Path = "/" + u.Path
444 // Remove any forwarded headers passed in by client.
446 for k, vl := range r.Header {
447 if k == "Forwarded" || k == "X-Forwarded" || strings.HasPrefix(k, "X-Forwarded-") {
454 // Add our own X-Forwarded headers. ReverseProxy will add X-Forwarded-For.
455 r.Header["X-Forwarded-Host"] = []string{r.Host}
460 r.Header["X-Forwarded-Proto"] = []string{proto}
461 // note: We are not using "ws" or "wss" for websocket. The request we are
462 // forwarding is http(s), and we don't yet know if the backend even supports
465 // todo: add Forwarded header? is anyone using it?
467 // If we see an Upgrade: websocket, we're going to assume the client needs
468 // websocket and only attempt to talk websocket with the backend. If the backend
469 // doesn't do websocket, we'll send back a "bad request" response. For other values
470 // of Upgrade, we don't do anything special.
471 // https://www.iana.org/assignments/http-upgrade-tokens/http-upgrade-tokens.xhtml
475 upgrade := r.Header.Get("Upgrade")
476 if upgrade != "" && !(r.ProtoMajor == 1 && r.ProtoMinor == 0) {
477 // Websockets have case-insensitive string "websocket".
478 for _, s := range strings.Split(upgrade, ",") {
479 if strings.EqualFold(textproto.TrimString(s), "websocket") {
480 forwardWebsocket(h, w, r, path)
486 // ReverseProxy will append any remaining path to the configured target URL.
487 proxy := httputil.NewSingleHostReverseProxy(h.TargetURL)
488 proxy.FlushInterval = time.Duration(-1) // Flush after each write.
489 proxy.ErrorLog = golog.New(mlog.LogWriter(mlog.New("net/http/httputil", nil).WithContext(r.Context()), mlog.LevelDebug, "reverseproxy error"), "", 0)
490 proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
491 if errors.Is(err, context.Canceled) {
492 log().Debugx("forwarding request to backend webserver", err, slog.Any("url", r.URL))
495 log().Errorx("forwarding request to backend webserver", err, slog.Any("url", r.URL))
496 if os.IsTimeout(err) {
497 http.Error(w, "504 - gateway timeout"+recvid(r), http.StatusGatewayTimeout)
499 http.Error(w, "502 - bad gateway"+recvid(r), http.StatusBadGateway)
503 for k, v := range h.ResponseHeaders {
506 proxy.ServeHTTP(w, r)
510var errResponseNotWebsocket = errors.New("not a valid websocket response to request")
511var errNotImplemented = errors.New("functionality not yet implemented")
513// Request has an Upgrade: websocket header. Check more websocketiness about the
514// request. If it looks good, we forward it to the backend. If the backend responds
515// with a valid websocket response, indicating it is indeed a websocket server, we
516// pass the response along and start copying data between the client and the
517// backend. We don't look at the frames and payloads. The backend already needs to
518// know enough websocket to handle the frames. It wouldn't necessarily hurt to
519// monitor the frames too, and check if they are valid, but it's quite a bit of
520// work for little benefit. Besides, the whole point of websockets is to exchange
521// bytes without HTTP being in the way, so let's do that.
522func forwardWebsocket(h *config.WebForward, w http.ResponseWriter, r *http.Request, path string) (handled bool) {
523 log := func() mlog.Log {
524 return pkglog.WithContext(r.Context())
527 lw := w.(*loggingWriter)
528 lw.WebsocketRequest = true // For correct protocol in metrics.
530 // We check the requested websocket version first. A future websocket version may
531 // have different request requirements.
533 wsversion := r.Header.Get("Sec-WebSocket-Version")
534 if wsversion != "13" {
535 // Indicate we only support version 13. Should get a client from the future to fall back to version 13.
537 w.Header().Set("Sec-WebSocket-Version", "13")
538 http.Error(w, "400 - bad request - websockets only supported with version 13"+recvid(r), http.StatusBadRequest)
539 lw.error(fmt.Errorf("Sec-WebSocket-Version %q not supported", wsversion))
544 if r.Method != "GET" {
545 http.Error(w, "400 - bad request - websockets only allowed with method GET"+recvid(r), http.StatusBadRequest)
546 lw.error(fmt.Errorf("websocket request only allowed with method GET"))
551 var connectionUpgrade bool
552 for _, s := range strings.Split(r.Header.Get("Connection"), ",") {
553 if strings.EqualFold(textproto.TrimString(s), "upgrade") {
554 connectionUpgrade = true
558 if !connectionUpgrade {
559 http.Error(w, "400 - bad request - connection header must be \"upgrade\""+recvid(r), http.StatusBadRequest)
560 lw.error(fmt.Errorf(`connection header is %q, must be "upgrade"`, r.Header.Get("Connection")))
565 wskey := r.Header.Get("Sec-WebSocket-Key")
566 key, err := base64.StdEncoding.DecodeString(wskey)
567 if err != nil || len(key) != 16 {
568 http.Error(w, "400 - bad request - websockets requires Sec-WebSocket-Key with 16 bytes base64-encoded value"+recvid(r), http.StatusBadRequest)
569 lw.error(fmt.Errorf("bad Sec-WebSocket-Key %q, must be 16 byte base64-encoded value", wskey))
574 // We don't look at the origin header. The backend needs to handle it, if it thinks
576 // We also don't look at Sec-WebSocket-Protocol and Sec-WebSocket-Extensions. The
577 // backend can set them, but it doesn't influence our forwarding of the data.
579 // If this is not a hijacker, there is not point in connecting to the backend.
580 hj, ok := lw.W.(http.Hijacker)
581 var cbr *bufio.ReadWriter
583 log().Info("cannot turn http connection into tcp connection (http.Hijacker)")
584 http.Error(w, "501 - not implemented - cannot turn this connection into websocket"+recvid(r), http.StatusNotImplemented)
585 lw.error(fmt.Errorf("connection not a http.Hijacker (%T)", lw.W))
590 freq.Proto = "HTTP/1.1"
593 fresp, beconn, err := websocketTransact(r.Context(), h.TargetURL, &freq)
595 if errors.Is(err, errResponseNotWebsocket) {
596 http.Error(w, "400 - bad request - websocket not supported"+recvid(r), http.StatusBadRequest)
597 } else if errors.Is(err, errNotImplemented) {
598 http.Error(w, "501 - not implemented - "+err.Error()+recvid(r), http.StatusNotImplemented)
599 } else if os.IsTimeout(err) {
600 http.Error(w, "504 - gateway timeout"+recvid(r), http.StatusGatewayTimeout)
602 http.Error(w, "502 - bad gateway"+recvid(r), http.StatusBadGateway)
609 if err := beconn.Close(); err != nil {
610 log().Check(err, "closing backend websocket connection")
615 // Hijack the client connection so we can write the response ourselves, and start
616 // copying the websocket frames.
618 cconn, cbr, err = hj.Hijack()
620 log().Debugx("cannot turn http transaction into websocket connection", err)
621 http.Error(w, "501 - not implemented - cannot turn this connection into websocket"+recvid(r), http.StatusNotImplemented)
627 if err := cconn.Close(); err != nil {
628 log().Check(err, "closing client websocket connection")
633 // Below this point, we can no longer write to the ResponseWriter.
635 // Mark as websocket response, for logging.
636 lw.WebsocketResponse = true
637 lw.setStatusCode(fresp.StatusCode)
639 for k, v := range h.ResponseHeaders {
640 fresp.Header.Add(k, v)
643 // Write the response to the client, completing its websocket handshake.
644 if err := fresp.Write(cconn); err != nil {
645 lw.error(fmt.Errorf("writing websocket response to client: %w", err))
649 errc := make(chan error, 1)
651 // Copy from client to backend.
653 buf, err := cbr.Peek(cbr.Reader.Buffered())
659 n, err := beconn.Write(buf)
664 lw.SizeFromClient += int64(n)
666 n, err := io.Copy(beconn, cconn)
667 lw.SizeFromClient += n
671 // Copy from backend to client.
673 n, err := io.Copy(cconn, beconn)
678 // Stop and close connection on first error from either size, typically a closed
679 // connection whose closing was already announced with a websocket frame.
681 // Close connections so other goroutine stops as well.
682 if err := cconn.Close(); err != nil {
683 log().Check(err, "closing client websocket connection")
685 if err := beconn.Close(); err != nil {
686 log().Check(err, "closing backend websocket connection")
688 // Wait for goroutine so it has updated the logWriter.Size*Client fields before we
689 // continue with logging.
695func websocketTransact(ctx context.Context, targetURL *url.URL, r *http.Request) (rresp *http.Response, rconn net.Conn, rerr error) {
696 log := func() mlog.Log {
697 return pkglog.WithContext(r.Context())
700 // Dial the backend, possibly doing TLS. We assume the net/http DefaultTransport is
702 transport := http.DefaultTransport.(*http.Transport)
704 // We haven't implemented using a proxy for websocket requests yet. If we need one,
705 // return an error instead of trying to connect directly, which would be a
706 // potential security issue.
709 if purl, err := transport.Proxy(&treq); err != nil {
710 return nil, nil, fmt.Errorf("determining proxy for websocket backend connection: %w", err)
711 } else if purl != nil {
712 return nil, nil, fmt.Errorf("%w: proxy required for websocket connection to backend", errNotImplemented) // todo: implement?
715 host, port, err := net.SplitHostPort(targetURL.Host)
717 host = targetURL.Host
718 if targetURL.Scheme == "https" {
724 addr := net.JoinHostPort(host, port)
725 conn, err := transport.DialContext(r.Context(), "tcp", addr)
727 return nil, nil, fmt.Errorf("dial: %w", err)
729 if targetURL.Scheme == "https" {
730 tlsconn := tls.Client(conn, transport.TLSClientConfig)
731 ctx, cancel := context.WithTimeout(r.Context(), transport.TLSHandshakeTimeout)
733 if err := tlsconn.HandshakeContext(ctx); err != nil {
734 return nil, nil, fmt.Errorf("tls handshake: %w", err)
740 if xerr := conn.Close(); xerr != nil {
741 log().Check(xerr, "cleaning up websocket connection")
746 // todo: make timeout configurable?
747 if err := conn.SetDeadline(time.Now().Add(30 * time.Second)); err != nil {
748 log().Check(err, "set deadline for websocket request to backend")
751 // Set clean connection headers.
752 removeHopByHopHeaders(r.Header)
753 r.Header.Set("Connection", "Upgrade")
754 r.Header.Set("Upgrade", "websocket")
756 // Write the websocket request to the backend.
757 if err := r.Write(conn); err != nil {
758 return nil, nil, fmt.Errorf("writing request to backend: %w", err)
761 // Read response from backend.
762 br := bufio.NewReader(conn)
763 resp, err := http.ReadResponse(br, r)
765 return nil, nil, fmt.Errorf("reading response from backend: %w", err)
769 if xerr := resp.Body.Close(); xerr != nil {
770 log().Check(xerr, "closing response body after error")
774 if err := conn.SetDeadline(time.Time{}); err != nil {
775 log().Check(err, "clearing deadline on websocket connection to backend")
778 // Check that the response from the backend server indicates it is websocket. If
779 // not, don't pass the backend response, but an error that websocket is not
781 if err := checkWebsocketResponse(resp, r); err != nil {
782 return resp, nil, err
785 // note: net/http.Response.Body documents that it implements io.Writer for a
786 // status: 101 response. But that's not the case when the response has been read
787 // with http.ReadResponse. We'll write to the connection directly.
789 buf, err := br.Peek(br.Buffered())
791 return resp, nil, fmt.Errorf("peek at buffered data written by backend: %w", err)
793 return resp, websocketConn{io.MultiReader(bytes.NewReader(buf), conn), conn}, nil
796// A net.Conn but with reads coming from an io multireader (due to buffered reader
797// needed for http.ReadResponse).
798type websocketConn struct {
803func (c websocketConn) Read(buf []byte) (int, error) {
807// Check that an HTTP response (from a backend) is a valid websocket response, i.e.
808// that it accepts the WebSocket "upgrade".
810func checkWebsocketResponse(resp *http.Response, req *http.Request) error {
811 if resp.StatusCode != 101 {
812 return fmt.Errorf("%w: response http status not 101 but %s", errResponseNotWebsocket, resp.Status)
814 if upgrade := resp.Header.Get("Upgrade"); !strings.EqualFold(upgrade, "websocket") {
815 return fmt.Errorf(`%w: response http status is 101, but Upgrade header is %q, should be "websocket"`, errResponseNotWebsocket, upgrade)
817 if connection := resp.Header.Get("Connection"); !strings.EqualFold(connection, "upgrade") {
818 return fmt.Errorf(`%w: response http status is 101, Upgrade is websocket, but Connection header is %q, should be "Upgrade"`, errResponseNotWebsocket, connection)
820 accept, err := base64.StdEncoding.DecodeString(resp.Header.Get("Sec-WebSocket-Accept"))
822 return fmt.Errorf(`%w: response http status, Upgrade and Connection header are websocket, but Sec-WebSocket-Accept header is not valid base64: %v`, errResponseNotWebsocket, err)
824 exp := sha1.Sum([]byte(req.Header.Get("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))
825 if !bytes.Equal(accept, exp[:]) {
826 return fmt.Errorf(`%w: response http status, Upgrade and Connection header are websocket, but backend Sec-WebSocket-Accept value does not match`, errResponseNotWebsocket)
832// From Go 1.20.4 src/net/http/httputil/reverseproxy.go:
833// Hop-by-hop headers. These are removed when sent to the backend.
834// As of RFC 7230, hop-by-hop headers are required to appear in the
835// Connection header field. These are the headers defined by the
836// obsoleted RFC 2616 (section 13.5.1) and are used for backward
839var hopHeaders = []string{
841 "Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
843 "Proxy-Authenticate",
844 "Proxy-Authorization",
845 "Te", // canonicalized version of "TE"
846 "Trailer", // not Trailers per URL above; https://www.rfc-editor.org/errata_search.php?eid=4522
851// From Go 1.20.4 src/net/http/httputil/reverseproxy.go:
852// removeHopByHopHeaders removes hop-by-hop headers.
853func removeHopByHopHeaders(h http.Header) {
854 // RFC 7230, section 6.1: Remove headers listed in the "Connection" header.
856 for _, f := range h["Connection"] {
857 for _, sf := range strings.Split(f, ",") {
858 if sf = textproto.TrimString(sf); sf != "" {
863 // RFC 2616, section 13.5.1: Remove a set of known hop-by-hop headers.
864 // This behavior is superseded by the RFC 7230 Connection header, but
865 // preserve it for backwards compatibility.
867 for _, f := range hopHeaders {