Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mis-route with multiple webservice and regex routeToken #547

Closed
haitch opened this issue May 24, 2024 · 3 comments
Closed

mis-route with multiple webservice and regex routeToken #547

haitch opened this issue May 24, 2024 · 3 comments

Comments

@haitch
Copy link
Contributor

haitch commented May 24, 2024

when dealing with multiple webservice in same container, and regex routeToken, go-restful have defect on selecting webservice.

here is a code snippit reproduce the issue: go playground link

package main

import (
	"fmt"
	"io"
	"net/http/httptest"

	restful "github.com/emicklei/go-restful/v3"
)

func main() {
	holaWS := new(restful.WebService).Path("/{:hola}")
	helloWS := new(restful.WebService).Path("/{:hello}")

	holaWS.Route(holaWS.GET("/{name:*}").To(hola))
	helloWS.Route(helloWS.GET("/{name:*}").To(hello))
	restful.DefaultContainer.Add(holaWS)
	restful.DefaultContainer.Add(helloWS)

	holaRequest := httptest.NewRequest("GET", "/hola/Juan", nil)
	holaRespWriter := httptest.NewRecorder()
	restful.DefaultContainer.ServeHTTP(holaRespWriter, holaRequest)

	helloRequest := httptest.NewRequest("GET", "/hello/Juan", nil)
	helloRespWriter := httptest.NewRecorder()
	restful.DefaultContainer.ServeHTTP(helloRespWriter, helloRequest)

	fmt.Println(holaRequest.Method, holaRequest.URL.Path, "-", holaRespWriter.Result().Status, holaRespWriter.Body.String())
	fmt.Println(helloRequest.Method, helloRequest.URL.Path, "-", helloRespWriter.Result().Status, helloRespWriter.Body.String())
}

func hola(req *restful.Request, resp *restful.Response) {
	io.WriteString(resp, "hola "+req.PathParameter("name"))
}

func hello(req *restful.Request, resp *restful.Response) {
	io.WriteString(resp, "hello "+req.PathParameter("name"))
}

output:

GET /hola/Juan - 200 OK hola Juan
GET /hello/Juan - 404 Not Found 404: Page Not Found
@haitch
Copy link
Contributor Author

haitch commented May 24, 2024

when dispatching request go-restful have 2 phases: detectWebService, and detectRoute.
there is a defect in detectWebService, where it ignores any regex pattern and treat them as matched with same score.
so holaWS and helloWS get same score, thus first holaWS wins.
now when detectRoute, it does regex matching, for all route tokens, include webservice root path.
holaRequest get perfectly matched, belloRequest failed to match any route in holaWS.

@haitch
Copy link
Contributor Author

haitch commented May 25, 2024

did a deep dive, and proposed 2 PR:

@emicklei
Copy link
Owner

thank you reporting this and suggestion the fixes, will have a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants