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
77.95k stars 7.97k forks source link

A routing bug? #3519

Open zituocn opened 1 year ago

zituocn commented 1 year ago

Description

code:

package main

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

func main() {
    r := gin.Default()
    r.GET("/read_:id.htm", func(c *gin.Context) {
        id := c.Param("id")
        c.JSON(http.StatusOK, gin.H{
            "message": "pong",
            "id":      id,
        })
    })
    r.Run()
}

can match the following requests:

  1. /read_1
  2. /read_1.htm
  3. /read_1.html
  4. /read_1.aaaaaaaa
  5. ......

How to do a unique match?

Environment

Cookiery commented 1 year ago

Strange usage, maybe you can write id := c.Param("id.htm") Or you can control your route in middleware or function.

zituocn commented 1 year ago

Routes should have unique properties

pscheid92 commented 1 year ago

The router's properties are pretty unique. It just is not the properties we expect here 🙈 According to Gin's radix-tree router, a wild card starts with : and ends at the next / or at the end of the given string. In your example, the wild card is id.htm and not id.


I guess you expected the router to pars something like this:


But as the wildcard ends with the next / or end of the string. So it actually is: