mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-09-27 05:23:06 +00:00
Implement entry compacting
This commit is contained in:
@@ -188,7 +188,7 @@ func (g *SpecGen) handlePathObj(entryWithSource *EntryWithSource) (string, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
if entry.Request.Method == "OPTIONS" {
|
if entry.Request.Method == "OPTIONS" {
|
||||||
logger.Log.Debugf("Dropped traffic entry due to its method: %s", urlParsed.Path)
|
logger.Log.Debugf("Dropped traffic entry due to its method: %s %s", entry.Request.Method, urlParsed.Path)
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -214,7 +214,48 @@ func (n *Node) searchInConstants(pathChunk string) *Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) compact() {
|
func (n *Node) compact() {
|
||||||
// TODO
|
// TODO: introduce and leverage "dirty" flag?
|
||||||
|
var param *Node
|
||||||
|
// find the param
|
||||||
|
for _, subnode := range n.children {
|
||||||
|
if subnode.constant != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
param = subnode
|
||||||
|
}
|
||||||
|
|
||||||
|
if param != nil {
|
||||||
|
// take its regex
|
||||||
|
pRegex := param.pathParam.Schema.Pattern
|
||||||
|
if pRegex == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
newChildren := make([]*Node, 0)
|
||||||
|
|
||||||
|
// compact the constants via regex
|
||||||
|
for _, subnode := range n.children {
|
||||||
|
if subnode.constant != nil {
|
||||||
|
if pRegex.Match([]byte(*subnode.constant)) {
|
||||||
|
param.merge(subnode)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newChildren = append(newChildren, subnode)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(n.children) != len(newChildren) {
|
||||||
|
logger.Log.Debugf("Shrinking children from %d to %d", len(n.children), len(newChildren))
|
||||||
|
n.children = newChildren
|
||||||
|
n.compact()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// recurse into next tree level
|
||||||
|
for _, subnode := range n.children {
|
||||||
|
subnode.compact()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) listPaths() *openapi.Paths {
|
func (n *Node) listPaths() *openapi.Paths {
|
||||||
@@ -303,3 +344,18 @@ func (n *Node) countParentParams() int {
|
|||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Node) merge(other *Node) {
|
||||||
|
outer:
|
||||||
|
for _, oChild := range other.children {
|
||||||
|
for _, nChild := range n.children {
|
||||||
|
matchedConst := oChild.constant != nil && oChild.constant == nChild.constant
|
||||||
|
matchedParam := oChild.constant == nil && nChild.constant == nil
|
||||||
|
if matchedConst || matchedParam {
|
||||||
|
nChild.merge(oChild)
|
||||||
|
continue outer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
n.children = append(n.children, oChild)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user