emicklei / go-restful

package for building REST-style Web Services using Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

using path.Join breaks the current api

SVilgelm opened this issue · comments

the issue with the path.Join function is that it removes the leading /
so if I register a route with a slash and the end, then I'm not able to reach that url with the slash

here is the modified hello example:

package main

import (
	"io"
	"log"
	"net/http"

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

// This example shows the minimal code needed to get a restful.WebService working.
//
// GET http://localhost:8080/hello

func main() {
	ws := new(restful.WebService)
	ws.Route(ws.GET("/hello/").To(hello))
	restful.Add(ws)
	log.Fatal(http.ListenAndServe(":8080", nil))
}

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

the only difference is the slash in hello route: /hello/

if I run the app using v3.10.1 then the curl http://localhost:8080/hello/ failed with 404, but the curl http://localhost:8080/hello works

if I run with v3.9.0 then both curls works fine.
the go system mux has a special code that checks if you are trying to reach an endpoint /hello and there is no registered route, but there is a route with the slash at then end: /hello/, then it returns 301 redirect to /hello/ and everything works

but go does not check for the opposite logic and does not trim the leading slash automatically, so everything is broken now, because we already use the routes with / a lot

I would suggest to add a small change to concatPath function:

func concatPath(path1, path2 string) string {
	p := path.Join(path1, path2)
	if strings.HasSuffix(path2, "/") {
		p += "/"
	}
	return p
}

we already got into a situation when we cannot upgrade some of our dependencies due to they require v3.10.1 of go-restful

could you please release an emergency fix for this?

@SVilgelm thank you for your analyses. I will investigate your suggestion to see if that fixes other reported issues or we should proceed with #523 or a combination

your suggestion breaks some existing tests:

--- FAIL: TestContainerCompressResponse (0.00s)
    container_test.go:112: unexpected code 404
    container_test.go:115: unexpected body 404: Page Not Found
    container_test.go:126: unexpected code 404
    container_test.go:129: unexpected body 404: Page Not Found
    container_test.go:140: unexpected code 404
    container_test.go:143: unexpected body 404: Page Not Found
    container_test.go:155: unexpected code 404
    container_test.go:170: unexpected body 404: Page Not Found

however, with #523 you could set the strategy with your own function if the 2 proposed ones do not work for you.

thank you! as I said on another topic, it is ok to me if can configure it

ok, I'm closing this one and will wait #523