Ejemplo n.º 1
0
    def _implicitResult(self, body):
        encoding = getCharsetUsingRequest(self._request) or 'utf-8'
        content_type = self.getHeader('content-type')

        if isinstance(body, unicode):
            try:
                if not content_type.startswith('text/'):
                    raise ValueError(
                        'Unicode results must have a text content type.')
            except AttributeError:
                    raise ValueError(
                        'Unicode results must have a text content type.')


            major, minor, params = contenttype.parse(content_type)

            if 'charset' in params:
                encoding = params['charset']
            else:
                content_type += ';charset=%s' %encoding

            body = body.encode(encoding)

        if content_type:
            headers = [('content-type', content_type),
                       ('content-length', str(len(body)))]
        else:
            headers = [('content-length', str(len(body)))]

        return body, headers
Ejemplo n.º 2
0
    def _implicitResult(self, body):
        encoding = getCharsetUsingRequest(self._request) or 'utf-8'
        content_type = self.getHeader('content-type')

        if isinstance(body, unicode):
            try:
                if not content_type.startswith('text/'):
                    raise ValueError(
                        'Unicode results must have a text content type.')
            except AttributeError:
                    raise ValueError(
                        'Unicode results must have a text content type.')


            major, minor, params = contenttype.parse(content_type)

            if 'charset' in params:
                encoding = params['charset']
            else:
                content_type += ';charset=%s' %encoding

            body = body.encode(encoding)

        if content_type:
            headers = [('content-type', content_type),
                       ('content-length', str(len(body)))]
        else:
            headers = [('content-length', str(len(body)))]

        return body, headers
Ejemplo n.º 3
0
def extractCharset(content_type):
    """Extract charset information from a MIME type.

        >>> extractCharset('text/plain; charset=UTF-8')
        'UTF-8'
        >>> extractCharset('text/html; charset=ISO-8859-1')
        'ISO-8859-1'
        >>> extractCharset('text/plain')
        'ASCII'

    """
    if content_type and content_type.strip():
        major, minor, params = contenttype.parse(content_type)
        return params.get("charset", "ASCII")
    else:
        return "ASCII"