Skip to content

Commit

Permalink
xml: attribute set refactorized.
Browse files Browse the repository at this point in the history
  • Loading branch information
ortuman committed Mar 27, 2018
1 parent 004138e commit 7f34851
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 136 deletions.
10 changes: 5 additions & 5 deletions module/roster.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ func (r *ModRoster) rosterItemJID(ri *model.RosterItem) *xml.JID {

func (r *ModRoster) rosterItemFromElement(item xml.Element) (*model.RosterItem, error) {
ri := &model.RosterItem{}
if jid := item.Attribute("jid"); len(jid) > 0 {
if jid := item.Attributes().Get("jid"); len(jid) > 0 {
j, err := xml.NewJIDString(jid, false)
if err != nil {
return nil, err
Expand All @@ -820,9 +820,9 @@ func (r *ModRoster) rosterItemFromElement(item xml.Element) (*model.RosterItem,
} else {
return nil, errors.New("item 'jid' attribute is required")
}
ri.Name = item.Attribute("name")
ri.Name = item.Attributes().Get("name")

subscription := item.Attribute("subscription")
subscription := item.Attributes().Get("subscription")
if len(subscription) > 0 {
switch subscription {
case subscriptionBoth, subscriptionFrom, subscriptionTo, subscriptionNone, subscriptionRemove:
Expand All @@ -832,7 +832,7 @@ func (r *ModRoster) rosterItemFromElement(item xml.Element) (*model.RosterItem,
}
ri.Subscription = subscription
}
ask := item.Attribute("ask")
ask := item.Attributes().Get("ask")
if len(ask) > 0 {
if ask != "subscribe" {
return nil, fmt.Errorf("unrecognized 'ask' enum type: %s", subscription)
Expand All @@ -841,7 +841,7 @@ func (r *ModRoster) rosterItemFromElement(item xml.Element) (*model.RosterItem,
}
groups := item.FindElements("group")
for _, group := range groups {
if group.AttributesCount() > 0 {
if group.Attributes().Len() > 0 {
return nil, errors.New("group element must not contain any attribute")
}
ri.Groups = append(ri.Groups, group.Text())
Expand Down
12 changes: 6 additions & 6 deletions module/roster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func TestRoster_Subscribed(t *testing.T) {
qRes := elem.FindElementNamespace("query", rosterNamespace)
require.NotNil(t, qRes)
iRes := qRes.FindElement("item")
require.Equal(t, subscriptionFrom, iRes.Attribute("subscription"))
require.Equal(t, subscriptionFrom, iRes.Attributes().Get("subscription"))

rns, err := storage.Instance().FetchRosterNotifications("noelia")
require.Nil(t, err)
Expand Down Expand Up @@ -378,7 +378,7 @@ func TestRoster_Unsubscribe(t *testing.T) {
qRes := elem.FindElementNamespace("query", rosterNamespace)
require.NotNil(t, qRes)
iRes := qRes.FindElement("item")
require.Equal(t, subscriptionTo, iRes.Attribute("subscription"))
require.Equal(t, subscriptionTo, iRes.Attributes().Get("subscription"))

elem = stm2.FetchElement()
require.Equal(t, "presence", elem.Name())
Expand Down Expand Up @@ -413,7 +413,7 @@ func TestRoster_Unsubscribed(t *testing.T) {
qRes := elem.FindElementNamespace("query", rosterNamespace)
require.NotNil(t, qRes)
iRes := qRes.FindElement("item")
require.Equal(t, subscriptionFrom, iRes.Attribute("subscription"))
require.Equal(t, subscriptionFrom, iRes.Attributes().Get("subscription"))

elem = stm1.FetchElement()
require.Equal(t, "presence", elem.Name())
Expand Down Expand Up @@ -465,7 +465,7 @@ func TestRoster_DeleteItem(t *testing.T) {
qRes := elem.FindElementNamespace("query", rosterNamespace)
require.NotNil(t, qRes)
iRes := qRes.FindElement("item")
require.Equal(t, subscriptionRemove, iRes.Attribute("subscription"))
require.Equal(t, subscriptionRemove, iRes.Attributes().Get("subscription"))

elem = stm1.FetchElement()
require.Equal(t, "presence", elem.Name())
Expand All @@ -483,15 +483,15 @@ func TestRoster_DeleteItem(t *testing.T) {
qRes = elem.FindElementNamespace("query", rosterNamespace)
require.NotNil(t, qRes)
iRes = qRes.FindElement("item")
require.Equal(t, subscriptionTo, iRes.Attribute("subscription"))
require.Equal(t, subscriptionTo, iRes.Attributes().Get("subscription"))

elem = stm2.FetchElement()
require.Equal(t, "iq", elem.Name())
require.Equal(t, xml.SetType, elem.Type())
qRes = elem.FindElementNamespace("query", rosterNamespace)
require.NotNil(t, qRes)
iRes = qRes.FindElement("item")
require.Equal(t, subscriptionNone, iRes.Attribute("subscription"))
require.Equal(t, subscriptionNone, iRes.Attributes().Get("subscription"))

elem = stm2.FetchElement()
require.Equal(t, "presence", elem.Name())
Expand Down
2 changes: 1 addition & 1 deletion server/auth_digest_md5.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (d *digestMD5Authenticator) handleStart(elem xml.Element) error {
}

func (d *digestMD5Authenticator) handleChallenged(elem xml.Element) error {
if elem.TextLen() == 0 {
if len(elem.Text()) == 0 {
return errSASLMalformedRequest
}
b, err := base64.StdEncoding.DecodeString(elem.Text())
Expand Down
2 changes: 1 addition & 1 deletion server/auth_plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (p *plainAuthenticator) ProcessElement(elem xml.Element) error {
if p.authenticated {
return nil
}
if elem.TextLen() == 0 {
if len(elem.Text()) == 0 {
return errSASLMalformedRequest
}
b, err := base64.StdEncoding.DecodeString(elem.Text())
Expand Down
2 changes: 1 addition & 1 deletion server/auth_scram.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (s *scramAuthenticator) handleChallenged(elem xml.Element) error {
}

func (s *scramAuthenticator) getElementPayload(elem xml.Element) (string, error) {
if elem.TextLen() == 0 {
if len(elem.Text()) == 0 {
return "", errSASLIncorrectEncoding
}
b, err := base64.StdEncoding.DecodeString(elem.Text())
Expand Down
6 changes: 3 additions & 3 deletions server/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func (s *serverStream) compress(elem xml.Element) {
return
}
method := elem.FindElement("method")
if method == nil || method.TextLen() == 0 {
if method == nil || len(method.Text()) == 0 {
failure := xml.NewElementNamespace("failure", compressProtocolNamespace)
failure.AppendElement(xml.NewElementName("setup-failed"))
s.writeElement(failure)
Expand All @@ -553,7 +553,7 @@ func (s *serverStream) compress(elem xml.Element) {
}

func (s *serverStream) startAuthentication(elem xml.Element) {
mechanism := elem.Attribute("mechanism")
mechanism := elem.Attributes().Get("mechanism")
for _, authr := range s.authrs {
if authr.Mechanism() == mechanism {
if err := s.continueAuthentication(elem, authr); err != nil {
Expand Down Expand Up @@ -971,7 +971,7 @@ func (s *serverStream) validateStreamElement(elem xml.Element) *streamerror.Erro
if elem.Name() != "stream:stream" {
return streamerror.ErrUnsupportedStanzaType
}
if elem.Namespace() != jabberClientNamespace || elem.Attribute("xmlns:stream") != streamNamespace {
if elem.Namespace() != jabberClientNamespace || elem.Attributes().Get("xmlns:stream") != streamNamespace {
return streamerror.ErrInvalidNamespace
}

Expand Down
13 changes: 6 additions & 7 deletions util/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ func LoadCertificate(keyFile, certFile, domain string) (*tls.Config, error) {
}, nil
}

func selfSignedCertificateExists() bool {
keySt, _ := os.Stat(selfSignedCertPrivateKey)
certSt, _ := os.Stat(selfSignedCertPrivateKey)
return keySt != nil && certSt != nil
}

func generateSelfSignedCertificate(keyFile, certFile, domain string) error {
if err := os.MkdirAll(selfSignedCertFolder, os.ModePerm); err != nil {
return err
Expand All @@ -88,7 +82,6 @@ func generateSelfSignedCertificate(keyFile, certFile, domain string) error {
BasicConstraintsValid: true,
DNSNames: []string{domain},
}

// obtain private key
priv, err := rsa.GenerateKey(rand.Reader, selfSignedCertKeyRSABits)
if err != nil {
Expand All @@ -115,3 +108,9 @@ func generateSelfSignedCertificate(keyFile, certFile, domain string) error {
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
return nil
}

func selfSignedCertificateExists() bool {
keySt, _ := os.Stat(selfSignedCertPrivateKey)
certSt, _ := os.Stat(selfSignedCertPrivateKey)
return keySt != nil && certSt != nil
}
90 changes: 90 additions & 0 deletions xml/attribute_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/

package xml

import (
"encoding/gob"
"io"
)

// Attribute represents an XML node attribute (label=value).
type Attribute struct {
Label string
Value string
}

type AttributeSet struct {
attrs []Attribute
}

func (as *AttributeSet) Get(label string) string {
for _, attr := range as.attrs {
if attr.Label == label {
return attr.Value
}
}
return ""
}

func (as *AttributeSet) Len() int {
return len(as.attrs)
}

func (as *AttributeSet) setAttribute(label, value string) {
for i := 0; i < len(as.attrs); i++ {
if as.attrs[i].Label == label {
as.attrs[i].Value = value
return
}
}
as.attrs = append(as.attrs, Attribute{label, value})
}

func (as *AttributeSet) removeAttribute(label string) {
for i := 0; i < len(as.attrs); i++ {
if as.attrs[i].Label == label {
as.attrs = append(as.attrs[:i], as.attrs[i+1:]...)
return
}
}
}

func (as *AttributeSet) copyFrom(from *AttributeSet) {
as.attrs = make([]Attribute, from.Len())
copy(as.attrs, from.attrs)
}

func (as *AttributeSet) toXML(w io.Writer) {
for i := 0; i < len(as.attrs); i++ {
if len(as.attrs[i].Value) == 0 {
continue
}
w.Write([]byte(" "))
w.Write([]byte(as.attrs[i].Label))
w.Write([]byte(`="`))
w.Write([]byte(as.attrs[i].Value))
w.Write([]byte(`"`))
}
}

func (as *AttributeSet) fromGob(dec *gob.Decoder) {
var c int
dec.Decode(&c)
for i := 0; i < c; i++ {
var attr Attribute
dec.Decode(&attr.Label)
dec.Decode(&attr.Value)
as.attrs = append(as.attrs, attr)
}
}

func (as *AttributeSet) toGob(enc *gob.Encoder) {
enc.Encode(len(as.attrs))
for _, attr := range as.attrs {
enc.Encode(&attr.Label)
enc.Encode(&attr.Value)
}
}
6 changes: 6 additions & 0 deletions xml/attribute_set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/

package xml
1 change: 0 additions & 1 deletion xml/bytes.go

This file was deleted.

1 change: 0 additions & 1 deletion xml/bytes_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion xml/delay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ func TestDelay(t *testing.T) {
e.Delay("example.org", "any text")
delay := e.FindElement("delay")
require.NotNil(t, delay)
require.Equal(t, "example.org", delay.Attribute("from"))
require.Equal(t, "example.org", delay.Attributes().Get("from"))
require.Equal(t, "any text", delay.Text())
}
Loading

0 comments on commit 7f34851

Please sign in to comment.