Fix newline escaping in line protocol (#3992)
This commit is contained in:
parent
d9230ac92d
commit
cad0cf4c78
File diff suppressed because it is too large
Load Diff
|
@ -142,7 +142,7 @@ fieldfloat =
|
||||||
fieldinteger =
|
fieldinteger =
|
||||||
(integer 'i') >begin %integer;
|
(integer 'i') >begin %integer;
|
||||||
|
|
||||||
fieldunsigned =
|
fieldunsigned =
|
||||||
(unsigned 'u') >begin %unsigned;
|
(unsigned 'u') >begin %unsigned;
|
||||||
|
|
||||||
false =
|
false =
|
||||||
|
@ -155,7 +155,7 @@ fieldbool =
|
||||||
(true | false) >begin %bool;
|
(true | false) >begin %bool;
|
||||||
|
|
||||||
fieldstringchar =
|
fieldstringchar =
|
||||||
[^\\"] | '\\' [\\"];
|
[^\n\f\r\\"] | '\\' [\\"];
|
||||||
|
|
||||||
fieldstring =
|
fieldstring =
|
||||||
fieldstringchar* >begin %string;
|
fieldstringchar* >begin %string;
|
||||||
|
|
|
@ -1154,6 +1154,22 @@ var tests = []struct {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "invalid newline in string field",
|
||||||
|
input: []byte("cpu value=\"4\n2\""),
|
||||||
|
results: []Result{
|
||||||
|
Result{
|
||||||
|
Name: Measurement,
|
||||||
|
Value: []byte("cpu"),
|
||||||
|
},
|
||||||
|
Result{
|
||||||
|
err: ErrFieldParse,
|
||||||
|
},
|
||||||
|
Result{
|
||||||
|
err: ErrFieldParse,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "invalid field value",
|
name: "invalid field value",
|
||||||
input: []byte(`cpu value=howdy`),
|
input: []byte(`cpu value=howdy`),
|
||||||
|
|
|
@ -3,30 +3,42 @@ package influx
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
escapes = " ,="
|
escapes = "\t\n\f\r ,="
|
||||||
nameEscapes = " ,"
|
nameEscapes = "\t\n\f\r ,"
|
||||||
stringFieldEscapes = `\"`
|
stringFieldEscapes = "\t\n\f\r\\\""
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
escaper = strings.NewReplacer(
|
escaper = strings.NewReplacer(
|
||||||
|
"\t", `\t`,
|
||||||
|
"\n", `\n`,
|
||||||
|
"\f", `\f`,
|
||||||
|
"\r", `\r`,
|
||||||
`,`, `\,`,
|
`,`, `\,`,
|
||||||
`"`, `\"`, // ???
|
|
||||||
` `, `\ `,
|
` `, `\ `,
|
||||||
`=`, `\=`,
|
`=`, `\=`,
|
||||||
)
|
)
|
||||||
|
|
||||||
nameEscaper = strings.NewReplacer(
|
nameEscaper = strings.NewReplacer(
|
||||||
|
"\t", `\t`,
|
||||||
|
"\n", `\n`,
|
||||||
|
"\f", `\f`,
|
||||||
|
"\r", `\r`,
|
||||||
`,`, `\,`,
|
`,`, `\,`,
|
||||||
` `, `\ `,
|
` `, `\ `,
|
||||||
)
|
)
|
||||||
|
|
||||||
stringFieldEscaper = strings.NewReplacer(
|
stringFieldEscaper = strings.NewReplacer(
|
||||||
|
"\t", `\t`,
|
||||||
|
"\n", `\n`,
|
||||||
|
"\f", `\f`,
|
||||||
|
"\r", `\r`,
|
||||||
`"`, `\"`,
|
`"`, `\"`,
|
||||||
`\`, `\\`,
|
`\`, `\\`,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Escape a tagkey, tagvalue, or fieldkey
|
||||||
func escape(s string) string {
|
func escape(s string) string {
|
||||||
if strings.ContainsAny(s, escapes) {
|
if strings.ContainsAny(s, escapes) {
|
||||||
return escaper.Replace(s)
|
return escaper.Replace(s)
|
||||||
|
@ -35,6 +47,7 @@ func escape(s string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Escape a measurement name
|
||||||
func nameEscape(s string) string {
|
func nameEscape(s string) string {
|
||||||
if strings.ContainsAny(s, nameEscapes) {
|
if strings.ContainsAny(s, nameEscapes) {
|
||||||
return nameEscaper.Replace(s)
|
return nameEscaper.Replace(s)
|
||||||
|
@ -43,6 +56,7 @@ func nameEscape(s string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Escape a string field
|
||||||
func stringFieldEscape(s string) string {
|
func stringFieldEscape(s string) string {
|
||||||
if strings.ContainsAny(s, stringFieldEscapes) {
|
if strings.ContainsAny(s, stringFieldEscapes) {
|
||||||
return stringFieldEscaper.Replace(s)
|
return stringFieldEscaper.Replace(s)
|
||||||
|
|
|
@ -261,6 +261,50 @@ var tests = []struct {
|
||||||
),
|
),
|
||||||
output: []byte("cpu abc=123i 1519194109000000042\ncpu def=456i 1519194109000000042\n"),
|
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",
|
name: "need more space",
|
||||||
maxBytes: 32,
|
maxBytes: 32,
|
||||||
|
|
Loading…
Reference in New Issue