Exemplo n.º 1
0
    def _load_from_string(self, serialized_req_res):
        try:
            data = msgpack.loads(serialized_req_res, use_list=True)
        except ValueError:
            # ValueError: Extra data. returned when msgpack finds invalid
            # data in the file
            raise TraceReadException('Failed to load %s' % serialized_req_res)

        try:
            request_dict, response_dict, canary = data
        except TypeError:
            # https://github.com/andresriancho/w3af/issues/1101
            # 'NoneType' object is not iterable
            raise TraceReadException('Not all components found in %s' %
                                     serialized_req_res)

        if not canary == self._MSGPACK_CANARY:
            # read failed, most likely because the file write is not
            # complete but for some reason it was a valid msgpack file
            raise TraceReadException('Invalid canary in %s' %
                                     serialized_req_res)

        request = HTTPRequest.from_dict(request_dict)
        response = HTTPResponse.from_dict(response_dict)
        return request, response
Exemplo n.º 2
0
    def test_to_from_dict(self):
        headers = Headers([('Host', 'www.w3af.com')])
        req = HTTPRequest(URL("http://www.w3af.com/"),
                          data='spameggs',
                          headers=headers)

        msg = msgpack.dumps(req.to_dict())
        loaded_dict = msgpack.loads(msg)
        loaded_req = HTTPRequest.from_dict(loaded_dict)

        self.assertEqual(req, loaded_req)
        self.assertEqual(req.__dict__.values(), loaded_req.__dict__.values())
Exemplo n.º 3
0
 def test_to_from_dict(self):
     headers = Headers([('Host', 'www.w3af.com')])
     req = HTTPRequest(URL("http://www.w3af.com/"), data='spameggs',
                       headers=headers)
     
     msg = msgpack.dumps(req.to_dict())
     loaded_dict = msgpack.loads(msg)
     loaded_req = HTTPRequest.from_dict(loaded_dict)
     
     self.assertEqual(req, loaded_req)
     self.assertEqual(req.__dict__.values(),
                      loaded_req.__dict__.values())
Exemplo n.º 4
0
    def load_from_file(self, _id):
        fname = self._get_fname_for_id(_id)
        WAIT_TIME = 0.05

        #
        #    Due to some concurrency issues, we need to perform these checks
        #
        for _ in xrange(int(1 / WAIT_TIME)):
            if not os.path.exists(fname):
                time.sleep(WAIT_TIME)
                continue

            # Ok... the file exists, but it might still be being written
            req_res = gzip.open(fname,
                                'rb',
                                compresslevel=self.COMPRESSION_LEVEL)

            try:
                data = msgpack.load(req_res, use_list=True)
            except ValueError:
                # ValueError: Extra data. returned when msgpack finds invalid
                # data in the file
                req_res.close()
                time.sleep(WAIT_TIME)
                continue

            try:
                request_dict, response_dict, canary = data
            except TypeError:
                # https://github.com/andresriancho/w3af/issues/1101
                # 'NoneType' object is not iterable
                req_res.close()
                time.sleep(WAIT_TIME)
                continue

            if not canary == self._MSGPACK_CANARY:
                # read failed, most likely because the file write is not
                # complete but for some reason it was a valid msgpack file
                req_res.close()
                time.sleep(WAIT_TIME)
                continue

            # Success!
            req_res.close()

            request = HTTPRequest.from_dict(request_dict)
            response = HTTPResponse.from_dict(response_dict)
            return request, response

        else:
            msg = 'Timeout expecting trace file to be ready "%s"' % fname
            raise DBException(msg)
Exemplo n.º 5
0
    def _load_from_file(self, id):
        fname = self._get_fname_for_id(id)
        WAIT_TIME = 0.05

        #
        #    Due to some concurrency issues, we need to perform these checks
        #
        for _ in xrange(int(1 / WAIT_TIME)):
            if not os.path.exists(fname):
                time.sleep(WAIT_TIME)
                continue

            # Ok... the file exists, but it might still be being written
            req_res = open(fname, 'rb')

            try:
                data = msgpack.load(req_res, use_list=True)
            except ValueError:
                # ValueError: Extra data. returned when msgpack finds invalid
                # data in the file
                req_res.close()
                time.sleep(WAIT_TIME)
                continue

            try:
                request_dict, response_dict, canary = data
            except TypeError:
                # https://github.com/andresriancho/w3af/issues/1101
                # 'NoneType' object is not iterable
                req_res.close()
                time.sleep(WAIT_TIME)
                continue

            if not canary == self._MSGPACK_CANARY:
                # read failed, most likely because the file write is not
                # complete but for some reason it was a valid msgpack file
                req_res.close()
                time.sleep(WAIT_TIME)
                continue

            # Success!
            req_res.close()

            request = HTTPRequest.from_dict(request_dict)
            response = HTTPResponse.from_dict(response_dict)
            return request, response

        else:
            msg = 'Timeout expecting trace file to be ready "%s"' % fname
            raise IOError(msg)