|
5 | 5 | package logger |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "encoding/xml" |
| 9 | + "os" |
| 10 | + "regexp" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/clbanning/mxj" |
8 | 14 | "github.com/dfeyer/flow-debugproxy/config" |
9 | 15 |
|
10 | 16 | "bytes" |
11 | 17 | "fmt" |
| 18 | + |
12 | 19 | "github.com/mgutz/ansi" |
13 | 20 | ) |
14 | 21 |
|
15 | 22 | 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]*`) |
19 | 27 | ) |
20 | 28 |
|
| 29 | +type node struct { |
| 30 | + Attr []xml.Attr |
| 31 | + XMLName xml.Name |
| 32 | + Children []node `xml:",any"` |
| 33 | + Text string `xml:",chardata"` |
| 34 | +} |
| 35 | + |
21 | 36 | // Logger handle log message |
22 | 37 | type Logger struct { |
23 | 38 | Config *config.Config |
@@ -45,6 +60,30 @@ func (l *Logger) Colorize(str, style string) string { |
45 | 60 | return ansi.Color(str, style) |
46 | 61 | } |
47 | 62 |
|
| 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 | + |
48 | 87 | //FormatTextProtocol replace NULL by a line break for output formatting |
49 | 88 | func (l *Logger) FormatTextProtocol(protocol []byte) []byte { |
50 | 89 | return bytes.Trim(bytes.Replace(protocol, []byte("\x00"), []byte("\n"), -1), "\n") |
|
0 commit comments