Add path option for proxying
This commit is contained in:
@@ -24,7 +24,7 @@ type rewrite struct {
|
||||
}
|
||||
|
||||
func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
||||
newURL, err := s.lookup(mapKey(r.Host))
|
||||
newURL, err := s.lookup(mapKey(r, config.GetProxyMode()))
|
||||
var transport http.RoundTripper
|
||||
transport = &redirPurge{
|
||||
proxyHost: r.Host,
|
||||
@@ -32,7 +32,7 @@ func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
||||
baseTransport: http.DefaultTransport,
|
||||
}
|
||||
transport = &rewrite{
|
||||
rewrites: config.GetRewrites(mapKey(r.Host)),
|
||||
rewrites: config.GetRewrites(mapKey(r, config.GetProxyMode())),
|
||||
baseTransport: transport,
|
||||
}
|
||||
if err != nil {
|
||||
@@ -52,10 +52,20 @@ func (s *Server) lookup(host string) (*url.URL, error) {
|
||||
return v.URL(), err
|
||||
}
|
||||
|
||||
func mapKey(host string) string {
|
||||
host = strings.Split(host, ".")[0]
|
||||
host = strings.Split(host, ":")[0]
|
||||
return host
|
||||
func mapKey(r *http.Request, proxyMode string) string {
|
||||
switch proxyMode {
|
||||
case "domain":
|
||||
host := strings.Split(r.Host, ".")[0]
|
||||
host = strings.Split(host, ":")[0]
|
||||
return host
|
||||
case "path":
|
||||
paths := strings.Split(r.URL.Path, "/")
|
||||
if len(paths) < 2 {
|
||||
return ""
|
||||
}
|
||||
return paths[1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (rp *redirPurge) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -40,3 +41,35 @@ func TestRewrite(t *testing.T) {
|
||||
t.Errorf("failed to replace: got %q, want \"b\"", b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapKey(t *testing.T) {
|
||||
r := &http.Request{
|
||||
Host: "a.b.c:123",
|
||||
URL: &url.URL{
|
||||
Path: "/c/d/e",
|
||||
},
|
||||
}
|
||||
|
||||
if v := mapKey(r, "domain"); v != "a" {
|
||||
t.Errorf("failed to get domain: got %v", v)
|
||||
}
|
||||
|
||||
if v := mapKey(r, "path"); v != "c" {
|
||||
t.Errorf("failed to get domain: got %v", v)
|
||||
}
|
||||
|
||||
r.Host = "a:123"
|
||||
if v := mapKey(r, "domain"); v != "a" {
|
||||
t.Errorf("failed to get domain: got %v", v)
|
||||
}
|
||||
|
||||
r.URL.Path = ""
|
||||
if v := mapKey(r, "path"); v != "" {
|
||||
t.Errorf("failed to get domain: got %v", v)
|
||||
}
|
||||
|
||||
r.URL.Path = "/"
|
||||
if v := mapKey(r, "path"); v != "" {
|
||||
t.Errorf("failed to get domain: got %v", v)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user