diff options
author | xengineering <me@xengineering.eu> | 2023-05-18 21:43:37 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-05-18 21:43:37 +0200 |
commit | 7fc8ea8ca5efb6adf6fa7c54d1b39f010e17eb7b (patch) | |
tree | 956dad006d8e623a36ae2a3213f7246ac5567502 | |
parent | 05f50be52b03c7e84ba47d3b5349321920748f8b (diff) | |
download | limox-7fc8ea8ca5efb6adf6fa7c54d1b39f010e17eb7b.tar limox-7fc8ea8ca5efb6adf6fa7c54d1b39f010e17eb7b.tar.zst limox-7fc8ea8ca5efb6adf6fa7c54d1b39f010e17eb7b.zip |
Handle server side debug in xmpp/decoder.go
-rw-r--r-- | xmpp/decoder.go | 32 | ||||
-rw-r--r-- | xmpp/xmpp.go | 10 |
2 files changed, 28 insertions, 14 deletions
diff --git a/xmpp/decoder.go b/xmpp/decoder.go index 97329c7..070f791 100644 --- a/xmpp/decoder.go +++ b/xmpp/decoder.go @@ -4,20 +4,29 @@ import ( "crypto/tls" "encoding/xml" "log" + "os" + "io" + "errors" ) type decoder struct { terminator chan bool data chan xml.Token decoder *xml.Decoder + debug *xml.Encoder } func newDecoder(conn *tls.Conn) decoder { - return decoder{ + d := decoder{ make(chan bool), make(chan xml.Token), xml.NewDecoder(conn), + xml.NewEncoder(os.Stdout), } + + d.debug.Indent("S: ", " ") + + return d } func (r *decoder) run() { @@ -27,12 +36,23 @@ func (r *decoder) run() { return default: t, err := r.decoder.Token() - if err != nil { - log.Print(err) + if t != nil && err == nil { + err := r.debug.EncodeToken(t) + if err != nil { + log.Println("Could not encode received XML token to debug output!") + } + err = r.debug.Flush() + if err != nil { + log.Println("Could not flush debug output after receive!") + } + r.data <- t } - if t != nil { - c := xml.CopyToken(t) - r.data <- c + if err != nil { + if errors.Is(err, io.EOF) { + return + } else { + log.Printf("Decoding error: %v", err) + } } } } diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index 4412a96..773ea44 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -5,7 +5,6 @@ import ( "crypto/x509" "encoding/xml" "log" - "os" ) type Event uint8 @@ -31,9 +30,6 @@ func Run(ch chan Event, jid string, pwd string) { enc := newEncoder(conn) defer enc.Close() - dbg := xml.NewEncoder(os.Stdout) - defer dbg.Close() - end := sendStreamStart(&enc, jid) defer sendStreamEnd(&enc, end) @@ -49,10 +45,8 @@ func Run(ch chan Event, jid string, pwd string) { default: log.Printf("Unknown Event '%d'!\n", ev) } - case rx := <-decoder.data: - dbg.Indent("S: ", " ") - dbg.EncodeToken(rx) - dbg.Flush() + case _ = <-decoder.data: + // do something with incoming tokens here } } } |