labstack / echo

High performance, minimalist Go web framework
https://echo.labstack.com
MIT License
29.94k stars 2.23k forks source link

fix: Unclear behaviour of * in routes #2657

Open iamgoroot opened 4 months ago

iamgoroot commented 4 months ago

This should hopefully bring more clarity into this issue https://github.com/labstack/echo/issues/2619

Examples would explain this better so here's similar to one from discussion

func main() {
    handler := func(c echo.Context) error {
        fmt.Printf("%s\n", c.Path())
        return nil
    }

    e := echo.New()
    v2 := e.Group("/v2")

    v2.DELETE("/*/blobs/:digest", handler)
    v2.GET("/*/blobs/:digest", handler)
    v2.HEAD("/*/blobs/:digest", handler)

    v2.DELETE("/*/manifests/:ref", handler)
    v2.GET("/*/manifests/:ref", handler)
    v2.HEAD("/*/manifests/:ref", handler)
    v2.PUT("/*/manifests/:ref", handler)

    v2.GET("/*/tags/list", handler)     // one wildcard
    v2.GET("/*/*/tags/list", handler)   // two wildcards
    v2.GET("/*/*/tags/list2*", handler) // two wildcards and trailing one
    v2.GET("/*/*/tags/list2", handler)  // two wildcards with fixed ending that may conflict with previous route

    v2.GET("/*/blobs/uploads/:ref", handler)
    v2.PATCH("/*/blobs/uploads/:ref", handler)
    v2.POST("/*/blobs/uploads", handler)
    v2.PUT("/*/blobs/uploads/:ref", handler)

    v2.GET("", handler)
    err := e.Start(":8080")
    if err != nil {
        panic(err)
    }
}

Example curl calls that should match those routes:

curl -ik  http://localhost:8080/v2/wildcard1/tags/list
# Matches /*/tags/list

curl -ik  http://localhost:8080/v2/wildcard1/wildcard2/tags/list
# Matches /*/*/tags/list

curl -ik  http://localhost:8080/v2/wildcard1/wildcard2/tags/list2wildcard3
# Matches /*/*/tags/list2*

curl -ik  http://localhost:8080/v2/wildcard1/wildcard2/tags/list2
# Matches /*/*/tags/list2

This one doesn't match since you would need to register it as /*/*/*/tags/list and this probably better implemented as separate ** wildcard

curl -ik  http://localhost:8080/v2/wildcard1/wildcard2/nowildcard/tags/list
{"message":"Method Not Allowed"}