Skip to content

Commit

Permalink
remove hop-by-hop headers
Browse files Browse the repository at this point in the history
  • Loading branch information
localvar committed Jun 14, 2022
1 parent 3376c03 commit 786c7c9
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion pkg/filters/proxy/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io"
"net/http"
"net/textproto"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -76,6 +77,55 @@ type serverPoolContext struct {
stdResp *http.Response
}

// Hop-by-hop headers. These are removed when sent to the backend.
// As of RFC 7230, hop-by-hop headers are required to appear in the
// Connection header field. These are the headers defined by the
// obsoleted RFC 2616 (section 13.5.1) and are used for backward
// compatibility.
var hopHeaders = []string{
"Connection",
"Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
"Keep-Alive",
"Proxy-Authenticate",
"Proxy-Authorization",
"Te", // canonicalized version of "TE"
"Trailer", // not Trailers per URL above; https://www.rfc-editor.org/errata_search.php?eid=4522
"Transfer-Encoding",
"Upgrade",
}

func cloneHeader(in http.Header) http.Header {
out := in.Clone()

// removeConnectionHeaders removes hop-by-hop headers listed in the
// "Connection" header of h. See RFC 7230, section 6.1
for _, f := range out["Connection"] {
for _, sf := range strings.Split(f, ",") {
if sf = textproto.TrimString(sf); sf != "" {
out.Del(sf)
}
}
}

for _, h := range hopHeaders {
out.Del(h)
}

// TODO: trailer support
//
// Few HTTP clients, servers, or proxies support HTTP trailers.
// It seems we need to do more than below to support it.
/*
// tell backend applications that care about trailer support
// that we support trailers.
if httpguts.HeaderValuesContainsToken(in["Te"], "trailers") {
out.Set("Te", "trailers")
}
*/

return out
}

func (spCtx *serverPoolContext) prepareRequest(svr *Server, ctx stdcontext.Context, mirror bool) error {
req := spCtx.req

Expand All @@ -96,7 +146,7 @@ func (spCtx *serverPoolContext) prepareRequest(svr *Server, ctx stdcontext.Conte
return err
}

stdr.Header = spCtx.req.HTTPHeader().Clone()
stdr.Header = cloneHeader(req.HTTPHeader())

// only set host when server address is not host name OR
// server is explicitly told to keep the host of the request.
Expand Down

0 comments on commit 786c7c9

Please sign in to comment.