2015-01-22 11:10:03 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Implementation of HTTP over XMPP transport
|
|
|
|
http://xmpp.org/extensions/xep-0332.html
|
|
|
|
Copyright (C) 2015 Riptide IO, sangeeth@riptideio.com
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
|
|
|
See the file LICENSE for copying permission.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2015-01-28 09:22:15 +00:00
|
|
|
from sleekxmpp import Iq
|
|
|
|
|
|
|
|
from sleekxmpp.xmlstream import register_stanza_plugin
|
|
|
|
from sleekxmpp.xmlstream.handler import Callback
|
|
|
|
from sleekxmpp.xmlstream.matcher import StanzaPath
|
|
|
|
|
2015-01-22 11:10:03 +00:00
|
|
|
from sleekxmpp.plugins.base import BasePlugin
|
2015-04-29 09:14:25 +00:00
|
|
|
from sleekxmpp.plugins.xep_0332.stanza import (
|
|
|
|
HTTPRequest, HTTPResponse, HTTPData
|
|
|
|
)
|
2015-01-28 09:22:15 +00:00
|
|
|
from sleekxmpp.plugins.xep_0131.stanza import Headers
|
|
|
|
|
2015-01-22 11:10:03 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class XEP_0332(BasePlugin):
|
|
|
|
"""
|
|
|
|
XEP-0332: HTTP over XMPP transport
|
|
|
|
"""
|
|
|
|
|
|
|
|
name = 'xep_0332'
|
|
|
|
description = 'XEP-0332: HTTP over XMPP transport'
|
2015-01-29 03:03:40 +00:00
|
|
|
|
|
|
|
#: xep_0047 not included.
|
|
|
|
#: xep_0001, 0137 and 0166 are missing
|
|
|
|
dependencies = set(['xep_0030', 'xep_0131'])
|
|
|
|
|
|
|
|
#: TODO: Do we really need to mention the supported_headers?!
|
|
|
|
default_config = {
|
|
|
|
'supported_headers': set([
|
|
|
|
'Content-Length', 'Transfer-Encoding', 'DateTime',
|
|
|
|
'Accept-Charset', 'Location', 'Content-ID', 'Description',
|
|
|
|
'Content-Language', 'Content-Transfer-Encoding', 'Timestamp',
|
|
|
|
'Expires', 'User-Agent', 'Host', 'Proxy-Authorization', 'Date',
|
|
|
|
'WWW-Authenticate', 'Accept-Encoding', 'Server', 'Error-Info',
|
|
|
|
'Identifier', 'Content-Location', 'Content-Encoding', 'Distribute',
|
|
|
|
'Accept', 'Proxy-Authenticate', 'ETag', 'Expect', 'Content-Type'
|
|
|
|
])
|
|
|
|
}
|
2015-01-22 11:10:03 +00:00
|
|
|
|
|
|
|
def plugin_init(self):
|
2015-04-29 09:14:25 +00:00
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback(
|
|
|
|
'HTTP Request',
|
|
|
|
StanzaPath('iq/http-req'),
|
|
|
|
self._handle_request
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback(
|
|
|
|
'HTTP Response',
|
|
|
|
StanzaPath('iq/http-resp'),
|
|
|
|
self._handle_response
|
|
|
|
)
|
|
|
|
)
|
|
|
|
register_stanza_plugin(Iq, HTTPRequest, iterable=True)
|
|
|
|
register_stanza_plugin(Iq, HTTPResponse, iterable=True)
|
|
|
|
register_stanza_plugin(HTTPRequest, Headers, iterable=True)
|
|
|
|
register_stanza_plugin(HTTPRequest, HTTPData, iterable=True)
|
|
|
|
register_stanza_plugin(HTTPResponse, Headers, iterable=True)
|
|
|
|
register_stanza_plugin(HTTPResponse, HTTPData, iterable=True)
|
2015-01-29 03:03:40 +00:00
|
|
|
# TODO: Should we register any api's here? self.api.register()
|
2015-01-28 09:22:15 +00:00
|
|
|
|
2015-01-22 11:10:03 +00:00
|
|
|
def plugin_end(self):
|
2015-01-28 09:22:15 +00:00
|
|
|
self.xmpp.remove_handler('HTTP Request')
|
|
|
|
self.xmpp.remove_handler('HTTP Response')
|
2015-01-29 03:03:40 +00:00
|
|
|
self.xmpp['xep_0030'].del_feature('urn:xmpp:http')
|
|
|
|
for header in self.supported_headers:
|
|
|
|
self.xmpp['xep_0030'].del_feature(
|
|
|
|
feature='%s#%s' % (Headers.namespace, header)
|
|
|
|
)
|
2015-01-22 11:10:03 +00:00
|
|
|
|
|
|
|
def session_bind(self, jid):
|
2015-01-29 03:03:40 +00:00
|
|
|
self.xmpp['xep_0030'].add_feature('urn:xmpp:http')
|
|
|
|
for header in self.supported_headers:
|
|
|
|
self.xmpp['xep_0030'].add_feature(
|
|
|
|
'%s#%s' % (Headers.namespace, header)
|
|
|
|
)
|
|
|
|
# TODO: Do we need to add the supported headers to xep_0131?
|
|
|
|
# self.xmpp['xep_0131'].supported_headers.add(header)
|
|
|
|
|
|
|
|
def _handle_request(self, iq):
|
2015-02-03 07:03:25 +00:00
|
|
|
self.xmpp.event('http_request', iq)
|
2015-01-28 09:22:15 +00:00
|
|
|
|
2015-01-29 03:03:40 +00:00
|
|
|
def _handle_response(self, iq):
|
2015-02-03 07:03:25 +00:00
|
|
|
self.xmpp.event('http_response', iq)
|
2015-01-28 09:22:15 +00:00
|
|
|
|
2015-01-29 03:03:40 +00:00
|
|
|
def send_request(self, to=None, method=None, resource=None, headers=None,
|
|
|
|
data=None, **kwargs):
|
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
iq['from'] = self.xmpp.boundjid
|
|
|
|
iq['to'] = to
|
|
|
|
iq['type'] = 'set'
|
2015-04-29 09:14:25 +00:00
|
|
|
iq['http-req']['headers'] = headers
|
|
|
|
iq['http-req']['method'] = method
|
|
|
|
iq['http-req']['resource'] = resource
|
|
|
|
iq['http-req']['version'] = '1.1' # TODO: set this implicitly
|
2015-02-05 12:05:04 +00:00
|
|
|
if data is not None:
|
2015-05-01 09:02:36 +00:00
|
|
|
iq['http-req']['data'] = data
|
2015-02-05 12:05:04 +00:00
|
|
|
return iq.send(
|
|
|
|
timeout=kwargs.get('timeout', None),
|
|
|
|
block=kwargs.get('block', True),
|
|
|
|
callback=kwargs.get('callback', None),
|
|
|
|
timeout_callback=kwargs.get('timeout_callback', None)
|
|
|
|
)
|
|
|
|
|
|
|
|
def send_response(self, to=None, code=None, message=None, headers=None,
|
|
|
|
data=None, **kwargs):
|
2015-01-29 03:03:40 +00:00
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
iq['from'] = self.xmpp.boundjid
|
|
|
|
iq['to'] = to
|
|
|
|
iq['type'] = 'result'
|
2015-04-29 09:14:25 +00:00
|
|
|
iq['http-resp']['headers'] = headers
|
|
|
|
iq['http-resp']['code'] = code
|
|
|
|
iq['http-resp']['message'] = message
|
|
|
|
iq['http-resp']['version'] = '1.1' # TODO: set this implicitly
|
2015-02-05 12:05:04 +00:00
|
|
|
if data is not None:
|
2015-05-01 09:02:36 +00:00
|
|
|
iq['http-resp']['data'] = data
|
2015-02-05 12:05:04 +00:00
|
|
|
return iq.send(
|
|
|
|
timeout=kwargs.get('timeout', None),
|
|
|
|
block=kwargs.get('block', True),
|
|
|
|
callback=kwargs.get('callback', None),
|
|
|
|
timeout_callback=kwargs.get('timeout_callback', None)
|
|
|
|
)
|
|
|
|
|
2015-05-01 10:04:53 +00:00
|
|
|
def send_error(self, to=None, ecode='500', etype='wait',
|
2015-02-05 12:05:04 +00:00
|
|
|
econd='internal-server-error', **kwargs):
|
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
iq['from'] = self.xmpp.boundjid
|
|
|
|
iq['to'] = to
|
2015-05-01 10:04:53 +00:00
|
|
|
iq['type'] = 'error'
|
2015-02-05 12:05:04 +00:00
|
|
|
iq['error']['code'] = ecode
|
|
|
|
iq['error']['type'] = etype
|
|
|
|
iq['error']['condition'] = econd
|
|
|
|
return iq.send(
|
|
|
|
timeout=kwargs.get('timeout', None),
|
|
|
|
block=kwargs.get('block', True),
|
|
|
|
callback=kwargs.get('callback', None),
|
|
|
|
timeout_callback=kwargs.get('timeout_callback', None)
|
|
|
|
)
|