Update go dep (#1560)

This fix updates go dep with `dep ensure --update` as well as the following:
- Removed github.com/ugorji/go restriction in Gopkg.toml (fixes  #1557)
- Added github.com/flynn/go-shlex in Makefile (neede by Caddy, maybe removed later)

This fix fixes #1557

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang
2018-02-23 12:10:34 -08:00
committed by Miek Gieben
parent 9047bdf3a0
commit 604c0045e7
563 changed files with 107278 additions and 95314 deletions

View File

@@ -1,5 +1,10 @@
Change history of go-restful
=
v2.6.0
- Make JSR 311 routing and path param processing consistent
- Adding description to RouteBuilder.Reads()
- Update example for Swagger12 and OpenAPI
2017-09-13
- added route condition functions using `.If(func)` in route building.

View File

@@ -259,7 +259,12 @@ func (c *Container) dispatch(httpWriter http.ResponseWriter, httpRequest *http.R
chain.ProcessFilter(NewRequest(httpRequest), NewResponse(writer))
return
}
wrappedRequest, wrappedResponse := route.wrapRequestResponse(writer, httpRequest)
pathProcessor, routerProcessesPath := c.router.(PathProcessor)
if !routerProcessesPath {
pathProcessor = defaultPathProcessor{}
}
pathParams := pathProcessor.ExtractParameters(route, webService, httpRequest.URL.Path)
wrappedRequest, wrappedResponse := route.wrapRequestResponse(writer, httpRequest, pathParams)
// pass through filters (if any)
if len(c.containerFilters)+len(webService.filters)+len(route.Filters) > 0 {
// compose filter chain

View File

@@ -158,6 +158,13 @@ func TestExtractParameters_Wildcard3(t *testing.T) {
}
}
func TestExtractParameters_Wildcard4(t *testing.T) {
params := doExtractParams("/static/{var:*}/sub", 3, "/static/test/sub", t)
if params["var"] != "test/sub" {
t.Errorf("parameter mismatch var: %s", params["var"])
}
}
// clear && go test -v -test.run TestCurly_ISSUE_34 ...restful
func TestCurly_ISSUE_34(t *testing.T) {
ws1 := new(WebService).Path("/")

View File

@@ -39,6 +39,31 @@ func (r RouterJSR311) SelectRoute(
return dispatcher, route, ok
}
// ExtractParameters is used to obtain the path parameters from the route using the same matching
// engine as the JSR 311 router.
func (r RouterJSR311) ExtractParameters(route *Route, webService *WebService, urlPath string) map[string]string {
webServiceExpr := webService.pathExpr
webServiceMatches := webServiceExpr.Matcher.FindStringSubmatch(urlPath)
pathParameters := r.extractParams(webServiceExpr, webServiceMatches)
routeExpr := route.pathExpr
routeMatches := routeExpr.Matcher.FindStringSubmatch(webServiceMatches[len(webServiceMatches)-1])
routeParams := r.extractParams(routeExpr, routeMatches)
for key, value := range routeParams {
pathParameters[key] = value
}
return pathParameters
}
func (RouterJSR311) extractParams(pathExpr *pathExpression, matches []string) map[string]string {
params := map[string]string{}
for i := 1; i < len(matches); i++ {
if len(pathExpr.VarNames) >= i {
params[pathExpr.VarNames[i-1]] = matches[i]
}
}
return params
}
// http://jsr311.java.net/nonav/releases/1.1/spec/spec3.html#x3-360003.7.2
func (r RouterJSR311) detectRoute(routes []Route, httpRequest *http.Request) (*Route, error) {
ifOk := []Route{}

View File

@@ -3,6 +3,7 @@ package restful
import (
"io"
"net/http"
"reflect"
"sort"
"testing"
)
@@ -13,16 +14,17 @@ import (
var paths = []struct {
// url with path (1) is handled by service with root (2) and last capturing group has value final (3)
path, root, final string
params map[string]string
}{
{"/", "/", "/"},
{"/p", "/p", ""},
{"/p/x", "/p/{q}", ""},
{"/q/x", "/q", "/x"},
{"/p/x/", "/p/{q}", "/"},
{"/p/x/y", "/p/{q}", "/y"},
{"/q/x/y", "/q", "/x/y"},
{"/z/q", "/{p}/q", ""},
{"/a/b/c/q", "/", "/a/b/c/q"},
{"/", "/", "/", map[string]string{}},
{"/p", "/p", "", map[string]string{}},
{"/p/x", "/p/{q}", "", map[string]string{"q": "x"}},
{"/q/x", "/q", "/x", map[string]string{}},
{"/p/x/", "/p/{q}", "/", map[string]string{"q": "x"}},
{"/p/x/y", "/p/{q}", "/y", map[string]string{"q": "x"}},
{"/q/x/y", "/q", "/x/y", map[string]string{}},
{"/z/q", "/{p}/q", "", map[string]string{"p": "z"}},
{"/a/b/c/q", "/", "/a/b/c/q", map[string]string{}},
}
func TestDetectDispatcher(t *testing.T) {
@@ -37,6 +39,7 @@ func TestDetectDispatcher(t *testing.T) {
wc := NewContainer()
for _, each := range dispatchers {
each.Route(each.GET("").To(dummy))
wc.Add(each)
}
@@ -57,6 +60,11 @@ func TestDetectDispatcher(t *testing.T) {
t.Logf("[line:%v] Unexpected final, expected:%v, actual:%v", i, fixture.final, final)
ok = false
}
params := router.ExtractParameters(&who.Routes()[0], who, fixture.path)
if !reflect.DeepEqual(params, fixture.params) {
t.Logf("[line:%v] Unexpected params, expected:%v, actual:%v", i, fixture.params, params)
ok = false
}
}
if !ok {
t.Fail()
@@ -248,4 +256,77 @@ func TestDetectRouteReturns404IfNoRoutePassesConditions(t *testing.T) {
}
}
var extractParams = []struct {
name string
routePath string
urlPath string
expectedParams map[string]string
}{
{"wildcardLastPart", "/fixed/{var:*}", "/fixed/remainder", map[string]string{"var": "remainder"}},
{"wildcardMultipleParts", "/fixed/{var:*}", "/fixed/remain/der", map[string]string{"var": "remain/der"}},
{"wildcardManyParts", "/fixed/{var:*}", "/fixed/test/sub/hi.html", map[string]string{"var": "test/sub/hi.html"}},
{"wildcardInMiddle", "/fixed/{var:*}/morefixed", "/fixed/middle/stuff/morefixed", map[string]string{"var": "middle/stuff"}},
{"wildcardFollowedByVar", "/fixed/{var:*}/morefixed/{otherVar}", "/fixed/middle/stuff/morefixed/end", map[string]string{"var": "middle/stuff", "otherVar": "end"}},
{"singleParam", "/fixed/{var}", "/fixed/remainder", map[string]string{"var": "remainder"}},
{"slash", "/", "/", map[string]string{}},
{"NoVars", "/fixed", "/fixed", map[string]string{}},
{"TwoVars", "/from/{source}/to/{destination}", "/from/LHR/to/AMS", map[string]string{"source": "LHR", "destination": "AMS"}},
{"VarOnFront", "/{what}/from/{source}", "/who/from/SOS", map[string]string{"what": "who", "source": "SOS"}},
}
func TestExtractParams(t *testing.T) {
for _, testCase := range extractParams {
t.Run(testCase.name, func(t *testing.T) {
ws1 := new(WebService).Path("/")
ws1.Route(ws1.GET(testCase.routePath).To(dummy))
router := RouterJSR311{}
req, _ := http.NewRequest(http.MethodGet, testCase.urlPath, nil)
params := router.ExtractParameters(&ws1.Routes()[0], ws1, req.URL.Path)
if len(params) != len(testCase.expectedParams) {
t.Fatalf("Wrong length of params on selected route, expected: %v, got: %v", testCase.expectedParams, params)
}
for expectedParamKey, expectedParamValue := range testCase.expectedParams {
if expectedParamValue != params[expectedParamKey] {
t.Errorf("Wrong parameter for key '%v', expected: %v, got: %v", expectedParamKey, expectedParamValue, params[expectedParamKey])
}
}
})
}
}
func TestSelectRouteInvalidMethod(t *testing.T) {
ws1 := new(WebService).Path("/")
ws1.Route(ws1.GET("/simple").To(dummy))
router := RouterJSR311{}
req, _ := http.NewRequest(http.MethodPost, "/simple", nil)
_, _, err := router.SelectRoute([]*WebService{ws1}, req)
if err == nil {
t.Fatal("Expected an error as the wrong method is used but was nil")
}
}
func TestParameterInWebService(t *testing.T) {
for _, testCase := range extractParams {
t.Run(testCase.name, func(t *testing.T) {
ws1 := new(WebService).Path("/{wsParam}")
ws1.Route(ws1.GET(testCase.routePath).To(dummy))
router := RouterJSR311{}
req, _ := http.NewRequest(http.MethodGet, "/wsValue"+testCase.urlPath, nil)
params := router.ExtractParameters(&ws1.Routes()[0], ws1, req.URL.Path)
expectedParams := map[string]string{"wsParam": "wsValue"}
for key, value := range testCase.expectedParams {
expectedParams[key] = value
}
if len(params) != len(expectedParams) {
t.Fatalf("Wrong length of params on selected route, expected: %v, got: %v", testCase.expectedParams, params)
}
for expectedParamKey, expectedParamValue := range testCase.expectedParams {
if expectedParamValue != params[expectedParamKey] {
t.Errorf("Wrong parameter for key '%v', expected: %v, got: %v", expectedParamKey, expectedParamValue, params[expectedParamKey])
}
}
})
}
}
func dummy(req *Request, resp *Response) { io.WriteString(resp.ResponseWriter, "dummy") }

View File

@@ -14,8 +14,9 @@ import (
// PathExpression holds a compiled path expression (RegExp) needed to match against
// Http request paths and to extract path parameter values.
type pathExpression struct {
LiteralCount int // the number of literal characters (means those not resulting from template variable substitution)
VarCount int // the number of named parameters (enclosed by {}) in the path
LiteralCount int // the number of literal characters (means those not resulting from template variable substitution)
VarNames []string // the names of parameters (enclosed by {}) in the path
VarCount int // the number of named parameters (enclosed by {}) in the path
Matcher *regexp.Regexp
Source string // Path as defined by the RouteBuilder
tokens []string
@@ -24,16 +25,16 @@ type pathExpression struct {
// NewPathExpression creates a PathExpression from the input URL path.
// Returns an error if the path is invalid.
func newPathExpression(path string) (*pathExpression, error) {
expression, literalCount, varCount, tokens := templateToRegularExpression(path)
expression, literalCount, varNames, varCount, tokens := templateToRegularExpression(path)
compiled, err := regexp.Compile(expression)
if err != nil {
return nil, err
}
return &pathExpression{literalCount, varCount, compiled, expression, tokens}, nil
return &pathExpression{literalCount, varNames, varCount, compiled, expression, tokens}, nil
}
// http://jsr311.java.net/nonav/releases/1.1/spec/spec3.html#x3-370003.7.3
func templateToRegularExpression(template string) (expression string, literalCount int, varCount int, tokens []string) {
func templateToRegularExpression(template string) (expression string, literalCount int, varNames []string, varCount int, tokens []string) {
var buffer bytes.Buffer
buffer.WriteString("^")
//tokens = strings.Split(template, "/")
@@ -46,8 +47,10 @@ func templateToRegularExpression(template string) (expression string, literalCou
if strings.HasPrefix(each, "{") {
// check for regular expression in variable
colon := strings.Index(each, ":")
var varName string
if colon != -1 {
// extract expression
varName = strings.TrimSpace(each[1:colon])
paramExpr := strings.TrimSpace(each[colon+1 : len(each)-1])
if paramExpr == "*" { // special case
buffer.WriteString("(.*)")
@@ -56,8 +59,10 @@ func templateToRegularExpression(template string) (expression string, literalCou
}
} else {
// plain var
varName = strings.TrimSpace(each[1 : len(each)-1])
buffer.WriteString("([^/]+?)")
}
varNames = append(varNames, varName)
varCount += 1
} else {
literalCount += len(each)
@@ -65,5 +70,5 @@ func templateToRegularExpression(template string) (expression string, literalCou
buffer.WriteString(regexp.QuoteMeta(encoded))
}
}
return strings.TrimRight(buffer.String(), "/") + "(/.*)?$", literalCount, varCount, tokens
return strings.TrimRight(buffer.String(), "/") + "(/.*)?$", literalCount, varNames, varCount, tokens
}

View File

@@ -1,23 +1,27 @@
package restful
import "testing"
import (
"reflect"
"testing"
)
var tempregexs = []struct {
template, regex string
names []string
literalCount, varCount int
}{
{"", "^(/.*)?$", 0, 0},
{"/a/{b}/c/", "^/a/([^/]+?)/c(/.*)?$", 2, 1},
{"/{a}/{b}/{c-d-e}/", "^/([^/]+?)/([^/]+?)/([^/]+?)(/.*)?$", 0, 3},
{"/{p}/abcde", "^/([^/]+?)/abcde(/.*)?$", 5, 1},
{"/a/{b:*}", "^/a/(.*)(/.*)?$", 1, 1},
{"/a/{b:[a-z]+}", "^/a/([a-z]+)(/.*)?$", 1, 1},
{"", "^(/.*)?$", nil, 0, 0},
{"/a/{b}/c/", "^/a/([^/]+?)/c(/.*)?$", []string{"b"}, 2, 1},
{"/{a}/{b}/{c-d-e}/", "^/([^/]+?)/([^/]+?)/([^/]+?)(/.*)?$", []string{"a", "b", "c-d-e"}, 0, 3},
{"/{p}/abcde", "^/([^/]+?)/abcde(/.*)?$", []string{"p"}, 5, 1},
{"/a/{b:*}", "^/a/(.*)(/.*)?$", []string{"b"}, 1, 1},
{"/a/{b:[a-z]+}", "^/a/([a-z]+)(/.*)?$", []string{"b"}, 1, 1},
}
func TestTemplateToRegularExpression(t *testing.T) {
ok := true
for i, fixture := range tempregexs {
actual, lCount, vCount, _ := templateToRegularExpression(fixture.template)
actual, lCount, varNames, vCount, _ := templateToRegularExpression(fixture.template)
if actual != fixture.regex {
t.Logf("regex mismatch, expected:%v , actual:%v, line:%v\n", fixture.regex, actual, i) // 11 = where the data starts
ok = false
@@ -30,6 +34,10 @@ func TestTemplateToRegularExpression(t *testing.T) {
t.Logf("variable count mismatch, expected:%v , actual:%v, line:%v\n", fixture.varCount, vCount, i)
ok = false
}
if !reflect.DeepEqual(fixture.names, varNames) {
t.Logf("variable name mismatch, expected:%v , actual:%v, line:%v\n", fixture.names, varNames, i)
ok = false
}
}
if !ok {
t.Fatal("one or more expression did not match")

View File

@@ -0,0 +1,63 @@
package restful
import (
"bytes"
"strings"
)
// Copyright 2018 Ernest Micklei. All rights reserved.
// Use of this source code is governed by a license
// that can be found in the LICENSE file.
// PathProcessor is extra behaviour that a Router can provide to extract path parameters from the path.
// If a Router does not implement this interface then the default behaviour will be used.
type PathProcessor interface {
// ExtractParameters gets the path parameters defined in the route and webService from the urlPath
ExtractParameters(route *Route, webService *WebService, urlPath string) map[string]string
}
type defaultPathProcessor struct{}
// Extract the parameters from the request url path
func (d defaultPathProcessor) ExtractParameters(r *Route, _ *WebService, urlPath string) map[string]string {
urlParts := tokenizePath(urlPath)
pathParameters := map[string]string{}
for i, key := range r.pathParts {
var value string
if i >= len(urlParts) {
value = ""
} else {
value = urlParts[i]
}
if strings.HasPrefix(key, "{") { // path-parameter
if colon := strings.Index(key, ":"); colon != -1 {
// extract by regex
regPart := key[colon+1 : len(key)-1]
keyPart := key[1:colon]
if regPart == "*" {
pathParameters[keyPart] = untokenizePath(i, urlParts)
break
} else {
pathParameters[keyPart] = value
}
} else {
// without enclosing {}
pathParameters[key[1:len(key)-1]] = value
}
}
}
return pathParameters
}
// Untokenize back into an URL path using the slash separator
func untokenizePath(offset int, parts []string) string {
var buffer bytes.Buffer
for p := offset; p < len(parts); p++ {
buffer.WriteString(parts[p])
// do not end
if p < len(parts)-1 {
buffer.WriteString("/")
}
}
return buffer.String()
}

View File

@@ -0,0 +1,55 @@
package restful
import "testing"
func TestMatchesPath_OneParam(t *testing.T) {
params := doExtractParams("/from/{source}", 2, "/from/here", t)
if params["source"] != "here" {
t.Errorf("parameter mismatch here")
}
}
func TestMatchesPath_Slash(t *testing.T) {
params := doExtractParams("/", 0, "/", t)
if len(params) != 0 {
t.Errorf("expected empty parameters")
}
}
func TestMatchesPath_SlashNonVar(t *testing.T) {
params := doExtractParams("/any", 1, "/any", t)
if len(params) != 0 {
t.Errorf("expected empty parameters")
}
}
func TestMatchesPath_TwoVars(t *testing.T) {
params := doExtractParams("/from/{source}/to/{destination}", 4, "/from/AMS/to/NY", t)
if params["source"] != "AMS" {
t.Errorf("parameter mismatch AMS")
}
}
func TestMatchesPath_VarOnFront(t *testing.T) {
params := doExtractParams("{what}/from/{source}/", 3, "who/from/SOS/", t)
if params["source"] != "SOS" {
t.Errorf("parameter mismatch SOS")
}
}
func TestExtractParameters_EmptyValue(t *testing.T) {
params := doExtractParams("/fixed/{var}", 2, "/fixed/", t)
if params["var"] != "" {
t.Errorf("parameter mismatch var")
}
}
func doExtractParams(routePath string, size int, urlPath string, t *testing.T) map[string]string {
r := Route{Path: routePath}
r.postBuild()
if len(r.pathParts) != size {
t.Fatalf("len not %v %v, but %v", size, r.pathParts, len(r.pathParts))
}
pathProcessor := defaultPathProcessor{}
return pathProcessor.ExtractParameters(&r, nil, urlPath)
}

View File

@@ -5,7 +5,6 @@ package restful
// that can be found in the LICENSE file.
import (
"bytes"
"net/http"
"strings"
)
@@ -54,10 +53,9 @@ func (r *Route) postBuild() {
}
// Create Request and Response from their http versions
func (r *Route) wrapRequestResponse(httpWriter http.ResponseWriter, httpRequest *http.Request) (*Request, *Response) {
params := r.extractParameters(httpRequest.URL.Path)
func (r *Route) wrapRequestResponse(httpWriter http.ResponseWriter, httpRequest *http.Request, pathParams map[string]string) (*Request, *Response) {
wrappedRequest := NewRequest(httpRequest)
wrappedRequest.pathParameters = params
wrappedRequest.pathParameters = pathParams
wrappedRequest.selectedRoutePath = r.Path
wrappedResponse := NewResponse(httpWriter)
wrappedResponse.requestAccept = httpRequest.Header.Get(HEADER_Accept)
@@ -137,50 +135,6 @@ func (r Route) matchesContentType(mimeTypes string) bool {
return false
}
// Extract the parameters from the request url path
func (r Route) extractParameters(urlPath string) map[string]string {
urlParts := tokenizePath(urlPath)
pathParameters := map[string]string{}
for i, key := range r.pathParts {
var value string
if i >= len(urlParts) {
value = ""
} else {
value = urlParts[i]
}
if strings.HasPrefix(key, "{") { // path-parameter
if colon := strings.Index(key, ":"); colon != -1 {
// extract by regex
regPart := key[colon+1 : len(key)-1]
keyPart := key[1:colon]
if regPart == "*" {
pathParameters[keyPart] = untokenizePath(i, urlParts)
break
} else {
pathParameters[keyPart] = value
}
} else {
// without enclosing {}
pathParameters[key[1:len(key)-1]] = value
}
}
}
return pathParameters
}
// Untokenize back into an URL path using the slash separator
func untokenizePath(offset int, parts []string) string {
var buffer bytes.Buffer
for p := offset; p < len(parts); p++ {
buffer.WriteString(parts[p])
// do not end
if p < len(parts)-1 {
buffer.WriteString("/")
}
}
return buffer.String()
}
// Tokenize an URL path using the slash separator ; the result does not have empty tokens
func tokenizePath(path string) []string {
if "/" == path {

View File

@@ -69,59 +69,8 @@ func TestMatchesContentTypeCharsetInformation(t *testing.T) {
}
}
func TestMatchesPath_OneParam(t *testing.T) {
params := doExtractParams("/from/{source}", 2, "/from/here", t)
if params["source"] != "here" {
t.Errorf("parameter mismatch here")
}
}
func TestMatchesPath_Slash(t *testing.T) {
params := doExtractParams("/", 0, "/", t)
if len(params) != 0 {
t.Errorf("expected empty parameters")
}
}
func TestMatchesPath_SlashNonVar(t *testing.T) {
params := doExtractParams("/any", 1, "/any", t)
if len(params) != 0 {
t.Errorf("expected empty parameters")
}
}
func TestMatchesPath_TwoVars(t *testing.T) {
params := doExtractParams("/from/{source}/to/{destination}", 4, "/from/AMS/to/NY", t)
if params["source"] != "AMS" {
t.Errorf("parameter mismatch AMS")
}
}
func TestMatchesPath_VarOnFront(t *testing.T) {
params := doExtractParams("{what}/from/{source}/", 3, "who/from/SOS/", t)
if params["source"] != "SOS" {
t.Errorf("parameter mismatch SOS")
}
}
func TestExtractParameters_EmptyValue(t *testing.T) {
params := doExtractParams("/fixed/{var}", 2, "/fixed/", t)
if params["var"] != "" {
t.Errorf("parameter mismatch var")
}
}
func TestTokenizePath(t *testing.T) {
if len(tokenizePath("/")) != 0 {
t.Errorf("not empty path tokens")
}
}
func doExtractParams(routePath string, size int, urlPath string, t *testing.T) map[string]string {
r := Route{Path: routePath}
r.postBuild()
if len(r.pathParts) != size {
t.Fatalf("len not %v %v, but %v", size, r.pathParts, len(r.pathParts))
}
return r.extractParameters(urlPath)
}

View File

@@ -7,6 +7,8 @@ package restful
import "net/http"
// A RouteSelector finds the best matching Route given the input HTTP Request
// RouteSelectors can optionally also implement the PathProcessor interface to also calculate the
// path parameters after the route has been selected.
type RouteSelector interface {
// SelectRoute finds a Route given the input HTTP Request and a list of WebServices.