Skip to content

Commit 7ecacf2

Browse files
fix: 🐛 cors issues
1 parent 19e6a46 commit 7ecacf2

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

proxy.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66
"net/http/httputil"
77
"net/url"
8-
"strings"
98
)
109

1110
func NewReverseProxy(base string) (*httputil.ReverseProxy, error) {
@@ -18,7 +17,7 @@ func NewReverseProxy(base string) (*httputil.ReverseProxy, error) {
1817
origDirector := proxy.Director
1918
proxy.Director = func(req *http.Request) {
2019
origDirector(req)
21-
req.URL.Path = joinPath(target.Path, req.URL.Path)
20+
// req.URL.Path is already updated by origDirector (singleJoiningSlash)
2221
req.Header.Set("X-Forwarded-Host", req.Host)
2322
if req.TLS != nil {
2423
req.Header.Set("X-Forwarded-Proto", "https")
@@ -27,22 +26,19 @@ func NewReverseProxy(base string) (*httputil.ReverseProxy, error) {
2726
}
2827
}
2928

29+
proxy.ModifyResponse = func(resp *http.Response) error {
30+
// Drop CORS headers from upstream to avoid duplicates with our middleware
31+
resp.Header.Del("Access-Control-Allow-Origin")
32+
resp.Header.Del("Access-Control-Allow-Methods")
33+
resp.Header.Del("Access-Control-Allow-Headers")
34+
resp.Header.Del("Access-Control-Expose-Headers")
35+
return nil
36+
}
37+
3038
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
3139
log.Printf("proxy error for %s: %v", r.URL.String(), err)
3240
http.Error(w, "Upstream error", http.StatusBadGateway)
3341
}
3442

3543
return proxy, nil
36-
}
37-
38-
func joinPath(a, b string) string {
39-
aslash := strings.HasSuffix(a, "/")
40-
bslash := strings.HasPrefix(b, "/")
41-
switch {
42-
case aslash && bslash:
43-
return a + b[1:]
44-
case !aslash && !bslash:
45-
return a + "/" + b
46-
}
47-
return a + b
48-
}
44+
}

0 commit comments

Comments
 (0)