Beispiel #1
0
    def render(self, request):
        arg = ""
        request.content.seek(0, 0)
        jsonp = request.args.get('jsonp', [''])[0]
        if jsonp:
            data = request.args.get('packet', [''])[0]
        elif 'packet' in request.args:
            data = request.args['packet'][0]
        else:
            data = request.content.read()
        if "PacketPing" not in data:
            _host_type, host = request.findProxiedIP()
            self._log.debug("(%s) render %s", host, data)

        try:
            arg = Packet.JSON.decode(data)
        except Exception:
            resp = 'invalid request'
            request.setResponseCode(http.BAD_REQUEST)
            request.setHeader('content-type', "text/html")
            request.setHeader('content-length', str(len(resp)))
            return resp

        packet, packet_type_numeric = dict2packet(arg)

        deferred = defer.succeed(None)

        if request.site.pipes:
            request.site.pipe(deferred, request, packet)

        def pipesFailed(reason):
            #
            # Report only if the request has not been finished already.
            # It is the responsibility of each filter to handle errors
            # either by passing them on the errback chain or by filling
            # the request with a proper report.
            #
            body = reason.getTraceback()
            if not request.finished:
                request.setResponseCode(http.INTERNAL_SERVER_ERROR)
                request.setHeader('content-type', "text/html")
                request.setHeader('content-length', str(len(body)))
                request.write(body)
            if request.code != 200:
                _host_type, host = request.findProxiedIP()
                self._log.warn("(%s) %s", host, body)
            if not (request.finished or request._disconnected):
                request.finish()

            #
            # Return a value that is not a Failure so that the next
            # incoming request is accepted (otherwise the server keeps
            # returning error on every request)
            #
            return True

        deferred.addCallback(lambda result: self.deferRender(
            request, jsonp, packet, data, packet_type_numeric))
        deferred.addErrback(pipesFailed)
        return server.NOT_DONE_YET
Beispiel #2
0
    def render(self, request):
        arg = ""
        request.content.seek(0, 0)
        jsonp = request.args.get('jsonp', [''])[0]
        if jsonp:
            data = request.args.get('packet', [''])[0]
        elif 'packet' in request.args:
            data = request.args['packet'][0];
        else:
            data = request.content.read()
        if "PacketPing" not in data:
            _host_type, host = request.findProxiedIP()
            self._log.debug("(%s) render %s", host, data)

        try:
            arg = Packet.JSON.decode(data)
        except Exception:
            resp = 'invalid request'
            request.setResponseCode(http.BAD_REQUEST)
            request.setHeader('content-type',"text/html")
            request.setHeader('content-length', str(len(resp)))
            return resp
        
        packet, packet_type_numeric = dict2packet(arg)

        deferred = defer.succeed(None)

        if request.site.pipes:
            request.site.pipe(deferred, request, packet)

        def pipesFailed(reason):
            #
            # Report only if the request has not been finished already.
            # It is the responsibility of each filter to handle errors
            # either by passing them on the errback chain or by filling
            # the request with a proper report.
            #
            body = reason.getTraceback()
            if not request.finished:
                request.setResponseCode(http.INTERNAL_SERVER_ERROR)
                request.setHeader('content-type',"text/html")
                request.setHeader('content-length', str(len(body)))
                request.write(body)
            if request.code != 200:
                _host_type, host = request.findProxiedIP()
                self._log.warn("(%s) %s", host, body)
            if not (request.finished or request._disconnected):
                request.finish()
                    
            #
            # Return a value that is not a Failure so that the next
            # incoming request is accepted (otherwise the server keeps
            # returning error on every request)
            #
            return True

        deferred.addCallback(lambda result: self.deferRender(request, jsonp, packet, data, packet_type_numeric))
        deferred.addErrback(pipesFailed)
        return server.NOT_DONE_YET
    def receivePacket(self, data):
        self.log.debug("receivePacket %s", data)

        if self.pendingLongPoll:
            self.scheduleLongPoll(0)
        self.pendingLongPoll = False

        packets = [dict2packet(dict_packet)[0] for dict_packet in Packet.JSON.decode(data)]
        return packets
Beispiel #4
0
    def receivePacket(self, data):
        self.log.debug("receivePacket %s", data)

        if self.pendingLongPoll:
            self.scheduleLongPoll(0)
        self.pendingLongPoll = False

        packets = [
            dict2packet(dict_packet)[0]
            for dict_packet in Packet.JSON.decode(data)
        ]
        return packets