diff --git a/curly.go b/curly.go index d8aa0bf1..f3ea5f73 100644 --- a/curly.go +++ b/curly.go @@ -82,7 +82,7 @@ func (c CurlyRouter) matchesRouteByPathTokens(routeTokens, requestTokens []strin // regularMatchesPathToken tests whether the regular expression part of routeToken matches the requestToken or all remaining tokens // format routeToken is {someVar:someExpression}, e.g. {zipcode:[\d][\d][\d][\d][A-Z][A-Z]} -func (c CurlyRouter) regularMatchesPathToken(routeToken string, colon int, requestToken string) (bool, bool) { +func (c CurlyRouter) regularMatchesPathToken(routeToken string, colon int, requestToken string) (matchesToken bool, matchesRemainder bool) { regPart := routeToken[colon+1 : len(routeToken)-1] if regPart == "*" { return true, true diff --git a/curly_test.go b/curly_test.go index f79d42c0..9d3195ad 100644 --- a/curly_test.go +++ b/curly_test.go @@ -137,16 +137,24 @@ func Test_matchesRouteByPathTokens(t *testing.T) { // clear && go test -v -test.run TestExtractParameters_Wildcard1 ...restful func TestExtractParameters_Wildcard1(t *testing.T) { params := doExtractParams("/fixed/{var:*}", 2, "/fixed/remainder", t) - if params["var"] == "remainder" { - t.Errorf("parameter mismatch var") + if params["var"] != "remainder" { + t.Errorf("parameter mismatch var: %s", params["var"]) } } // clear && go test -v -test.run TestExtractParameters_Wildcard2 ...restful func TestExtractParameters_Wildcard2(t *testing.T) { params := doExtractParams("/fixed/{var:*}", 2, "/fixed/remain/der", t) - if params["var"] == "remain/der" { - t.Errorf("parameter mismatch var") + if params["var"] != "remain/der" { + t.Errorf("parameter mismatch var: %s", params["var"]) + } +} + +// clear && go test -v -test.run TestExtractParameters_Wildcard3 ...restful +func TestExtractParameters_Wildcard3(t *testing.T) { + params := doExtractParams("/static/{var:*}", 2, "/static/test/sub/hi.html", t) + if params["var"] != "test/sub/hi.html" { + t.Errorf("parameter mismatch var: %s", params["var"]) } } diff --git a/examples/restful-serve-static.go b/examples/restful-serve-static.go index c1e9c728..8cb7848c 100644 --- a/examples/restful-serve-static.go +++ b/examples/restful-serve-static.go @@ -1,9 +1,11 @@ package main import ( - "github.com/emicklei/go-restful" + "fmt" "net/http" "path" + + "github.com/emicklei/go-restful" ) // This example shows how to define methods that serve static files @@ -17,9 +19,10 @@ import ( var rootdir = "/tmp" func main() { + restful.DefaultContainer.Router(restful.CurlyRouter{}) ws := new(restful.WebService) - ws.Route(ws.GET("/static/{resource}").To(staticFromPathParam)) + ws.Route(ws.GET("/static/{subpath:*}").To(staticFromPathParam)) ws.Route(ws.GET("/static").To(staticFromQueryParam)) restful.Add(ws) @@ -28,10 +31,12 @@ func main() { } func staticFromPathParam(req *restful.Request, resp *restful.Response) { + actual := path.Join(rootdir, req.PathParameter("subpath")) + fmt.Printf("serving %s ... (from %s)\n", actual, req.PathParameter("subpath")) http.ServeFile( resp.ResponseWriter, req.Request, - path.Join(rootdir, req.PathParameter("resource"))) + actual) } func staticFromQueryParam(req *restful.Request, resp *restful.Response) { diff --git a/route.go b/route.go index b8c07309..0c8018bc 100644 --- a/route.go +++ b/route.go @@ -124,7 +124,7 @@ func (r Route) extractParameters(urlPath string) map[string]string { regPart := key[colon+1 : len(key)-1] keyPart := key[1:colon] if regPart == "*" { - pathParameters[keyPart] = untokenizePath(i+1, urlParts) + pathParameters[keyPart] = untokenizePath(i, urlParts) break } else { pathParameters[keyPart] = value