gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
76.76k stars 7.92k forks source link

GET 404 #3032

Open vito-go opened 2 years ago

vito-go commented 2 years ago

Why 404

when GET /universe/api/v1/im/unilog/ima/ads and /universe/api/v1/im/unilog/iam/ads.

Description

How to reproduce

package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    gin.SetMode(gin.DebugMode)
    engine := gin.Default()
    f := func(ctx *gin.Context) {
        ctx.JSON(200, "halo: "+ctx.Request.URL.Path)
    }
    engine.GET("/universe/api/v1/im/unilog/:app/*a", f)
    engine.POST("/universe/api/v1/im/unilog/:app/*a", f)
    engine.GET("/universe/api/v1/im/unilog/im/", f)
    engine.Run(":8989")
}

Expectations

http://127.0.0.1:8989/universe/api/v1/im/unilog/iam/ads
http://127.0.0.1:8989/universe/api/v1/im/unilog/aaa/ads
http://127.0.0.1:8989/universe/api/v1/im/unilog/iam/ads
[GIN-debug] Listening and serving HTTP on :8989
[GIN] 2022/01/20 - 14:43:43 | 404 |         892ns |       127.0.0.1 | GET      "/universe/api/v1/im/unilog/ima/ads"
[GIN] 2022/01/20 - 14:43:48 | 200 |       86.96µs |       127.0.0.1 | GET      "/universe/api/v1/im/unilog/aaa/ads"
[GIN] 2022/01/20 - 14:43:54 | 404 |       1.284µs |       127.0.0.1 | GET      "/universe/api/v1/im/unilog/iam/ads"

Environment

gin: v1.7.1 go version go1.17.5 linux/amd64

vito-go commented 2 years ago

func main() {
    gin.SetMode(gin.DebugMode)
    engine := gin.Default()
    f1 := func(ctx *gin.Context) {
        ctx.JSON(200, "/:app/*a"+ctx.Request.URL.Path)
    }
    f2 := func(ctx *gin.Context) {
        ctx.JSON(200, "/helloworld"+ctx.Request.URL.Path)
    }

    engine.GET("/:app/*a", f1) 
    engine.GET("/helloworld", f2)
    engine.Run(":8989")
}
// all the request path like /*/* will execute f1 except that the path begin with `h`,   
// e.g  /haha/hi   /hello/liushihao /hi/world

Is it a bug? Or why just match only the first letter h ?

Log

[GIN-debug] Listening and serving HTTP on :8989
[GIN] 2022/01/20 - 15:30:41 | 404 |         832ns |       127.0.0.1 | GET      "/hello/liushihao"
[GIN] 2022/01/20 - 15:30:51 | 404 |         918ns |       127.0.0.1 | GET      "/hi/vito-go"
[GIN] 2022/01/20 - 15:30:59 | 200 |      35.791µs |       127.0.0.1 | GET      "/gin/vito-go"
[GIN] 2022/01/20 - 15:31:08 | 200 |      21.047µs |       127.0.0.1 | GET      "/helloworld"
loickengit commented 2 years ago

Path /helloworld will only match /helloworld, so any path like // will match /:app/*a

vito-go commented 2 years ago

Path /helloworld will only match /helloworld, so any path like // will match /:app/*a

I can not aggree with you. When the Path /helloworld router exists, any path like /h/ will not match /:app/*a

Bisstocuz commented 2 years ago

Can you test it on v1.7.7?