Fix newline escaping in line protocol (#3992)

This commit is contained in:
Daniel Nelson
2018-04-09 15:29:52 -07:00
committed by GitHub
parent d9230ac92d
commit cad0cf4c78
5 changed files with 5302 additions and 5084 deletions

View File

@@ -3,30 +3,42 @@ package influx
import "strings"
const (
escapes = " ,="
nameEscapes = " ,"
stringFieldEscapes = `\"`
escapes = "\t\n\f\r ,="
nameEscapes = "\t\n\f\r ,"
stringFieldEscapes = "\t\n\f\r\\\""
)
var (
escaper = strings.NewReplacer(
"\t", `\t`,
"\n", `\n`,
"\f", `\f`,
"\r", `\r`,
`,`, `\,`,
`"`, `\"`, // ???
` `, `\ `,
`=`, `\=`,
)
nameEscaper = strings.NewReplacer(
"\t", `\t`,
"\n", `\n`,
"\f", `\f`,
"\r", `\r`,
`,`, `\,`,
` `, `\ `,
)
stringFieldEscaper = strings.NewReplacer(
"\t", `\t`,
"\n", `\n`,
"\f", `\f`,
"\r", `\r`,
`"`, `\"`,
`\`, `\\`,
)
)
// Escape a tagkey, tagvalue, or fieldkey
func escape(s string) string {
if strings.ContainsAny(s, escapes) {
return escaper.Replace(s)
@@ -35,6 +47,7 @@ func escape(s string) string {
}
}
// Escape a measurement name
func nameEscape(s string) string {
if strings.ContainsAny(s, nameEscapes) {
return nameEscaper.Replace(s)
@@ -43,6 +56,7 @@ func nameEscape(s string) string {
}
}
// Escape a string field
func stringFieldEscape(s string) string {
if strings.ContainsAny(s, stringFieldEscapes) {
return stringFieldEscaper.Replace(s)

View File

@@ -261,6 +261,50 @@ var tests = []struct {
),
output: []byte("cpu abc=123i 1519194109000000042\ncpu def=456i 1519194109000000042\n"),
},
{
name: "name newline",
input: MustMetric(
metric.New(
"c\npu",
map[string]string{},
map[string]interface{}{
"value": 42,
},
time.Unix(0, 0),
),
),
output: []byte("c\\npu value=42i 0\n"),
},
{
name: "tag newline",
input: MustMetric(
metric.New(
"cpu",
map[string]string{
"host": "x\ny",
},
map[string]interface{}{
"value": 42,
},
time.Unix(0, 0),
),
),
output: []byte("cpu,host=x\\ny value=42i 0\n"),
},
{
name: "string newline",
input: MustMetric(
metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"value": "x\ny",
},
time.Unix(0, 0),
),
),
output: []byte("cpu value=\"x\\ny\" 0\n"),
},
{
name: "need more space",
maxBytes: 32,