diff options
| -rw-r--r-- | example.conf.hcl | 4 | ||||
| -rw-r--r-- | server.go | 22 |
2 files changed, 11 insertions, 15 deletions
diff --git a/example.conf.hcl b/example.conf.hcl index e6f9d51..9274fa4 100644 --- a/example.conf.hcl +++ b/example.conf.hcl @@ -4,7 +4,7 @@ server http { proxy a { from { - host = "a.localhost" + host = "a.localhost:8000" } to { host = "127.0.0.1" @@ -30,7 +30,7 @@ server http { proxy c { from { - host = "c.localhost" + host = "c.localhost:8000" path = "/c/" } to { @@ -24,14 +24,17 @@ type rewriteRule struct { } func (r *rewriteRule) matches(host, path string) bool { - ret := true if r.fromHost != "" { - ret = ret && r.fromHost == host + if r.fromHost != host { + return false + } } if r.fromPath != "" { - ret = ret && strings.HasPrefix(path+"/", r.fromPath) + if !strings.HasPrefix(path+"/", r.fromPath) { + return false + } } - return ret + return true } func basicAuthHandler(handler http.Handler, realm, username, passwordHash string) http.Handler { @@ -95,10 +98,9 @@ func newMultipleReverseProxyServer(ps []ProxyConfig) (*multipleReverseProxyServe func (s *multipleReverseProxyServer) tryServeHTTP( w http.ResponseWriter, r *http.Request, - hostWithoutPort string, ) bool { for _, rule := range s.rules { - if rule.matches(hostWithoutPort, r.URL.Path) { + if rule.matches(r.Host, r.URL.Path) { rule.proxy.ServeHTTP(w, r) return true } @@ -134,13 +136,7 @@ func NewServer(cfg *ServerConfig) (*Server, error) { return nil, err } h.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - // r.Host may have ":port" part. - hostWithoutPort, _, err := net.SplitHostPort(r.Host) - if err != nil { - http.Error(w, "400 invalid host", http.StatusBadRequest) - return - } - found := reverseProxyServer.tryServeHTTP(w, r, hostWithoutPort) + found := reverseProxyServer.tryServeHTTP(w, r) if !found { http.NotFound(w, r) } |
