Skip to content

Commit b1cdb0a

Browse files
committed
TASK: Beautify XML protocol CLI output
1 parent 7229ba4 commit b1cdb0a

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

logger/logger.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,34 @@
55
package logger
66

77
import (
8+
"encoding/xml"
9+
"os"
10+
"regexp"
11+
"strings"
12+
13+
"github.com/clbanning/mxj"
814
"github.com/dfeyer/flow-debugproxy/config"
915

1016
"bytes"
1117
"fmt"
18+
1219
"github.com/mgutz/ansi"
1320
)
1421

1522
var (
16-
debugize = ansi.ColorFunc("green+h:black")
17-
greenize = ansi.ColorFunc("green")
18-
redize = ansi.ColorFunc("red")
23+
debugize = ansi.ColorFunc("green+h:black")
24+
greenize = ansi.ColorFunc("green")
25+
redize = ansi.ColorFunc("red")
26+
regexpFirstNumber = regexp.MustCompile(`^[0-9]*`)
1927
)
2028

29+
type node struct {
30+
Attr []xml.Attr
31+
XMLName xml.Name
32+
Children []node `xml:",any"`
33+
Text string `xml:",chardata"`
34+
}
35+
2136
// Logger handle log message
2237
type Logger struct {
2338
Config *config.Config
@@ -45,6 +60,30 @@ func (l *Logger) Colorize(str, style string) string {
4560
return ansi.Color(str, style)
4661
}
4762

63+
func normalizeXMLProtocol(buffer []byte) string {
64+
b := bytes.Replace(buffer, []byte("\x00"), []byte("\n"), -1)
65+
buf := make([]rune, len(b))
66+
for i, b := range b {
67+
buf[i] = rune(b)
68+
}
69+
s := strings.Replace(string(buf), "iso-8859-1", "utf-8", 1)
70+
s = regexpFirstNumber.ReplaceAllString(s, "")
71+
s = strings.Replace(s, "<?xml version=\"1.0\" encoding=\"utf-8\"?>", "", 1)
72+
s = strings.Trim(s, "\n")
73+
return s
74+
}
75+
76+
//FormatXMLProtocol beautify XML output
77+
func (l *Logger) FormatXMLProtocol(protocol []byte) []byte {
78+
p := normalizeXMLProtocol(protocol)
79+
output, err := mxj.BeautifyXml([]byte(p), "", " ")
80+
if err != nil {
81+
fmt.Println(err)
82+
os.Exit(1)
83+
}
84+
return output
85+
}
86+
4887
//FormatTextProtocol replace NULL by a line break for output formatting
4988
func (l *Logger) FormatTextProtocol(protocol []byte) []byte {
5089
return bytes.Trim(bytes.Replace(protocol, []byte("\x00"), []byte("\n"), -1), "\n")

xdebugproxy/xdebugproxy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (p *Proxy) pipe(src, dst *net.TCPConn) {
107107
p.log(h, f)
108108
if p.Config.VeryVerbose {
109109
if isFromDebugger {
110-
p.log("Raw protocol:\n%s\n", p.Logger.Colorize(fmt.Sprintf(h, b), "blue"))
110+
p.log("Raw protocol:\n%s\n", p.Logger.Colorize(fmt.Sprintf(h, p.Logger.FormatXMLProtocol(b)), "blue"))
111111
} else {
112112
p.log("Raw protocol:\n%s\n", p.Logger.Colorize(fmt.Sprintf(h, p.Logger.FormatTextProtocol(b)), "blue"))
113113
}
@@ -132,7 +132,7 @@ func (p *Proxy) pipe(src, dst *net.TCPConn) {
132132
// show output
133133
if p.Config.VeryVerbose {
134134
if isFromDebugger {
135-
p.log("Processed protocol:\n%s\n", p.Logger.Colorize(fmt.Sprintf(h, b), "blue"))
135+
p.log("Processed protocol:\n%s\n", p.Logger.Colorize(fmt.Sprintf(h, p.Logger.FormatXMLProtocol(b)), "blue"))
136136
} else {
137137
p.log("Processed protocol:\n%s\n", p.Logger.Colorize(fmt.Sprintf(h, p.Logger.FormatTextProtocol(b)), "blue"))
138138
}

0 commit comments

Comments
 (0)