From a96f608469e74d39d3e7a2a86399dbb724ffadec Mon Sep 17 00:00:00 2001 From: Sangeeth Saravanaraj Date: Thu, 29 Jan 2015 08:33:40 +0530 Subject: Composing request and response. --- sleekxmpp/plugins/xep_0332/__init__.py | 3 +- sleekxmpp/plugins/xep_0332/http.py | 94 +++++++++++++++++++++------ sleekxmpp/plugins/xep_0332/stanza/__init__.py | 4 +- sleekxmpp/plugins/xep_0332/stanza/data.py | 29 +++++++++ sleekxmpp/plugins/xep_0332/stanza/request.py | 26 +++++++- sleekxmpp/plugins/xep_0332/stanza/response.py | 17 ++++- 6 files changed, 146 insertions(+), 27 deletions(-) create mode 100644 sleekxmpp/plugins/xep_0332/stanza/data.py (limited to 'sleekxmpp') diff --git a/sleekxmpp/plugins/xep_0332/__init__.py b/sleekxmpp/plugins/xep_0332/__init__.py index cdbfa5d5..27755faa 100644 --- a/sleekxmpp/plugins/xep_0332/__init__.py +++ b/sleekxmpp/plugins/xep_0332/__init__.py @@ -10,7 +10,8 @@ from sleekxmpp.plugins.base import register_plugin +from sleekxmpp.plugins.xep_0332 import stanza from sleekxmpp.plugins.xep_0332.http import XEP_0332 -# from sleekxmpp.plugins.xep_0332 import stanza + register_plugin(XEP_0332) diff --git a/sleekxmpp/plugins/xep_0332/http.py b/sleekxmpp/plugins/xep_0332/http.py index f91cc1ff..166e6ec3 100644 --- a/sleekxmpp/plugins/xep_0332/http.py +++ b/sleekxmpp/plugins/xep_0332/http.py @@ -17,11 +17,7 @@ from sleekxmpp.xmlstream.handler import Callback from sleekxmpp.xmlstream.matcher import StanzaPath from sleekxmpp.plugins.base import BasePlugin - -from sleekxmpp.plugins.xep_0332.stanza import NAMESPACE -from sleekxmpp.plugins.xep_0332.stanza.request import Request -from sleekxmpp.plugins.xep_0332.stanza.response import Response - +from sleekxmpp.plugins.xep_0332.stanza import Request, Response, Data from sleekxmpp.plugins.xep_0131.stanza import Headers @@ -35,8 +31,23 @@ class XEP_0332(BasePlugin): name = 'xep_0332' description = 'XEP-0332: HTTP over XMPP transport' - dependencies = set(['xep_0030', 'xep_0047', 'xep_0131']) #: xep 1, 137 and 166 are missing - default_config = {} + + #: 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' + ]) + } def plugin_init(self): log.debug("XEP_0332:: plugin_init()") @@ -48,30 +59,75 @@ class XEP_0332(BasePlugin): 'HTTP Response', StanzaPath('iq/resp'), self._handle_response )) - register_stanza_plugin(Iq, Request) - register_stanza_plugin(Iq, Response) - register_stanza_plugin(Request, Headers) - register_stanza_plugin(Response, Headers) + register_stanza_plugin(Iq, Request, iterable=True) + register_stanza_plugin(Iq, Response, iterable=True) + register_stanza_plugin(Request, Headers, iterable=True) + register_stanza_plugin(Request, Data, iterable=True) + register_stanza_plugin(Response, Headers, iterable=True) + register_stanza_plugin(Response, Data, iterable=True) + + # TODO: Should we register any api's here? self.api.register() def plugin_end(self): log.debug("XEP_0332:: plugin_end()") self.xmpp.remove_handler('HTTP Request') self.xmpp.remove_handler('HTTP Response') - self.xmpp['xep_0030'].del_feature(NAMESPACE) + 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) + ) def session_bind(self, jid): log.debug("XEP_0332:: session_bind()") - self.xmpp['xep_0030'].add_feature(NAMESPACE) - - def _handle_request(self): + 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): log.debug("XEP_0332:: _handle_request()") + print iq - def _handle_response(self): + def _handle_response(self, iq): log.debug("XEP_0332:: _handle_response()") + print iq - def send_request(self, method=None, resource=None, headers=None, data=None, **kwargs): + def send_request(self, to=None, method=None, resource=None, headers=None, + data=None, **kwargs): log.debug("XEP_0332:: send_request()") - - def send_response(self, status_code=None, headers=None, data=None, **kwargs): + iq = self.xmpp.Iq() + iq['from'] = self.xmpp.boundjid + iq['to'] = to + iq['type'] = 'set' + iq['req']['headers'] = headers + iq['req']['method'] = method + iq['req']['resource'] = resource + iq['req']['version'] = '1.1' # TODO: set this implicitly + iq['req']['data'] = data + print iq + # 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, headers=None, data=None, + **kwargs): log.debug("XEP_0332:: send_response()") + iq = self.xmpp.Iq() + iq['from'] = self.xmpp.boundjid + iq['to'] = to + iq['type'] = 'result' + iq['resp']['headers'] = headers + iq['resp']['code'] = code + iq['resp']['version'] = '1.1' # TODO: set this implicitly + iq['resp']['data'] = data + print iq + # 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)) diff --git a/sleekxmpp/plugins/xep_0332/stanza/__init__.py b/sleekxmpp/plugins/xep_0332/stanza/__init__.py index 5d9283c7..eeab3f31 100644 --- a/sleekxmpp/plugins/xep_0332/stanza/__init__.py +++ b/sleekxmpp/plugins/xep_0332/stanza/__init__.py @@ -8,4 +8,6 @@ See the file LICENSE for copying permission. """ -NAMESPACE = 'urn:xmpp:http' +from sleekxmpp.plugins.xep_0332.stanza.request import Request +from sleekxmpp.plugins.xep_0332.stanza.response import Response +from sleekxmpp.plugins.xep_0332.stanza.data import Data diff --git a/sleekxmpp/plugins/xep_0332/stanza/data.py b/sleekxmpp/plugins/xep_0332/stanza/data.py new file mode 100644 index 00000000..e77d2585 --- /dev/null +++ b/sleekxmpp/plugins/xep_0332/stanza/data.py @@ -0,0 +1,29 @@ +""" + 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. +""" + +from sleekxmpp.xmlstream import ElementBase + + +class Data(ElementBase): + """ + The data element. + """ + name = 'data' + namespace = '' + interfaces = set(['data']) + plugin_attrib = 'data' + + def get_data(self): + print "Data:: get_data()" + + def set_data(self, data, encoding='text'): + print "Data:: set_data()" + self._set_sub_text(encoding, text=data) + diff --git a/sleekxmpp/plugins/xep_0332/stanza/request.py b/sleekxmpp/plugins/xep_0332/stanza/request.py index ea2650c0..51e565e5 100644 --- a/sleekxmpp/plugins/xep_0332/stanza/request.py +++ b/sleekxmpp/plugins/xep_0332/stanza/request.py @@ -9,7 +9,6 @@ """ from sleekxmpp.xmlstream import ElementBase -from sleekxmpp.plugins.xep_0332.stanza import NAMESPACE class Request(ElementBase): @@ -43,7 +42,28 @@ class Request(ElementBase): """ name = 'request' - namespace = NAMESPACE - interfaces = set(('method', 'resource', 'version')) + namespace = 'urn:xmpp:http' + interfaces = set(['method', 'resource', 'version']) plugin_attrib = 'req' + def get_method(self): + print "Request:: get_method()" + + def set_method(self, method): + print "Request:: set_method()" + self._set_attr('method', method) + + def get_resource(self): + print "Request:: get_resource()" + + def set_resource(self, resource): + print "Request:: set_resource()" + self._set_attr('resource', resource) + + def get_version(self): + print "Request:: get_version()" + + def set_version(self, version='1.1'): + print "Request:: set_version()" + self._set_attr('version', version) + diff --git a/sleekxmpp/plugins/xep_0332/stanza/response.py b/sleekxmpp/plugins/xep_0332/stanza/response.py index 4dc14344..d863efa3 100644 --- a/sleekxmpp/plugins/xep_0332/stanza/response.py +++ b/sleekxmpp/plugins/xep_0332/stanza/response.py @@ -9,7 +9,6 @@ """ from sleekxmpp.xmlstream import ElementBase -from sleekxmpp.plugins.xep_0332.stanza import NAMESPACE class Response(ElementBase): @@ -41,6 +40,18 @@ class Response(ElementBase): """ name = 'response' - namespace = NAMESPACE - interfaces = set(('statusCode', 'statusMessage', 'version')) + namespace = 'urn:xmpp:http' + interfaces = set(['code', 'version']) plugin_attrib = 'resp' + + def get_code(self): + print "Response:: get_code()" + + def set_code(self, code): + print "Response:: set_code()" + self._set_attr('statusCode', str(code)) + self._set_attr('statusMessage', str(code)) + + def set_version(self, version='1.1'): + print "Response:: set_version()" + self._set_attr('version', version) -- cgit v1.2.3