almost all ready
This commit is contained in:
@@ -129,6 +129,9 @@ func (r *CategoryRoute) Match(path string) (slug string, ok bool) {
|
||||
}
|
||||
|
||||
func (r *CategoryRoute) MatchInfo(path string) (*CategoryMatch, bool) {
|
||||
if hasExcludedStaticSegment(path) {
|
||||
return nil, false
|
||||
}
|
||||
if r == nil || r.regex == nil {
|
||||
return fallbackCategoryMatch(path)
|
||||
}
|
||||
@@ -262,6 +265,9 @@ func (r *ProductRoute) Match(path string) (slug string, ok bool) {
|
||||
}
|
||||
|
||||
func (r *ProductRoute) MatchInfo(path string) (*ProductMatch, bool) {
|
||||
if hasExcludedStaticSegment(path) {
|
||||
return nil, false
|
||||
}
|
||||
if r == nil || r.regex == nil {
|
||||
return fallbackProductMatch(path)
|
||||
}
|
||||
@@ -500,6 +506,9 @@ func fallbackProductMatch(path string) (*ProductMatch, bool) {
|
||||
if path == "" {
|
||||
return nil, false
|
||||
}
|
||||
if hasExcludedStaticSegment(path) {
|
||||
return nil, false
|
||||
}
|
||||
if hasExcludedContentSegment(path) {
|
||||
return nil, false
|
||||
}
|
||||
@@ -543,6 +552,9 @@ func fallbackCategoryMatch(path string) (*CategoryMatch, bool) {
|
||||
if path == "" {
|
||||
return nil, false
|
||||
}
|
||||
if hasExcludedStaticSegment(path) {
|
||||
return nil, false
|
||||
}
|
||||
if hasExcludedContentSegment(path) {
|
||||
return nil, false
|
||||
}
|
||||
@@ -601,3 +613,19 @@ func hasExcludedContentSegment(path string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func hasExcludedStaticSegment(path string) bool {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
return false
|
||||
}
|
||||
path = strings.Trim(path, "/")
|
||||
if path == "" {
|
||||
return false
|
||||
}
|
||||
first := path
|
||||
if idx := strings.IndexByte(first, '/'); idx >= 0 {
|
||||
first = first[:idx]
|
||||
}
|
||||
return strings.EqualFold(strings.TrimSpace(first), "img")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package routes
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCategoryRouteDoesNotOwnImagePath(t *testing.T) {
|
||||
route, err := CompileCategoryRoute("/{id}-{rewrite}")
|
||||
if err != nil {
|
||||
t.Fatalf("compile category route: %v", err)
|
||||
}
|
||||
|
||||
if match, ok := route.MatchInfo("/img/p/1/1/9/6/1/2/119612-large_default.webp"); ok || match != nil {
|
||||
t.Fatalf("expected image path to bypass category route, got ok=%v match=%+v", ok, match)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProductRouteDoesNotOwnImagePath(t *testing.T) {
|
||||
route, err := CompileProductRoute("/{id}-{rewrite}")
|
||||
if err != nil {
|
||||
t.Fatalf("compile product route: %v", err)
|
||||
}
|
||||
|
||||
if match, ok := route.MatchInfo("/img/p/1/1/9/6/1/2/119612-large_default.webp"); ok || match != nil {
|
||||
t.Fatalf("expected image path to bypass product route, got ok=%v match=%+v", ok, match)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user