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 __init__(self, request):
     from hack.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.º 3
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.º 4
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)

        s.finish(IOError('Test Error'))

        return self.assertFailure(d, IOError)
Exemplo n.º 5
0
Arquivo: wsgi.py Projeto: lzimm/360io
 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.º 6
0
Arquivo: wsgi.py Projeto: lzimm/360io
    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.º 7
0
 def __init__(self):
     self.headers = http_headers.Headers()
     self.stream = stream.ProducerStream()
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)