MSP-8976 Up deps

This commit is contained in:
Иван Перлов committed 2026-11-20 17:05:48 +03:00
1 parent ebc3c1dbc5
commit 0d9921bbce
603 files changed
+47715 -13202

No files matched your search

+83 -5
View File
@@ -26,6 +26,9 @@ func (e MalformedSequenceError) Error() string {
type op struct {
code utilities.OpCode
operand int
// multisegment is set on an OpPush that is one segment of a variable
// spanning several segments, such as the "*" in {name=shelves/*}.
multisegment bool
}
// Pattern is a template pattern of http request paths defined in
@@ -43,6 +46,20 @@ type Pattern struct {
tailLen int
// verb is the VERB part of the path pattern. It is empty if the pattern does not have VERB part.
verb string
// strictDeepWildcard makes a deep wildcard ("**") preceded by a segment require at least one
// remaining path component.
strictDeepWildcard bool
}
// PatternOpt configures optional behavior of a Pattern.
type PatternOpt func(*Pattern)
// WithStrictDeepWildcard makes a deep wildcard ("**") that is preceded by another
// segment refuse to match zero path segments. For example, "/v3/files/{path=**}"
// will not match "/v3/files". A bare "**" with nothing preceding it still matches
// zero segments. See https://github.com/grpc-ecosystem/grpc-gateway/issues/5771.
func WithStrictDeepWildcard() PatternOpt {
return func(p *Pattern) { p.strictDeepWildcard = true }
}
// NewPattern returns a new Pattern from the given definition values.
@@ -51,6 +68,12 @@ type Pattern struct {
// "version" must be 1 for now.
// It returns an error if the given definition is invalid.
func NewPattern(version int, ops []int, pool []string, verb string) (Pattern, error) {
return NewPatternWithOptions(version, ops, pool, verb)
}
// NewPatternWithOptions is like NewPattern but additionally accepts options that modify
// the matching behavior of the returned Pattern, e.g. WithStrictDeepWildcard.
func NewPatternWithOptions(version int, ops []int, pool []string, verb string, opts ...PatternOpt) (Pattern, error) {
if version != 1 {
grpclog.Errorf("unsupported version: %d", version)
return Pattern{}, ErrInvalidPattern
@@ -106,6 +129,15 @@ func NewPattern(version int, ops []int, pool []string, verb string) (Pattern, er
return Pattern{}, ErrInvalidPattern
}
stack++
// The segments of a variable are the ops pushed right before its
// OpConcatN, so these are the ones of a multi-segment variable.
if op.operand > 1 {
for j := len(typedOps) - op.operand; j < len(typedOps); j++ {
if typedOps[j].code == utilities.OpPush {
typedOps[j].multisegment = true
}
}
}
case utilities.OpCapture:
if op.operand < 0 || len(pool) <= op.operand {
grpclog.Errorf("variable name index out of bound: %d", op.operand)
@@ -129,14 +161,18 @@ func NewPattern(version int, ops []int, pool []string, verb string) (Pattern, er
}
typedOps = append(typedOps, op)
}
return Pattern{
pat := Pattern{
ops: typedOps,
pool: pool,
vars: vars,
stacksize: maxstack,
tailLen: tailLen,
verb: verb,
}, nil
}
for _, opt := range opts {
opt(&pat)
}
return pat, nil
}
// MustPattern is a helper function which makes it easier to call NewPattern in variable initialization.
@@ -152,6 +188,10 @@ func MustPattern(p Pattern, err error) Pattern {
// matched but contained malformed escape sequences. If successful, the function
// returns a mapping from field paths to their captured values.
func (p Pattern) MatchAndEscape(components []string, verb string, unescapingMode UnescapingMode) (map[string]string, error) {
return p.matchAndEscape(components, verb, unescapingMode, false)
}
func (p Pattern) matchAndEscape(components []string, verb string, unescapingMode UnescapingMode, strictDeepWildcard bool) (map[string]string, error) {
if p.verb != verb {
if p.verb != "" {
return nil, ErrNotMatch
@@ -165,8 +205,29 @@ func (p Pattern) MatchAndEscape(components []string, verb string, unescapingMode
}
var pos int
stack := make([]string, 0, p.stacksize)
captured := make([]string, len(p.vars))
// Scratch space for the match. Neither backing array escapes -- only the individual
// strings they hold are copied into the returned bindings -- so fixed-size arrays keep
// both off the heap for the patterns generated code registers, where the stack size and
// the variable count are single digits.
//
// This matters beyond one allocation each: ServeHTTP calls matchAndEscape once per
// registered pattern until one matches, and both allocations happened before the
// non-match return below, so a request paid for them once for every route it did not
// match. With a few dozen routes registered that was the largest single source of
// allocations in routing.
var stackArr, capturedArr [8]string
stack := stackArr[:0]
if p.stacksize > len(stackArr) {
stack = make([]string, 0, p.stacksize)
}
var captured []string
if n := len(p.vars); n <= len(capturedArr) {
captured = capturedArr[:n]
} else {
captured = make([]string, n)
}
l := len(components)
for _, op := range p.ops {
var err error
@@ -184,7 +245,14 @@ func (p Pattern) MatchAndEscape(components []string, verb string, unescapingMode
return nil, ErrNotMatch
}
} else if op.code == utilities.OpPush {
if c, err = unescape(c, unescapingMode, false); err != nil {
// google.api.http decodes a variable spanning several segments
// "except %2F and %2f", so a "*" in it can't add a path separator
// to the bound value. Other escapes are decoded as for {var}.
mode := unescapingMode
if op.multisegment && mode == UnescapingModeAllExceptReserved {
mode = UnescapingModeAllExceptSlash
}
if c, err = unescape(c, mode, op.multisegment); err != nil {
return nil, err
}
}
@@ -196,6 +264,16 @@ func (p Pattern) MatchAndEscape(components []string, verb string, unescapingMode
return nil, ErrNotMatch
}
end -= p.tailLen
if (p.strictDeepWildcard || strictDeepWildcard) && pos > 0 && pos == end {
// A deep wildcard ("**") is allowed to match zero path segments when it is
// the first thing being matched in the pattern (pos == 0). But once a literal
// or variable segment has already consumed a component, "**" needs the request
// path to continue past that segment — it cannot succeed with nothing left to
// consume at all (though an empty trailing segment, e.g. from a trailing slash,
// still counts as a real component and is fine).
// See https://github.com/grpc-ecosystem/grpc-gateway/issues/5771.
return nil, ErrNotMatch
}
c := strings.Join(components[pos:end], "/")
if c, err = unescape(c, unescapingMode, true); err != nil {
return nil, err