-
Notifications
You must be signed in to change notification settings - Fork 22
/
proxy.go
31 lines (26 loc) · 636 Bytes
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main
import (
"net/http"
"net/http/httputil"
"net/url"
)
func proxy(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=604800")
src := r.URL.Query().Get("src")
urlParsed, err := url.Parse(src)
if err != nil {
http.Error(w, "Invalid URL", http.StatusBadRequest)
return
}
if urlParsed.Scheme != "http" && urlParsed.Scheme != "https" {
http.Error(w, "The URL scheme is neither HTTP nor HTTPS", http.StatusBadRequest)
return
}
proxy := httputil.ReverseProxy{
Director: func(r *http.Request) {
r.URL = urlParsed
r.Host = urlParsed.Host
},
}
proxy.ServeHTTP(w, r)
}