Exemplo n.º 1
0
    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))
Exemplo n.º 2
0
    def write(self, output):
        # Called in application thread
        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)
Exemplo n.º 3
0
 def __init__(self, request):
     from twisted.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()
Exemplo n.º 4
0
 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
Exemplo n.º 5
0
 def render(self, req):
     # Create a stream object which can be written in pieces.
     s=stream.ProducerStream()
     # Write a string, and then, later, write another string, and
     # call it done.  (Also write spaces so browsers don't wait
     # before displaying anything at all)
     s.write("Hello\n")
     s.write(' '*10000+'\n')
     reactor.callLater(1, s.write, "World!\n")
     reactor.callLater(2, s.finish)
     # Return a response. Use the default response code of OK, and
     # the default headers
     return http.Response(stream=s)
Exemplo n.º 6
0
    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
Exemplo n.º 7
0
    def writeAll(self, result):
        # Called in application thread
        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)
Exemplo n.º 8
0
 def __init__(self, request, deferred):
     self.request = request
     self.deferred = deferred
     self.stream = stream.ProducerStream()
     self.response = http.Response(stream=self.stream)
Exemplo n.º 9
0
 def __init__(self):
     self.headers = http_headers.Headers()
     self.stream = stream.ProducerStream()
Exemplo n.º 10
0
    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