Skip to content

Matching prefixes among multiple routers causes 404 #2696

@muety

Description

@muety

I'm not particularly experienced with Gin, so I might well have gotten something wrong here. However, the following behavior appears strange to me. Consider this small example:

package main

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

func main() {
	r := gin.New()

	// useless because of https://github.com/golang/go/blob/a7e16abb22f1b249d2691b32a5d20206282898f2/src/net/http/fs.go#L587, but anyway ...
	r.StaticFile("/index.html", "./public/index.html")
	r.StaticFile("/robots.txt", "./public/robots.txt")
	r.GET("/:id", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{"status": "success"})
	})
	
	r.Run()
}

Expected vs. actual behavior

$ curl http://localhost:8080/aaaa # -> shall return 200, does return 200
$ curl http://localhost:8080/iaaa # -> shall return 200, does return 404
$ curl http://localhost:8080/raaa # -> shall return 200, does return 404

If I remove the robots.txt handler, the third request will be 200 instead. Looks like if my request's path has a prefix, which is also a prefix of one of the other handlers, things get messed up.

Might be a duplicate of #2682.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions