Esempio n. 1
0
    def return_httpb(self, data, session, request):
        # create body
        b = domish.Element((NS_BIND, "body"))
        punjab.uriCheck(b, NS_BIND)
        session.touch()
        if getattr(session, 'terminated', False):
            b['type'] = 'terminate'
        if data:
            b.children += data

        self.return_body(request, b, session.charset)
Esempio n. 2
0
    def return_httpb(self, data, session, request):
        # create body
        b = domish.Element((NS_BIND, "body"))
        punjab.uriCheck(b, NS_BIND)
        session.touch()
        if getattr(session,'terminated', False):
            b['type']      = 'terminate'
        if data:
            b.children += data

        self.return_body(request, b, session.charset)
Esempio n. 3
0
    def send_http_error(self,
                        code,
                        request,
                        condition='undefined-condition',
                        typ='terminate',
                        data='',
                        charset='utf-8',
                        children=None):
        request.setResponseCode(int(code))
        xml_prefixes = ns.XMPP_PREFIXES.copy()

        b = domish.Element((NS_BIND, "body"))
        if condition:
            b['condition'] = str(condition)
        else:
            b['condition'] = 'undefined-condition'

        if typ:
            b['type'] = str(typ)
        else:
            b['type'] = 'terminate'
        punjab.uriCheck(b, NS_BIND)
        bxml = b.toXml(prefixes=xml_prefixes).encode(charset, 'replace')

        if children:
            b.children += children

        if self.service.v:
            log.msg('HTTPB Error %d' % (int(code), ))

        if int(code) != 400 and int(code) != 404 and int(code) != 403:
            if data != '':
                if condition == 'see-other-uri':
                    b.addElement('uri', None, content=str(data))
                else:
                    t = b.addElement('text', content=str(data))
                    t['xmlns'] = 'urn:ietf:params:xml:ns:xmpp-streams'

            bxml = b.toXml(prefixes=xml_prefixes).encode(charset, 'replace')
            if self.service.v:
                log.msg('HTTPB Return Error: ' + str(code) + ' -> ' + bxml)
            request.setHeader("content-type", "text/xml")
            request.setHeader("content-length", len(bxml))
            request.write(bxml)
        else:
            request.setHeader("content-length", "0")
        request.finish()
Esempio n. 4
0
    def return_session(self, data, session, request):
        # create body
        if session.xmlstream is None:
            self.send_http_error(200, request, 'remote-connection-failed',
                                 'terminate')
            return server.NOT_DONE_YET

        b = domish.Element((NS_BIND, "body"),
                           localPrefixes={
                               'xmpp': NS_XMPP,
                               'stream': NS_FEATURES
                           })
        # if we don't have an authid, we have to fail
        if session.authid != 0:
            b['authid'] = session.authid
        else:
            self.send_http_error(500, request, 'internal-server-error',
                                 'terminate')
            return server.NOT_DONE_YET

        b['sid'] = session.sid
        b['wait'] = str(session.wait)
        if session.secure == 0:
            b['secure'] = 'false'
        else:
            b['secure'] = 'true'

        b['inactivity'] = str(session.inactivity)
        b['polling'] = str(self.polling)
        b['requests'] = str(session.hold + 1)
        b['window'] = str(session.window)
        b[(NS_XMPP, 'version')] = '1.0'

        punjab.uriCheck(b, NS_BIND)
        if session.attrs.has_key('content'):
            b['content'] = session.attrs['content']

        # We need to send features
        while len(data) > 0:
            felem = data.pop(0)
            if isinstance(felem, domish.Element):
                b.addChild(felem)
            else:
                b.addRawXml(felem)

        self.return_body(request, b)
Esempio n. 5
0
    def return_session(self, data, session, request):
        # create body
        if session.xmlstream is None:
            self.send_http_error(200, request, 'remote-connection-failed',
                                 'terminate')
            return server.NOT_DONE_YET

        b = domish.Element((NS_BIND, "body"), localPrefixes = {'xmpp' : NS_XMPP, 'stream' : NS_FEATURES })
        # if we don't have an authid, we have to fail
        if session.authid != 0:
            b['authid'] = session.authid
        else:
            self.send_http_error(500, request, 'internal-server-error',
                                 'terminate')
            return server.NOT_DONE_YET

        b['sid']  = session.sid
        b['wait'] = str(session.wait)
        if session.secure == 0:
            b['secure'] = 'false'
        else:
            b['secure'] = 'true'

        b['inactivity'] = str(session.inactivity)
        b['polling'] = str(self.polling)
        b['requests'] = str(session.hold + 1)
        b['window'] = str(session.window)
        b[(NS_XMPP, 'version')] = '1.0'

        punjab.uriCheck(b, NS_BIND)
        if session.attrs.has_key('content'):
            b['content'] = session.attrs['content']

        # We need to send features
        while len(data) > 0:
            felem = data.pop(0)
            if isinstance(felem, domish.Element):
                b.addChild(felem)
            else:
                b.addRawXml(felem)

        self.return_body(request, b)
Esempio n. 6
0
    def send_http_error(
        self, code, request, condition="undefined-condition", typ="terminate", data="", charset="utf-8", children=None
    ):
        request.setResponseCode(int(code))
        xml_prefixes = ns.XMPP_PREFIXES.copy()

        b = domish.Element((NS_BIND, "body"))
        if condition:
            b["condition"] = str(condition)
        else:
            b["condition"] = "undefined-condition"

        if typ:
            b["type"] = str(typ)
        else:
            b["type"] = "terminate"
        punjab.uriCheck(b, NS_BIND)
        bxml = b.toXml(prefixes=xml_prefixes).encode(charset, "replace")

        if children:
            b.children += children

        if self.service.v:
            log.msg("HTTPB Error %d" % (int(code),))

        if int(code) != 400 and int(code) != 404 and int(code) != 403:
            if data != "":
                if condition == "see-other-uri":
                    b.addElement("uri", None, content=str(data))
                else:
                    t = b.addElement("text", content=str(data))
                    t["xmlns"] = "urn:ietf:params:xml:ns:xmpp-streams"

            bxml = b.toXml(prefixes=xml_prefixes).encode(charset, "replace")
            if self.service.v:
                log.msg("HTTPB Return Error: " + str(code) + " -> " + bxml)
            request.setHeader("content-type", "text/xml")
            request.setHeader("content-length", len(bxml))
            request.write(bxml)
        else:
            request.setHeader("content-length", "0")
        request.finish()
Esempio n. 7
0
    def send_http_error(self, code, request, condition = 'undefined-condition', typ = 'terminate', data = '', charset = 'utf-8', children=None):
        request.setResponseCode(int(code))
        xml_prefixes = ns.XMPP_PREFIXES.copy()

        b = domish.Element((NS_BIND, "body"))
        if condition:
            b['condition'] = str(condition)
        else:
            b['condition'] = 'undefined-condition'

        if typ:
            b['type']      = str(typ)
        else:
            b['type']      = 'terminate'
        punjab.uriCheck(b, NS_BIND)
        bxml = b.toXml(prefixes=xml_prefixes).encode(charset, 'replace')

        if children:
            b.children += children

        if self.service.v:
            log.msg('HTTPB Error %d' %(int(code),))

        if int(code) != 400 and int(code) != 404 and int(code) != 403:
            if data != '':
                if condition == 'see-other-uri':
                    b.addElement('uri', None, content = str(data))
                else:
                    t = b.addElement('text', content = str(data))
                    t['xmlns'] = 'urn:ietf:params:xml:ns:xmpp-streams'

            bxml = b.toXml(prefixes=xml_prefixes).encode(charset, 'replace')
            if self.service.v:
                log.msg('HTTPB Return Error: ' + str(code) + ' -> ' + bxml)
            request.setHeader("content-type", "text/xml")
            request.setHeader("content-length", len(bxml))
            request.write(bxml)
        else:
            request.setHeader("content-length", "0")
        request.finish()
Esempio n. 8
0
    def return_session(self, data, session, request):
        # create body
        if session.xmlstream is None:
            self.send_http_error(200, request, "remote-connection-failed", "terminate")
            return server.NOT_DONE_YET

        b = domish.Element((NS_BIND, "body"))
        # if we don't have an authid, we have to fail
        if session.authid != 0:
            b["authid"] = session.authid
        else:
            self.send_http_error(500, request, "internal-server-error", "terminate")
            return server.NOT_DONE_YET

        b["sid"] = session.sid
        b["wait"] = str(session.wait)
        if session.secure == 0:
            b["secure"] = "false"
        else:
            b["secure"] = "true"

        b["inactivity"] = str(session.inactivity)
        ##b['polling'] = '15' # TODO: make this configurable
        b["polling"] = str(self.polling)
        b["requests"] = str(session.hold + 1)
        b["window"] = str(session.window)

        punjab.uriCheck(b, NS_BIND)
        if session.attrs.has_key("content"):
            b["content"] = session.attrs["content"]

        # We need to send features
        while len(data) > 0:
            felem = data.pop(0)
            if isinstance(felem, domish.Element):
                b.addChild(felem)
            else:
                b.addRawXml(felem)

        self.return_body(request, b)