Skip to content

Response.calculate_content_length doesn't use encoded self.response #705

@touilleMan

Description

@touilleMan

The implementation of calculate_content_length doesn't encode the elements of self.response before using them:
https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/wrappers.py#L926

    def calculate_content_length(self):
        [...]
        return sum(len(x) for x in self.response)

In case self.response is composed of utf-8 encoded unicode strings, this will lead to an invalid return length:

>>> from werkzeug import Response
>>> r = Response()
>>> r.response.append(u'你好')
>>> r.calculate_content_length()
2
>>> len(u'你好'.encode('utf-8'))
6

The easier solution to this would be to use iter_encoded inside calculate_content_length, but would decrease performances given the data would be encoded two time (for calculating content-lenght, then for actualy writing the data)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions