~samwhited/xmpp

a8e3ffb3b023272e5b3010a96da9db9e0103a2b3 — Sam Whited 3 years ago 34ff471
stream: rename streamerror package to stream
11 files changed, 56 insertions(+), 56 deletions(-)

M bind.go
M compress/compression.go
M features.go
M ibr2/ibr2.go
M sasl.go
M sasl2/sasl.go
M session.go
M starttls.go
M stream.go
R streamerror/streamerror.go => stream/error.go
R streamerror/streamerror_test.go => stream/error_test.go
M bind.go => bind.go +4 -4
@@ 13,7 13,7 @@ import (
	"mellium.im/xmpp/internal"
	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/streamerror"
	"mellium.im/xmpp/stream"
)

const (


@@ 70,7 70,7 @@ func BindResource() StreamFeature {
			}
			start, ok := tok.(xml.StartElement)
			if !ok {
				return mask, nil, streamerror.BadFormat
				return mask, nil, stream.BadFormat
			}
			resp := struct {
				IQ


@@ 85,12 85,12 @@ func BindResource() StreamFeature {
					return mask, nil, err
				}
			default:
				return mask, nil, streamerror.BadFormat
				return mask, nil, stream.BadFormat
			}

			switch {
			case resp.ID != reqID:
				return mask, nil, streamerror.UndefinedCondition
				return mask, nil, stream.UndefinedCondition
			case resp.Type == ResultIQ:
				session.origin = resp.Bind.JID
			case resp.Type == ErrorIQ:

M compress/compression.go => compress/compression.go +2 -2
@@ 14,7 14,7 @@ import (
	"io"

	"mellium.im/xmpp"
	"mellium.im/xmpp/streamerror"
	"mellium.im/xmpp/stream"
)

// Namespaces used by stream compression.


@@ 161,7 161,7 @@ func New(methods ...Method) xmpp.StreamFeature {
			}

			// TODO: Use appropriate errors.
			return mask, nil, streamerror.BadFormat
			return mask, nil, stream.BadFormat
		},
	}
}

M features.go => features.go +8 -8
@@ 10,7 10,7 @@ import (
	"io"

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

// A StreamFeature represents a feature that may be selected during stream


@@ 88,7 88,7 @@ func (s *Session) negotiateFeatures(ctx context.Context) (done bool, rw io.ReadW
		}
		start, ok = t.(xml.StartElement)
		if !ok {
			return done, nil, streamerror.BadFormat
			return done, nil, stream.BadFormat
		}

		// If we're the client read the rest of the stream features list.


@@ 120,7 120,7 @@ func (s *Session) negotiateFeatures(ctx context.Context) (done bool, rw io.ReadW
			}
			start, ok = t.(xml.StartElement)
			if !ok {
				return done, nil, streamerror.BadFormat
				return done, nil, stream.BadFormat
			}

			// If the feature was not sent or was already negotiated, error.


@@ 129,7 129,7 @@ func (s *Session) negotiateFeatures(ctx context.Context) (done bool, rw io.ReadW
			data, sent = list.cache[start.Name.Space]
			if !sent || negotiated {
				// TODO: What should we return here?
				return done, rw, streamerror.PolicyViolation
				return done, rw, stream.PolicyViolation
			}
		} else {
			// If we're the client, iterate through the cached features and select one


@@ 236,9 236,9 @@ func writeStreamFeatures(ctx context.Context, s *Session) (list *streamFeaturesL
func readStreamFeatures(ctx context.Context, s *Session, start xml.StartElement) (*streamFeaturesList, error) {
	switch {
	case start.Name.Local != "features":
		return nil, streamerror.InvalidXML
		return nil, stream.InvalidXML
	case start.Name.Space != ns.Stream:
		return nil, streamerror.BadNamespacePrefix
		return nil, stream.BadNamespacePrefix
	}

	// Lock the connection features list.


@@ 298,9 298,9 @@ parsefeatures:
			}
			// Oops, how did that happen? We shouldn't have been able to hit an end
			// element that wasn't the </stream:features> token.
			return nil, streamerror.InvalidXML
			return nil, stream.InvalidXML
		default:
			return nil, streamerror.RestrictedXML
			return nil, stream.RestrictedXML
		}
	}
}

M ibr2/ibr2.go => ibr2/ibr2.go +6 -6
@@ 16,7 16,7 @@ import (
	"io"

	"mellium.im/xmpp"
	"mellium.im/xmpp/streamerror"
	"mellium.im/xmpp/stream"
)

// Namespaces used by IBR.


@@ 114,7 114,7 @@ func decodeClientResp(ctx context.Context, d *xml.Decoder, decode func(ctx conte
	start, ok := tok.(xml.StartElement)
	switch {
	case !ok:
		err = streamerror.RestrictedXML
		err = stream.RestrictedXML
		return
	case start.Name.Local == "cancel" && start.Name.Space == NS:
		cancel = true


@@ 126,7 126,7 @@ func decodeClientResp(ctx context.Context, d *xml.Decoder, decode func(ctx conte
		}
	}

	err = streamerror.BadFormat
	err = stream.BadFormat
	return
}



@@ 184,10 184,10 @@ func negotiateFunc(challenges ...Challenge) func(context.Context, *xmpp.Session,
		start, ok := tok.(xml.StartElement)
		switch {
		case !ok:
			err = streamerror.RestrictedXML
			err = stream.RestrictedXML
			return
		case start.Name.Local != "challenge" || start.Name.Space != NS:
			err = streamerror.BadFormat
			err = stream.BadFormat
			return
		}
		var typ string


@@ 199,7 199,7 @@ func negotiateFunc(challenges ...Challenge) func(context.Context, *xmpp.Session,
		}
		// If there was no type attr, an illegal challenge was sent.
		if typ == "" {
			err = streamerror.BadFormat
			err = stream.BadFormat
			return
		}


M sasl.go => sasl.go +5 -5
@@ 15,7 15,7 @@ import (
	"mellium.im/sasl"
	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/internal/saslerr"
	"mellium.im/xmpp/streamerror"
	"mellium.im/xmpp/stream"
)

// BUG(ssw): SASL feature does not have security layer byte precision.


@@ 139,7 139,7 @@ func SASL(mechanisms ...sasl.Mechanism) StreamFeature {
						return mask, nil, err
					}
				} else {
					return mask, nil, streamerror.BadFormat
					return mask, nil, stream.BadFormat
				}
			}



@@ 161,7 161,7 @@ func SASL(mechanisms ...sasl.Mechanism) StreamFeature {
						return mask, nil, err
					}
				} else {
					return mask, nil, streamerror.BadFormat
					return mask, nil, stream.BadFormat
				}
				if more, resp, err = client.Step(challenge); err != nil {
					return mask, nil, err


@@ 185,7 185,7 @@ func decodeSASLChallenge(d *xml.Decoder, start xml.StartElement, allowChallenge 
	switch start.Name {
	case xml.Name{Space: ns.SASL, Local: "challenge"}, xml.Name{Space: ns.SASL, Local: "success"}:
		if !allowChallenge && start.Name.Local == "challenge" {
			return nil, false, streamerror.UnsupportedStanzaType
			return nil, false, stream.UnsupportedStanzaType
		}
		challenge := struct {
			Data []byte `xml:",chardata"`


@@ 201,6 201,6 @@ func decodeSASLChallenge(d *xml.Decoder, start xml.StartElement, allowChallenge 
		}
		return nil, false, fail
	default:
		return nil, false, streamerror.UnsupportedStanzaType
		return nil, false, stream.UnsupportedStanzaType
	}
}

M sasl2/sasl.go => sasl2/sasl.go +5 -5
@@ 21,7 21,7 @@ import (
	"mellium.im/sasl"
	"mellium.im/xmpp"
	"mellium.im/xmpp/internal/saslerr"
	"mellium.im/xmpp/streamerror"
	"mellium.im/xmpp/stream"
)

// BUG(ssw): feature may provide a security layer, but is not byte precise.


@@ 159,7 159,7 @@ func SASL(mechanisms ...sasl.Mechanism) xmpp.StreamFeature {
						return mask, nil, err
					}
				} else {
					return mask, nil, streamerror.BadFormat
					return mask, nil, stream.BadFormat
				}
			}



@@ 181,7 181,7 @@ func SASL(mechanisms ...sasl.Mechanism) xmpp.StreamFeature {
						return mask, nil, err
					}
				} else {
					return mask, nil, streamerror.BadFormat
					return mask, nil, stream.BadFormat
				}
				if more, resp, err = client.Step(challenge); err != nil {
					return mask, nil, err


@@ 205,7 205,7 @@ func decodeSASLChallenge(d *xml.Decoder, start xml.StartElement, allowChallenge 
	switch start.Name {
	case xml.Name{Space: NS, Local: "challenge"}:
		if !allowChallenge {
			return nil, false, streamerror.UnsupportedStanzaType
			return nil, false, stream.UnsupportedStanzaType
		}
		challenge := struct {
			Data []byte `xml:",chardata"`


@@ 230,6 230,6 @@ func decodeSASLChallenge(d *xml.Decoder, start xml.StartElement, allowChallenge 
		}
		return nil, false, fail
	default:
		return nil, false, streamerror.UnsupportedStanzaType
		return nil, false, stream.UnsupportedStanzaType
	}
}

M session.go => session.go +5 -5
@@ 13,7 13,7 @@ import (
	"sync"

	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/streamerror"
	"mellium.im/xmpp/stream"
)

// SessionState is a bitmask that represents the current state of an XMPP


@@ 224,7 224,7 @@ func (s *Session) handleInputStream(handler Handler) {
			default:
				// TODO: We need a way to figure out if this was an XML error or an
				// error with the underlying connection.
				s.Encoder().Encode(streamerror.BadFormat)
				s.Encoder().Encode(stream.BadFormat)
				return
			}
		}


@@ 232,11 232,11 @@ func (s *Session) handleInputStream(handler Handler) {
		case xml.StartElement:
			if err = handler.HandleXMPP(s, &t); err != nil {
				switch err.(type) {
				case StanzaError, streamerror.StreamError:
				case StanzaError, stream.StreamError:
					s.Encoder().Encode(err)
				default:
					// TODO: Should this error have a payload?
					s.Encoder().Encode(streamerror.UndefinedCondition)
					s.Encoder().Encode(stream.UndefinedCondition)
				}
			}
		default:


@@ 246,7 246,7 @@ func (s *Session) handleInputStream(handler Handler) {
			default:
				// TODO: We need a way to figure out if this was an XML error or an
				// error with the underlying connection.
				s.Encoder().Encode(streamerror.BadFormat)
				s.Encoder().Encode(stream.BadFormat)
			}
		}
	}

M starttls.go => starttls.go +6 -6
@@ 14,7 14,7 @@ import (
	"net"

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

// BUG(ssw): STARTTLS feature does not have security layer byte precision.


@@ 92,17 92,17 @@ func StartTLS(required bool) StreamFeature {
				case xml.StartElement:
					switch {
					case tok.Name.Space != ns.StartTLS:
						return mask, nil, streamerror.UnsupportedStanzaType
						return mask, nil, stream.UnsupportedStanzaType
					case tok.Name.Local == "proceed":
						// Skip the </proceed> token.
						if err = d.Skip(); err != nil {
							return mask, nil, streamerror.InvalidXML
							return mask, nil, stream.InvalidXML
						}
						rw = tls.Client(conn, tlsconf)
					case tok.Name.Local == "failure":
						// Skip the </failure> token.
						if err = d.Skip(); err != nil {
							err = streamerror.InvalidXML
							err = stream.InvalidXML
						}
						// Failure is not an "error", it's expected behavior. Immediately
						// afterwards the server will end the stream. However, if we


@@ 110,10 110,10 @@ func StartTLS(required bool) StreamFeature {
						// that error.
						return mask, nil, err
					default:
						return mask, nil, streamerror.UnsupportedStanzaType
						return mask, nil, stream.UnsupportedStanzaType
					}
				default:
					return mask, nil, streamerror.RestrictedXML
					return mask, nil, stream.RestrictedXML
				}
			}
			mask = Secure

M stream.go => stream.go +13 -13
@@ 14,7 14,7 @@ import (
	"mellium.im/xmpp/internal"
	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/streamerror"
	"mellium.im/xmpp/stream"
)

const (


@@ 39,12 39,12 @@ func streamFromStartElement(s xml.StartElement) (streamInfo, error) {
		case xml.Name{Space: "", Local: "to"}:
			streamData.to = &jid.JID{}
			if err := streamData.to.UnmarshalXMLAttr(attr); err != nil {
				return streamData, streamerror.ImproperAddressing
				return streamData, stream.ImproperAddressing
			}
		case xml.Name{Space: "", Local: "from"}:
			streamData.from = &jid.JID{}
			if err := streamData.from.UnmarshalXMLAttr(attr); err != nil {
				return streamData, streamerror.ImproperAddressing
				return streamData, stream.ImproperAddressing
			}
		case xml.Name{Space: "", Local: "id"}:
			streamData.id = attr.Value


@@ 52,12 52,12 @@ func streamFromStartElement(s xml.StartElement) (streamInfo, error) {
			(&streamData.version).UnmarshalXMLAttr(attr)
		case xml.Name{Space: "", Local: "xmlns"}:
			if attr.Value != "jabber:client" && attr.Value != "jabber:server" {
				return streamData, streamerror.InvalidNamespace
				return streamData, stream.InvalidNamespace
			}
			streamData.xmlns = attr.Value
		case xml.Name{Space: "xmlns", Local: "stream"}:
			if attr.Value != ns.Stream {
				return streamData, streamerror.InvalidNamespace
				return streamData, stream.InvalidNamespace
			}
		case xml.Name{Space: "xml", Local: "lang"}:
			streamData.lang = language.Make(attr.Value)


@@ 128,15 128,15 @@ func expectNewStream(ctx context.Context, s *Session) error {
		case xml.StartElement:
			switch {
			case tok.Name.Local == "error" && tok.Name.Space == ns.Stream:
				se := streamerror.StreamError{}
				se := stream.StreamError{}
				if err := d.DecodeElement(&se, &tok); err != nil {
					return err
				}
				return se
			case tok.Name.Local != "stream":
				return streamerror.BadFormat
				return stream.BadFormat
			case tok.Name.Space != ns.Stream:
				return streamerror.InvalidNamespace
				return stream.InvalidNamespace
			}

			streamData, err := streamFromStartElement(tok)


@@ 144,12 144,12 @@ func expectNewStream(ctx context.Context, s *Session) error {
			case err != nil:
				return err
			case streamData.version != internal.DefaultVersion:
				return streamerror.UnsupportedVersion
				return stream.UnsupportedVersion
			}

			if (s.state&Received) != Received && streamData.id == "" {
				// if we are the initiating entity and there is no stream ID…
				return streamerror.BadFormat
				return stream.BadFormat
			}
			s.in.streamInfo = streamData
			return nil


@@ 159,11 159,11 @@ func expectNewStream(ctx context.Context, s *Session) error {
				foundHeader = true
				continue
			}
			return streamerror.RestrictedXML
			return stream.RestrictedXML
		case xml.EndElement:
			return streamerror.NotWellFormed
			return stream.NotWellFormed
		default:
			return streamerror.RestrictedXML
			return stream.RestrictedXML
		}
	}
}

R streamerror/streamerror.go => stream/error.go +1 -1
@@ 8,7 8,7 @@
// conflicts with similarly named stanza error conditions and because they are
// less frequently used. Most people will want to use the facilities of the
// mellium.im/xmpp package and not create stream errors directly.
package streamerror // import "mellium.im/xmpp/streamerror"
package stream // import "mellium.im/xmpp/stream"

import (
	"encoding/xml"

R streamerror/streamerror_test.go => stream/error_test.go +1 -1
@@ 2,7 2,7 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package streamerror
package stream

import (
	"encoding/xml"