diff options
| author | nsfisis <nsfisis@gmail.com> | 2023-10-09 10:10:24 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2023-10-09 10:10:24 +0900 |
| commit | 638b9f28a59613a128d01ff56ad92258ef3c6d03 (patch) | |
| tree | 0c2626efb75c5a153a1dbbdf5f3aaad8bad94a52 /server.go | |
| parent | ceb264cb65f4a62531e11b3ce666f931074b778a (diff) | |
| download | mioproxy-638b9f28a59613a128d01ff56ad92258ef3c6d03.tar.gz mioproxy-638b9f28a59613a128d01ff56ad92258ef3c6d03.tar.zst mioproxy-638b9f28a59613a128d01ff56ad92258ef3c6d03.zip | |
revert: fix unexpected 404 error if host header has port partv0.2.1
Diffstat (limited to 'server.go')
| -rw-r--r-- | server.go | 22 |
1 files changed, 9 insertions, 13 deletions
@@ -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) } |
