Routing not working properly with catch-all params
alex-rufo opened this issue · comments
I have some problems routing some requests when using catch-all params. I am using version v1.1.1 and this is the code:
package main
import (
"log"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
func Catchall(ctx *fasthttp.RequestCtx) {
ctx.WriteString("Catchall!")
}
func Specific(ctx *fasthttp.RequestCtx) {
ctx.WriteString("Specific!")
}
func main() {
r := router.New()
r.ANY("/{path:*}", Catchall)
r.POST("/specific", Specific)
log.Fatal(fasthttp.ListenAndServe(":8080", r.Handler))
}
I would expect any request different than POST /specific
to be caught by the Catchall
handler, but this is not what is actually happening. For the exact POST /specific
and any request that is not starting with /specific
it works fine but for the following cases it is not:
- GET
/specific
- GET
/specific/whatever
- POST
/specific/whatever
The server is just returning a redirect. I think this is due to the radix tree, but I would expect another behavior.
Hey 👋
I think I spotted a related issue. Using a custom OPTIONS
handler with catch-all parameters always leads to 308
. This happens only since 1.1.0
.
I adapted @alex-rufo 's code to be able to reproduce:
package main
import (
"log"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
func Catchall(ctx *fasthttp.RequestCtx) {
ctx.WriteString("Catchall!")
}
func Specific(ctx *fasthttp.RequestCtx) {
ctx.WriteString("Specific!")
}
func main() {
r := router.New()
r.HandleOPTIONS = false
r.OPTIONS("/{path:*}", Catchall)
r.POST("/specific/", Specific)
log.Fatal(fasthttp.ListenAndServe(":8080", r.Handler))
}
Before, calling
curl -v -X OPTIONS 'localhost:8080/specific/'
would lead to 200
and Catchall!
being printed.
For 1.1.1
and 1.1.0
, the same command leads to 308
.