Esempio n. 1
0
 def setScrollRegion(self, first=None, last=None):
     if first is not None:
         first = intToBytes(first)
     else:
         first = b''
     if last is not None:
         last = intToBytes(last)
     else:
         last = b''
     self.write(b'\x1b[' + first + b';' + last + b'r')
Esempio n. 2
0
 def n(self, proto, handler, buf):
     if buf == b'6':
         x, y = handler.reportCursorPosition()
         proto.transport.write(b'\x1b['
                               + intToBytes(x+1)
                               + b';'
                               + intToBytes(y+1)
                               + b'R')
     else:
         handler.unhandledControlSequence(b'\x1b[' + buf + b'n')
Esempio n. 3
0
        def cbStarted(ignored):
            connectionRefused = client.startedDeferred = defer.Deferred()
            client.transport.connect("127.0.0.1", 80)

            for i in range(10):
                client.transport.write(intToBytes(i))
                server.transport.write(intToBytes(i), ("127.0.0.1", 80))

            return self.assertFailure(
                connectionRefused,
                error.ConnectionRefusedError)
Esempio n. 4
0
 def test_selectGraphicRendition(self):
     """
     L{ServerProtocol.selectGraphicRendition} writes a control
     sequence containing the requested attributes and ending with
     L{CSFinalByte.SGR}
     """
     self.protocol.selectGraphicRendition(str(BLINK), str(UNDERLINE))
     self.assertEqual(self.transport.value(),
                      self.CSI +
                      intToBytes(BLINK) + b';' + intToBytes(UNDERLINE) +
                      CSFinalByte.SGR.value)
Esempio n. 5
0
    def __bytes__(self):
        """
        Return a byte string representation of the channel
        """
        name = self.name
        if not name:
            name = b'None'

        return (b'<SSHChannel ' + name +
                b' (lw ' + intToBytes(self.localWindowLeft) +
                b' rw ' + intToBytes(self.remoteWindowLeft) +
                b')>')
Esempio n. 6
0
    def testSimpleCardinals(self):
        self.parser.dataReceived(
            b''.join(
                    [b''.join([b'\x1b[' + n + ch
                             for n in (b'', intToBytes(2), intToBytes(20), intToBytes(200))]
                           ) for ch in iterbytes(b'BACD')
                    ]))
        occs = occurrences(self.proto)

        for meth in ("Down", "Up", "Forward", "Backward"):
            for count in (1, 2, 20, 200):
                result = self.assertCall(occs.pop(0), "cursor" + meth, (count,))
                self.assertFalse(occurrences(result))
        self.assertFalse(occs)
def _spawn(script, outputFD):
    """
    Start a script that is a peer of this test as a subprocess.

    @param script: the module name of the script in this directory (no
        package prefix, no '.py')
    @type script: C{str}

    @rtype: L{StartStopProcessProtocol}
    """
    pyExe = FilePath(sys.executable).asBytesMode().path
    env = bytesEnviron()
    env[b"PYTHONPATH"] = FilePath(
        pathsep.join(sys.path)).asBytesMode().path
    sspp = StartStopProcessProtocol()
    reactor.spawnProcess(
        sspp, pyExe, [
            pyExe,
            FilePath(__file__).sibling(script + ".py").asBytesMode().path,
            intToBytes(outputFD),
        ],
        env=env,
        childFDs={0: "w", 1: "r", 2: "r", outputFD: outputFD}
    )
    return sspp
Esempio n. 8
0
    def test_blksize(self):
        self.s = MockSession()
        opts = self.proto.processOptions(OrderedDict({b'blksize':b'8'}))
        self.proto.applyOptions(self.s, opts)
        self.assertEqual(self.s.block_size, 8)
        self.assertEqual(opts, OrderedDict({b'blksize':b'8'}))

        self.s = MockSession()
        opts = self.proto.processOptions(OrderedDict({b'blksize':b'foo'}))
        self.proto.applyOptions(self.s, opts)
        self.assertEqual(self.s.block_size, 512)
        self.assertEqual(opts, OrderedDict())

        self.s = MockSession()
        opts = self.proto.processOptions(OrderedDict({b'blksize':b'65464'}))
        self.proto.applyOptions(self.s, opts)
        self.assertEqual(self.s.block_size, MAX_BLOCK_SIZE)
        self.assertEqual(opts, OrderedDict({b'blksize':intToBytes(MAX_BLOCK_SIZE)}))

        self.s = MockSession()
        opts = self.proto.processOptions(OrderedDict({b'blksize':b'65465'}))
        self.proto.applyOptions(self.s, opts)
        self.assertEqual(self.s.block_size, 512)
        self.assertEqual(opts, OrderedDict())

        self.s = MockSession()
        opts = self.proto.processOptions(OrderedDict({b'blksize':b'7'}))
        self.proto.applyOptions(self.s, opts)
        self.assertEqual(self.s.block_size, 512)
        self.assertEqual(opts, OrderedDict())
 def render_GET(self, request):
     data = json.dumps(request.args)
     request.setHeader(b'content-type', networkString('application/json'))
     request.setHeader(b'content-length', intToBytes(len(data)))
     if request.method == b'HEAD':
         return b''
     return data
Esempio n. 10
0
 def render_GET(self, request):
     data = json.dumps(request.args)
     request.setHeader(b"content-type", networkString("application/json"))
     request.setHeader(b"content-length", intToBytes(len(data)))
     if request.method == b"HEAD":
         return b""
     return data
    def render_GET(self, request):
        def decode(x):
            if isinstance(x, bytes):
                return bytes2unicode(x)
            elif isinstance(x, (list, tuple)):
                return [bytes2unicode(y) for y in x]
            elif isinstance(x, dict):
                newArgs = {}
                for a, b in x.items():
                    newArgs[decode(a)] = decode(b)
                return newArgs
            return x

        args = decode(request.args)
        content_type = request.getHeader(b'content-type')
        if content_type == b"application/json":
            jsonBytes = request.content.read()
            jsonStr = bytes2unicode(jsonBytes)
            args['json_received'] = json.loads(jsonStr)

        data = json.dumps(args)
        data = unicode2bytes(data)
        request.setHeader(b'content-type', b'application/json')
        request.setHeader(b'content-length', intToBytes(len(data)))
        if request.method == b'HEAD':
            return b''
        return data
Esempio n. 12
0
    def _cbRender(self, result, request, responseFailed=None):
        if responseFailed:
            return

        if isinstance(result, Handler):
            result = result.result
        if not isinstance(result, Fault):
            result = (result,)
        try:
            try:
                content = xmlrpclib.dumps(
                    result, methodresponse=True,
                    allow_none=self.allowNone)
            except Exception as e:
                f = Fault(self.FAILURE, "Can't serialize output: %s" % (e,))
                content = xmlrpclib.dumps(f, methodresponse=True,
                                          allow_none=self.allowNone)

            if isinstance(content, unicode):
                content = content.encode('utf8')
            request.setHeader(
                b"content-length", intToBytes(len(content)))
            request.write(content)
        except:
            log.err()
        request.finish()
Esempio n. 13
0
    def __init__(self, code, message=None, response=None):
        """
        Initializes a basic exception.

        @type code: L{bytes} or L{int}
        @param code: Refers to an HTTP status code (for example, 200) either as
            an integer or a bytestring representing such. If no C{message} is
            given, C{code} is mapped to a descriptive bytestring that is used
            instead.

        @type message: L{bytes}
        @param message: A short error message, for example "NOT FOUND".

        @type response: L{bytes}
        @param response: A complete HTML document for an error page.
        """
        message = message or _codeToMessage(code)

        Exception.__init__(self, code, message, response)

        if isinstance(code, int):
            # If we're given an int, convert it to a bytestring
            # downloadPage gives a bytes, Agent gives an int, and it worked by
            # accident previously, so just make it keep working.
            code = intToBytes(code)

        self.status = code
        self.message = message
        self.response = response
Esempio n. 14
0
 def spin():
     for value in count:
         if value == howMany:
             connection.loseConnection()
             return
         connection.write(intToBytes(value))
         break
     reactor.callLater(0, spin)
Esempio n. 15
0
    def contentFinish(self, content):
        if self.obj.client_supports_gzip:
            self.setHeader(b'content-encoding', b'gzip')
            content = self.zip(content, True)

        self.setHeader(b'content-length', intToBytes(len(content)))
        self.write(content)
        self.finish()
Esempio n. 16
0
def _extractURLparts(request):
    """
    Extracts and decodes URI parts from C{request}.

    All strings must be UTF8-decodable.

    @param request: A Twisted Web request.
    @type request: L{twisted.web.iweb.IRequest}

    @raise URLDecodeError: If one of the parts could not be decoded as UTF-8.

    @return: L{tuple} of the URL scheme, the server name, the server port, the
        path info and the script name.
    @rtype: L{tuple} of L{unicode}, L{unicode}, L{int}, L{unicode}, L{unicode}
    """
    server_name = request.getRequestHostname()
    if hasattr(request.getHost(), 'port'):
        server_port = request.getHost().port
    else:
        server_port = 0
    if (bool(request.isSecure()), server_port) not in [
            (True, 443), (False, 80), (False, 0), (True, 0)]:
        server_name = server_name + b":" + intToBytes(server_port)

    script_name = b''
    if request.prepath:
        script_name = b'/'.join(request.prepath)

        if not script_name.startswith(b'/'):
            script_name = b'/' + script_name

    path_info = b''
    if request.postpath:
        path_info = b'/'.join(request.postpath)

        if not path_info.startswith(b'/'):
            path_info = b'/' + path_info

    url_scheme = u'https' if request.isSecure() else u'http'

    utf8Failures = []
    try:
        server_name = server_name.decode("utf-8")
    except UnicodeDecodeError:
        utf8Failures.append(("SERVER_NAME", failure.Failure()))
    try:
        path_info = path_info.decode("utf-8")
    except UnicodeDecodeError:
        utf8Failures.append(("PATH_INFO", failure.Failure()))
    try:
        script_name = script_name.decode("utf-8")
    except UnicodeDecodeError:
        utf8Failures.append(("SCRIPT_NAME", failure.Failure()))

    if utf8Failures:
        raise _URLDecodeError(utf8Failures)

    return url_scheme, server_name, server_port, path_info, script_name
Esempio n. 17
0
    def _render(self, request):
        origin = request.getHeader('Origin')
        if origin:
            request.setHeader('Access-Control-Allow-Origin', origin)
            request.setHeader('Access-Control-Allow-Credentials', 'true')
        else:
            request.setHeader('Access-Control-Allow-Origin', '*')
        request.setHeader('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS, HEAD')
        request.setHeader('Access-Control-Allow-Headers',
                          'Origin, Content-Type, Cache-Control, X-Requested-With, Authorization')

        ret = None
        try:
            ret = yield self.handle_request(request)
            # allow views to take full control of output streaming
            if ret is not NOT_DONE_YET and ret is not EmptyResponse:
                request.setHeader('Content-Type', 'application/json')
                json_data = json.dumps(ret, indent=2, cls=JsonSetEncoder)
                request.setHeader('Content-Length', intToBytes(len(json_data)))
                request.write(json_data)
        except HttpStatus as exc:
            request.setResponseCode(exc.status_code, exc.status_description)
            for name, value in exc.headers.items():
                request.responseHeaders.addRawHeader(name, value)
            # emit a header with location of the trusted keystone instance
            if get_config().getboolean('auth', 'use_keystone', False):
                keystone_uri = get_config().getstring('keystone', 'keystone_uri')
                request.setHeader('WWW-Authenticate', 'Keystone uri=%s' % keystone_uri)
            if exc.body:
                request.write(json.dumps(exc.body))
            else:
                request.write("%s %s\n" % (exc.status_code, exc.status_description))
            if exc.message:
                request.write("%s\n" % exc.message)
        except Exception:
            request.setHeader('Content-Type', 'text/plain')
            request.setResponseCode(500, "Server Error")
            error_message = "%s %s\n\n" % (500, "Server Error")
            request.setHeader('Content-Length', intToBytes(len(error_message)))
            request.write(error_message)
            log.err(system='httprest')
            failure.Failure().printTraceback(request)
        finally:
            if ret is not NOT_DONE_YET:
                request.finish()
Esempio n. 18
0
 def lineReceived(self, line):
     self.i += 1
     if self.i < 4:
         # by this point we should have connection closed,
         # but just in case we didn't we won't ever send 'Hello 4'
         self.sendLine(b"Hello " + intToBytes(self.i))
     SimpleProtocol.lineReceived(self, line)
     if self.lines[-1] == b"Hello 3":
         self.transport.loseConnection()
 def testHostHeader(self):
     # if we pass Host header explicitly, it should be used, otherwise
     # it should extract from url
     return defer.gatherResults([
         client.getPage(self.getURL("host")).addCallback(
                 self.assertEqual, b"127.0.0.1:" + intToBytes(self.portno)),
         client.getPage(self.getURL("host"),
                        headers={b"Host": b"www.example.com"}).addCallback(
                 self.assertEqual, b"www.example.com")])
Esempio n. 20
0
    def forwardData(self, data, end=False):
        if not self.startedWriting:
            if self.obj.client_supports_gzip:
                self.setHeader(b'content-encoding', b'gzip')

            if data != '' and end:
                self.setHeader(b'content-length', intToBytes(len(data)))

        if data != '':
            self.write(data)
Esempio n. 21
0
def pack(sexp):
    s = b""
    for o in sexp:
        if type(o) in (type(()), type([])):
            s+=b'('
            s+=pack(o)
            s+=b')'
        else:
            s+=intToBytes(len(o)) + b":" + o
    return s
Esempio n. 22
0
    def contentFinish(self, data):
        if self.obj.client_supports_gzip:
            self.setHeader(b'content-encoding', b'gzip')
            data = self.zip(data, True)

        self.setHeader(b'content-length', intToBytes(len(data)))
        self.write(data)
        try:
            self.finish()
        except:
            pass
Esempio n. 23
0
        def makeAttempt():
            """
            Send one packet to the listening BadClient.  Set up a 0.1 second
            timeout to do re-transmits in case the packet is dropped.  When two
            packets have been received by the BadClient, stop sending and let
            the finalDeferred's callbacks do some assertions.
            """
            if not attempts:
                try:
                    self.fail("Not enough packets received")
                except:
                    finalDeferred.errback()

            self.failIfIdentical(client.transport, None, "UDP Protocol lost its transport")

            packet = intToBytes(attempts.pop(0))
            packetDeferred = defer.Deferred()
            client.setDeferred(packetDeferred)
            client.transport.write(packet, (addr.host, addr.port))

            def cbPacketReceived(packet):
                """
                A packet arrived.  Cancel the timeout for it, record it, and
                maybe finish the test.
                """
                timeoutCall.cancel()
                succeededAttempts.append(packet)
                if len(succeededAttempts) == 2:
                    # The second error has not yet been logged, since the
                    # exception which causes it hasn't even been raised yet.
                    # Give the datagramReceived call a chance to finish, then
                    # let the test finish asserting things.
                    reactor.callLater(0, finalDeferred.callback, None)
                else:
                    makeAttempt()

            def ebPacketTimeout(err):
                """
                The packet wasn't received quickly enough.  Try sending another
                one.  It doesn't matter if the packet for which this was the
                timeout eventually arrives: makeAttempt throws away the
                Deferred on which this function is the errback, so when
                datagramReceived callbacks, so it won't be on this Deferred, so
                it won't raise an AlreadyCalledError.
                """
                makeAttempt()

            packetDeferred.addCallbacks(cbPacketReceived, ebPacketTimeout)
            packetDeferred.addErrback(finalDeferred.errback)

            timeoutCall = reactor.callLater(
                0.1, packetDeferred.errback,
                error.TimeoutError(
                    "Timed out in testDatagramReceivedError"))
    def getUidl(self, i):
        """
        Construct a UID which is simply the string representation of the given
        index.

        @param i: See L{pop3.IMailbox.getUidl}.
        @return: See L{pop3.IMailbox.getUidl}.
        """
        if i >= len(self.messages):
            raise self.exceptionType()
        return intToBytes(i)
Esempio n. 25
0
 def testHostHeader(self):
     # if we pass Host header explicitly, it should be used, otherwise
     # it should extract from url
     return defer.gatherResults([
         client.getPage(self.getURL("host")).addCallback(
             self.assertEqual, b"127.0.0.1:" + intToBytes(self.portno)),
         client.getPage(self.getURL("host"),
                        headers={
                            b"Host": b"www.example.com"
                        }).addCallback(self.assertEqual, b"www.example.com")
     ])
Esempio n. 26
0
        def makeAttempt():
            """
            Send one packet to the listening BadClient.  Set up a 0.1 second
            timeout to do re-transmits in case the packet is dropped.  When two
            packets have been received by the BadClient, stop sending and let
            the finalDeferred's callbacks do some assertions.
            """
            if not attempts:
                try:
                    self.fail("Not enough packets received")
                except:
                    finalDeferred.errback()

            self.failIfIdentical(client.transport, None,
                                 "UDP Protocol lost its transport")

            packet = intToBytes(attempts.pop(0))
            packetDeferred = defer.Deferred()
            client.setDeferred(packetDeferred)
            client.transport.write(packet, (addr.host, addr.port))

            def cbPacketReceived(packet):
                """
                A packet arrived.  Cancel the timeout for it, record it, and
                maybe finish the test.
                """
                timeoutCall.cancel()
                succeededAttempts.append(packet)
                if len(succeededAttempts) == 2:
                    # The second error has not yet been logged, since the
                    # exception which causes it hasn't even been raised yet.
                    # Give the datagramReceived call a chance to finish, then
                    # let the test finish asserting things.
                    reactor.callLater(0, finalDeferred.callback, None)
                else:
                    makeAttempt()

            def ebPacketTimeout(err):
                """
                The packet wasn't received quickly enough.  Try sending another
                one.  It doesn't matter if the packet for which this was the
                timeout eventually arrives: makeAttempt throws away the
                Deferred on which this function is the errback, so when
                datagramReceived callbacks, so it won't be on this Deferred, so
                it won't raise an AlreadyCalledError.
                """
                makeAttempt()

            packetDeferred.addCallbacks(cbPacketReceived, ebPacketTimeout)
            packetDeferred.addErrback(finalDeferred.errback)

            timeoutCall = reactor.callLater(
                0.1, packetDeferred.errback,
                error.TimeoutError("Timed out in testDatagramReceivedError"))
Esempio n. 27
0
    def processingFailed(self, reason):
        log.err(reason)

        body = (b"<html><head><title>Processing Failed</title></head><body>"
                b"<b>Processing Failed</b></body></html>")

        self.setResponseCode(http.INTERNAL_SERVER_ERROR)
        self.setHeader(b'content-type', b"text/html")
        self.setHeader(b'content-length', intToBytes(len(body)))
        self.write(body)
        self.finish()
        return reason
Esempio n. 28
0
 def resumeProducing(self):
     """
     Write the counter value once.
     """
     if self.consumer is None or self.counter >= 10:
         raise RuntimeError("BUG: resume after unregister/stop.")
     else:
         self.consumer.write(intToBytes(self.counter))
         self.counter += 1
         if self.counter == 10:
             self.consumer.unregisterProducer()
             self._done()
Esempio n. 29
0
 def test_option_tsize(self):
     # A tsize option of 0 sent as part of a read session prompts a tsize
     # response with the actual size of the file.
     self.options[b'tsize'] = b'0'
     self.rs.startProtocol()
     self.clock.advance(0.1)
     self.transport.clear()
     self.clock.advance(3)
     # The response contains the size of the test data.
     self.options[b'tsize'] = intToBytes(len(self.test_data))
     oack_datagram = OACKDatagram(self.options).to_wire()
     self.assertEqual(self.transport.value(), oack_datagram)
Esempio n. 30
0
 def test_frameElementLineNumber(self):
     """
     The I{lineNumber} renderer of L{_FrameElement} renders the line number
     associated with the frame object used to initialize the
     L{_FrameElement}.
     """
     element = _FrameElement(TagLoader(tags.span(render="lineNumber")),
                             self.frame)
     d = flattenString(None, element)
     d.addCallback(self.assertEqual,
                   b"<span>" + intToBytes(self.base + 1) + b"</span>")
     return d
Esempio n. 31
0
 def test_option_tsize(self):
     # A tsize option of 0 sent as part of a read session prompts a tsize
     # response with the actual size of the file.
     self.options[b'tsize'] = b'0'
     self.rs.startProtocol()
     self.clock.advance(0.1)
     self.transport.clear()
     self.clock.advance(3)
     # The response contains the size of the test data.
     self.options[b'tsize'] = intToBytes(len(self.test_data))
     oack_datagram = OACKDatagram(self.options).to_wire()
     self.assertEqual(self.transport.value(), oack_datagram)
Esempio n. 32
0
    def _setContentHeaders(self, request, size=None):
        if size is None:
            size = self.getFileSize()

        if size:
            request.setHeader(b'content-length', intToBytes(size))
        if self.contentType:
            request.setHeader(b'content-type', networkString(self.contentType))
        if self.encoding:
            request.setHeader(b'content-encoding', networkString(self.encoding))
        if self.filename:
            request.setHeader(b'content-disposition', build_header(self.filename).encode('latin-1'))
Esempio n. 33
0
 def resumeProducing(self):
     """
     Write the counter value once.
     """
     if self.consumer is None or self.counter >= 10:
         raise RuntimeError("BUG: resume after unregister/stop.")
     else:
         self.consumer.write(intToBytes(self.counter))
         self.counter += 1
         if self.counter == 10:
             self.consumer.unregisterProducer()
             self._done()
Esempio n. 34
0
    def contentFinish(self, data):
        if self.obj.client_supports_gzip:
            self.setHeader(b'content-encoding', b'gzip')
            data = self.zip(data, True)

        self.setHeader(b'content-length', intToBytes(len(data)))

        try:
            self.write(data)
            self.finish()
        except:
            pass
Esempio n. 35
0
    def test_invalid(self):
        """
        Sentences with a bad checksum raise L{base.InvalidChecksum} when
        attempting to validate them.
        """
        validate = nmea._validateChecksum

        bareSentence, checksum = GPGGA.split(b"*")
        badChecksum = intToBytes(int(checksum, 16) + 1)
        sentences = [bareSentence + b"*" + badChecksum]

        for s in sentences:
            self.assertRaises(base.InvalidChecksum, validate, s)
Esempio n. 36
0
    def forwardData(self, data, end=False):
        if not self.startedWriting:
            if self.obj.client_supports_gzip:
                self.setHeader(b'content-encoding', b'gzip')

            if data != '' and end:
                self.setHeader(b'content-length', intToBytes(len(data)))

        if data != '':
            try:
                self.write(data)
            except:
                pass
Esempio n. 37
0
 def test_frameElementLineNumber(self):
     """
     The I{lineNumber} renderer of L{_FrameElement} renders the line number
     associated with the frame object used to initialize the
     L{_FrameElement}.
     """
     element = _FrameElement(
         TagLoader(tags.span(render="lineNumber")),
         self.frame)
     d = flattenString(None, element)
     d.addCallback(
         self.assertEqual, b"<span>" + intToBytes(self.base + 1) + b"</span>")
     return d
Esempio n. 38
0
    def test_invalid(self):
        """
        Sentences with a bad checksum raise L{base.InvalidChecksum} when
        attempting to validate them.
        """
        validate = nmea._validateChecksum

        bareSentence, checksum = GPGGA.split(b"*")
        badChecksum = intToBytes(int(checksum, 16) + 1)
        sentences = [bareSentence + b"*" + badChecksum]

        for s in sentences:
            self.assertRaises(base.InvalidChecksum, validate, s)
Esempio n. 39
0
    def test_concurrentRetrieves(self):
        """
        Issue three retrieve calls immediately without waiting for any to
        succeed and make sure they all do succeed eventually.
        """
        p, t = setUp()
        messages = [
            p.retrieve(i).addCallback(self.assertEqual, [
                b"First line of " + intToBytes(i + 1) + b".",
                b"Second line of " + intToBytes(i + 1) + b"."
            ]) for i in range(3)
        ]

        for i in range(1, 4):
            self.assertEqual(t.value(), b"RETR " + intToBytes(i) + b"\r\n")
            t.clear()
            p.dataReceived(b"+OK 2 lines on the way\r\n")
            p.dataReceived(b"First line of " + intToBytes(i) + b".\r\n")
            p.dataReceived(b"Second line of " + intToBytes(i) + b".\r\n")
            self.assertEqual(t.value(), b"")
            p.dataReceived(b".\r\n")

        return defer.DeferredList(messages, fireOnOneErrback=True)
    def delete(self, index):
        """
        Send a DELE command to delete a message from the server.

        @type index: L{int}
        @param index: The 0-based index of the message to delete.

        @rtype: L{Deferred <defer.Deferred>} which successfully fires with
            L{bytes} or fails with L{ServerErrorResponse}
        @return: A deferred which fires when the server response is received.
            On an OK response, the deferred succeeds with the server
            response minus the status indicator.  On an ERR response, the
            deferred fails with a server error response failure.
        """
        return self.sendShort(b'DELE', intToBytes(index + 1))
Esempio n. 41
0
    def connectionMade(self):
        self._response = None
        self.sendCommand(b'POST', self.factory.path)
        self.sendHeader(b'User-Agent', b'Twisted/XMLRPClib')
        self.sendHeader(b'Host', self.factory.host)
        self.sendHeader(b'Content-type', b'text/xml; charset=utf-8')
        payload = self.factory.payload
        self.sendHeader(b'Content-length', intToBytes(len(payload)))

        if self.factory.user:
            auth = b':'.join([self.factory.user, self.factory.password])
            authHeader = b''.join([b'Basic ', base64.b64encode(auth)])
            self.sendHeader(b'Authorization', authHeader)
        self.endHeaders()
        self.transport.write(payload)
Esempio n. 42
0
    def connectionMade(self):
        self._response = None
        self.sendCommand(b"POST", self.factory.path)
        self.sendHeader(b"User-Agent", b"Twisted/XMLRPClib")
        self.sendHeader(b"Host", self.factory.host)
        self.sendHeader(b"Content-type", b"text/xml; charset=utf-8")
        payload = self.factory.payload
        self.sendHeader(b"Content-length", intToBytes(len(payload)))

        if self.factory.user:
            auth = b":".join([self.factory.user, self.factory.password])
            authHeader = b"".join([b"Basic ", base64.b64encode(auth)])
            self.sendHeader(b"Authorization", authHeader)
        self.endHeaders()
        self.transport.write(payload)
Esempio n. 43
0
    def connectionMade(self):
        self._response = None
        self.sendCommand(b'POST', self.factory.path)
        self.sendHeader(b'User-Agent', b'Twisted/XMLRPClib')
        self.sendHeader(b'Host', self.factory.host)
        self.sendHeader(b'Content-type', b'text/xml; charset=utf-8')
        payload = self.factory.payload
        self.sendHeader(b'Content-length', intToBytes(len(payload)))

        if self.factory.user:
            auth = b':'.join([self.factory.user, self.factory.password])
            authHeader = b''.join([b'Basic ', base64.b64encode(auth)])
            self.sendHeader(b'Authorization', authHeader)
        self.endHeaders()
        self.transport.write(payload)
Esempio n. 44
0
    def option_tsize(self, val):
        """Process tsize option.

        If tsize is zero, get the size of the file to be read so that it can
        be returned in the OACK datagram.

        @see: L{TFTPBootstrap.option_tsize}

        """
        val = TFTPBootstrap.option_tsize(self, val)
        if val == b"0":
            val = self.session.reader.size
            if val is not None:
                val = intToBytes(val)
        return val
Esempio n. 45
0
        def _handleRangeHeader(size):
            try:
                range = self._parseRange(request.getHeader('Range'))
            except RangeNotSatisfiable:
                content_range = 'bytes */%d' % size
                content_range = networkString(content_range)
                request.setResponseCode(416)
                request.setHeader(b'content-range', content_range)
                request.finish()
                return

            if not range:
                start = end = None
                request.setResponseCode(200)
                request.setHeader(b'content-length', intToBytes(size))
            else:
                start, end = range
                content_range = 'bytes %d-%d/%d' % (start, end, size)
                content_range = networkString(content_range)
                length = intToBytes(end - start)
                request.setResponseCode(206)
                request.setHeader(b'content-range', content_range)
                request.setHeader(b'content-length', length)
            return self._get_blob(request, user, blob_id, namespace, range)
Esempio n. 46
0
    def option_tsize(self, val):
        """Process tsize option.

        If tsize is zero, get the size of the file to be read so that it can
        be returned in the OACK datagram.

        @see: L{TFTPBootstrap.option_tsize}

        """
        val = TFTPBootstrap.option_tsize(self, val)
        if val == b"0":
            val = self.session.reader.size
            if val is not None:
                val = intToBytes(val)
        return val
Esempio n. 47
0
    def contentFinish(self, data):
        if self.obj.client_supports_gzip:
            self.setHeader(b'content-encoding', b'gzip')
            data = self.zip(data, True)

        self.setHeader(b'content-length', intToBytes(len(data)))
        self.setHeader(b'cache-control', b'no-cache')

        if self.isSecure():
            self.setHeader(b'strict-transport-security', b'max-age=31536000')

        try:
            self.write(data)
            self.finish()
        except Exception:
            pass
Esempio n. 48
0
    def processingFailed(self, reason):
        if self.is_ajax():
            log.err(reason)
            if self.site.displayTracebacks:
                body = reason.getTraceback()
            else:
                body = b"Processing Failed"

            self.setResponseCode(http.INTERNAL_SERVER_ERROR)
            self.setHeader(b'content-type', b"text/plain")
            self.setHeader(b'content-length', intToBytes(len(body)))
            self.write(body)
            self.finish()
            return reason

        return WebRequest.processingFailed(self, reason)
Esempio n. 49
0
 def _incrdecr(self, cmd, key, val):
     """
     Internal wrapper for incr/decr.
     """
     if self._disconnected:
         return fail(RuntimeError("not connected"))
     if not isinstance(key, bytes):
         return fail(
             ClientError("Invalid type for key: %s, expecting bytes" %
                         (type(key), )))
     if len(key) > self.MAX_KEY_LENGTH:
         return fail(ClientError("Key too long"))
     fullcmd = b" ".join([cmd, key, intToBytes(int(val))])
     self.sendLine(fullcmd)
     cmdObj = Command(cmd, key=key)
     self._current.append(cmdObj)
     return cmdObj._deferred
Esempio n. 50
0
    def start(self):
        d = defer.Deferred()
        self.__notify_waiting.append(d)

        args = [b"mongod",
                b"--port", intToBytes(self.port),
                b"--dbpath", self.__datadir,
                b"--noprealloc", b"--nojournal",
                b"--smallfiles", b"--nssize", b"1",
                b"--oplogSize", b"1",
        ]
        if self.auth: args.append(b"--auth")
        if self.replset: args.extend([b"--replSet", self.replset])
        args.extend(arg.encode() for arg in self.args)
        from os import environ
        self.__proc = reactor.spawnProcess(self, b"mongod", args, env=environ)
        return d
Esempio n. 51
0
    def processingFailed(self, reason):
        log.err(reason)
        # Re-enable on Python 3 as part of #6178:
        if not _PY3 and self.site.displayTracebacks:
            body = ("<html><head><title>web.Server Traceback (most recent call last)</title></head>"
                    "<body><b>web.Server Traceback (most recent call last):</b>\n\n"
                    "%s\n\n</body></html>\n"
                    % webutil.formatFailure(reason))
        else:
            body = (b"<html><head><title>Processing Failed</title></head><body>"
                    b"<b>Processing Failed</b></body></html>")

        self.setResponseCode(http.INTERNAL_SERVER_ERROR)
        self.setHeader(b'content-type', b"text/html")
        self.setHeader(b'content-length', intToBytes(len(body)))
        self.write(body)
        self.finish()
        return reason
Esempio n. 52
0
    def option_tsize(self, val):
        """Process tsize interval option
        (U{RFC2349<http://tools.ietf.org/html/rfc2349>}). Valid range is 0 and up.

        @param val: value of the option
        @type val: C{bytes}

        @return: accepted option value or C{None}, if it is invalid
        @rtype: C{bytes} or C{None}

        """
        try:
            int_tsize = int(val)
        except ValueError:
            return None
        if int_tsize < 0:
            return None
        return intToBytes(int_tsize)
Esempio n. 53
0
    def _generateOpaque(self, nonce, clientip):
        """
        Generate an opaque to be returned to the client.  This is a unique
        string that can be returned to us and verified.
        """
        # Now, what we do is encode the nonce, client ip and a timestamp in the
        # opaque value with a suitable digest.
        now = intToBytes(int(self._getTime()))

        if not clientip:
            clientip = b''
        elif isinstance(clientip, unicode):
            clientip = clientip.encode('ascii')

        key = b",".join((nonce, clientip, now))
        digest = hexlify(md5(key + self.privateKey).digest())
        ekey = base64.b64encode(key)
        return b"-".join((digest, ekey.replace(b'\n', b'')))
Esempio n. 54
0
    def _setContentHeaders(self, request, size=None):
        """
        Set the Content-length and Content-type headers for this request.

        This method is not appropriate for requests for multiple byte ranges;
        L{_doMultipleRangeRequest} will set these headers in that case.

        @param request: The L{Request} object.
        @param size: The size of the response.  If not specified, default to
            C{self.getFileSize()}.
        """
        if size is None:
            size = self.getFileSize()
        request.setHeader(b'content-length', intToBytes(size))
        if self.type:
            request.setHeader(b'content-type', networkString(self.type))
        if self.encoding:
            request.setHeader(b'content-encoding', networkString(self.encoding))
Esempio n. 55
0
    def processingFailed(self, reason):
        log.err(reason)
        if self.site.displayTracebacks:
            body = (b"<html><head><title>web.Server Traceback"
                    b" (most recent call last)</title></head>"
                    b"<body><b>web.Server Traceback"
                    b" (most recent call last):</b>\n\n" +
                    util.formatFailure(reason) + b"\n\n</body></html>\n")
        else:
            body = (b"<html><head><title>Processing Failed"
                    b"</title></head><body>"
                    b"<b>Processing Failed</b></body></html>")

        self.setResponseCode(http.INTERNAL_SERVER_ERROR)
        self.setHeader(b'content-type', b"text/html")
        self.setHeader(b'content-length', intToBytes(len(body)))
        self.write(body)
        self.finish()
        return reason
Esempio n. 56
0
    def option_timeout(self, val):
        """Process timeout interval option
        (U{RFC2349<http://tools.ietf.org/html/rfc2349>}). Valid range is between 1
        and 255, inclusive.

        @param val: value of the option
        @type val: C{bytes}

        @return: accepted option value or C{None}, if it is invalid
        @rtype: C{bytes} or C{None}

        """
        try:
            int_timeout = int(val)
        except ValueError:
            return None
        if int_timeout < 1 or int_timeout > 255:
            return None
        return intToBytes(int_timeout)
Esempio n. 57
0
    def processingFailed(self, failure):
        self._logger.exception(failure)
        response = {
            u"error": {
                u"handled": False,
                u"code": failure.value.__class__.__name__,
                u"message": failure.value.message
            }
        }
        if self.site.displayTracebacks:
            response[u"error"][u"trace"] = format_tb(failure.getTracebackObject())

        body = json.dumps(response)
        self.setResponseCode(http.INTERNAL_SERVER_ERROR)
        self.setHeader(b'content-type', self.defaultContentType)
        self.setHeader(b'content-length', intToBytes(len(body)))
        self.write(body)
        self.finish()
        return failure
Esempio n. 58
0
    def option_blksize(self, val):
        """Process the block size option. Valid range is between 8 and 65464,
        inclusive. If the value is more, than L{MAX_BLOCK_SIZE}, L{MAX_BLOCK_SIZE}
        is returned instead.

        @param val: value of the option
        @type val: C{bytes}

        @return: accepted option value or C{None}, if it is invalid
        @rtype: C{bytes} or C{None}

        """
        try:
            int_blksize = int(val)
        except ValueError:
            return None
        if int_blksize < 8 or int_blksize > 65464:
            return None
        int_blksize = min((int_blksize, MAX_BLOCK_SIZE))
        return intToBytes(int_blksize)
Esempio n. 59
0
    def setHost(self, host, port, ssl=0):
        """
        Change the host and port the request thinks it's using.

        @type host: C{bytes}
        @param host: The value to which to change the host header.

        @type ssl: C{bool}
        @param ssl: A flag which, if C{True}, indicates that the request is
            considered secure (if C{True}, L{isSecure} will return C{True}).
        """
        self._forceSSL = ssl  # set first so isSecure will work
        if self.isSecure():
            default = 443
        else:
            default = 80
        if port == default:
            hostHeader = host
        else:
            hostHeader = host + b":" + intToBytes(port)
        self.requestHeaders.addRawHeader(b"host", hostHeader)
Esempio n. 60
0
    def render_GET(self, request):
        if self.size:
            request.setHeader(b'content-length', intToBytes(self.size))
        if self.type:
            request.setHeader(b'content-type', networkString(self.type))
        if self.encoding:
            request.setHeader(b'content-encoding', networkString(self.encoding))

        request.setResponseCode(http.OK)

        # FIXME: depending on self.size, eg when 128kB, we might want to directly return bytes
        # instead of the overhead setting up a producer etc ..

        # if we want to serve directly out of ZIP files, we cannot support HTTP
        # range requests, as the file-like object returned for a file within an archive
        # does not support seek() (it will raise "not implemented" - 2018/06 with Python 3.6.5)
        producer = NoRangeStaticProducer(request, self.file)
        producer.start()

        # and make sure the connection doesn't get closed
        return server.NOT_DONE_YET