Skip to content

Commit

Permalink
Handle pre-flight requests (#20)
Browse files Browse the repository at this point in the history
* Revert "Add cors headers to every response (#19)"

This reverts commit 87b0601.

* Handle pre-flight requests
  • Loading branch information
altafan authored Sep 26, 2022
1 parent 87b0601 commit 3e0a180
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/torproxy/torproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,12 @@ func reverseProxy(redirects []*url.URL, lis net.Listener, dialer proxy.Dialer) e
http.HandleFunc(removeForUpstream+"/", func(w http.ResponseWriter, r *http.Request) {

// add cors headers
addCorsHeader(w)
addCorsHeader(w, r)

// Handler pre-flight requests
if r.Method == http.MethodOptions {
return
}

// prepare request removing useless headers
if err := prepareRequest(r); err != nil {
Expand All @@ -286,7 +291,10 @@ func withoutOnion(host string) string {
return strings.ReplaceAll(hostWithoutPort, ".onion", "")
}

func addCorsHeader(w http.ResponseWriter) {
func addCorsHeader(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodOptions {
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "*")
Expand Down

0 comments on commit 3e0a180

Please sign in to comment.