vardius / gorouter

Go Server/API micro framework, HTTP request router, multiplexer, mux
https://rafallorenz.com/gorouter
MIT License
154 stars 17 forks source link

fix route matching issues #57

Closed ceriath closed 10 months ago

ceriath commented 2 years ago

The router matched an additional character on static routes, which is fixed here.

Without this fix, the following happens: let's take this route as an example

r.GET("/user", h.handleUserGetAll)

some curl against it

:~/go/pkg/mod/github.com/vardius/gorouter/v4@v4.5.1/mux$ curl localhost:8080/user -XGET -i 
HTTP/1.1 200 OK
Server: fasthttp

[
  {
    "id": "1",
  }
]
:~/go/pkg/mod/github.com/vardius/gorouter/v4@v4.5.1/mux$ curl localhost:8080/users -XGET -i
HTTP/1.1 200 OK
Server: fasthttp

[
  {
    "id": "1",
  }
]

Note that the additional s is entirely ignored, since the route does not need to match exactly with the current implementation. It is enough for the route+1 to match. This is likely not intended behaviour and definitely not what i expected to happen.

This fix provides the following behaviour:

:~/go/pkg/mod/github.com/vardius/gorouter/v4@v4.5.1/mux$ curl localhost:8080/user -XGET -i
HTTP/1.1 200 OK
Server: fasthttp

[
  {
    "id": "1",
  }
]
:~/go/pkg/mod/github.com/vardius/gorouter/v4@v4.5.1/mux$ curl localhost:8080/users -XGET -i
HTTP/1.1 200 OK
Server: fasthttp

null

There is still a "null" and a 200 OK instead of a 404 Not found which i expected, so i investigated further and found another issue. Since i had

 r.GET("/user/{id}", h.handleUserGet)

defined aswell, there actually were children against which the route matched then, since it doesn't care if there is a slash in between or not. So i added a check to verify that there is actually a slash after the matching part of the path. That finally resulted in

:~/go/pkg/mod/github.com/vardius/gorouter/v4@v4.5.1/mux$ curl localhost:8080/hive/v1/user -XGET -i
HTTP/1.1 200 OK
Server: fasthttp

[
  {
    "id": "1",
  }
]
:~/go/pkg/mod/github.com/vardius/gorouter/v4@v4.5.1/mux$ curl localhost:8080/hive/v1/users -XGET -i
HTTP/1.1 404 Not Found
Server: fasthttp