diff --git a/README.md b/README.md index 068c876e..99b8c77e 100644 --- a/README.md +++ b/README.md @@ -650,7 +650,8 @@ Currently supported placeholders are: Before replacing the placeholders in the process config, all references (see below) will be resolved. If the value that gets filled in on the place of the placeholder needs escaping, you can define the character to be escaped in the placeholder by adding it to the placeholder name and prefix it with a `^`. -E.g. escape all `:` in the value (`http://example.com:8080`) for `{memfs}` placeholder, write `{memfs^:}`. It will then be replaced by `http\://example.com\:8080`. The escape character is always `\`. +E.g. escape all `:` in the value (`http://example.com:8080`) for `{memfs}` placeholder, write `{memfs^:}`. It will then be replaced by `http\://example.com\:8080`. The escape character is always `\`. In +case there are `\` in the value, they will also get escaped. If the placeholder doesn't imply escaping, the value will be uses as-is. ### References diff --git a/restream/app/process.go b/restream/app/process.go index a726c51f..d9178f2e 100644 --- a/restream/app/process.go +++ b/restream/app/process.go @@ -88,12 +88,13 @@ func replace(what, placeholder, value string) string { what = re.ReplaceAllStringFunc(what, func(match string) string { matches := re.FindStringSubmatch(match) - var v string + v := value if matches[2] != "" { - v = strings.ReplaceAll(value, matches[2], `\`+matches[2]) - } else { - v = value + if matches[2] != `\` { + v = strings.ReplaceAll(v, `\`, `\\`) + } + v = strings.ReplaceAll(v, matches[2], `\`+matches[2]) } return strings.Replace(match, match, v, 1) diff --git a/restream/app/process_test.go b/restream/app/process_test.go index 96ef22a0..b7f296da 100644 --- a/restream/app/process_test.go +++ b/restream/app/process_test.go @@ -7,21 +7,22 @@ import ( ) func TestReplace(t *testing.T) { - foobar := `;:.,-_$£!^` + foobar := `;:.,-_$\£!^` samples := [][2]string{ {"{foobar}", foobar}, - {"{foobar^:}", `;\:.,-_$£!^`}, - {"{foobar^:}barfoo{foobar^:}", `;\:.,-_$£!^barfoo;\:.,-_$£!^`}, + {"{foobar^:}", `;\:.,-_$\\£!^`}, + {"{foobar^:}barfoo{foobar^:}", `;\:.,-_$\\£!^barfoo;\:.,-_$\\£!^`}, {"{foobar^:.}", "{foobar^:.}"}, {"{foobar^}", "{foobar^}"}, {"{barfoo^:}", "{barfoo^:}"}, - {"{foobar^^}", `;:.,-_$£!\^`}, + {"{foobar^^}", `;:.,-_$\\£!\^`}, + {`{foobar^\}`, `;:.,-_$\\£!^`}, } for _, e := range samples { replaced := replace(e[0], "foobar", foobar) - require.Equal(t, e[1], replaced) + require.Equal(t, e[1], replaced, e[0]) } }