diff options
| author | nsfisis <nsfisis@gmail.com> | 2023-10-09 00:02:49 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2023-10-09 00:02:49 +0900 |
| commit | 6f97769e4fc893e7607652e3cbfcf5698e2256f4 (patch) | |
| tree | dda21aeb41c60892c8a3637e1ad025673694f0e4 | |
| parent | 273ef30991b4e186db27903c489cb0e1bda15f30 (diff) | |
| download | mioproxy-6f97769e4fc893e7607652e3cbfcf5698e2256f4.tar.gz mioproxy-6f97769e4fc893e7607652e3cbfcf5698e2256f4.tar.zst mioproxy-6f97769e4fc893e7607652e3cbfcf5698e2256f4.zip | |
fix unexpected 404 error if host header has port part
| -rw-r--r-- | server.go | 16 |
1 files changed, 13 insertions, 3 deletions
@@ -44,9 +44,13 @@ func newMultipleReverseProxyServer(ps []ProxyConfig) *multipleReverseProxyServer } } -func (s *multipleReverseProxyServer) tryServeHTTP(w http.ResponseWriter, r *http.Request) bool { +func (s *multipleReverseProxyServer) tryServeHTTP( + w http.ResponseWriter, + r *http.Request, + hostWithoutPort string, +) bool { for _, rule := range s.rules { - if r.Host == rule.fromHost { + if rule.fromHost == hostWithoutPort { rule.proxy.ServeHTTP(w, r) return true } @@ -79,7 +83,13 @@ func NewServer(cfg *ServerConfig) *Server { } else { reverseProxyServer := newMultipleReverseProxyServer(cfg.Proxies) h.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - found := reverseProxyServer.tryServeHTTP(w, r) + // 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) if !found { http.NotFound(w, r) } |
