MCPcopy Index your code
hub / github.com/RustPython/RustPython / _get_content_length

Method _get_content_length

Lib/http/client.py:856–885  ·  view source on GitHub ↗

Get the content-length based on the body. If the body is None, we set Content-Length: 0 for methods that expect a body (RFC 7230, Section 3.3.2). We also set the Content-Length for any method if the body is a str or bytes-like object and not a file.

(body, method)

Source from the content-addressed store, hash-verified

854
855 @staticmethod
856 def _get_content_length(body, method):
857 """Get the content-length based on the body.
858
859 If the body is None, we set Content-Length: 0 for methods that expect
860 a body (RFC 7230, Section 3.3.2). We also set the Content-Length for
861 any method if the body is a str or bytes-like object and not a file.
862 """
863 if body is None:
864 # do an explicit check for not None here to distinguish
865 # between unset and set but empty
866 if method.upper() in _METHODS_EXPECTING_BODY:
867 return 0
868 else:
869 return None
870
871 if hasattr(body, 'read'):
872 # file-like object.
873 return None
874
875 try:
876 # does it implement the buffer protocol (bytes, bytearray, array)?
877 mv = memoryview(body)
878 return mv.nbytes
879 except TypeError:
880 pass
881
882 if isinstance(body, str):
883 return len(body)
884
885 return None
886
887 def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
888 source_address=None, blocksize=8192):

Callers 1

_send_requestMethod · 0.95

Calls 4

hasattrFunction · 0.85
isinstanceFunction · 0.85
lenFunction · 0.85
upperMethod · 0.45

Tested by

no test coverage detected