Ejemplo n.º 1
0
    def get_file_upload(self):
        try:
            if (int(self.request.arguments['flowTotalSize'][0]) / (1024 * 1024)) > GLSetting.defaults.maximum_filesize:
                log.err("File upload request rejected: file too big")
                raise errors.FileTooBig

            if self.request.arguments['flowIdentifier'][0] not in GLUploads:
                f = GLSecureTemporaryFile(GLSetting.tmp_upload_path)
                GLUploads[self.request.arguments['flowIdentifier'][0]] = f
            else:
                f = GLUploads[self.request.arguments['flowIdentifier'][0]]

            f.write(self.request.files['file'][0]['body'])

            if self.request.arguments['flowChunkNumber'][0] != self.request.arguments['flowTotalChunks'][0]:
                return None

            uploaded_file = {}
            uploaded_file['filename'] = self.request.files['file'][0]['filename']
            uploaded_file['content_type'] = self.request.files['file'][0]['content_type']
            uploaded_file['body_len'] = int(self.request.arguments['flowTotalSize'][0])
            uploaded_file['body_filepath'] = f.filepath
            uploaded_file['body'] = f

            return uploaded_file

        except errors.FileTooBig:
            raise # propagate the exception

        except Exception as exc:
            log.err("Error while handling file upload %s" % exc)
            return None
Ejemplo n.º 2
0
    def get_dummy_file(self, filename=None, content_type=None, content=None):
        if filename is None:
            filename = ''.join(unichr(x) for x in range(0x400, 0x40A))

        if content_type is None:
            content_type = 'application/octet'

        if content is None:
            content = 'LA VEDI LA SUPERCAZZOLA ? PREMATURA ? unicode €'

        temporary_file = GLSecureTemporaryFile(GLSettings.tmp_upload_path)

        temporary_file.write(content)
        temporary_file.avoid_delete()

        dummy_file = {
            'body': temporary_file,
            'body_len': len(content),
            'body_filepath': temporary_file.filepath,
            'filename': filename,
            'content_type': content_type,
            'submission': False
        }

        return dummy_file
Ejemplo n.º 3
0
    def get_dummy_file(self, filename=None, content_type=None, content=None):

        if filename is None:
            filename = ''.join(unichr(x) for x in range(0x400, 0x40A))

        if content_type is None:
            content_type = 'application/octet'

        if content is None:
            content = "ANTANI"

        temporary_file = GLSecureTemporaryFile(GLSetting.tmp_upload_path)

        temporary_file.write(content)
        temporary_file.avoid_delete()

        dummy_file = {
            'body': temporary_file,
            'body_len': len(content),
            'body_filepath': temporary_file.filepath,
            'filename': filename,
            'content_type': content_type,
        }

        return dummy_file
Ejemplo n.º 4
0
    def get_file_upload(self):
        try:
            if (int(self.request.arguments['flowTotalSize'][0]) / (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize:
                log.err("File upload request rejected: file too big")
                raise errors.FileTooBig(GLSettings.memory_copy.maximum_filesize)

            if self.request.arguments['flowIdentifier'][0] not in GLUploads:
                f = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
                GLUploads[self.request.arguments['flowIdentifier'][0]] = f
            else:
                f = GLUploads[self.request.arguments['flowIdentifier'][0]]

            f.write(self.request.files['file'][0]['body'])

            if self.request.arguments['flowChunkNumber'][0] != self.request.arguments['flowTotalChunks'][0]:
                return None

            uploaded_file = {}
            uploaded_file['filename'] = self.request.files['file'][0]['filename']
            uploaded_file['content_type'] = self.request.files['file'][0]['content_type']
            uploaded_file['body_len'] = int(self.request.arguments['flowTotalSize'][0])
            uploaded_file['body_filepath'] = f.filepath
            uploaded_file['body'] = f

            return uploaded_file

        except errors.FileTooBig:
            raise  # propagate the exception

        except Exception as exc:
            log.err("Error while handling file upload %s" % exc)
            return None
Ejemplo n.º 5
0
    def get_file_upload(self):
        try:
            if len(self.request.files) != 1:
                raise errors.InvalidInputFormat(
                    "cannot accept more than a file upload at once")

            chunk_size = len(self.request.files['file'][0]['body'])
            total_file_size = int(
                self.request.arguments['flowTotalSize'][0]
            ) if 'flowTotalSize' in self.request.arguments else chunk_size
            flow_identifier = self.request.arguments['flowIdentifier'][
                0] if 'flowIdentifier' in self.request.arguments else generateRandomKey(
                    10)

            if ((chunk_size /
                 (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize or
                (total_file_size /
                 (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize):
                log.err("File upload request rejected: file too big")
                raise errors.FileTooBig(
                    GLSettings.memory_copy.maximum_filesize)

            if flow_identifier not in GLUploads:
                f = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
                GLUploads[flow_identifier] = f
            else:
                f = GLUploads[flow_identifier]

            f.write(self.request.files['file'][0]['body'])

            if 'flowChunkNumber' in self.request.arguments and 'flowTotalChunks' in self.request.arguments:
                if self.request.arguments['flowChunkNumber'][
                        0] != self.request.arguments['flowTotalChunks'][0]:
                    return None

            uploaded_file = {}
            uploaded_file['filename'] = self.request.files['file'][0][
                'filename']
            uploaded_file['content_type'] = self.request.files['file'][0][
                'content_type']
            uploaded_file['body_len'] = total_file_size
            uploaded_file['body_filepath'] = f.filepath
            uploaded_file['body'] = f

            upload_time = time.time() - f.creation_date

            track_handler(self)

            return uploaded_file

        except errors.FileTooBig:
            raise  # propagate the exception

        except Exception as exc:
            log.err("Error while handling file upload %s" % exc)
            return None
Ejemplo n.º 6
0
 def test_temporary_file_lost_key_due_to_eventual_bug_or_reboot(self):
     a = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
     a.avoid_delete()
     antani = "0123456789" * 10000
     a.write(antani)
     a.close()
     self.assertTrue(os.path.exists(a.filepath))
     os.remove(a.keypath)
     self.assertRaises(IOError, GLSecureFile, a.filepath)
     a.close()
Ejemplo n.º 7
0
    def get_file_upload(self):
        try:
            if len(self.request.files) != 1:
                raise errors.InvalidInputFormat("cannot accept more than a file upload at once")

            ###############################################################
            # checks needed in order to offer compatibility with flow.js/fusty-flow.js
            chunk_size = len(self.request.files['file'][0]['body'])
            total_file_size = int(self.request.arguments['flowTotalSize'][0]) if 'flowTotalSize' in self.request.arguments else chunk_size
            flow_identifier = self.request.arguments['flowIdentifier'][0] if 'flowIdentifier' in self.request.arguments else os.random()
            if 'flowChunkNumber' in self.request.arguments and 'flowTotalChunks' in self.request.arguments:
                if self.request.arguments['flowChunkNumber'][0] != self.request.arguments['flowTotalChunks'][0]:
                    return None
            ###############################################################

            if ((chunk_size / (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize or
                (total_file_size / (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize):
                log.err("File upload request rejected: file too big")
                raise errors.FileTooBig(GLSettings.memory_copy.maximum_filesize)

            if flow_identifier not in GLUploads:
                f = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
                GLUploads[flow_identifier] = f
            else:
                f = GLUploads[flow_identifier]

            f.write(self.request.files['file'][0]['body'])

            uploaded_file = {}
            uploaded_file['filename'] = self.request.files['file'][0]['filename']
            uploaded_file['content_type'] = self.request.files['file'][0]['content_type']
            uploaded_file['body_len'] = total_file_size
            uploaded_file['body_filepath'] = f.filepath
            uploaded_file['body'] = f

            upload_time = time.time() - f.creation_date

            # file uploads works on chunk basis so that we count 1 the file upload
            # as a whole in function get_file_upload()
            for event in outcoming_event_monitored:
                if event['status_checker'](self._status_code) and \
                        event['method'] == self.request.method and \
                        event['handler_check'](self.request.uri):
                    EventTrack(event, upload_time)

            return uploaded_file

        except errors.FileTooBig:
            raise  # propagate the exception

        except Exception as exc:
            log.err("Error while handling file upload %s" % exc)
            return None
Ejemplo n.º 8
0
    def get_file_upload(self):
        try:
            if len(self.request.files) != 1:
                raise errors.InvalidInputFormat("cannot accept more than a file upload at once")

            chunk_size = len(self.request.files['file'][0]['body'])
            total_file_size = int(self.request.arguments['flowTotalSize'][0]) if 'flowTotalSize' in self.request.arguments else chunk_size
            flow_identifier = self.request.arguments['flowIdentifier'][0] if 'flowIdentifier' in self.request.arguments else generateRandomKey(10)

            if ((chunk_size / (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize or
                (total_file_size / (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize):
                log.err("File upload request rejected: file too big")
                raise errors.FileTooBig(GLSettings.memory_copy.maximum_filesize)

            if flow_identifier not in GLUploads:
                f = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
                GLUploads[flow_identifier] = f
            else:
                f = GLUploads[flow_identifier]

            f.write(self.request.files['file'][0]['body'])

            if 'flowChunkNumber' in self.request.arguments and 'flowTotalChunks' in self.request.arguments:
                if self.request.arguments['flowChunkNumber'][0] != self.request.arguments['flowTotalChunks'][0]:
                    return None

            uploaded_file = {}
            uploaded_file['filename'] = self.request.files['file'][0]['filename']
            uploaded_file['content_type'] = self.request.files['file'][0]['content_type']
            uploaded_file['body_len'] = total_file_size
            uploaded_file['body_filepath'] = f.filepath
            uploaded_file['body'] = f

            upload_time = time.time() - f.creation_date

            # file uploads works on chunk basis so that we count 1 the file upload
            # as a whole in function get_file_upload()
            for event in outcoming_event_monitored:
                if event['status_checker'](self._status_code) and \
                        event['method'] == self.request.method and \
                        event['handler_check'](self.request.uri):
                    EventTrack(event, upload_time)

            return uploaded_file

        except errors.FileTooBig:
            raise  # propagate the exception

        except Exception as exc:
            log.err("Error while handling file upload %s" % exc)
            return None
Ejemplo n.º 9
0
    def get_file_upload(self):
        try:
            chunk_size = len(self.request.args['file'][0])
            total_file_size = int(
                self.request.args['flowTotalSize']
                [0]) if 'flowTotalSize' in self.request.args else chunk_size
            flow_identifier = self.request.args['flowIdentifier'][
                0] if 'flowIdentifier' in self.request.args else generateRandomKey(
                    10)

            if ((chunk_size /
                 (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize or
                (total_file_size /
                 (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize):
                log.err("File upload request rejected: file too big")
                raise errors.FileTooBig(
                    GLSettings.memory_copy.maximum_filesize)

            if flow_identifier not in GLUploads:
                f = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
                GLUploads[flow_identifier] = f
            else:
                f = GLUploads[flow_identifier]

            f.write(self.request.args['file'][0])

            if 'flowChunkNumber' in self.request.args and 'flowTotalChunks' in self.request.args:
                if self.request.args['flowChunkNumber'][
                        0] != self.request.args['flowTotalChunks'][0]:
                    return None

            mime_type, encoding = mimetypes.guess_type(
                self.request.args['flowFilename'][0])

            uploaded_file = {
                'name': self.request.args['flowFilename'][0],
                'type': mime_type,
                'size': total_file_size,
                'path': f.filepath,
                'body': f,
                'description': self.request.args.get('description', [''])[0]
            }

            return uploaded_file

        except errors.FileTooBig:
            raise  # propagate the exception

        except Exception as exc:
            log.err("Error while handling file upload %s" % exc)
            return None
Ejemplo n.º 10
0
 def test_temporary_file_lost_key_due_to_eventual_bug_or_reboot(self):
     a = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
     a.avoid_delete()
     antani = "0123456789" * 10000
     a.write(antani)
     a.close()
     self.assertTrue(os.path.exists(a.filepath))
     os.remove(a.keypath)
     self.assertRaises(IOError, GLSecureFile, a.filepath)
     a.close()
Ejemplo n.º 11
0
    def get_file_upload(self):
        try:
            if (int(self.request.arguments['flowTotalSize'][0]) /
                (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize:
                log.err("File upload request rejected: file too big")
                raise errors.FileTooBig(
                    GLSettings.memory_copy.maximum_filesize)

            if self.request.arguments['flowIdentifier'][0] not in GLUploads:
                f = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
                GLUploads[self.request.arguments['flowIdentifier'][0]] = f
            else:
                f = GLUploads[self.request.arguments['flowIdentifier'][0]]

            f.write(self.request.files['file'][0]['body'])

            if self.request.arguments['flowChunkNumber'][
                    0] != self.request.arguments['flowTotalChunks'][0]:
                return None

            uploaded_file = {}
            uploaded_file['filename'] = self.request.files['file'][0][
                'filename']
            uploaded_file['content_type'] = self.request.files['file'][0][
                'content_type']
            uploaded_file['body_len'] = int(
                self.request.arguments['flowTotalSize'][0])
            uploaded_file['body_filepath'] = f.filepath
            uploaded_file['body'] = f

            upload_time = time.time() - f.creation_date

            # file uploads works on chunk basis so that we count 1 the file upload
            # as a whole in function get_file_upload()
            for event in outcoming_event_monitored:
                if event['status_checker'](self._status_code) and \
                        event['method'] == self.request.method and \
                        event['handler_check'](self.request.uri):
                    EventTrack(event, upload_time)

            return uploaded_file

        except errors.FileTooBig:
            raise  # propagate the exception

        except Exception as exc:
            log.err("Error while handling file upload %s" % exc)
            return None
Ejemplo n.º 12
0
    def get_file_upload(self):
        try:
            if len(self.request.files) != 1:
                raise errors.InvalidInputFormat("cannot accept more than a file upload at once")

            chunk_size = len(self.request.files['file'][0]['body'])
            total_file_size = int(self.request.arguments['flowTotalSize'][0]) if 'flowTotalSize' in self.request.arguments else chunk_size
            flow_identifier = self.request.arguments['flowIdentifier'][0] if 'flowIdentifier' in self.request.arguments else generateRandomKey(10)

            if ((chunk_size / (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize or
                (total_file_size / (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize):
                log.err("File upload request rejected: file too big")
                raise errors.FileTooBig(GLSettings.memory_copy.maximum_filesize)

            if flow_identifier not in GLUploads:
                f = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
                GLUploads[flow_identifier] = f
            else:
                f = GLUploads[flow_identifier]

            f.write(self.request.files['file'][0]['body'])

            if 'flowChunkNumber' in self.request.arguments and 'flowTotalChunks' in self.request.arguments:
                if self.request.arguments['flowChunkNumber'][0] != self.request.arguments['flowTotalChunks'][0]:
                    return None


            uploaded_file = {
                'name': self.request.files['file'][0]['filename'],
                'type': self.request.files['file'][0]['content_type'],
                'size': total_file_size,
                'path': f.filepath,
                'body': f,
                'description': self.request.arguments.get('description', [''])[0]
            }

            self.request._start_time = f.creation_date
            track_handler(self)

            return uploaded_file

        except errors.FileTooBig:
            raise  # propagate the exception

        except Exception as exc:
            log.err("Error while handling file upload %s" % exc)
            return None
Ejemplo n.º 13
0
 def test_003_temporary_file_avoid_delete(self):
     a = GLSecureTemporaryFile(GLSetting.tmp_upload_path)
     a.avoid_delete()
     antani = "0123456789" * 10000
     a.write(antani)
     a.close()
     self.assertTrue(os.path.exists(a.filepath))
     b = GLSecureFile(a.filepath)
     self.assertTrue(antani == b.read())
Ejemplo n.º 14
0
 def test_temporary_file(self):
     a = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
     antani = "0123456789" * 10000
     a.write(antani)
     self.assertTrue(antani == a.read())
     a.close()
     self.assertFalse(os.path.exists(a.filepath))
Ejemplo n.º 15
0
 def test_temporary_file_write_after_read(self):
     a = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
     antani = "0123456789" * 10000
     a.write(antani)
     self.assertTrue(antani == a.read())
     self.assertRaises(AssertionError, a.write, antani)
     a.close()
Ejemplo n.º 16
0
    def get_file_upload(self):
        try:
            if (int(self.request.arguments['flowTotalSize'][0]) / (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize:
                log.err("File upload request rejected: file too big")
                raise errors.FileTooBig(GLSettings.memory_copy.maximum_filesize)

            if self.request.arguments['flowIdentifier'][0] not in GLUploads:
                f = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
                GLUploads[self.request.arguments['flowIdentifier'][0]] = f
            else:
                f = GLUploads[self.request.arguments['flowIdentifier'][0]]

            f.write(self.request.files['file'][0]['body'])

            if self.request.arguments['flowChunkNumber'][0] != self.request.arguments['flowTotalChunks'][0]:
                return None

            uploaded_file = {}
            uploaded_file['filename'] = self.request.files['file'][0]['filename']
            uploaded_file['content_type'] = self.request.files['file'][0]['content_type']
            uploaded_file['body_len'] = int(self.request.arguments['flowTotalSize'][0])
            uploaded_file['body_filepath'] = f.filepath
            uploaded_file['body'] = f

            upload_time = time.time() - f.creation_date

            # file uploads works on chunk basis so that we count 1 the file upload
            # as a whole in function get_file_upload()
            for event in outcoming_event_monitored:
                if event['status_checker'](self._status_code) and \
                        event['method'] == self.request.method and \
                        event['handler_check'](self.request.uri):
                    EventTrack(event, upload_time)

            return uploaded_file

        except errors.FileTooBig:
            raise  # propagate the exception

        except Exception as exc:
            log.err("Error while handling file upload %s" % exc)
            return None
Ejemplo n.º 17
0
def get_dummy_file(filename=None, content_type=None, content=None):
    filename = ''.join(unichr(x) for x in range(0x400, 0x40A))

    content_type = 'application/octet'

    content = ''.join(unichr(x) for x in range(0x400, 0x40A))

    temporary_file = GLSecureTemporaryFile(GLSettings.tmp_upload_path)

    temporary_file.write(content)
    temporary_file.avoid_delete()

    return {
        'body': temporary_file,
        'body_len': len(content),
        'body_filepath': temporary_file.filepath,
        'filename': filename,
        'content_type': content_type,
        'submission': False
    }
Ejemplo n.º 18
0
    def _on_headers(self, data):
        try:
            data = native_str(data.decode("latin1"))
            eol = data.find("\r\n")
            start_line = data[:eol]

            try:
                method, uri, version = start_line.split(" ")
            except ValueError:
                raise _BadRequestException("Malformed HTTP request line")

            if not version.startswith("HTTP/"):
                raise _BadRequestException(
                    "Malformed HTTP version in HTTP Request-Line")

            try:
                headers = httputil.HTTPHeaders.parse(data[eol:])
                content_length = int(headers.get("Content-Length", 0))
            except ValueError:
                raise _BadRequestException("Malformed Content-Length header")

            self._request = HTTPRequest(connection=self,
                                        method=method,
                                        uri=uri,
                                        version=version,
                                        headers=headers,
                                        remote_ip=self._remote_ip)

            if content_length:
                megabytes = int(content_length) / (1024 * 1024)
                if megabytes > GLSettings.memory_copy.maximum_filesize:
                    raise _BadRequestException(
                        "Request exceeded size limit %d" %
                        GLSettings.memory_copy.maximum_filesize)

                if headers.get("Expect") == "100-continue":
                    self.transport.write("HTTP/1.1 100 (Continue)\r\n\r\n")

                if content_length < 100000:
                    self._contentbuffer = StringIO()
                else:
                    self._contentbuffer = GLSecureTemporaryFile(
                        GLSettings.tmp_upload_path)

                self.content_length = content_length
                self.setRawMode()
                return

            self.request_callback(self._request)
        except _BadRequestException as e:
            log.msg("Exception while handling HTTP request from %s: %s" %
                    (self._remote_ip, e))
            self.transport.loseConnection()
Ejemplo n.º 19
0
 def test_temporary_file_write_after_read(self):
     a = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
     antani = "0123456789" * 10000
     a.write(antani)
     self.assertTrue(antani == a.read())
     self.assertRaises(AssertionError, a.write, antani)
     a.close()
Ejemplo n.º 20
0
 def test_temporary_file(self):
     a = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
     antani = "0123456789" * 10000
     a.write(antani)
     self.assertTrue(antani == a.read())
     a.close()
     self.assertFalse(os.path.exists(a.filepath))
Ejemplo n.º 21
0
    def get_dummy_file(self, filename=None, content_type=None, content=None):

        if filename is None:
            filename = ''.join(unichr(x) for x in range(0x400, 0x40A))

        if content_type is None:
            content_type = 'application/octet'

        if content is None:
            content = "ANTANI"

        temporary_file = GLSecureTemporaryFile(GLSetting.tmp_upload_path)

        temporary_file.write(content)
        temporary_file.avoid_delete()

        dummy_file = {
            'body': temporary_file,
            'body_len': len(content),
            'body_filepath': temporary_file.filepath,
            'filename': filename,
            'content_type': content_type,
        }

        return dummy_file
Ejemplo n.º 22
0
def get_dummy_file(filename=None, content_type=None, content=None):
    global files_count
    files_count += 1
    filename = ''.join(unichr(x) for x in range(0x400, 0x40A)).join('-%d' % files_count)

    content_type = 'application/octet'

    content = ''.join(unichr(x) for x in range(0x400, 0x40A))

    temporary_file = GLSecureTemporaryFile(GLSettings.tmp_upload_path)

    temporary_file.write(content)
    temporary_file.avoid_delete()

    return {
        'name': filename,
        'description': 'description',
        'body': temporary_file,
        'size': len(content),
        'path': temporary_file.filepath,
        'type': content_type,
        'submission': False
    }
Ejemplo n.º 23
0
    def get_file_upload(self):
        if 'flowFilename' not in self.request.args:
            return None

        total_file_size = int(self.request.args['flowTotalSize'][0])
        flow_identifier = self.request.args['flowIdentifier'][0]

        chunk_size = len(self.request.args['file'][0])
        if ((chunk_size /
             (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize
                or (total_file_size /
                    (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize):
            log.err("File upload request rejected: file too big")
            raise errors.FileTooBig(GLSettings.memory_copy.maximum_filesize)

        if flow_identifier not in GLUploads:
            GLUploads[flow_identifier] = GLSecureTemporaryFile(
                GLSettings.tmp_upload_path)

        f = GLUploads[flow_identifier]
        f.write(self.request.args['file'][0])

        if self.request.args['flowChunkNumber'][0] != self.request.args[
                'flowTotalChunks'][0]:
            return None

        mime_type, encoding = mimetypes.guess_type(
            self.request.args['flowFilename'][0])
        if mime_type is None:
            mime_type = 'application/octet-stream'

        return {
            'name': self.request.args['flowFilename'][0],
            'type': mime_type,
            'size': total_file_size,
            'path': f.filepath,
            'body': f,
            'description': self.request.args.get('description', [''])[0]
        }
Ejemplo n.º 24
0
def get_dummy_file(filename=None, content_type=None, content=None):
    filename = ''.join(unichr(x) for x in range(0x400, 0x40A))

    content_type = 'application/octet'

    content = ''.join(unichr(x) for x in range(0x400, 0x40A))

    temporary_file = GLSecureTemporaryFile(GLSettings.tmp_upload_path)

    temporary_file.write(content)
    temporary_file.avoid_delete()

    return {
        'body': temporary_file,
        'body_len': len(content),
        'body_filepath': temporary_file.filepath,
        'filename': filename,
        'content_type': content_type,
        'submission': False
    }
Ejemplo n.º 25
0
    def test_post(self):

        temporary_file = GLSecureTemporaryFile(GLSetting.tmp_upload_path)
        temporary_file.write("ANTANI")
        temporary_file.avoid_delete()

        request_body = {
            'body': temporary_file,
            'body_len': len("ANTANI"),
            'body_filepath': temporary_file.filepath,
            'filename': 'en.json',
            'content_type': 'application/json'
        }

        handler = self.request({}, role='admin', kwargs={'path': GLSetting.static_path}, body=request_body)

        yield handler.post(lang='en')

        self.assertTrue(isinstance(self.responses, list))
        self.assertEqual(len(self.responses), 1)
        self._handler.validate_message(json.dumps(self.responses[0]), requests.staticFile)
Ejemplo n.º 26
0
def get_dummy_file(filename=None, content_type=None, content=None):
    global files_count
    files_count += 1
    filename = ''.join(unichr(x) for x in range(0x400, 0x40A)).join('-%d' % files_count)

    content_type = 'application/octet'

    content = ''.join(unichr(x) for x in range(0x400, 0x40A))

    temporary_file = GLSecureTemporaryFile(GLSettings.tmp_upload_path)

    temporary_file.write(content)
    temporary_file.avoid_delete()

    return {
        'name': filename,
        'description': 'description',
        'body': temporary_file,
        'size': len(content),
        'path': temporary_file.filepath,
        'type': content_type,
        'submission': False
    }
Ejemplo n.º 27
0
    def get_file_upload(self):
        try:
            if len(self.request.files) != 1:
                raise errors.InvalidInputFormat("cannot accept more than a file upload at once")

            chunk_size = len(self.request.files["file"][0]["body"])
            total_file_size = (
                int(self.request.arguments["flowTotalSize"][0])
                if "flowTotalSize" in self.request.arguments
                else chunk_size
            )
            flow_identifier = (
                self.request.arguments["flowIdentifier"][0]
                if "flowIdentifier" in self.request.arguments
                else generateRandomKey(10)
            )

            if (chunk_size / (1024 * 1024)) > GLSettings.memory_copy.maximum_filesize or (
                total_file_size / (1024 * 1024)
            ) > GLSettings.memory_copy.maximum_filesize:
                log.err("File upload request rejected: file too big")
                raise errors.FileTooBig(GLSettings.memory_copy.maximum_filesize)

            if flow_identifier not in GLUploads:
                f = GLSecureTemporaryFile(GLSettings.tmp_upload_path)
                GLUploads[flow_identifier] = f
            else:
                f = GLUploads[flow_identifier]

            f.write(self.request.files["file"][0]["body"])

            if "flowChunkNumber" in self.request.arguments and "flowTotalChunks" in self.request.arguments:
                if self.request.arguments["flowChunkNumber"][0] != self.request.arguments["flowTotalChunks"][0]:
                    return None

            uploaded_file = {}
            uploaded_file["filename"] = self.request.files["file"][0]["filename"]
            uploaded_file["content_type"] = self.request.files["file"][0]["content_type"]
            uploaded_file["body_len"] = total_file_size
            uploaded_file["body_filepath"] = f.filepath
            uploaded_file["body"] = f

            upload_time = time.time() - f.creation_date

            # file uploads works on chunk basis so that we count 1 the file upload
            # as a whole in function get_file_upload()
            for event in outcoming_event_monitored:
                if (
                    event["status_checker"](self._status_code)
                    and event["method"] == self.request.method
                    and event["handler_check"](self.request.uri)
                ):
                    EventTrack(event, upload_time)

            return uploaded_file

        except errors.FileTooBig:
            raise  # propagate the exception

        except Exception as exc:
            log.err("Error while handling file upload %s" % exc)
            return None
Ejemplo n.º 28
0
    def _on_headers(self, data):
        try:
            data = native_str(data.decode("latin1"))
            eol = data.find("\r\n")
            start_line = data[:eol]
            try:
                method, uri, version = start_line.split(" ")
            except ValueError:
                raise _BadRequestException("Malformed HTTP request line")
            if not version.startswith("HTTP/"):
                raise _BadRequestException(
                    "Malformed HTTP version in HTTP Request-Line")
            headers = httputil.HTTPHeaders.parse(data[eol:])
            self._request = HTTPRequest(
                connection=self, method=method, uri=uri, version=version,
                headers=headers, remote_ip=self._remote_ip)

            try:
                self.content_length = int(headers.get("Content-Length", 0))
            except ValueError:
                raise _BadRequestException("Malformed Content-Length header")

            # we always use secure temporary files in case of large json or file uploads
            if self.content_length < 100000 and self._request.headers.get("Content-Disposition") is None:
                self._contentbuffer = StringIO('')
            else:
                self._contentbuffer = GLSecureTemporaryFile(GLSetting.tmp_upload_path)

            if headers.get("Expect") == "100-continue":
                self.transport.write("HTTP/1.1 100 (Continue)\r\n\r\n")

            c_d_header = self._request.headers.get("Content-Disposition")
            if c_d_header is not None:
                key, pdict = parse_header(c_d_header)
                if key != 'attachment' or 'filename' not in pdict:
                    raise _BadRequestException("Malformed Content-Disposition header")

                self.file_upload = True
                self.uploaded_file['filename'] = pdict['filename']
                self.uploaded_file['content_type'] = self._request.headers.get("Content-Type",
                                                                               'application/octet-stream')

                self.uploaded_file['body'] = self._contentbuffer
                self.uploaded_file['body_len'] = int(self.content_length)
                self.uploaded_file['body_filepath'] = self._contentbuffer.filepath

            megabytes = int(self.content_length) / (1024 * 1024)

            if self.file_upload:
                limit_type = "upload"
                limit = GLSetting.memory_copy.maximum_filesize
            else:
                limit_type = "json"
                limit = 1000000 # 1MB fixme: add GLSetting.memory_copy.maximum_jsonsize
                # is 1MB probably too high. probably this variable must be in kB

            # less than 1 megabytes is always accepted
            if megabytes > limit:
                log.err("Tried %s request larger than expected (%dMb > %dMb)" %
                        (limit_type,
                         megabytes,
                         limit))

                # In HTTP Protocol errors need to be managed differently than handlers
                raise errors.HTTPRawLimitReach

            if self.content_length > 0:
                self.setRawMode()
                return
            elif self.file_upload:
                self._on_request_body(self.uploaded_file)
                self.file_upload = False
                self.uploaded_file = {}
                return

            self.request_callback(self._request)
        except Exception as exception:
            log.msg("Malformed HTTP request from %s: %s" % (self._remote_ip, exception))
            log.exception(exception)
            if self._request:
                self._request.finish()
            if self.transport:
                self.transport.loseConnection()