def _finishInit(self, resp): def handleJSON(jsonStr): json = simplejson.loads(jsonStr)[0] if not json["authSuccessful"]: log.err("authentication failure!") self.handshakeDeferred.errback(json) return if json["version"] < self.minimumVersion: log.err( "cometd protocol version mismatch. We wanted", self.minimumVersion, "but got", str(json["version"]) ) self.handshakeDeferred.errback(json) return if not "mime-message-block" in json["supportedConnectionTypes"]: log.err("desired transport type (mime-message-block) not supported by server") self.handshakeDeferred.errback(json) return self.clientId = json["clientId"] self.initialized = True self.handshakeDeferred.callback(json) # print json stream.readStream(resp.stream, handleJSON)
def gotResp(resp, num): def print_(n): print "DATA %s: %r" % (num, n) def printdone(n): print "DONE %s" % num print "GOT RESPONSE %s: %s" % (num, resp) stream_mod.readStream(resp.stream, print_).addCallback(printdone)
def http_POST(self, request): def trapNotFound(failure): err = failure.trap(*self.errorMap.keys()) code, msg = self.errorMap[err] return http.StatusResponse(code, msg) def respond(result): return http.Response(responsecode.NO_CONTENT) def gotRequest(result): uri = self.params['uri'] callback = self.params['callback'] jid, nodeIdentifier = getServiceAndNode(uri) method = getattr(self.service, self.serviceMethod) d = method(jid, nodeIdentifier, callback) return d def storeParams(data): self.params = simplejson.loads(data) def trapXMPPURIParseError(failure): failure.trap(XMPPURIParseError) return http.StatusResponse(responsecode.BAD_REQUEST, "Malformed XMPP URI: %s" % failure.value) d = readStream(request.stream, storeParams) d.addCallback(gotRequest) d.addCallback(respond) d.addErrback(trapNotFound) d.addErrback(trapXMPPURIParseError) return d
def http_POST(self, request): def gotRequest(result): url = result['url'] handle = result['handle'] d = self.service.setFeed(handle, url) d.addCallback(lambda _: self.service.handler.checkNode(handle)) return d def createResponse(result): response = {'uri': 'xmpp:%s?;node=%s' % ( self.service.handler.service.full(), result)} return http.Response(responsecode.OK, stream=simplejson.dumps(response)) def trapInvalidHandle(failure): failure.trap(InvalidHandleError) return http.StatusResponse(responsecode.BAD_REQUEST, """Invalid handle""") data = [] d = readStream(request.stream, data.append) d.addCallback(lambda _: ''.join(data)) d.addCallback(simplejson.loads) d.addCallback(gotRequest) d.addCallback(createResponse) d.addErrback(trapInvalidHandle) return d
def http_POST(self, request): def gotRequest(result): url = result['url'] handle = result['handle'] d = self.service.setFeed(handle, url) d.addCallback(lambda _: self.service.handler.checkNode(handle)) return d def createResponse(result): response = { 'uri': 'xmpp:%s?;node=%s' % (self.service.handler.service.full(), result) } return http.Response(responsecode.OK, stream=simplejson.dumps(response)) def trapInvalidHandle(failure): failure.trap(InvalidHandleError) return http.StatusResponse(responsecode.BAD_REQUEST, """Invalid handle""") data = [] d = readStream(request.stream, data.append) d.addCallback(lambda _: ''.join(data)) d.addCallback(simplejson.loads) d.addCallback(gotRequest) d.addCallback(createResponse) d.addErrback(trapInvalidHandle) return d
def renderHTTP(self, request): """ Authenticate the request then return the result of calling renderHTTP on C{self.wrappedResource} """ if request.method not in self.allowedMethods(): response = http.Response(responsecode.NOT_ALLOWED) response.headers.setHeader("allow", self.allowedMethods()) return response def _renderResource(resource): return resource.renderHTTP(request) def _finished_reading(ignore, result): data = ''.join(result) request.attachment = data d = self.authenticate(request) d.addCallback(_renderResource) return d if request.method in ('PUT', 'DELETE'): # we need to authenticate the request after all the attachment stream # has been read # QQQ DELETE doesn't have any attachments, does it? nor does GET. # QQQ Reading attachment when there isn't one won't hurt, will it? # QQQ So why don't we just do it all the time for all requests? data = [] d = stream.readStream(request.stream, data.append) d.addCallback(_finished_reading, data) return d else: d = self.authenticate(request) d.addCallback(_renderResource) return d
def renderHTTP(self, request): """ Authenticate the request then return the result of calling renderHTTP on C{self.wrappedResource} """ if request.method not in self.allowedMethods(): response = http.Response(responsecode.NOT_ALLOWED) response.headers.setHeader("allow", self.allowedMethods()) return response def _renderResource(resource): return resource.renderHTTP(request) def _finished_reading(ignore, result): data = "".join(result) request.attachment = data d = self.authenticate(request) d.addCallback(_renderResource) return d if request.method in ("PUT", "DELETE"): # we need to authenticate the request after all the attachment stream # has been read # QQQ DELETE doesn't have any attachments, does it? nor does GET. # QQQ Reading attachment when there isn't one won't hurt, will it? # QQQ So why don't we just do it all the time for all requests? data = [] d = stream.readStream(request.stream, data.append) d.addCallback(_finished_reading, data) return d else: d = self.authenticate(request) d.addCallback(_renderResource) return d
def http_POST(self, request): parser, unmarshaller = xmlrpclib.getparser() deferred = stream.readStream(request.stream, parser.feed) deferred.addCallback(lambda x: self._cbDispatch(request, parser, unmarshaller)) deferred.addErrback(self._ebRender) deferred.addCallback(self._cbRender, request) return deferred
def http_POST(self, request): # we override the upstream version because it doesn't handle # JSON mime types from twisted.web2 import http, fileupload, responsecode, stream if request.stream.length == 0: d = defer.succeed(None) else: parser = None ctype = request.headers.getHeader('content-type') if ctype is None: d = defer.succeed(None) else: def updateArgs(data): args = data request.args.update(args) def updateJson(data): request.args['message'] = [data] def error(f): raise http.HTTPError(responsecode.BAD_REQUEST) if ctype.mediaType == 'application' and ctype.mediaSubtype == 'x-www-form-urlencoded': d = fileupload.parse_urlencoded(request.stream) d.addCallbacks(updateArgs, error) elif ctype.mediaType in ('application','text') and ctype.mediaSubtype == 'json': d = stream.readStream(request.stream, updateJson) d.addErrback(error) else: raise http.HTTPError(responsecode.BAD_REQUEST) return d.addCallback(lambda res: self.render(request))
def run(self): """Start the streaming. @rtype: L{twisted.internet.defer.Deferred} """ self.doneDefer = stream.readStream(self.stream, self._gotData) self.doneDefer.addCallbacks(self._done, self._error) return self.doneDefer
def test_output(self): p = self.runCode("import sys\nfor i in range(100): sys.stdout.write('x' * 1000)") l = [] d = stream.readStream(p.outStream, l.append) def verify(_): self.assertEquals("".join(l), ("x" * 1000) * 100) d2 = p.run() return d.addCallback(verify).addCallback(lambda _: d2)
def http_POST(self, request): parser, unmarshaller = xmlrpclib.getparser() deferred = stream.readStream(request.stream, parser.feed) deferred.addCallback( lambda x: self._cbDispatch(request, parser, unmarshaller)) deferred.addErrback(self._ebRender) deferred.addCallback(self._cbRender, request) return deferred
def test_errouput(self): p = self.runCode("import sys\nfor i in range(100): sys.stderr.write('x' * 1000)") l = [] d = stream.readStream(p.errStream, l.append) def verify(_): self.assertEquals("".join(l), ("x" * 1000) * 100) p.run() return d.addCallback(verify)
def test_pullException(self): class Failer: def read(self): raise RuntimeError return stream.readStream( Failer(), lambda _: None).addErrback(lambda _: _.trap(RuntimeError))
def test_pullFailure(self): l = [] s = TestStreamer(['abcd', defer.fail(RuntimeError()), 'ijkl']) def test(result): result.trap(RuntimeError) self.assertEquals(l, ["abcd"]) return stream.readStream(s, l.append).addErrback(test)
def test_pullFailure(self): l = [] s = TestStreamer(["abcd", defer.fail(RuntimeError()), "ijkl"]) def test(result): result.trap(RuntimeError) self.assertEquals(l, ["abcd"]) return stream.readStream(s, l.append).addErrback(test)
def parse(self, stream): def endOfStream(result): if not self.done: raise Exception("No more stuff?") else: return self.document d = readStream(stream, self.elementStream.parse) d.addCallback(endOfStream) return d
def test_processclosedinput(self): p = self.runCode("import sys; sys.stdout.write(sys.stdin.read(3));" + "sys.stdin.close(); sys.stdout.write('def')", "abc123") l = [] d = stream.readStream(p.outStream, l.append) def verify(_): self.assertEquals("".join(l), "abcdef") d2 = p.run() return d.addCallback(verify).addCallback(lambda _: d2)
def test_inputerror(self): p = self.runCode("import sys\nsys.stdout.write(sys.stdin.read())", TestStreamer(["hello", defer.fail(ZeroDivisionError())])) l = [] d = stream.readStream(p.outStream, l.append) d2 = p.run() def verify(_): self.assertEquals("".join(l), "hello") return d2 return d.addCallback(verify).addCallback(lambda _: log.flushErrors(ZeroDivisionError))
def allDataFromStream(stream, filter=None): data = [] def gotAllData(_): if not data: return None result = "".join([str(x) for x in data]) if filter is None: return result else: return filter(result) return readStream(stream, data.append).addCallback(gotAllData)
def test_input(self): p = self.runCode("import sys\nsys.stdout.write(sys.stdin.read())", "hello world") l = [] d = stream.readStream(p.outStream, l.append) d2 = p.run() def verify(_): self.assertEquals("".join(l), "hello world") return d2 return d.addCallback(verify)
def http_POST(self, request): ''' Handle post request ''' parser = SimpleParser() deferred = stream.readStream(request.stream, parser.feed) deferred.addCallback(lambda x: self._cbDispatch(request, parser)) deferred.addErrback(self._ebRender) deferred.addCallback(self._cbRender, request) return deferred
def test_processclosedinput(self): p = self.runCode( "import sys; sys.stdout.write(sys.stdin.read(3));" + "sys.stdin.close(); sys.stdout.write('def')", "abc123" ) l = [] d = stream.readStream(p.outStream, l.append) def verify(_): self.assertEquals("".join(l), "abcdef") d2 = p.run() return d.addCallback(verify).addCallback(lambda _: d2)
def http_POST(self, request): ctype = request.headers.getHeader('content-type') if ctype.mediaType == 'text' and ctype.mediaSubtype == 'json': out = [] d = stream.readStream(request.stream, lambda x: out.append(x)) d.addCallback(lambda _: simplejson.loads(''.join(out))) d.addCallback(lambda res: self.render(request, res)) else: d = server.parsePOSTData( request, self.maxMem, self.maxFields, self.maxSize) d.addCallback(lambda res: self.render(request)) return d
def gotResp(self, resp, num, expect): self.failUnless(resp.code >= 200 and resp.code < 300, "Got a non-200 response: %r" % resp.code) if expect is not None: self.failUnless(resp.stream.length == expect, "Length was incorrect, got %r, expected %r" % (resp.stream.length, expect)) while len(self.length) <= num: self.length.append(0) self.length[num] = 0 def addData(data, self = self, num = num): self.length[num] += len(data) def checkLength(resp, self = self, num = num, length = resp.stream.length): self.failUnlessEqual(self.length[num], length) return resp df = stream_mod.readStream(resp.stream, addData) df.addCallback(checkLength) return df
def test_render(self): """ Test that the result from Data.render is acceptable, including the response code, the content-type header, and the actual response body itself. """ response = iweb.IResponse(self.data.render(None)) self.assertEqual(response.code, 200) self.assert_(response.headers.hasHeader("content-type")) self.assertEqual(response.headers.getHeader("content-type"), http_headers.MimeType("text", "plain")) def checkStream(data): self.assertEquals(str(data), self.text) return stream.readStream(iweb.IResponse(self.data.render(None)).stream, checkStream)
def readIntoFile(stream, outFile, maxlen): """Read the stream into a file, but not if it's longer than maxlen. Returns Deferred which will be triggered on finish. """ curlen = [0] def done(_): return _ def write(data): curlen[0] += len(data) if curlen[0] > maxlen: raise MimeFormatError("Maximum length of %d bytes exceeded." % maxlen) outFile.write(data) return readStream(stream, write).addBoth(done)
def readStreamToString(s): """ Read all data from a stream into a string. @param s: a L{twisted.web2.stream.IByteStream} to read from. @return: a L{Deferred} results in a str """ allData = [] def gotData(data): allData.append(data) d = stream.readStream(s, gotData) d.addCallback(lambda ign: ''.join(allData)) return d
def _getPeerPieces(self, response, key, site): """Process the retrieved response from the peer.""" log.msg('Got a piece hash response %d from %r' % (response.code, site)) if response.code != 200: # Request failed, try a different peer self.getPeerPieces(key, site) else: # Read the response stream to a string self.peers[site]['pieces'] = '' def _gotPeerPiece(data, self = self, site = site): log.msg('Peer %r got %d bytes of piece hashes' % (site, len(data))) self.peers[site]['pieces'] += data log.msg('Streaming piece hashes from peer') df = stream.readStream(response.stream, _gotPeerPiece) df.addCallbacks(self._gotPeerPieces, self._gotPeerError, callbackArgs=(key, site), errbackArgs=(key, site))
def test_render(self): """ Test that the result from Data.render is acceptable, including the response code, the content-type header, and the actual response body itself. """ response = iweb.IResponse(self.data.render(None)) self.assertEqual(response.code, 200) self.assert_(response.headers.hasHeader("content-type")) self.assertEqual(response.headers.getHeader("content-type"), http_headers.MimeType("text", "plain")) def checkStream(data): self.assertEquals(str(data), self.text) return stream.readStream( iweb.IResponse(self.data.render(None)).stream, checkStream)
def test_inputerror(self): p = self.runCode( "import sys\nsys.stdout.write(sys.stdin.read())", TestStreamer(["hello", defer.fail(ZeroDivisionError())]) ) l = [] d = stream.readStream(p.outStream, l.append) d2 = p.run() def verify(_): self.assertEquals("".join(l), "hello") return d2 def cbVerified(ignored): excs = self.flushLoggedErrors(ZeroDivisionError) self.assertEqual(len(excs), 1) return d.addCallback(verify).addCallback(cbVerified)
def test_inputerror(self): p = self.runCode( "import sys\nsys.stdout.write(sys.stdin.read())", TestStreamer(["hello", defer.fail(ZeroDivisionError())])) l = [] d = stream.readStream(p.outStream, l.append) d2 = p.run() def verify(_): self.assertEquals("".join(l), "hello") return d2 def cbVerified(ignored): excs = self.flushLoggedErrors(ZeroDivisionError) self.assertEqual(len(excs), 1) return d.addCallback(verify).addCallback(cbVerified)
def http_POST(self, request): """ Respond to a POST request to create a new node. """ def gotStream(_): if request.args.get('uri'): jid, nodeIdentifier = getServiceAndNode(request.args['uri'][0]) return defer.succeed(nodeIdentifier) else: raise http.HTTPError(http.Response(responsecode.BAD_REQUEST, "No URI given")) def doDelete(nodeIdentifier, data): if data: params = simplejson.loads(''.join(data)) redirectURI = params.get('redirect_uri') else: redirectURI = None return self.backend.deleteNode(nodeIdentifier, self.owner, redirectURI) def respond(result): return http.Response(responsecode.NO_CONTENT) def trapNotFound(failure): failure.trap(error.NodeNotFound) return http.StatusResponse(responsecode.NOT_FOUND, "Node not found") def trapXMPPURIParseError(failure): failure.trap(XMPPURIParseError) return http.StatusResponse(responsecode.BAD_REQUEST, "Malformed XMPP URI: %s" % failure.value) data = [] d = readStream(request.stream, data.append) d.addCallback(gotStream) d.addCallback(doDelete, data) d.addCallback(respond) d.addErrback(trapNotFound) d.addErrback(trapXMPPURIParseError) return d
def _getPeerPieces(self, response, key, site): """Process the retrieved response from the peer.""" log.msg('Got a piece hash response %d from %r' % (response.code, site)) if response.code != 200: # Request failed, try a different peer self.getPeerPieces(key, site) else: # Read the response stream to a string self.peers[site]['pieces'] = '' def _gotPeerPiece(data, self=self, site=site): log.msg('Peer %r got %d bytes of piece hashes' % (site, len(data))) self.peers[site]['pieces'] += data log.msg('Streaming piece hashes from peer') df = stream.readStream(response.stream, _gotPeerPiece) df.addCallbacks(self._gotPeerPieces, self._gotPeerError, callbackArgs=(key, site), errbackArgs=(key, site))
def http_POST(self, request): # we override the upstream version because it doesn't handle # JSON mime types from twisted.web2 import http, fileupload, responsecode, stream if request.stream.length == 0: d = defer.succeed(None) else: parser = None ctype = request.headers.getHeader('content-type') if ctype is None: d = defer.succeed(None) else: def updateArgs(data): args = data request.args.update(args) def updateJson(data): request.args['message'] = [data] def error(f): raise http.HTTPError(responsecode.BAD_REQUEST) if ctype.mediaType == 'application' and ctype.mediaSubtype == 'x-www-form-urlencoded': d = fileupload.parse_urlencoded(request.stream) d.addCallbacks(updateArgs, error) elif ctype.mediaType in ('application', 'text' ) and ctype.mediaSubtype == 'json': d = stream.readStream(request.stream, updateJson) d.addErrback(error) else: raise http.HTTPError(responsecode.BAD_REQUEST) return d.addCallback(lambda res: self.render(request))
def test_pull(self): l = [] s = TestStreamer(['abcd', defer.succeed('efgh'), 'ijkl']) return stream.readStream(s, l.append).addCallback( lambda _: self.assertEquals(l, ["abcd", "efgh", "ijkl"]))
def test_processingException(self): s = TestStreamer(['abcd', defer.succeed('efgh'), 'ijkl']) return stream.readStream( s, lambda x: 1 / 0).addErrback(lambda _: _.trap(ZeroDivisionError))
def test_processingException(self): s = TestStreamer(["abcd", defer.succeed("efgh"), "ijkl"]) return stream.readStream(s, lambda x: 1 / 0).addErrback(lambda _: _.trap(ZeroDivisionError))
def _readResponse(resp): # print "_readResponse" def _print(data): print data stream.readStream(resp.stream, _print)
def test_pull(self): l = [] s = TestStreamer(["abcd", defer.succeed("efgh"), "ijkl"]) return stream.readStream(s, l.append).addCallback(lambda _: self.assertEquals(l, ["abcd", "efgh", "ijkl"]))
def noDataFromStream(stream): def gotData(data): if data: raise ValueError("Stream contains unexpected data.") return readStream(stream, gotData)
def test_pullException(self): class Failer: def read(self): raise RuntimeError return stream.readStream(Failer(), lambda _: None).addErrback(lambda _: _.trap(RuntimeError))