stream.go
changeset 36 9fe022261dcc
parent 35 569833f08780
child 37 fbda8e925fdf
equal deleted inserted replaced
35:569833f08780 36:9fe022261dcc
    10 
    10 
    11 package xmpp
    11 package xmpp
    12 
    12 
    13 import (
    13 import (
    14 	"big"
    14 	"big"
       
    15 	"bytes"
    15 	"crypto/md5"
    16 	"crypto/md5"
    16 	"crypto/rand"
    17 	"crypto/rand"
    17 	"crypto/tls"
    18 	"crypto/tls"
    18 	"encoding/base64"
    19 	"encoding/base64"
    19 	"fmt"
    20 	"fmt"
    78 			break
    79 			break
    79 		}
    80 		}
    80 	}
    81 	}
    81 }
    82 }
    82 
    83 
    83 func readXml(r io.Reader, ch chan<- interface{}) {
    84 func readXml(r io.Reader, ch chan<- interface{},
       
    85 	extStanza map[string] func(*xml.Name) ExtendedStanza) {
    84 	if debug {
    86 	if debug {
    85 		pr, pw := io.Pipe()
    87 		pr, pw := io.Pipe()
    86 		go tee(r, pw, "S: ")
    88 		go tee(r, pw, "S: ")
    87 		r = pr
    89 		r = pr
    88 	}
    90 	}
   142 		if err != nil {
   144 		if err != nil {
   143 			log.Printf("unmarshal: %v", err)
   145 			log.Printf("unmarshal: %v", err)
   144 			break
   146 			break
   145 		}
   147 		}
   146 
   148 
   147 		// BUG(cjyar) If it's a Stanza, use reflection to
   149 		// If it's a Stanza, we check its "Any" element for a
   148 		// search for any Generic elements and fill in
   150 		// namespace that's registered with one of our
   149 		// their attributes.
   151 		// extensions. If so, we need to re-unmarshal into an
       
   152 		// object of the correct type.
       
   153 		if st, ok := obj.(Stanza) ; ok && st.XChild() != nil {
       
   154 			name := st.XChild().XMLName
       
   155 			ns := name.Space
       
   156 			con := extStanza[ns]
       
   157 			if con != nil {
       
   158 				obj = con(&name)
       
   159 				xmlStr, _ := marshalXML(st)
       
   160 				r := bytes.NewBuffer(xmlStr)
       
   161 				err = xml.Unmarshal(r, &obj)
       
   162 				if err != nil {
       
   163 					log.Printf("ext unmarshal: %v",
       
   164 						err)
       
   165 					break
       
   166 				}
       
   167 			}
       
   168 		}
   150 
   169 
   151 		// Put it on the channel.
   170 		// Put it on the channel.
   152 		ch <- obj
   171 		ch <- obj
   153 	}
   172 	}
   154 }
   173 }