def _WriteResponseToPath(self, response, report_hooks):

        content_length = response.getheader('Content-Length')

        if content_length is not None: content_length = int(content_length)

        temp_path = HC.GetTempPath()

        size_of_response = 0

        with open(temp_path, 'wb') as f:

            for block in HC.ReadFileLikeAsBlocks(response,
                                                 self.read_block_size):

                if HC.shutdown:
                    raise Exception('Application is shutting down!')

                size_of_response += len(block)

                if content_length is not None and size_of_response > content_length:

                    raise Exception('Response was longer than suggested!')

                f.write(block)

                for hook in report_hooks:

                    hook(content_length, size_of_response)

        return (temp_path, size_of_response)
def GetHashFromPath(path):

    h = hashlib.sha256()

    with open(path, 'rb') as f:

        for block in HC.ReadFileLikeAsBlocks(f, 65536):
            h.update(block)

    return h.digest()
Exemple #3
0
    def _callbackParsePOSTArgs(self, request):

        request.content.seek(0)

        if not request.requestHeaders.hasHeader('Content-Type'):
            raise HydrusExceptions.ForbiddenException(
                'No Content-Type header found!')

        content_types = request.requestHeaders.getRawHeaders('Content-Type')

        content_type = content_types[0]

        try:
            mime = HC.mime_enum_lookup[content_type]
        except:
            raise HydrusExceptions.ForbiddenException(
                'Did not recognise Content-Type header!')

        if mime == HC.APPLICATION_YAML:

            yaml_string = request.content.read()

            request.hydrus_request_data_usage += len(yaml_string)

            hydrus_args = yaml.safe_load(yaml_string)

        else:

            temp_path = HC.GetTempPath()

            with open(temp_path, 'wb') as f:

                for block in HC.ReadFileLikeAsBlocks(request.content, 65536):

                    f.write(block)

                    request.hydrus_request_data_usage += len(block)

            hydrus_args = ParseFileArguments(temp_path)

        request.hydrus_args = hydrus_args

        return request
def GetExtraHashesFromPath(path):

    h_md5 = hashlib.md5()
    h_sha1 = hashlib.sha1()
    h_sha512 = hashlib.sha512()

    with open(path, 'rb') as f:

        for block in HC.ReadFileLikeAsBlocks(f, 65536):

            h_md5.update(block)
            h_sha1.update(block)
            h_sha512.update(block)

    md5 = h_md5.digest()
    sha1 = h_sha1.digest()
    sha512 = h_sha512.digest()

    return (md5, sha1, sha512)
    def _ParseResponse(self, response, report_hooks):

        content_length = response.getheader('Content-Length')

        if content_length is not None: content_length = int(content_length)

        data = ''

        for block in HC.ReadFileLikeAsBlocks(response, self.read_block_size):

            if HC.shutdown: raise Exception('Application is shutting down!')

            data += block

            if content_length is not None and len(data) > content_length:

                raise Exception('Response was longer than suggested!')

            for hook in report_hooks:

                hook(content_length, len(data))

        size_of_response = len(data)

        content_type = response.getheader('Content-Type')

        if content_type is None: parsed_response = data
        else:

            if '; ' in content_type:
                (mime_string, additional_info) = content_type.split('; ', 1)
            else:
                (mime_string, additional_info) = (content_type, '')

            if 'charset=' in additional_info:

                # this does utf-8, ISO-8859-4, whatever

                (gumpf, charset) = additional_info.split('=')

                try:
                    parsed_response = data.decode(charset)
                except:
                    parsed_response = data

            elif content_type in HC.mime_enum_lookup and HC.mime_enum_lookup[
                    content_type] == HC.APPLICATION_YAML:

                try:
                    parsed_response = yaml.safe_load(data)
                except Exception as e:
                    raise HydrusExceptions.NetworkVersionException(
                        'Failed to parse a response object!' + os.linesep +
                        u(e))

            elif content_type == 'text/html':

                try:
                    parsed_response = data.decode('utf-8')
                except:
                    parsed_response = data

            else:
                parsed_response = data

        return (parsed_response, size_of_response)