def write(self, output): # Called in application thread from twisted.internet import reactor if self.response is None: raise RuntimeError( "Application didn't call startResponse before writing data!") if not self.headersSent: self.stream = self.response.stream = stream.ProducerStream() self.headersSent = True # threadsafe event object to communicate paused state. self.unpaused = threading.Event() # After this, we cannot touch self.response from this # thread any more def _start(): # Called in IO thread self.stream.registerProducer(self, True) self.__callback() # Notify application thread to start writing self.unpaused.set() reactor.callFromThread(_start) # Wait for unpaused to be true self.unpaused.wait() reactor.callFromThread(self.stream.write, output)
def __init__(self, request): from OPSI.web2 import http components.Componentized.__init__(self) self.request = request self.response = http.Response(stream=stream.ProducerStream()) # This deferred will be fired by the first call to write on OldRequestAdapter # and will cause the headers to be output. self.deferredResponse = defer.Deferred()
def test_failfinish(self): p = stream.ProducerStream() p.write("hello") p.finish(RuntimeError()) self.assertEquals(p.read(), "hello") d = p.read() l = [] d.addErrback(lambda _: (l.append(1), _.trap(RuntimeError))).addCallback( lambda _: self.assertEquals(l, [1])) return d
def test_errorReadingRequestStream(self): """Ensure that stream errors are propagated to the response.""" cxn = self.connect(inputTimeOut=None) s = stream.ProducerStream() s.write('Foo') req = http.ClientRequest('GET', '/', None, s) d = cxn.client.submitRequest(req) self.assertFailure(d, IOError) s.finish(IOError('Test Error')) return d
def test_chunkedUpload(self): """Ensure chunked data is correctly decoded on upload.""" cxn = self.connect(inputTimeOut=None) data = 'Foo bar baz bax' s = stream.ProducerStream(length=None) s.write(data) req = http.ClientRequest('PUT', '/', None, s) d = cxn.client.submitRequest(req) s.finish() self.assertReceived( cxn, 'PUT / HTTP/1.1', ['Connection: close', 'Transfer-Encoding: chunked'], '%X\r\n%s\r\n0\r\n\r\n' % (len(data), data)) self.writeLines(cxn, ('HTTP/1.1 200 OK', 'Connection: close', 'Content-Length: 0', '\r\n')) return d.addCallback(lambda _: self.assertDone(cxn))
def writeAll(self, result): # Called in application thread from twisted.internet import reactor if not self.headersSent: if self.response is None: raise RuntimeError( "Application didn't call startResponse before writing data!" ) l = 0 for item in result: l += len(item) self.response.stream = stream.ProducerStream(length=l) self.response.stream.buffer = list(result) self.response.stream.finish() reactor.callFromThread(self.__callback) else: # Has already been started, cannot replace the stream def _write(): # Called in IO thread for s in result: self.stream.write(s) self.stream.finish() reactor.callFromThread(_write)
def createRequest(self): self.stream = stream_mod.ProducerStream(self.length) self.response = http.Response(self.code, self.inHeaders, self.stream) self.stream.registerProducer(self, True) del self.inHeaders
def __init__(self): self.headers = http_headers.Headers() self.stream = stream.ProducerStream()
def __init__(self, request, deferred): self.request = request self.deferred = deferred self.stream = stream.ProducerStream() self.response = http.Response(stream=self.stream)