2018-02-14 18:49:26 +01:00
|
|
|
package xmpp
|
2018-02-11 19:35:32 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2018-02-11 22:03:58 +01:00
|
|
|
func XMLStartElementToString(element *xml.StartElement) string {
|
|
|
|
if element == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
debug := fmt.Sprintf("<%s xmlns=\"%s\"", element.Name.Local, element.Name.Space)
|
|
|
|
for _, attr := range element.Attr {
|
2018-02-11 22:43:42 +01:00
|
|
|
debug = fmt.Sprintf("%s %s=\"%s\"", debug, attr.Name.Local, attr.Value)
|
2018-02-11 22:03:58 +01:00
|
|
|
}
|
|
|
|
debug += ">"
|
|
|
|
return debug
|
|
|
|
}
|
|
|
|
|
2018-02-11 19:35:32 +01:00
|
|
|
func XMLChildrenString(o interface{}) (result string) {
|
|
|
|
val := reflect.ValueOf(o)
|
2018-02-16 08:29:35 +01:00
|
|
|
if val.Kind() == reflect.Ptr {
|
|
|
|
val = val.Elem()
|
2018-02-11 19:35:32 +01:00
|
|
|
}
|
2018-02-14 18:49:26 +01:00
|
|
|
if val.Kind() == reflect.Struct {
|
|
|
|
first := true
|
|
|
|
for i := 0; i < val.NumField(); i++ {
|
|
|
|
valueField := val.Field(i)
|
2018-02-11 19:35:32 +01:00
|
|
|
|
2018-02-14 18:49:26 +01:00
|
|
|
if xmlElement, ok := valueField.Interface().(*xml.Name); ok && xmlElement != nil {
|
|
|
|
if first {
|
|
|
|
first = false
|
|
|
|
result += xmlElement.Local
|
|
|
|
} else {
|
|
|
|
result += ", " + xmlElement.Local
|
|
|
|
}
|
2018-02-11 19:35:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cookie is used to give a unique identifier to each request.
|
|
|
|
type Cookie uint64
|
|
|
|
|
|
|
|
func CreateCookie() Cookie {
|
|
|
|
var buf [8]byte
|
|
|
|
if _, err := rand.Reader.Read(buf[:]); err != nil {
|
|
|
|
panic("Failed to read random bytes: " + err.Error())
|
|
|
|
}
|
|
|
|
return Cookie(binary.LittleEndian.Uint64(buf[:]))
|
|
|
|
}
|
|
|
|
func CreateCookieString() string {
|
|
|
|
return fmt.Sprintf("%x", CreateCookie())
|
|
|
|
}
|