~samwhited/xmpp

ref: 52be89df867be9fc4e8ba83b0c3880887a6485ba xmpp/compress/compression.go -rw-r--r-- 4.5 KiB
52be89dfSam Whited form: Rename Option and FieldOption types 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

// Package compress implements stream compression as specified in XEP-0138.
package compress

import (
	"context"
	"encoding/xml"
	"errors"
	"fmt"
	"io"

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

// Namespaces used by stream compression.
const (
	NSFeatures = "http://jabber.org/features/compress"
	NSProtocol = "http://jabber.org/protocol/compress"
)

var (
	errNoMethods = errors.New("No supported compression methods were found")
)

// New returns a new xmpp.StreamFeature that can be used to negotiate stream
// compression. The returned stream feature always supports ZLIB compression;
// other compression methods are optional.
func New(methods ...Method) xmpp.StreamFeature {
	// TODO: Throw them into a map to dedup and then iterate over that?
	methods = append(methods, zlibMethod)
	return xmpp.StreamFeature{
		Name:      xml.Name{Local: "compression", Space: NSFeatures},
		Necessary: xmpp.Secure | xmpp.Authn,
		List: func(ctx context.Context, e *xml.Encoder, start xml.StartElement) (req bool, err error) {
			if err = e.EncodeToken(start); err != nil {
				return
			}

			methodStart := xml.StartElement{Name: xml.Name{Local: "method"}}

			for _, m := range methods {
				select {
				case <-ctx.Done():
					return req, ctx.Err()
				default:
				}

				if err = e.EncodeToken(methodStart); err != nil {
					return
				}
				if err = e.EncodeToken(xml.CharData(m.Name)); err != nil {
					return
				}
				if err = e.EncodeToken(methodStart.End()); err != nil {
					return
				}
			}

			if err = e.EncodeToken(start.End()); err != nil {
				return
			}
			return false, e.Flush()
		},
		Parse: func(ctx context.Context, d *xml.Decoder, start *xml.StartElement) (bool, interface{}, error) {
			listed := struct {
				XMLName xml.Name `xml:"http://jabber.org/features/compress compression"`
				Methods []string `xml:"http://jabber.org/features/compress method"`
			}{}
			if err := d.DecodeElement(&listed, start); err != nil {
				return false, nil, err
			}

			// TODO: Is this actually an error, or is this a bad implementation that
			//       we can ignore? If someone strips all security mechanisms do we
			//       care that we'll effectively negotiate "none" compression?
			if len(listed.Methods) == 0 {
				return false, nil, errNoMethods
			}

			return true, listed.Methods, nil
		},
		Negotiate: func(ctx context.Context, session *xmpp.Session, data interface{}) (mask xmpp.SessionState, rw io.ReadWriter, err error) {
			conn := session.Conn()

			// If we're a server.
			if (session.State() & xmpp.Received) == xmpp.Received {
				clientSelected := struct {
					XMLName xml.Name `xml:"http://jabber.org/protocol/compress compress"`
					Method  string   `xml:"method"`
				}{}
				if err = session.Decoder().Decode(&clientSelected); err != nil {
					return
				}

				// If no method was selected, or something weird happened with decoding…
				if clientSelected.Method == "" {
					_, err = fmt.Fprint(conn, `<failure xmlns='`+NSProtocol+`'><setup-failed/></failure>`)
					return
				}

				var selected Method
				for _, method := range methods {
					if method.Name == clientSelected.Method {
						selected = method
						break
					}
				}

				// The client requested a method that we did not send…
				if selected.Name == "" {
					_, err = fmt.Fprint(conn, `<failure xmlns='`+NSProtocol+`'><unsupported-method/></failure>`)
					return
				}

				if _, err = fmt.Fprint(conn, `<compressed xmlns='`+NSProtocol+`'/>`); err != nil {
					return
				}

				rw, err = selected.Wrapper(conn)
				return mask, rw, err
			}

			var selected Method
		selectmethod:
			for _, m := range methods {
				for _, name := range data.([]string) {
					if name == m.Name {
						selected = m
						break selectmethod
					}
				}
			}

			if selected.Name == "" {
				return mask, nil, errors.New(`No matching compression method found`)
			}

			_, err = fmt.Fprintf(conn, `<compress xmlns='`+NSProtocol+`'><method>%s</method></compress>`, selected.Name)
			if err != nil {
				return
			}

			d := session.Decoder()
			tok, err := d.Token()
			if err != nil {
				return mask, nil, err
			}

			if t, ok := tok.(xml.StartElement); ok && t.Name.Local == "compressed" && t.Name.Space == NSProtocol {
				if err = d.Skip(); err != nil {
					return mask, nil, err
				}
				rw, err = selected.Wrapper(conn)
				return mask, rw, err
			}

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