Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing bug with #22 #31

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 49 additions & 49 deletions config/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,63 @@
package config

import (
"fmt"
"github.com/pb33f/wiretap/shared"
"strings"
"fmt"
"github.com/pb33f/wiretap/shared"
"strings"
)

func FindPaths(path string, configuration *shared.WiretapConfiguration) []*shared.WiretapPathConfig {
var foundConfigurations []*shared.WiretapPathConfig
for key := range configuration.CompiledPaths {
if configuration.CompiledPaths[key].CompiledKey.Match(path) {
foundConfigurations = append(foundConfigurations, configuration.CompiledPaths[key].PathConfig)
}
}
return foundConfigurations
var foundConfigurations []*shared.WiretapPathConfig
for key := range configuration.CompiledPaths {
if configuration.CompiledPaths[key].CompiledKey.Match(path) {
foundConfigurations = append(foundConfigurations, configuration.CompiledPaths[key].PathConfig)
}
}
return foundConfigurations
}

func RewritePath(path string, configuration *shared.WiretapConfiguration) string {
paths := FindPaths(path, configuration)
var replaced string
if len(paths) > 0 {
// extract first path
pathConfig := paths[0]
replaced = ""
for key := range pathConfig.CompiledPath.CompiledPathRewrite {
if pathConfig.CompiledPath.CompiledPathRewrite[key].MatchString(path) {
replace := pathConfig.PathRewrite[key]
rex := pathConfig.CompiledPath.CompiledPathRewrite[key]
replacedPath := rex.ReplaceAllString(path, replace)
paths := FindPaths(path, configuration)
var replaced string
if len(paths) > 0 {
// extract first path
pathConfig := paths[0]
replaced = ""
for key := range pathConfig.CompiledPath.CompiledPathRewrite {
if pathConfig.CompiledPath.CompiledPathRewrite[key].MatchString(path) {
replace := pathConfig.PathRewrite[key]
rex := pathConfig.CompiledPath.CompiledPathRewrite[key]
replacedPath := rex.ReplaceAllString(path, replace)

scheme := "http://"
if pathConfig.Secure {
scheme = "https://"
}
if replacedPath[0] != '/' && pathConfig.Target[len(pathConfig.Target)-1] != '/' {
replacedPath = fmt.Sprintf("/%s", replacedPath)
}
target := strings.ReplaceAll(strings.ReplaceAll(configuration.ReplaceWithVariables(pathConfig.Target),
"http://", ""), "https://", "")
scheme := "http://"
if pathConfig.Secure {
scheme = "https://"
}
if replacedPath[0] != '/' && pathConfig.Target[len(pathConfig.Target)-1] != '/' {
replacedPath = fmt.Sprintf("/%s", replacedPath)
}
target := strings.ReplaceAll(strings.ReplaceAll(configuration.ReplaceWithVariables(pathConfig.Target),
"http://", ""), "https://", "")

replaced = fmt.Sprintf("%s%s%s", scheme, target, replacedPath)
break
}
}
// no rewriting, just replace target.
if replaced == "" {
scheme := "http://"
if pathConfig.Secure {
scheme = "https://"
}
target := strings.ReplaceAll(strings.ReplaceAll(configuration.ReplaceWithVariables(pathConfig.Target),
"http://", ""), "https://", "")
replaced = fmt.Sprintf("%s%s%s", scheme, target, replacedPath)
break
}
}
// no rewriting, just replace target.
if replaced == "" {
scheme := "http://"
if pathConfig.Secure {
scheme = "https://"
}
target := strings.ReplaceAll(strings.ReplaceAll(configuration.ReplaceWithVariables(pathConfig.Target),
"http://", ""), "https://", "")

if path[0] != '/' && pathConfig.Target[len(pathConfig.Target)-1] != '/' {
path = fmt.Sprintf("/%s", path)
}
replaced = fmt.Sprintf("%s%s%s", scheme, target, path)
}
}
if path[0] != '/' && pathConfig.Target[len(pathConfig.Target)-1] != '/' {
path = fmt.Sprintf("/%s", path)
}
replaced = fmt.Sprintf("%s%s%s", scheme, target, path)
}
}

return replaced
return replaced
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/google/uuid v1.3.0
github.com/pb33f/libopenapi v0.9.4
github.com/pb33f/libopenapi-validator v0.0.10
github.com/pb33f/libopenapi-validator v0.0.11
github.com/pb33f/ranch v0.2.2
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pb33f/libopenapi v0.9.4 h1:JRuL1nJZf0+K+cysjo9jXczfy94Vlm6kMMXib6sRzas=
github.com/pb33f/libopenapi v0.9.4/go.mod h1:8lr9sjsI5uZxtiEvHgg1A9/p/70briQ5WUGoJiuTFPc=
github.com/pb33f/libopenapi-validator v0.0.10 h1:+8WE5TPJ35ptp0FoiurRy0gPq9/BoezSBUgk140xZNQ=
github.com/pb33f/libopenapi-validator v0.0.10/go.mod h1:NjY6ZsalzzcenfYJe9NFB4Hy9DKWjM/7DTnf4MF+n/M=
github.com/pb33f/libopenapi-validator v0.0.11 h1:9xNAE2R7ZJU2J98sg0ZIhlHX2emJDT42DcCvVDPJAoQ=
github.com/pb33f/libopenapi-validator v0.0.11/go.mod h1:NjY6ZsalzzcenfYJe9NFB4Hy9DKWjM/7DTnf4MF+n/M=
github.com/pb33f/ranch v0.2.2 h1:pxff0FI0T+zr2o3bv5nKcTKk+41CjMX7fJr9L2tKGg4=
github.com/pb33f/ranch v0.2.2/go.mod h1:BHhS0EZw9pkK8cNtOxvIHaPMyFVd77I9cmy1kgKKA6M=
github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ=
Expand Down
12 changes: 7 additions & 5 deletions ui/src/wiretap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class WiretapComponent extends LitElement {
requestSpec() {
this._bus.publish({
destination: "/pub/queue/specs",
body: JSON.stringify({requestCommand: GetCurrentSpecCommand}),
body: JSON.stringify({request: GetCurrentSpecCommand}),
})
}

Expand All @@ -211,10 +211,12 @@ export class WiretapComponent extends LitElement {

specHandler(): BusCallback<CommandResponse> {
return (msg: CommandResponse) => {
const decoded = atob(msg.payload.payload);
this._specStore.set(WiretapCurrentSpec, decoded)
localforage.setItem(WiretapCurrentSpec, decoded);
this.requestUpdate();
if (msg.payload?.payload != null) {
const decoded = atob(msg.payload.payload);
this._specStore.set(WiretapCurrentSpec, decoded)
localforage.setItem(WiretapCurrentSpec, decoded);
this.requestUpdate();
}
}
}

Expand Down