# HG changeset patch # User Goffi # Date 1620221853 -7200 # Node ID e84ffb48acd4f94dd824e47dfe1c1ed04b1c1bc4 # Parent 7f5bf108961ace14c0345e055959e42015596da9 plugin XEP-0363: allow async callbacks in handlers + method to generate `file-too-large` element diff -r 7f5bf108961a -r e84ffb48acd4 sat/plugins/plugin_xep_0363.py --- a/sat/plugins/plugin_xep_0363.py Wed May 05 15:37:33 2021 +0200 +++ b/sat/plugins/plugin_xep_0363.py Wed May 05 15:37:33 2021 +0200 @@ -24,6 +24,7 @@ from wokkel import disco, iwokkel from zope.interface import implementer from twisted.words.protocols.jabber import jid, xmlstream, error +from twisted.words.xish import domish from twisted.internet import reactor from twisted.internet import defer from twisted.web import client as http_client @@ -33,7 +34,7 @@ from sat.core.constants import Const as C from sat.core.log import getLogger from sat.core import exceptions -from sat.tools import web as sat_web +from sat.tools import web as sat_web, utils log = getLogger(__name__) @@ -122,6 +123,12 @@ self.handlers.append(req_handler) self.handlers.sort(key=lambda handler: handler.priority, reverse=True) + def getFileTooLargeElt(self, max_size: int) -> domish.Element: + """Generate app condition for errors""" + file_too_large_elt = domish.Element((NS_HTTP_UPLOAD, "file-too-large")) + file_too_large_elt.addElement("max-file-size", str(max_size)) + return file_too_large_elt + async def getHTTPUploadEntity(self, client, upload_jid=None): """Get HTTP upload capable entity @@ -356,6 +363,9 @@ def onComponentRequest(self, iq_elt, client): iq_elt.handled=True + defer.ensureDeferred(self.handleComponentRequest(client, iq_elt)) + + async def handleComponentRequest(self, client, iq_elt): try: request_elt = next(iq_elt.elements(NS_HTTP_UPLOAD, "request")) request = UploadRequest( @@ -372,7 +382,7 @@ for handler in self.handlers: try: - slot = handler.callback(client, request) + slot = await utils.asDeferred(handler.callback, client, request) except error.StanzaError as e: log.warning( "a stanza error has been raised while processing HTTP Upload of " @@ -389,11 +399,9 @@ log.warning( _("no service can handle HTTP Upload request: {elt}") .format(elt=iq_elt.toXml())) - if err is not None: - condition = err.condition - else: - condition = "feature-not-implemented" - client.sendError(iq_elt, condition) + if err is None: + err = error.StanzaError("feature-not-implemented") + client.send(err.toResponse(iq_elt)) return iq_result_elt = xmlstream.toResponse(iq_elt, "result")