[issue23350] Content-length is incorrect when request body is a list or tuple

Demian Brecht report at bugs.python.org
Fri Feb 13 06:50:55 CET 2015


Demian Brecht added the comment:

Thanks for the clarification Martin. After giving this some further thought, I think that the best way to go is to /only/ calculate and add the Content-Length header if each element in the list or tuple is pre-encoded. If it's mixed or only strings, then there are one of three options:

1. Don't try to compute the Content-Length automatically as encoding may change the number of bytes being sent across as the body (which is what the Content-Length represents)
2. Encode the entire body twice, once during the computation of the Content-Length and again on the fly as the body is being written to the socket. This will incur 2x the CPU cost, which can be costly if the body is sufficiently large.
3. Encode the entire body once and store it in memory. Given body sizes can be quite large, I don't think that duplicating the body would be a good approach.

The attached patch uses option 1 in order to not add any CPU or memory cost to the operation, but still fix the Content-Length issue as reported. I've also updated the docs to indicate as much.

----------
Added file: http://bugs.python.org/file38125/list_content_length_2.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23350>
_______________________________________


More information about the Python-bugs-list mailing list