refactor ciims proxy transport and handlers
This commit is contained in:
+41
-36
@@ -3,7 +3,7 @@ package internal
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"regexp"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -82,66 +82,71 @@ const (
|
||||
|
||||
</soap:Envelope>
|
||||
`
|
||||
|
||||
msgExp = `<ns1:string>.*<\/ns1:string>`
|
||||
errExp = `<errorMessage .*<\/errorMessage>`
|
||||
)
|
||||
|
||||
// CreateSend xml string for sending
|
||||
func CreateSend(user string, pass string, priority int, event string, valXML bool, message string) string {
|
||||
var mb bytes.Buffer
|
||||
xml.Escape(&mb, []byte(message))
|
||||
msg := strings.Replace(sendtpl, "##user##", user, -1)
|
||||
msg = strings.Replace(msg, "##pass##", pass, -1)
|
||||
msg = strings.Replace(msg, "##event##", event, -1)
|
||||
msg := strings.Replace(sendtpl, "##user##", xmlEscape(user), -1)
|
||||
msg = strings.Replace(msg, "##pass##", xmlEscape(pass), -1)
|
||||
msg = strings.Replace(msg, "##event##", xmlEscape(event), -1)
|
||||
msg = strings.Replace(msg, "##priority##", strconv.Itoa(priority), -1)
|
||||
msg = strings.Replace(msg, "##xml##", strconv.FormatBool(valXML), -1)
|
||||
msg = strings.Replace(msg, "##msg##", mb.String(), -1)
|
||||
return msg
|
||||
}
|
||||
|
||||
//CreateReceive message
|
||||
// CreateReceive message
|
||||
func CreateReceive(user string, pass string, count int) string {
|
||||
msg := strings.Replace(receivetpl, "##user##", user, -1)
|
||||
msg = strings.Replace(msg, "##pass##", pass, -1)
|
||||
msg := strings.Replace(receivetpl, "##user##", xmlEscape(user), -1)
|
||||
msg = strings.Replace(msg, "##pass##", xmlEscape(pass), -1)
|
||||
msg = strings.Replace(msg, "##count##", strconv.Itoa(count), -1)
|
||||
return msg
|
||||
}
|
||||
|
||||
//Map get values
|
||||
func Map(vs []string, f func(string) string) []string {
|
||||
vsm := make([]string, len(vs))
|
||||
for i, v := range vs {
|
||||
vsm[i] = f(v)
|
||||
}
|
||||
return vsm
|
||||
func xmlEscape(s string) string {
|
||||
var buf bytes.Buffer
|
||||
xml.Escape(&buf, []byte(s))
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
//GetMsgs get message from soap response
|
||||
// GetMsgs get message from soap response
|
||||
func GetMsgs(soap string) []string {
|
||||
r, _ := regexp.Compile(msgExp)
|
||||
msgs := r.FindAllString(soap, -1)
|
||||
return Map(msgs, split)
|
||||
return xmlElementTexts(soap, "string")
|
||||
}
|
||||
|
||||
//GetErrMsg get error message
|
||||
// GetErrMsg get error message
|
||||
func GetErrMsg(soap string) string {
|
||||
r, _ := regexp.Compile(errExp)
|
||||
msg := r.FindAllString(soap, -1)
|
||||
if len(msg) < 1 {
|
||||
return ""
|
||||
matches := xmlElementTexts(soap, "errorMessage")
|
||||
if len(matches) > 0 {
|
||||
return matches[0]
|
||||
}
|
||||
if len(msg[0]) > 40 {
|
||||
size := len(msg[0])
|
||||
return msg[0][54 : size-15]
|
||||
}
|
||||
return msg[0]
|
||||
return ""
|
||||
}
|
||||
|
||||
func split(msg string) string {
|
||||
if len(msg) > 12 {
|
||||
return msg[12 : len(msg)-13]
|
||||
}
|
||||
return msg
|
||||
func xmlElementTexts(soap string, localName string) []string {
|
||||
decoder := xml.NewDecoder(strings.NewReader(soap))
|
||||
var result []string
|
||||
for {
|
||||
token, err := decoder.Token()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return result
|
||||
}
|
||||
|
||||
start, ok := token.(xml.StartElement)
|
||||
if !ok || start.Name.Local != localName {
|
||||
continue
|
||||
}
|
||||
|
||||
var text string
|
||||
if err := decoder.DecodeElement(&text, &start); err != nil {
|
||||
return result
|
||||
}
|
||||
result = append(result, text)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user