[27] | 1 | # -*- test-case-name: wokkel.test.test_shim -*- |
---|
| 2 | # |
---|
[96] | 3 | # Copyright (c) Ralph Meijer. |
---|
[27] | 4 | # See LICENSE for details. |
---|
| 5 | |
---|
| 6 | """ |
---|
| 7 | XMPP Stanza Headers and Internet Metadata. |
---|
| 8 | |
---|
| 9 | This protocol is specified in |
---|
[165] | 10 | U{XEP-0131<http://xmpp.org/extensions/xep-0131.html>}. |
---|
[27] | 11 | """ |
---|
| 12 | |
---|
[201] | 13 | from __future__ import division, absolute_import |
---|
| 14 | |
---|
| 15 | from twisted.python.compat import unicode |
---|
[27] | 16 | from twisted.words.xish import domish |
---|
| 17 | |
---|
| 18 | NS_SHIM = "http://jabber.org/protocol/shim" |
---|
| 19 | |
---|
| 20 | class Headers(domish.Element): |
---|
| 21 | def __init__(self, headers): |
---|
| 22 | domish.Element.__init__(self, (NS_SHIM, 'headers')) |
---|
| 23 | for name, value in headers: |
---|
| 24 | self.addElement('header', content=value)['name'] = name |
---|
| 25 | |
---|
| 26 | def extractHeaders(stanza): |
---|
| 27 | """ |
---|
| 28 | Extract SHIM headers from stanza. |
---|
| 29 | |
---|
| 30 | @param stanza: The stanza to extract headers from. |
---|
| 31 | @type stanza: L{Element<twisted.words.xish.domish.Element>} |
---|
| 32 | @return: Headers as a mapping from header name to a list of values. |
---|
| 33 | @rtype: C{dict} |
---|
| 34 | """ |
---|
| 35 | headers = {} |
---|
| 36 | |
---|
| 37 | for element in domish.generateElementsQNamed(stanza.children, |
---|
| 38 | 'headers', NS_SHIM): |
---|
| 39 | for header in domish.generateElementsQNamed(element.children, |
---|
| 40 | 'header', NS_SHIM): |
---|
| 41 | headers.setdefault(header['name'], []).append(unicode(header)) |
---|
| 42 | |
---|
| 43 | return headers |
---|