@@ -27,7 +27,6 @@ import (
27
27
mxj "github.com/clbanning/mxj/v2"
28
28
"github.com/iann0036/goproxy"
29
29
"github.com/mitchellh/go-homedir"
30
- "github.com/ucarion/urlpath"
31
30
)
32
31
33
32
//go:embed service/*
@@ -587,7 +586,7 @@ func handleAWSRequest(req *http.Request, body []byte, respCode int) {
587
586
588
587
pathMatches := regexp .MustCompile (regexStr ).FindAllStringSubmatch (path , - 1 )
589
588
590
- if len (pathMatches ) > 0 && len (pathMatches ) > 0 && len (templateMatches ) == len (pathMatches [0 ])- 1 {
589
+ if len (pathMatches ) > 0 && len (templateMatches ) > 0 && len (templateMatches ) == len (pathMatches [0 ])- 1 {
591
590
for i := 0 ; i < len (templateMatches ); i ++ {
592
591
uriparams [templateMatches [i ][1 ]] = pathMatches [0 ][1 :][i ]
593
592
}
@@ -748,6 +747,8 @@ func handleAWSRequest(req *http.Request, body []byte, respCode int) {
748
747
handleLoggedCall ()
749
748
}
750
749
750
+ var azurermregex = regexp .MustCompile (`^/subscriptions/.+/resourcegroups/.+/providers/Microsoft\.Resources/deployments/.+` )
751
+
751
752
func handleAzureRequest (req * http.Request , body []byte , respCode int ) {
752
753
host := req .Host
753
754
@@ -764,9 +765,7 @@ func handleAzureRequest(req *http.Request, body []byte, respCode int) {
764
765
765
766
// Handle AzureRM deployments (inline only)
766
767
if req .Method == "PUT" { // TODO: other similar methods
767
- pathmatch := urlpath .New ("/subscriptions/:subscriptionId/resourcegroups/:resourceGroupName/providers/Microsoft.Resources/deployments/:deploymentName" )
768
- _ , ok := pathmatch .Match (req .URL .Path )
769
- if ok {
768
+ if azurermregex .MatchString (req .URL .Path ) {
770
769
data := struct {
771
770
Properties struct {
772
771
Template interface {} `json:"template"`
@@ -820,6 +819,27 @@ func handleAzureRequest(req *http.Request, body []byte, respCode int) {
820
819
handleLoggedCall ()
821
820
}
822
821
822
+ func generateMethodTemplate (path string ) string {
823
+ path = regexp .QuoteMeta (path )
824
+ i := strings .Index (path , "\\ {" )
825
+ for i != - 1 {
826
+ j := strings .Index (path [i :], "\\ }" )
827
+ if j != - 1 {
828
+ path = path [:i ] + ".+" + path [i + j + len ("\\ }" ):]
829
+ } else {
830
+ break
831
+ }
832
+ i = strings .Index (path , "\\ {" )
833
+ }
834
+
835
+ pathtemplate := "^/" + path
836
+ if pathtemplate [0 :3 ] == "^//" {
837
+ pathtemplate = "^" + pathtemplate [2 :]
838
+ }
839
+
840
+ return pathtemplate
841
+ }
842
+
823
843
func gcpProcessResource (req * http.Request , gcpResource GCPResourceDefinition , basePath string ) string {
824
844
for _ , gcpSubresource := range gcpResource .Resources {
825
845
apiID := gcpProcessResource (req , gcpSubresource , basePath )
@@ -830,14 +850,10 @@ func gcpProcessResource(req *http.Request, gcpResource GCPResourceDefinition, ba
830
850
831
851
for _ , gcpMethod := range gcpResource .Methods {
832
852
if req .Method == gcpMethod .HTTPMethod {
833
- pathtemplate := "/" + strings .ReplaceAll (strings .ReplaceAll (basePath + gcpMethod .FlatPath , "{" , ":" ), "}" , "" )
834
- if pathtemplate [0 :2 ] == "//" {
835
- pathtemplate = pathtemplate [1 :]
836
- }
837
- pathmatch := urlpath .New (pathtemplate )
853
+ pathtemplate := generateMethodTemplate (basePath + gcpMethod .FlatPath )
838
854
839
- _ , ok := pathmatch . Match ( req . URL . Path )
840
- if ok {
855
+ r , err := regexp . Compile ( pathtemplate )
856
+ if err == nil && r . MatchString ( req . URL . Path ) {
841
857
return gcpMethod .ID
842
858
}
843
859
}
0 commit comments