Replace xml lib

This commit is contained in:
程广 2024-11-07 23:26:17 +08:00
parent 21548f76b6
commit afe338f3d8
10 changed files with 72 additions and 30 deletions

28
ge_test.go Normal file
View File

@ -0,0 +1,28 @@
package goonvif
import (
"os"
"testing"
"git.pyer.club/kingecg/goonvif/onvif/media"
"git.pyer.club/kingecg/goxml"
)
func TestDecodeGetProfile(t *testing.T) {
type Envelope struct {
Header struct{}
Body struct {
GetProfilesResponse media.GetProfilesResponse
}
}
data, err := os.ReadFile("result.xml")
if err != nil {
t.Fatal(err)
}
e := Envelope{}
de := goxml.Unmarshal(data, &e)
if de != nil {
t.Fatal(de)
}
t.Log(e.Body.GetProfilesResponse)
}

1
go.mod
View File

@ -12,6 +12,7 @@ require (
) )
require ( require (
git.pyer.club/kingecg/goxml v1.0.1 // indirect
github.com/bytedance/sonic v1.11.6 // indirect github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/base64x v0.1.4 // indirect

4
go.sum
View File

@ -1,3 +1,7 @@
git.pyer.club/kingecg/goxml v1.0.0 h1:csJ+gcWjTvMTy2pR4XpYLoevTro8hT7l1kpoUt4Xmpw=
git.pyer.club/kingecg/goxml v1.0.0/go.mod h1:w/5zeL8crK/Be3k+RxEn4NNzQ/ybqZuU3uplw86mTt4=
git.pyer.club/kingecg/goxml v1.0.1 h1:V2PbUSDkoq4gQUqkFO4zud+41GtSKq28A24lDeLkXaM=
git.pyer.club/kingecg/goxml v1.0.1/go.mod h1:w/5zeL8crK/Be3k+RxEn4NNzQ/ybqZuU3uplw86mTt4=
github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs= github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs=
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A= github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=

View File

@ -1,7 +1,6 @@
package onvif package onvif
import ( import (
"encoding/xml"
"errors" "errors"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -10,6 +9,8 @@ import (
"strconv" "strconv"
"strings" "strings"
xml "git.pyer.club/kingecg/goxml"
"git.pyer.club/kingecg/goonvif/onvif/device" "git.pyer.club/kingecg/goonvif/onvif/device"
"git.pyer.club/kingecg/goonvif/onvif/gosoap" "git.pyer.club/kingecg/goonvif/onvif/gosoap"
"git.pyer.club/kingecg/goonvif/onvif/networking" "git.pyer.club/kingecg/goonvif/onvif/networking"

View File

@ -1,13 +1,14 @@
package gosoap package gosoap
import ( import (
"encoding/xml"
"log" "log"
xml "git.pyer.club/kingecg/goxml"
"github.com/beevik/etree" "github.com/beevik/etree"
) )
//SoapMessage type from string // SoapMessage type from string
type SoapMessage string type SoapMessage string
// NewEmptySOAP return new SoapMessage // NewEmptySOAP return new SoapMessage
@ -20,7 +21,7 @@ func NewEmptySOAP() SoapMessage {
return SoapMessage(res) return SoapMessage(res)
} }
//NewSOAP Get a new soap message // NewSOAP Get a new soap message
func NewSOAP(headContent []*etree.Element, bodyContent []*etree.Element, namespaces map[string]string) SoapMessage { func NewSOAP(headContent []*etree.Element, bodyContent []*etree.Element, namespaces map[string]string) SoapMessage {
doc := buildSoapRoot() doc := buildSoapRoot()
//doc.IndentTabs() //doc.IndentTabs()
@ -34,7 +35,7 @@ func (msg SoapMessage) String() string {
return string(msg) return string(msg)
} }
//StringIndent handle indent // StringIndent handle indent
func (msg SoapMessage) StringIndent() string { func (msg SoapMessage) StringIndent() string {
doc := etree.NewDocument() doc := etree.NewDocument()
@ -48,7 +49,7 @@ func (msg SoapMessage) StringIndent() string {
return res return res
} }
//Body return body from Envelope // Body return body from Envelope
func (msg SoapMessage) Body() string { func (msg SoapMessage) Body() string {
doc := etree.NewDocument() doc := etree.NewDocument()
@ -65,7 +66,7 @@ func (msg SoapMessage) Body() string {
return res return res
} }
//AddStringBodyContent for Envelope // AddStringBodyContent for Envelope
func (msg *SoapMessage) AddStringBodyContent(data string) { func (msg *SoapMessage) AddStringBodyContent(data string) {
doc := etree.NewDocument() doc := etree.NewDocument()
@ -89,7 +90,7 @@ func (msg *SoapMessage) AddStringBodyContent(data string) {
*msg = SoapMessage(res) *msg = SoapMessage(res)
} }
//AddBodyContent for Envelope // AddBodyContent for Envelope
func (msg *SoapMessage) AddBodyContent(element *etree.Element) { func (msg *SoapMessage) AddBodyContent(element *etree.Element) {
doc := etree.NewDocument() doc := etree.NewDocument()
if err := doc.ReadFromString(msg.String()); err != nil { if err := doc.ReadFromString(msg.String()); err != nil {
@ -105,7 +106,7 @@ func (msg *SoapMessage) AddBodyContent(element *etree.Element) {
*msg = SoapMessage(res) *msg = SoapMessage(res)
} }
//AddBodyContents for Envelope body // AddBodyContents for Envelope body
func (msg *SoapMessage) AddBodyContents(elements []*etree.Element) { func (msg *SoapMessage) AddBodyContents(elements []*etree.Element) {
doc := etree.NewDocument() doc := etree.NewDocument()
if err := doc.ReadFromString(msg.String()); err != nil { if err := doc.ReadFromString(msg.String()); err != nil {
@ -126,7 +127,7 @@ func (msg *SoapMessage) AddBodyContents(elements []*etree.Element) {
*msg = SoapMessage(res) *msg = SoapMessage(res)
} }
//AddStringHeaderContent for Envelope body // AddStringHeaderContent for Envelope body
func (msg *SoapMessage) AddStringHeaderContent(data string) error { func (msg *SoapMessage) AddStringHeaderContent(data string) error {
doc := etree.NewDocument() doc := etree.NewDocument()
@ -154,7 +155,7 @@ func (msg *SoapMessage) AddStringHeaderContent(data string) error {
return nil return nil
} }
//AddHeaderContent for Envelope body // AddHeaderContent for Envelope body
func (msg *SoapMessage) AddHeaderContent(element *etree.Element) { func (msg *SoapMessage) AddHeaderContent(element *etree.Element) {
doc := etree.NewDocument() doc := etree.NewDocument()
if err := doc.ReadFromString(msg.String()); err != nil { if err := doc.ReadFromString(msg.String()); err != nil {
@ -170,7 +171,7 @@ func (msg *SoapMessage) AddHeaderContent(element *etree.Element) {
*msg = SoapMessage(res) *msg = SoapMessage(res)
} }
//AddHeaderContents for Envelope body // AddHeaderContents for Envelope body
func (msg *SoapMessage) AddHeaderContents(elements []*etree.Element) { func (msg *SoapMessage) AddHeaderContents(elements []*etree.Element) {
doc := etree.NewDocument() doc := etree.NewDocument()
if err := doc.ReadFromString(msg.String()); err != nil { if err := doc.ReadFromString(msg.String()); err != nil {
@ -191,7 +192,7 @@ func (msg *SoapMessage) AddHeaderContents(elements []*etree.Element) {
*msg = SoapMessage(res) *msg = SoapMessage(res)
} }
//AddRootNamespace for Envelope body // AddRootNamespace for Envelope body
func (msg *SoapMessage) AddRootNamespace(key, value string) { func (msg *SoapMessage) AddRootNamespace(key, value string) {
doc := etree.NewDocument() doc := etree.NewDocument()
if err := doc.ReadFromString(msg.String()); err != nil { if err := doc.ReadFromString(msg.String()); err != nil {
@ -204,7 +205,7 @@ func (msg *SoapMessage) AddRootNamespace(key, value string) {
*msg = SoapMessage(res) *msg = SoapMessage(res)
} }
//AddRootNamespaces for Envelope body // AddRootNamespaces for Envelope body
func (msg *SoapMessage) AddRootNamespaces(namespaces map[string]string) { func (msg *SoapMessage) AddRootNamespaces(namespaces map[string]string) {
for key, value := range namespaces { for key, value := range namespaces {
msg.AddRootNamespace(key, value) msg.AddRootNamespace(key, value)
@ -242,7 +243,7 @@ func buildSoapRoot() *etree.Document {
return doc return doc
} }
//AddWSSecurity Header for soapMessage // AddWSSecurity Header for soapMessage
func (msg *SoapMessage) AddWSSecurity(username, password string) { func (msg *SoapMessage) AddWSSecurity(username, password string) {
//doc := etree.NewDocument() //doc := etree.NewDocument()
//if err := doc.ReadFromString(msg.String()); err != nil { //if err := doc.ReadFromString(msg.String()); err != nil {
@ -276,7 +277,7 @@ func (msg *SoapMessage) AddWSSecurity(username, password string) {
//*msg = SoapMessage(res) //*msg = SoapMessage(res)
} }
//AddAction Header handling for soapMessage // AddAction Header handling for soapMessage
func (msg *SoapMessage) AddAction() { func (msg *SoapMessage) AddAction() {
doc := etree.NewDocument() doc := etree.NewDocument()

View File

@ -1,10 +1,10 @@
package gosoap package gosoap
import ( import (
"encoding/xml" xml "git.pyer.club/kingecg/goxml"
) )
//Xlmns XML Scheam // Xlmns XML Scheam
var actionHeaders = map[string]string{ var actionHeaders = map[string]string{
"wsnt:Subscribe": "http://docs.oasis-open.org/wsn/bw-2/NotificationProducer/SubscribeRequest", "wsnt:Subscribe": "http://docs.oasis-open.org/wsn/bw-2/NotificationProducer/SubscribeRequest",
"ResumeSubscription": "http://docs.oasis-open.org/wsn/bw-2/PausableSubscriptionManager/ResumeSubscriptionRequest", "ResumeSubscription": "http://docs.oasis-open.org/wsn/bw-2/PausableSubscriptionManager/ResumeSubscriptionRequest",
@ -17,7 +17,7 @@ var actionHeaders = map[string]string{
Action Type in Header Action Type in Header
*************************/ *************************/
//Action type // Action type
type Action struct { type Action struct {
//XMLName xml.Name `xml:"wsse:Security"` //XMLName xml.Name `xml:"wsse:Security"`
XMLName xml.Name `xml:"wsa:Action"` XMLName xml.Name `xml:"wsa:Action"`
@ -30,13 +30,13 @@ type Action struct {
</wsa:Action> </wsa:Action>
*/ */
//NewAction get a new Action Section // NewAction get a new Action Section
func NewAction(key, value string) Action { func NewAction(key, value string) Action {
/** Generating Nonce sequence **/ /** Generating Nonce sequence **/
auth := Action{ auth := Action{
// Created: time.Now().UTC().Format(time.RFC3339Nano), // Created: time.Now().UTC().Format(time.RFC3339Nano),
} }
return auth return auth

View File

@ -3,21 +3,26 @@ package gosoap
import ( import (
"crypto/sha1" "crypto/sha1"
"encoding/base64" "encoding/base64"
"encoding/xml"
"time" "time"
xml "git.pyer.club/kingecg/goxml"
"github.com/elgs/gostrgen" "github.com/elgs/gostrgen"
) )
/************************* /*
************************
WS-Security types WS-Security types
*************************/
************************
*/
const ( const (
passwordType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest" passwordType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"
encodingType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" encodingType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
) )
//Security type :XMLName xml.Name `xml:"http://purl.org/rss/1.0/modules/content/ encoded"` // Security type :XMLName xml.Name `xml:"http://purl.org/rss/1.0/modules/content/ encoded"`
type Security struct { type Security struct {
//XMLName xml.Name `xml:"wsse:Security"` //XMLName xml.Name `xml:"wsse:Security"`
XMLName xml.Name `xml:"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd Security"` XMLName xml.Name `xml:"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd Security"`
@ -55,7 +60,7 @@ type wsAuth struct {
</Security> </Security>
*/ */
//NewSecurity get a new security // NewSecurity get a new security
func NewSecurity(username, passwd string) Security { func NewSecurity(username, passwd string) Security {
/** Generating Nonce sequence **/ /** Generating Nonce sequence **/
charsToGenerate := 32 charsToGenerate := 32
@ -81,7 +86,7 @@ func NewSecurity(username, passwd string) Security {
return auth return auth
} }
//Digest = B64ENCODE( SHA1( B64DECODE( Nonce ) + Date + Password ) ) // Digest = B64ENCODE( SHA1( B64DECODE( Nonce ) + Date + Password ) )
func generateToken(Username string, Nonce string, Created string, Password string) string { func generateToken(Username string, Nonce string, Created string, Password string) string {
sDec, _ := base64.StdEncoding.DecodeString(Nonce) sDec, _ := base64.StdEncoding.DecodeString(Nonce)

View File

@ -2,12 +2,13 @@ package sdk
import ( import (
"context" "context"
"encoding/xml"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"time" "time"
xml "git.pyer.club/kingecg/goxml"
"github.com/beevik/etree" "github.com/beevik/etree"
"github.com/juju/errors" "github.com/juju/errors"
"github.com/rs/zerolog" "github.com/rs/zerolog"

View File

@ -358,7 +358,7 @@ type Profile struct {
type VideoSourceConfiguration struct { type VideoSourceConfiguration struct {
ConfigurationEntity ConfigurationEntity
ViewMode string `xml:"ViewMode,attr"` ViewMode string `xml:"ViewMode,attr"`
SourceToken ReferenceToken `xml:"onvif:SourceToken"` SourceToken ReferenceToken `xml:"tt:SourceToken"`
Bounds IntRectangle `xml:"onvif:Bounds"` Bounds IntRectangle `xml:"onvif:Bounds"`
Extension *VideoSourceConfigurationExtension `xml:"onvif:Extension"` Extension *VideoSourceConfigurationExtension `xml:"onvif:Extension"`
} }

View File

@ -1,9 +1,10 @@
package search package search
import ( import (
"encoding/xml"
"testing" "testing"
xml "git.pyer.club/kingecg/goxml"
xsd "git.pyer.club/kingecg/goonvif/onvif/xsd" xsd "git.pyer.club/kingecg/goonvif/onvif/xsd"
) )