Skip to content

Commit

Permalink
support cors
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Feb 17, 2022
1 parent 283d1a3 commit 428df5d
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions alist-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ func downHandle(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(resp.Data.Url, "http") {
resp.Data.Url = "http:" + resp.Data.Url
}
fmt.Println(resp.Data.Url)
fmt.Println("proxy:", resp.Data.Url)
u, err := url.Parse(resp.Data.Url)
if err != nil {
errorResponse(w, 500, err.Error())
return
}
req, _ = http.NewRequest("GET", resp.Data.Url, nil)
req, _ = http.NewRequest(r.Method, resp.Data.Url, nil)
for h, val := range r.Header {
req.Header[h] = val
}
Expand All @@ -153,8 +153,14 @@ func downHandle(w http.ResponseWriter, r *http.Request) {
_ = res2.Body.Close()
}()
for h, v := range res2.Header {
if strings.ToLower(h) == strings.ToLower("Access-Control-Allow-Origin") {
continue
}
w.Header()[h] = v
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Add("Access-Control-Allow-Headers", "range")
_, err = io.Copy(w, res2.Body)
if err != nil {
errorResponse(w, 500, err.Error())
Expand Down

0 comments on commit 428df5d

Please # to comment.