~samwhited/xmpp

d554e50e5294bfd9091d0167943d93745b0609aa — Sam Whited 1 year, 9 months ago 08a995a
all: move stream namespaces into stream package
7 files changed, 34 insertions(+), 27 deletions(-)

M component/component.go
M features.go
M internal/ns/ns.go
M internal/stream.go
M session.go
A stream/doc.go
M stream/error.go
M component/component.go => component/component.go +1 -2
@@ 18,7 18,6 @@ import (
	"io"

	"mellium.im/xmpp"
	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/stream"
)


@@ 89,7 88,7 @@ func Negotiator(addr jid.JID, secret []byte, recv bool) xmpp.Negotiator {
			}
		}

		if start.Name.Local != "stream" || start.Name.Space != ns.Stream {
		if start.Name.Local != "stream" || start.Name.Space != stream.NS {
			return mask, nil, nil, errors.New("Expected stream:stream from server")
		}


M features.go => features.go +2 -2
@@ 298,7 298,7 @@ func readStreamFeatures(ctx context.Context, s *Session, start xml.StartElement,
	switch {
	case start.Name.Local != featuresLocal:
		return nil, stream.InvalidXML
	case start.Name.Space != ns.Stream:
	case start.Name.Space != stream.NS:
		return nil, stream.BadNamespacePrefix
	}



@@ 351,7 351,7 @@ parsefeatures:
				return nil, err
			}
		case xml.EndElement:
			if tok.Name.Local == featuresLocal && tok.Name.Space == ns.Stream {
			if tok.Name.Local == featuresLocal && tok.Name.Space == stream.NS {
				// We've reached the end of the features list!
				return sf, nil
			}

M internal/ns/ns.go => internal/ns/ns.go +8 -10
@@ 8,14 8,12 @@ package ns // import "mellium.im/xmpp/internal/ns"

// List of commonly used namespaces.
const (
	Bind       = "urn:ietf:params:xml:ns:xmpp-bind"
	Client     = "jabber:client"
	SASL       = "urn:ietf:params:xml:ns:xmpp-sasl"
	Server     = "jabber:server"
	Stanza     = "urn:ietf:params:xml:ns:xmpp-stanzas"
	StartTLS   = "urn:ietf:params:xml:ns:xmpp-tls"
	Stream     = "http://etherx.jabber.org/streams"
	XMPPStream = "urn:ietf:params:xml:ns:xmpp-streams"
	WS         = "urn:ietf:params:xml:ns:xmpp-framing"
	XML        = "http://www.w3.org/XML/1998/namespace"
	Bind     = "urn:ietf:params:xml:ns:xmpp-bind"
	Client   = "jabber:client"
	SASL     = "urn:ietf:params:xml:ns:xmpp-sasl"
	Server   = "jabber:server"
	Stanza   = "urn:ietf:params:xml:ns:xmpp-stanzas"
	StartTLS = "urn:ietf:params:xml:ns:xmpp-tls"
	WS       = "urn:ietf:params:xml:ns:xmpp-framing"
	XML      = "http://www.w3.org/XML/1998/namespace"
)

M internal/stream.go => internal/stream.go +3 -3
@@ 62,7 62,7 @@ func streamFromStartElement(s xml.StartElement) (StreamInfo, error) {
			}
			streamData.xmlns = attr.Value
		case xml.Name{Space: "xmlns", Local: "stream"}:
			if attr.Value != ns.Stream {
			if attr.Value != stream.NS {
				return streamData, stream.InvalidNamespace
			}
		case xml.Name{Space: "xml", Local: "lang"}:


@@ 152,7 152,7 @@ func ExpectNewStream(ctx context.Context, d xml.TokenReader, recv bool) (streamD
		switch tok := t.(type) {
		case xml.StartElement:
			switch {
			case tok.Name.Local == "error" && tok.Name.Space == ns.Stream:
			case tok.Name.Local == "error" && tok.Name.Space == stream.NS:
				se := stream.Error{}
				if err := xml.NewTokenDecoder(d).DecodeElement(&se, &tok); err != nil {
					return streamData, err


@@ 160,7 160,7 @@ func ExpectNewStream(ctx context.Context, d xml.TokenReader, recv bool) (streamD
				return streamData, se
			case tok.Name.Local != "stream":
				return streamData, stream.BadFormat
			case tok.Name.Space != ns.Stream:
			case tok.Name.Space != stream.NS:
				return streamData, stream.InvalidNamespace
			}


M session.go => session.go +2 -2
@@ 350,7 350,7 @@ func handleInputStream(s *Session, handler Handler) (err error) {
	case xml.StartElement:
		start = t
	case xml.EndElement:
		if t.Name.Space == ns.Stream && t.Name.Local == "stream" {
		if t.Name.Space == stream.NS && t.Name.Local == "stream" {
			return nil
		}
		// If this is a stream level end element but not </stream:stream>,


@@ 371,7 371,7 @@ func handleInputStream(s *Session, handler Handler) (err error) {

	// Handle stream errors and unknown stream namespaced tokens first, before
	// delegating to the normal handler.
	if start.Name.Space == ns.Stream {
	if start.Name.Space == stream.NS {
		switch start.Name.Local {
		case "error":
			// TODO: Unmarshal the error and return it.

A stream/doc.go => stream/doc.go +15 -0
@@ 0,0 1,15 @@
// Copyright 2019 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

// Package stream contains XMPP stream errors as defined by RFC 6120 §4.9.
//
// Most people will want to use the facilities of the mellium.im/xmpp package
// and not create stream errors directly.
package stream // import "mellium.im/xmpp/stream"

// Namespaces used by XMPP streams and stream errors, provided as a convenience.
const (
	NS      = "http://etherx.jabber.org/streams"
	ErrorNS = "urn:ietf:params:xml:ns:xmpp-streams"
)

M stream/error.go => stream/error.go +3 -8
@@ 2,11 2,7 @@
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

// Package stream contains XMPP stream errors as defined by RFC 6120 §4.9.
//
// Most people will want to use the facilities of the mellium.im/xmpp package
// and not create stream errors directly.
package stream // import "mellium.im/xmpp/stream"
package stream

import (
	"encoding/xml"


@@ 14,7 10,6 @@ import (
	"net"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/internal/ns"
)

// A list of stream errors defined in RFC 6120 §4.9.3


@@ 231,7 226,7 @@ func (s Error) WriteXML(w xmlstream.TokenWriter) (n int, err error) {
// TokenReader returns a new xml.TokenReader that returns an encoding of
// the error.
func (s Error) TokenReader(payload xml.TokenReader) xml.TokenReader {
	inner := xmlstream.Wrap(s.innerXML, xml.StartElement{Name: xml.Name{Local: s.Err, Space: ns.XMPPStream}})
	inner := xmlstream.Wrap(s.innerXML, xml.StartElement{Name: xml.Name{Local: s.Err, Space: ErrorNS}})
	if payload != nil {
		inner = xmlstream.MultiReader(
			inner,


@@ 241,7 236,7 @@ func (s Error) TokenReader(payload xml.TokenReader) xml.TokenReader {
	return xmlstream.Wrap(
		inner,
		xml.StartElement{
			Name: xml.Name{Local: "error", Space: ns.Stream},
			Name: xml.Name{Local: "error", Space: NS},
		},
	)
}