# HG changeset patch # User Chris Jones # Date 1379272166 21600 # Node ID d7679d991b1739ee3bfe9ee629ca7e1ecad5150a # Parent aa9a0ae8f875c8b3058cb09c819d32dfc521ab77 Function renames and improved doc. diff -r aa9a0ae8f875 -r d7679d991b17 xmpp/layer1.go --- a/xmpp/layer1.go Sun Sep 15 12:42:49 2013 -0600 +++ b/xmpp/layer1.go Sun Sep 15 13:09:26 2013 -0600 @@ -9,7 +9,7 @@ "time" ) -func (cl *Client) readTransport(w io.WriteCloser) { +func (cl *Client) recvTransport(w io.WriteCloser) { defer w.Close() p := make([]byte, 1024) for { @@ -35,7 +35,7 @@ } } -func (cl *Client) writeTransport(r io.Reader) { +func (cl *Client) sendTransport(r io.Reader) { defer cl.socket.Close() p := make([]byte, 1024) for { diff -r aa9a0ae8f875 -r d7679d991b17 xmpp/layer2.go --- a/xmpp/layer2.go Sun Sep 15 12:42:49 2013 -0600 +++ b/xmpp/layer2.go Sun Sep 15 13:09:26 2013 -0600 @@ -11,7 +11,9 @@ "strings" ) -func readXml(r io.Reader, ch chan<- interface{}, +// Read bytes from a reader, unmarshal them as XML into structures of +// the appropriate type, and send those structures on a channel. +func recvXml(r io.Reader, ch chan<- interface{}, extStanza map[xml.Name]reflect.Type) { if _, ok := Debug.(*noLog); !ok { pr, pw := io.Pipe() @@ -129,7 +131,9 @@ return nil } -func writeXml(w io.Writer, ch <-chan interface{}) { +// Receive structures on a channel, marshal them to XML, and send the +// bytes on a writer. +func sendXml(w io.Writer, ch <-chan interface{}) { if _, ok := Debug.(*noLog); !ok { pr, pw := io.Pipe() go tee(pr, w, "C: ") diff -r aa9a0ae8f875 -r d7679d991b17 xmpp/layer3.go --- a/xmpp/layer3.go Sun Sep 15 12:42:49 2013 -0600 +++ b/xmpp/layer3.go Sun Sep 15 13:09:26 2013 -0600 @@ -16,8 +16,10 @@ f func(Stanza) bool } -func (cl *Client) readStream(srvIn <-chan interface{}, cliOut chan<- Stanza) { - defer close(cliOut) +// Receive XMLish structures, handle all the stream-related ones, and +// send XMPP stanzas on to the client. +func (cl *Client) recvStream(recvXml <-chan interface{}, sendXmpp chan<- Stanza) { + defer close(sendXmpp) handlers := make(map[string]func(Stanza) bool) Loop: @@ -25,7 +27,7 @@ select { case h := <-cl.handlers: handlers[h.id] = h.f - case x, ok := <-srvIn: + case x, ok := <-recvXml: if !ok { break Loop } @@ -49,7 +51,7 @@ send = f(obj) } if send { - cliOut <- obj + sendXmpp <- obj } default: Warn.Logf("Unhandled non-stanza: %T %#v", x, x) @@ -58,13 +60,15 @@ } } -// This loop is paused until resource binding is complete. Otherwise -// the app might inject something inappropriate into our negotiations -// with the server. The control channel controls this loop's -// activity. -func writeStream(srvOut chan<- interface{}, cliIn <-chan Stanza, +// Receive XMPP stanzas from the client and send them on to the +// remote. Don't allow the client to send us any stanzas until +// negotiation has completed. This loop is paused until resource +// binding is complete. Otherwise the app might inject something +// inappropriate into our negotiations with the server. The control +// channel controls this loop's activity. +func sendStream(sendXml chan<- interface{}, recvXmpp <-chan Stanza, control <-chan sendCmd) { - defer close(srvOut) + defer close(sendXml) var input <-chan Stanza Loop: @@ -75,7 +79,7 @@ case sendDeny: input = nil case sendAllow: - input = cliIn + input = recvXmpp } case x, ok := <-input: if !ok { @@ -85,7 +89,7 @@ Info.Log("Refusing to send nil stanza") continue } - srvOut <- x + sendXml <- x } } } diff -r aa9a0ae8f875 -r d7679d991b17 xmpp/sasl.go --- a/xmpp/sasl.go Sun Sep 15 12:42:49 2013 -0600 +++ b/xmpp/sasl.go Sun Sep 15 13:09:26 2013 -0600 @@ -24,7 +24,8 @@ } if digestMd5 { - auth := &auth{XMLName: xml.Name{Space: NsSASL, Local: "auth"}, Mechanism: "DIGEST-MD5"} + auth := &auth{XMLName: xml.Name{Space: NsSASL, Local: "auth"}, + Mechanism: "DIGEST-MD5"} cl.sendXml <- auth } } @@ -124,7 +125,8 @@ // Encode the map and send it. clStr := packSasl(clMap) b64 := base64.StdEncoding - clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "response"}, Chardata: b64.EncodeToString([]byte(clStr))} + clObj := &auth{XMLName: xml.Name{Space: NsSASL, Local: "response"}, + Chardata: b64.EncodeToString([]byte(clStr))} cl.sendXml <- clObj } @@ -168,19 +170,19 @@ // Computes the response string for digest authentication. func saslDigestResponse(username, realm, passwd, nonce, cnonceStr, authenticate, digestUri, nonceCountStr string) string { - h := func(text string) []byte { + h := func(text string) string { h := md5.New() h.Write([]byte(text)) - return h.Sum(nil) + return string(h.Sum(nil)) } - hex := func(bytes []byte) string { - return fmt.Sprintf("%x", bytes) + hex := func(input string) string { + return fmt.Sprintf("%x", input) } - kd := func(secret, data string) []byte { + kd := func(secret, data string) string { return h(secret + ":" + data) } - a1 := string(h(username+":"+realm+":"+passwd)) + ":" + + a1 := h(username+":"+realm+":"+passwd) + ":" + nonce + ":" + cnonceStr a2 := authenticate + ":" + digestUri response := hex(kd(hex(h(a1)), nonce+":"+ diff -r aa9a0ae8f875 -r d7679d991b17 xmpp/structs_test.go --- a/xmpp/structs_test.go Sun Sep 15 12:42:49 2013 -0600 +++ b/xmpp/structs_test.go Sun Sep 15 13:09:26 2013 -0600 @@ -118,7 +118,7 @@ str := `foo!` r := strings.NewReader(str) ch := make(chan interface{}) - go readXml(r, ch, make(map[xml.Name]reflect.Type)) + go recvXml(r, ch, make(map[xml.Name]reflect.Type)) obs := <-ch exp := &Message{XMLName: xml.Name{Local: "message", Space: "jabber:client"}, Header: Header{To: "a@b.c", Innerxml: "foo!"}, diff -r aa9a0ae8f875 -r d7679d991b17 xmpp/xmpp.go --- a/xmpp/xmpp.go Sun Sep 15 12:42:49 2013 -0600 +++ b/xmpp/xmpp.go Sun Sep 15 13:09:26 2013 -0600 @@ -166,22 +166,22 @@ // Start the transport handler, initially unencrypted. recvReader, recvWriter := io.Pipe() sendReader, sendWriter := io.Pipe() - go cl.readTransport(recvWriter) - go cl.writeTransport(sendReader) + go cl.recvTransport(recvWriter) + go cl.sendTransport(sendReader) // Start the reader and writer that convert to and from XML. - recvXml := make(chan interface{}) - go readXml(recvReader, recvXml, extStanza) - sendXml := make(chan interface{}) - cl.sendXml = sendXml - go writeXml(sendWriter, sendXml) + recvXmlCh := make(chan interface{}) + go recvXml(recvReader, recvXmlCh, extStanza) + sendXmlCh := make(chan interface{}) + cl.sendXml = sendXmlCh + go sendXml(sendWriter, sendXmlCh) // Start the reader and writer that convert between XML and // XMPP stanzas. recvRawXmpp := make(chan Stanza) - go cl.readStream(recvXml, recvRawXmpp) + go cl.recvStream(recvXmlCh, recvRawXmpp) sendRawXmpp := make(chan Stanza) - go writeStream(sendXml, sendRawXmpp, cl.inputControl) + go sendStream(sendXmlCh, sendRawXmpp, cl.inputControl) // Start the manager for the filters that can modify what the // app sees. diff -r aa9a0ae8f875 -r d7679d991b17 xmpp/xmpp_test.go --- a/xmpp/xmpp_test.go Sun Sep 15 12:42:49 2013 -0600 +++ b/xmpp/xmpp_test.go Sun Sep 15 13:09:26 2013 -0600 @@ -13,7 +13,7 @@ r := strings.NewReader(`` + ``) ch := make(chan interface{}) - go readXml(r, ch, make(map[xml.Name]reflect.Type)) + go recvXml(r, ch, make(map[xml.Name]reflect.Type)) x := <-ch se, ok := x.(*streamError) if !ok { @@ -29,7 +29,7 @@ `Error text`) ch = make(chan interface{}) - go readXml(r, ch, make(map[xml.Name]reflect.Type)) + go recvXml(r, ch, make(map[xml.Name]reflect.Type)) x = <-ch se, ok = x.(*streamError) if !ok { @@ -47,7 +47,7 @@ `xmlns="` + NsClient + `" xmlns:stream="` + NsStream + `" version="1.0">`) ch := make(chan interface{}) - go readXml(r, ch, make(map[xml.Name]reflect.Type)) + go recvXml(r, ch, make(map[xml.Name]reflect.Type)) x := <-ch ss, ok := x.(*stream) if !ok { @@ -66,7 +66,7 @@ wg.Add(1) go func() { defer wg.Done() - writeXml(w, ch) + sendXml(w, ch) }() ch <- obj close(ch)