Skip to content

Commit 1375833

Browse files
Added removal of connection headers, small improvements
1 parent 89af738 commit 1375833

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

gohpts.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gohpts
22

33
import (
4+
"crypto/tls"
45
"fmt"
56
"io"
67
"net"
@@ -42,6 +43,18 @@ func delHopHeaders(header http.Header) {
4243
}
4344
}
4445

46+
// delConnectionHeaders removes hop-by-hop headers listed in the "Connection" header
47+
// https://datatracker.ietf.org/doc/html/rfc7230#section-6.1
48+
func delConnectionHeaders(h http.Header) {
49+
for _, f := range h["Connection"] {
50+
for _, sf := range strings.Split(f, ",") {
51+
if sf = strings.TrimSpace(sf); sf != "" {
52+
h.Del(sf)
53+
}
54+
}
55+
}
56+
}
57+
4558
func appendHostToXForwardHeader(header http.Header, host string) {
4659
if prior, ok := header["X-Forwarded-For"]; ok {
4760
host = strings.Join(prior, ", ") + ", " + host
@@ -78,6 +91,8 @@ func (app *app) handleForward(w http.ResponseWriter, r *http.Request) {
7891
w.WriteHeader(http.StatusInternalServerError)
7992
return
8093
}
94+
req.RequestURI = ""
95+
delConnectionHeaders(r.Header)
8196
delHopHeaders(r.Header)
8297
copyHeader(req.Header, r.Header)
8398
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
@@ -111,6 +126,7 @@ func (app *app) handleForward(w http.ResponseWriter, r *http.Request) {
111126
}
112127
defer resp.Body.Close()
113128

129+
delConnectionHeaders(resp.Header)
114130
delHopHeaders(resp.Header)
115131
copyHeader(w.Header(), resp.Header)
116132
w.WriteHeader(resp.StatusCode)
@@ -254,6 +270,9 @@ func New(conf *Config) *app {
254270
MaxHeaderBytes: 1 << 20,
255271
}
256272
hc := &http.Client{
273+
Transport: &http.Transport{
274+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
275+
},
257276
CheckRedirect: func(req *http.Request, via []*http.Request) error {
258277
return http.ErrUseLastResponse
259278
},

0 commit comments

Comments
 (0)