| 3034 | |
| 3035 | |
| 3036 | def determine_content_length(body): |
| 3037 | # No body, content length of 0 |
| 3038 | if not body: |
| 3039 | return 0 |
| 3040 | |
| 3041 | # Try asking the body for it's length |
| 3042 | try: |
| 3043 | return len(body) |
| 3044 | except (AttributeError, TypeError): |
| 3045 | pass |
| 3046 | |
| 3047 | # Try getting the length from a seekable stream |
| 3048 | if hasattr(body, 'seek') and hasattr(body, 'tell'): |
| 3049 | try: |
| 3050 | orig_pos = body.tell() |
| 3051 | body.seek(0, 2) |
| 3052 | end_file_pos = body.tell() |
| 3053 | body.seek(orig_pos) |
| 3054 | return end_file_pos - orig_pos |
| 3055 | except io.UnsupportedOperation: |
| 3056 | # in case when body is, for example, io.BufferedIOBase object |
| 3057 | # it has "seek" method which throws "UnsupportedOperation" |
| 3058 | # exception in such case we want to fall back to "chunked" |
| 3059 | # encoding |
| 3060 | pass |
| 3061 | # Failed to determine the length |
| 3062 | return None |
| 3063 | |
| 3064 | |
| 3065 | def get_encoding_from_headers(headers, default='ISO-8859-1'): |