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

Fix escaping of HTML characters #2322

Merged
merged 2 commits into from
Feb 17, 2017
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
9 changes: 7 additions & 2 deletions command/agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ const (
var (
// jsonHandle and jsonHandlePretty are the codec handles to JSON encode
// structs. The pretty handle will add indents for easier human consumption.
jsonHandle = &codec.JsonHandle{}
jsonHandlePretty = &codec.JsonHandle{Indent: 4}
jsonHandle = &codec.JsonHandle{
HTMLCharsAsIs: true,
}
jsonHandlePretty = &codec.JsonHandle{
HTMLCharsAsIs: true,
Indent: 4,
}
)

// HTTPServer is used to wrap an Agent and expose it over an HTTP interface
Expand Down
16 changes: 13 additions & 3 deletions command/data_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ package command

import (
"bytes"
"encoding/json"
"fmt"
"io"
"text/template"

"github.com/ugorji/go/codec"
)

var (
jsonHandlePretty = &codec.JsonHandle{
HTMLCharsAsIs: true,
Indent: 4,
}
)

//DataFormatter is a transformer of the data.
Expand Down Expand Up @@ -33,12 +41,14 @@ type JSONFormat struct {

// TransformData returns JSON format string data.
func (p *JSONFormat) TransformData(data interface{}) (string, error) {
out, err := json.MarshalIndent(&data, "", " ")
var buf bytes.Buffer
enc := codec.NewEncoder(&buf, jsonHandlePretty)
err := enc.Encode(data)
if err != nil {
return "", err
}

return string(out), nil
return buf.String(), nil
}

type TemplateFormat struct {
Expand Down
6 changes: 3 additions & 3 deletions command/data_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type testData struct {
}

const expectJSON = `{
"Region": "global",
"ID": "1",
"Name": "example"
"Name": "example",
"Region": "global"
}`

var (
Expand All @@ -36,7 +36,7 @@ func TestDataFormat(t *testing.T) {
}

if result != expectOutput[k] {
t.Fatalf("expected output: %s, actual: %s", expectOutput[k], result)
t.Fatalf("expected output:\n%s\nactual:\n%s", expectOutput[k], result)
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions command/inspect.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package command

import (
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -158,12 +157,17 @@ func (c *InspectCommand) Run(args []string) int {

// Print the contents of the job
req := api.RegisterJobRequest{Job: job}
buf, err := json.MarshalIndent(req, "", " ")
f, err := DataFormat("json", "")
if err != nil {
c.Ui.Error(fmt.Sprintf("Error converting job: %s", err))
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
return 1
}

c.Ui.Output(string(buf))
out, err := f.TransformData(req)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
return 0
}
4 changes: 2 additions & 2 deletions vendor/github.com/ugorji/go/codec/0doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/ugorji/go/codec/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions vendor/github.com/ugorji/go/codec/binc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion vendor/github.com/ugorji/go/codec/cbor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 57 additions & 10 deletions vendor/github.com/ugorji/go/codec/decode.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/github.com/ugorji/go/codec/decode_go.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions vendor/github.com/ugorji/go/codec/decode_go14.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading