summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-10-09 10:10:24 +0900
committernsfisis <nsfisis@gmail.com>2023-10-09 10:10:24 +0900
commit638b9f28a59613a128d01ff56ad92258ef3c6d03 (patch)
tree0c2626efb75c5a153a1dbbdf5f3aaad8bad94a52
parentceb264cb65f4a62531e11b3ce666f931074b778a (diff)
downloadmioproxy-638b9f28a59613a128d01ff56ad92258ef3c6d03.tar.gz
mioproxy-638b9f28a59613a128d01ff56ad92258ef3c6d03.tar.zst
mioproxy-638b9f28a59613a128d01ff56ad92258ef3c6d03.zip
revert: fix unexpected 404 error if host header has port partv0.2.1
-rw-r--r--example.conf.hcl4
-rw-r--r--server.go22
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 {
diff --git a/server.go b/server.go
index f971d45..7c7c6a6 100644
--- a/server.go
+++ b/server.go
@@ -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)
}