summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-10-09 00:02:49 +0900
committernsfisis <nsfisis@gmail.com>2023-10-09 00:02:49 +0900
commit6f97769e4fc893e7607652e3cbfcf5698e2256f4 (patch)
treedda21aeb41c60892c8a3637e1ad025673694f0e4
parent273ef30991b4e186db27903c489cb0e1bda15f30 (diff)
downloadmioproxy-6f97769e4fc893e7607652e3cbfcf5698e2256f4.tar.gz
mioproxy-6f97769e4fc893e7607652e3cbfcf5698e2256f4.tar.zst
mioproxy-6f97769e4fc893e7607652e3cbfcf5698e2256f4.zip
fix unexpected 404 error if host header has port part
-rw-r--r--server.go16
1 files changed, 13 insertions, 3 deletions
diff --git a/server.go b/server.go
index ad00d26..47c52dd 100644
--- a/server.go
+++ b/server.go
@@ -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)
}