fix redir stuff i hope
This commit is contained in:
@@ -9,8 +9,17 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type redirPurge struct {
|
||||
proxyHost string
|
||||
targetHost string
|
||||
}
|
||||
|
||||
func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
||||
newURL, err := s.lookup(r.Host)
|
||||
transport := &redirPurge{
|
||||
proxyHost: r.Host,
|
||||
targetHost: newURL.Host,
|
||||
}
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
log.Printf("unknown host lookup %q", r.Host)
|
||||
@@ -18,6 +27,7 @@ func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
r.Host = newURL.Host
|
||||
proxy := httputil.NewSingleHostReverseProxy(newURL)
|
||||
proxy.Transport = transport
|
||||
proxy.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
@@ -28,3 +38,14 @@ func (s *Server) lookup(host string) (*url.URL, error) {
|
||||
err := s.db.Get(nsRouting, host, v)
|
||||
return v.URL(), err
|
||||
}
|
||||
|
||||
func (rp *redirPurge) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
resp, err := http.DefaultTransport.RoundTrip(r)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
if loc := resp.Header.Get("Location"); loc != "" {
|
||||
resp.Header.Set("Location", strings.Replace(loc, rp.targetHost, rp.proxyHost, 1))
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user