fix(middleware): fix route bypass matching

- replace upstream headers approach with context value
This commit is contained in:
yusing
2025-05-08 17:49:36 +08:00
parent 1ce607029a
commit 75ee0e63bd
9 changed files with 116 additions and 77 deletions

View File

@@ -8,7 +8,7 @@ import (
"github.com/gobwas/glob"
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
"github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
@@ -242,7 +242,7 @@ var checkers = map[string]struct {
builder: func(args any) CheckFunc {
route := args.(string)
return func(_ Cache, r *http.Request) bool {
return r.Header.Get(httpheaders.HeaderUpstreamName) == route
return reverseproxy.TryGetUpstreamName(r) == route
}
},
},

View File

@@ -8,7 +8,7 @@ import (
"testing"
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
. "github.com/yusing/go-proxy/internal/utils/testing"
"golang.org/x/crypto/bcrypt"
)
@@ -305,11 +305,7 @@ func TestOnCorrectness(t *testing.T) {
{
name: "route_match",
checker: "route example",
input: &http.Request{
Header: http.Header{
httpheaders.HeaderUpstreamName: {"example"},
},
},
input: reverseproxy.NewReverseProxy("example", nil, http.DefaultTransport).WithContextValue(&http.Request{}),
want: true,
},
{